diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:35:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:35:49 +0000 |
commit | d8bbc7858622b6d9c278469aab701ca0b609cddf (patch) | |
tree | eff41dc61d9f714852212739e6b3738b82a2af87 /mobile/android/android-components/plugins/publicsuffixlist | |
parent | Releasing progress-linux version 125.0.3-1~progress7.99u1. (diff) | |
download | firefox-d8bbc7858622b6d9c278469aab701ca0b609cddf.tar.xz firefox-d8bbc7858622b6d9c278469aab701ca0b609cddf.zip |
Merging upstream version 126.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'mobile/android/android-components/plugins/publicsuffixlist')
3 files changed, 178 insertions, 0 deletions
diff --git a/mobile/android/android-components/plugins/publicsuffixlist/build.gradle b/mobile/android/android-components/plugins/publicsuffixlist/build.gradle new file mode 100644 index 0000000000..288e351078 --- /dev/null +++ b/mobile/android/android-components/plugins/publicsuffixlist/build.gradle @@ -0,0 +1,30 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +plugins { + id "org.gradle.kotlin.kotlin-dsl" version "4.2.1" +} + +repositories { + gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository -> + maven { + url repository + if (gradle.mozconfig.substs.ALLOW_INSECURE_GRADLE_REPOSITORIES) { + allowInsecureProtocol = true + } + } + } +} + +dependencies { + implementation ComponentsDependencies.thirdparty_okhttp + implementation ComponentsDependencies.thirdparty_okio +} + +gradlePlugin { + plugins.register("mozac.PublicSuffixListPlugin") { + id = "mozac.PublicSuffixListPlugin" + implementationClass = "PublicSuffixListPlugin" + } +} diff --git a/mobile/android/android-components/plugins/publicsuffixlist/settings.gradle b/mobile/android/android-components/plugins/publicsuffixlist/settings.gradle new file mode 100644 index 0000000000..bcc9e9afc2 --- /dev/null +++ b/mobile/android/android-components/plugins/publicsuffixlist/settings.gradle @@ -0,0 +1,26 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Prevents gradle builds from looking for a root settings.gradle + +pluginManagement { + apply from: file('../../../gradle/mozconfig.gradle') + + repositories { + gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository -> + maven { + url repository + if (gradle.mozconfig.substs.ALLOW_INSECURE_GRADLE_REPOSITORIES) { + allowInsecureProtocol = true + } + } + } + } + + includeBuild("../dependencies") +} + +plugins { + id "mozac.DependenciesPlugin" +} diff --git a/mobile/android/android-components/plugins/publicsuffixlist/src/main/java/PublicSuffixListPlugin.kt b/mobile/android/android-components/plugins/publicsuffixlist/src/main/java/PublicSuffixListPlugin.kt new file mode 100644 index 0000000000..964759c840 --- /dev/null +++ b/mobile/android/android-components/plugins/publicsuffixlist/src/main/java/PublicSuffixListPlugin.kt @@ -0,0 +1,122 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import okhttp3.OkHttpClient +import okhttp3.Request +import okio.ByteString +import okio.ByteString.Companion.encodeUtf8 +import okio.buffer +import okio.sink +import org.gradle.api.Plugin +import org.gradle.api.Project +import java.io.File +import java.util.TreeSet + +/** + * Gradle plugin to update the public suffix list used by the `lib-publicsuffixlist` component. + * + * Base on PublicSuffixListGenerator from OkHttp: + * https://github.com/square/okhttp/blob/master/okhttp/src/test/java/okhttp3/internal/publicsuffix/PublicSuffixListGenerator.java + */ +class PublicSuffixListPlugin : Plugin<Project> { + override fun apply(project: Project) { + project.tasks.register("updatePSL") { + doLast { + val filename = project.projectDir.absolutePath + "/src/main/assets/publicsuffixes" + updatePublicSuffixList(filename) + } + } + } + + private fun updatePublicSuffixList(destination: String) { + val list = fetchPublicSuffixList() + writeListToDisk(destination, list) + } + + private fun writeListToDisk(destination: String, data: PublicSuffixListData) { + val fileSink = File(destination).sink() + + fileSink.buffer().use { sink -> + sink.writeInt(data.totalRuleBytes) + + for (domain in data.sortedRules) { + sink.write(domain).writeByte('\n'.code) + } + + sink.writeInt(data.totalExceptionRuleBytes) + + for (domain in data.sortedExceptionRules) { + sink.write(domain).writeByte('\n'.code) + } + } + } + + private fun fetchPublicSuffixList(): PublicSuffixListData { + val client = OkHttpClient.Builder().build() + + val request = Request.Builder() + .url("https://publicsuffix.org/list/public_suffix_list.dat") + .build() + + client.newCall(request).execute().use { response -> + val source = response.body!!.source() + + val data = PublicSuffixListData() + + while (!source.exhausted()) { + val line = source.readUtf8LineStrict() + + if (line.trim { it <= ' ' }.isEmpty() || line.startsWith("//")) { + continue + } + + if (line.contains(WILDCARD_CHAR)) { + assertWildcardRule(line) + } + + var rule = line.encodeUtf8() + + if (rule.startsWith(EXCEPTION_RULE_MARKER)) { + rule = rule.substring(1) + // We use '\n' for end of value. + data.totalExceptionRuleBytes += rule.size + 1 + data.sortedExceptionRules.add(rule) + } else { + data.totalRuleBytes += rule.size + 1 // We use '\n' for end of value. + data.sortedRules.add(rule) + } + } + + return data + } + } + + @Suppress("TooGenericExceptionThrown", "ThrowsCount") + private fun assertWildcardRule(rule: String) { + if (rule.indexOf(WILDCARD_CHAR) != 0) { + throw RuntimeException("Wildcard is not not in leftmost position") + } + + if (rule.indexOf(WILDCARD_CHAR, 1) != -1) { + throw RuntimeException("Rule contains multiple wildcards") + } + + if (rule.length == 1) { + throw RuntimeException("Rule wildcards the first level") + } + } + + companion object { + private const val WILDCARD_CHAR = "*" + private val EXCEPTION_RULE_MARKER = "!".encodeUtf8() + } +} + +data class PublicSuffixListData( + var totalRuleBytes: Int = 0, + var totalExceptionRuleBytes: Int = 0, + + val sortedRules: TreeSet<ByteString> = TreeSet(), + val sortedExceptionRules: TreeSet<ByteString> = TreeSet(), +) |