Fix crash with invalid domain

This commit is contained in:
Koitharu
2023-02-17 07:39:59 +02:00
parent c09b0150ac
commit c520699f9f
2 changed files with 23 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package org.koitharu.kotatsu.core.network
import android.os.Build
import android.util.Log
import dagger.Lazy
import okhttp3.Headers
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
@@ -11,6 +12,7 @@ import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
import org.koitharu.kotatsu.parsers.model.MangaSource
import org.koitharu.kotatsu.parsers.util.mergeWith
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
import java.util.Locale
import javax.inject.Inject
import javax.inject.Singleton
@@ -39,12 +41,18 @@ class CommonHeadersInterceptor @Inject constructor(
headersBuilder[CommonHeaders.USER_AGENT] = userAgentFallback
}
if (headersBuilder[CommonHeaders.REFERER] == null && repository != null) {
headersBuilder[CommonHeaders.REFERER] = "https://${repository.domain}/"
headersBuilder.trySet(CommonHeaders.REFERER, "https://${repository.domain}/")
}
val newRequest = request.newBuilder().headers(headersBuilder.build()).build()
return repository?.intercept(ProxyChain(chain, newRequest)) ?: chain.proceed(newRequest)
}
private fun Headers.Builder.trySet(name: String, value: String) = try {
set(name, value)
} catch (e: IllegalArgumentException) {
e.printStackTraceDebug()
}
private class ProxyChain(
private val delegate: Interceptor.Chain,
private val request: Request,

View File

@@ -7,14 +7,24 @@ import org.koitharu.kotatsu.utils.EditTextValidator
class DomainValidator : EditTextValidator() {
override fun validate(text: String): ValidationResult {
if (text.isBlank()) {
val trimmed = text.trim()
if (trimmed.isEmpty()) {
return ValidationResult.Success
}
val host = text.trim().toCanonicalHost()
return if (host == null) {
return if (!checkCharacters(trimmed) || trimmed.toCanonicalHost() == null) {
ValidationResult.Failed(context.getString(R.string.invalid_domain_message))
} else {
ValidationResult.Success
}
}
}
private fun checkCharacters(value: String): Boolean {
for (i in value.indices) {
val c = value[i]
if (c !in '\u0020'..'\u007e') {
return false
}
}
return true
}
}