From 80a2a10e7162b8bfd5cbf91113dae300bfbd7698 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 6 Feb 2023 17:30:17 +0200 Subject: [PATCH 1/4] [Remanga] Fix headers --- .../kotlin/org/koitharu/kotatsu/parsers/site/RemangaParser.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/RemangaParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/RemangaParser.kt index dcdcdab5..c1a27795 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/RemangaParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/RemangaParser.kt @@ -233,7 +233,7 @@ internal class RemangaParser( } ?: return null val jo = JSONObject(URLDecoder.decode(userCookie.value, Charsets.UTF_8.name())) val accessToken = jo.getStringOrNull("access_token") ?: return null - return Headers.headersOf("authorization", "bearer $accessToken") + return headers.newBuilder().add("authorization", "bearer $accessToken").build() } private fun copyCookies() { From c3b1556816d889e81d95e5a21df5a4e3f907da57 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 6 Feb 2023 17:50:44 +0200 Subject: [PATCH 2/4] Add headers to CloudFlareException --- .../parsers/exception/CloudFlareProtectedException.kt | 2 ++ .../org/koitharu/kotatsu/parsers/CloudFlareInterceptor.kt | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/exception/CloudFlareProtectedException.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/exception/CloudFlareProtectedException.kt index 75337a12..e06415e3 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/exception/CloudFlareProtectedException.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/exception/CloudFlareProtectedException.kt @@ -1,7 +1,9 @@ package org.koitharu.kotatsu.parsers.exception +import okhttp3.Headers import okio.IOException class CloudFlareProtectedException( val url: String, + val headers: Headers, ) : IOException("Protected by CloudFlare: $url") \ No newline at end of file diff --git a/src/test/kotlin/org/koitharu/kotatsu/parsers/CloudFlareInterceptor.kt b/src/test/kotlin/org/koitharu/kotatsu/parsers/CloudFlareInterceptor.kt index 5f9a64c3..957eb889 100644 --- a/src/test/kotlin/org/koitharu/kotatsu/parsers/CloudFlareInterceptor.kt +++ b/src/test/kotlin/org/koitharu/kotatsu/parsers/CloudFlareInterceptor.kt @@ -13,11 +13,15 @@ private const val SERVER_CLOUDFLARE = "cloudflare" class CloudFlareInterceptor : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { - val response = chain.proceed(chain.request()) + val request = chain.request() + val response = chain.proceed(request) if (response.code == HTTP_FORBIDDEN || response.code == HTTP_UNAVAILABLE) { if (response.header(HEADER_SERVER)?.startsWith(SERVER_CLOUDFLARE) == true) { response.closeQuietly() - throw CloudFlareProtectedException(response.request.url.toString()) + throw CloudFlareProtectedException( + url = response.request.url.toString(), + headers = request.headers, + ) } } return response From d14168e3251d24b0c2ca94e807ffc6d22114e0f0 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 6 Feb 2023 18:06:14 +0200 Subject: [PATCH 3/4] Make headers accessible --- src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt index 0c2bc8fd..3d4047cd 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt @@ -1,7 +1,6 @@ package org.koitharu.kotatsu.parsers import androidx.annotation.CallSuper -import androidx.annotation.VisibleForTesting import okhttp3.Headers import okhttp3.HttpUrl import org.jsoup.nodes.Element @@ -35,8 +34,7 @@ abstract class MangaParser @InternalParsersApi constructor(val source: MangaSour */ protected abstract val configKeyDomain: ConfigKey.Domain - @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) - internal open val headers: Headers? = null + open val headers: Headers? = null /** * Used as fallback if value of `sortOrder` passed to [getList] is null From 00abaea32415fae2f4ae27614b13136fd7949f4b Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 6 Feb 2023 18:56:11 +0200 Subject: [PATCH 4/4] [Remanga] Fix headers --- .../org/koitharu/kotatsu/parsers/site/RemangaParser.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/RemangaParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/RemangaParser.kt index c1a27795..48f4530e 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/site/RemangaParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/site/RemangaParser.kt @@ -227,12 +227,12 @@ internal class RemangaParser( return jo.getJSONObject("content").getString("username") } - private fun getApiHeaders(): Headers? { + private fun getApiHeaders(): Headers { val userCookie = context.cookieJar.getCookies(getDomain()).find { it.name == "user" - } ?: return null + } ?: return headers val jo = JSONObject(URLDecoder.decode(userCookie.value, Charsets.UTF_8.name())) - val accessToken = jo.getStringOrNull("access_token") ?: return null + val accessToken = jo.getStringOrNull("access_token") ?: return headers return headers.newBuilder().add("authorization", "bearer $accessToken").build() }