diff --git a/app/src/main/java/org/koitharu/kotatsu/sync/data/SyncAuthApi.kt b/app/src/main/java/org/koitharu/kotatsu/sync/data/SyncAuthApi.kt index 55f983967..a3792664f 100644 --- a/app/src/main/java/org/koitharu/kotatsu/sync/data/SyncAuthApi.kt +++ b/app/src/main/java/org/koitharu/kotatsu/sync/data/SyncAuthApi.kt @@ -20,8 +20,9 @@ class SyncAuthApi @Inject constructor( val body = JSONObject( mapOf("email" to email, "password" to password), ).toRequestBody() + val scheme = getScheme(host) val request = Request.Builder() - .url("http://$host/auth") + .url("$scheme://$host/auth") .post(body) .build() val response = okHttpClient.newCall(request).await() @@ -33,4 +34,13 @@ class SyncAuthApi @Inject constructor( throw SyncApiException(message, code) } } + + private suspend fun getScheme(host: String): String { + val request = Request.Builder() + .url("http://$host/") + .head() + .build() + val response = okHttpClient.newCall(request).await() + return response.request.url.scheme + } } diff --git a/app/src/main/java/org/koitharu/kotatsu/sync/data/SyncAuthenticator.kt b/app/src/main/java/org/koitharu/kotatsu/sync/data/SyncAuthenticator.kt index c37809ecf..dc660e381 100644 --- a/app/src/main/java/org/koitharu/kotatsu/sync/data/SyncAuthenticator.kt +++ b/app/src/main/java/org/koitharu/kotatsu/sync/data/SyncAuthenticator.kt @@ -9,6 +9,7 @@ import okhttp3.Request import okhttp3.Response import okhttp3.Route import org.koitharu.kotatsu.R +import org.koitharu.kotatsu.core.network.CommonHeaders class SyncAuthenticator( context: Context, @@ -24,7 +25,7 @@ class SyncAuthenticator( val newToken = tryRefreshToken() ?: return null accountManager.setAuthToken(account, tokenType, newToken) return response.request.newBuilder() - .header("Authorization", "Bearer $newToken") + .header(CommonHeaders.AUTHORIZATION, "Bearer $newToken") .build() } diff --git a/app/src/main/java/org/koitharu/kotatsu/sync/data/SyncInterceptor.kt b/app/src/main/java/org/koitharu/kotatsu/sync/data/SyncInterceptor.kt index 0a9cc0db4..bcc677e6c 100644 --- a/app/src/main/java/org/koitharu/kotatsu/sync/data/SyncInterceptor.kt +++ b/app/src/main/java/org/koitharu/kotatsu/sync/data/SyncInterceptor.kt @@ -8,6 +8,7 @@ import okhttp3.Response import org.koitharu.kotatsu.BuildConfig import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.db.DATABASE_VERSION +import org.koitharu.kotatsu.core.network.CommonHeaders class SyncInterceptor( context: Context, @@ -21,10 +22,10 @@ class SyncInterceptor( val token = accountManager.peekAuthToken(account, tokenType) val requestBuilder = chain.request().newBuilder() if (token != null) { - requestBuilder.header("Authorization", "Bearer $token") + requestBuilder.header(CommonHeaders.AUTHORIZATION, "Bearer $token") } requestBuilder.header("X-App-Version", BuildConfig.VERSION_CODE.toString()) requestBuilder.header("X-Db-Version", DATABASE_VERSION.toString()) return chain.proceed(requestBuilder.build()) } -} \ No newline at end of file +} diff --git a/app/src/main/java/org/koitharu/kotatsu/sync/domain/SyncHelper.kt b/app/src/main/java/org/koitharu/kotatsu/sync/domain/SyncHelper.kt index 1e8c7d745..1b0e5e958 100644 --- a/app/src/main/java/org/koitharu/kotatsu/sync/domain/SyncHelper.kt +++ b/app/src/main/java/org/koitharu/kotatsu/sync/domain/SyncHelper.kt @@ -54,8 +54,11 @@ class SyncHelper( .addInterceptor(SyncInterceptor(context, account)) .addInterceptor(GZipInterceptor()) .build() - private val baseUrl: String - get() = "http://${settings.host}" + private val baseUrl: String by lazy { + val host = settings.host + val scheme = getScheme(host) + "$scheme://$host" + } private val defaultGcPeriod: Long // gc period if sync enabled get() = TimeUnit.DAYS.toMillis(4) @@ -260,6 +263,15 @@ class SyncHelper( return requireNotNull(tag) } + private fun getScheme(host: String): String { + val request = Request.Builder() + .url("http://$host/") + .head() + .build() + val response = httpClient.newCall(request).execute() + return response.request.url.scheme + } + private fun gcFavourites() { val deletedAt = System.currentTimeMillis() - defaultGcPeriod val selection = "deleted_at != 0 AND deleted_at < ?"