Add isTrusted property to HttpFetchResult

This commit is contained in:
cketti 2023-06-08 16:33:47 +02:00
parent 0653fbfb68
commit 18ee442d97
3 changed files with 25 additions and 13 deletions

View file

@ -10,9 +10,11 @@ internal sealed interface HttpFetchResult {
* The HTTP request returned a success response.
*
* @param inputStream Contains the response body.
* @param isTrusted `true` iff all associated requests were using HTTPS with a trusted certificate.
*/
data class SuccessResponse(
val inputStream: InputStream,
val isTrusted: Boolean,
) : HttpFetchResult
/**

View file

@ -35,6 +35,7 @@ internal class OkHttpFetcher(
if (response.isSuccessful) {
val result = HttpFetchResult.SuccessResponse(
inputStream = response.body!!.byteStream(),
isTrusted = response.isTrusted(),
)
cancellableContinuation.resume(result)
} else {
@ -50,4 +51,13 @@ internal class OkHttpFetcher(
call.enqueue(responseCallback)
}
}
private tailrec fun Response.isTrusted(): Boolean {
val priorResponse = priorResponse
return when {
!request.isHttps -> false
priorResponse == null -> true
else -> priorResponse.isTrusted()
}
}
}

View file

@ -8,7 +8,6 @@ import app.k9mail.autodiscovery.autoconfig.HttpFetchResult.SuccessResponse
import app.k9mail.core.common.mail.EmailAddress
import com.fsck.k9.logging.Timber
import java.io.IOException
import java.io.InputStream
import okhttp3.HttpUrl
internal class RealAutoconfigFetcher(
@ -19,9 +18,7 @@ internal class RealAutoconfigFetcher(
return try {
when (val fetchResult = fetcher.fetch(autoconfigUrl)) {
is SuccessResponse -> {
fetchResult.inputStream.use { inputStream ->
parseSettings(inputStream, email, autoconfigUrl)
}
parseSettings(fetchResult, email, autoconfigUrl)
}
is ErrorResponse -> AutoDiscoveryResult.NoUsableSettingsFound
}
@ -32,20 +29,23 @@ internal class RealAutoconfigFetcher(
}
private suspend fun parseSettings(
inputStream: InputStream,
fetchResult: SuccessResponse,
email: EmailAddress,
autoconfigUrl: HttpUrl,
): AutoDiscoveryResult {
return try {
return when (val parserResult = parser.parseSettings(inputStream, email)) {
is Settings -> {
AutoDiscoveryResult.Settings(
incomingServerSettings = parserResult.incomingServerSettings,
outgoingServerSettings = parserResult.outgoingServerSettings,
isTrusted = false,
)
fetchResult.inputStream.use { inputStream ->
return when (val parserResult = parser.parseSettings(inputStream, email)) {
is Settings -> {
AutoDiscoveryResult.Settings(
incomingServerSettings = parserResult.incomingServerSettings,
outgoingServerSettings = parserResult.outgoingServerSettings,
isTrusted = fetchResult.isTrusted,
)
}
is ParserError -> AutoDiscoveryResult.NoUsableSettingsFound
}
is ParserError -> AutoDiscoveryResult.NoUsableSettingsFound
}
} catch (e: AutoconfigParserException) {
Timber.d(e, "Failed to parse config from URL: %s", autoconfigUrl)