Add headers to CloudFlareException

Koitharu 3 years ago
parent 80a2a10e71
commit c3b1556816
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -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")

@ -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

Loading…
Cancel
Save