From 9f4597b48c4f3877f63fb7daf068b0673c1e8f90 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 9 May 2023 16:41:05 +0200 Subject: [PATCH] Add code to extract the base domain from a hostname --- .../autoconfig/BaseDomainExtractor.kt | 10 +++++++ .../autoconfig/OkHttpBaseDomainExtractor.kt | 16 ++++++++++ .../OkHttpBaseDomainExtractorTest.kt | 29 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 feature/autodiscovery/autoconfig/src/main/kotlin/app/k9mail/autodiscovery/autoconfig/BaseDomainExtractor.kt create mode 100644 feature/autodiscovery/autoconfig/src/main/kotlin/app/k9mail/autodiscovery/autoconfig/OkHttpBaseDomainExtractor.kt create mode 100644 feature/autodiscovery/autoconfig/src/test/kotlin/app/k9mail/autodiscovery/autoconfig/OkHttpBaseDomainExtractorTest.kt diff --git a/feature/autodiscovery/autoconfig/src/main/kotlin/app/k9mail/autodiscovery/autoconfig/BaseDomainExtractor.kt b/feature/autodiscovery/autoconfig/src/main/kotlin/app/k9mail/autodiscovery/autoconfig/BaseDomainExtractor.kt new file mode 100644 index 000000000..b14e127dd --- /dev/null +++ b/feature/autodiscovery/autoconfig/src/main/kotlin/app/k9mail/autodiscovery/autoconfig/BaseDomainExtractor.kt @@ -0,0 +1,10 @@ +package app.k9mail.autodiscovery.autoconfig + +/** + * Extract the base domain from a host name. + * + * An implementation needs to respect the [Public Suffix List](https://publicsuffix.org/). + */ +interface BaseDomainExtractor { + fun extractBaseDomain(domain: String): String +} diff --git a/feature/autodiscovery/autoconfig/src/main/kotlin/app/k9mail/autodiscovery/autoconfig/OkHttpBaseDomainExtractor.kt b/feature/autodiscovery/autoconfig/src/main/kotlin/app/k9mail/autodiscovery/autoconfig/OkHttpBaseDomainExtractor.kt new file mode 100644 index 000000000..60f0f68a7 --- /dev/null +++ b/feature/autodiscovery/autoconfig/src/main/kotlin/app/k9mail/autodiscovery/autoconfig/OkHttpBaseDomainExtractor.kt @@ -0,0 +1,16 @@ +package app.k9mail.autodiscovery.autoconfig + +import okhttp3.HttpUrl + +class OkHttpBaseDomainExtractor : BaseDomainExtractor { + override fun extractBaseDomain(domain: String): String { + return domain.toHttpUrlOrNull().topPrivateDomain() ?: domain + } + + private fun String.toHttpUrlOrNull(): HttpUrl { + return HttpUrl.Builder() + .scheme("https") + .host(this) + .build() + } +} diff --git a/feature/autodiscovery/autoconfig/src/test/kotlin/app/k9mail/autodiscovery/autoconfig/OkHttpBaseDomainExtractorTest.kt b/feature/autodiscovery/autoconfig/src/test/kotlin/app/k9mail/autodiscovery/autoconfig/OkHttpBaseDomainExtractorTest.kt new file mode 100644 index 000000000..420b915f3 --- /dev/null +++ b/feature/autodiscovery/autoconfig/src/test/kotlin/app/k9mail/autodiscovery/autoconfig/OkHttpBaseDomainExtractorTest.kt @@ -0,0 +1,29 @@ +package app.k9mail.autodiscovery.autoconfig + +import assertk.assertThat +import assertk.assertions.isEqualTo +import org.junit.Test + +class OkHttpBaseDomainExtractorTest { + private val baseDomainExtractor = OkHttpBaseDomainExtractor() + + @Test + fun `basic domain`() { + assertThat(baseDomainExtractor.extractBaseDomain("domain.example")).isEqualTo("domain.example") + } + + @Test + fun `basic subdomain`() { + assertThat(baseDomainExtractor.extractBaseDomain("subdomain.domain.example")).isEqualTo("domain.example") + } + + @Test + fun `domain with public suffix`() { + assertThat(baseDomainExtractor.extractBaseDomain("example.co.uk")).isEqualTo("example.co.uk") + } + + @Test + fun `subdomain with public suffix`() { + assertThat(baseDomainExtractor.extractBaseDomain("subdomain.example.co.uk")).isEqualTo("example.co.uk") + } +}