Scoop API

scoop:scoop.http:0.1.0

src/cookie/public_suffix.scoop

Selected production source · src/cookie/public_suffix.scoop

1package scoop.http
3import scoop.collections.*
4import scoop.idna.*
5import scoop.text.*
6import scoop.url.UrlOrigin
8/**
9 * The normalized domain, effective public suffix, optional registrable domain, and matched rule
10 * metadata.
11 */
12public class PublicSuffixMatch internal constructor(
13 /**
14 * The normalized ASCII domain without a trailing dot.
15 */
16 public val domain: String,
17 /**
18 * The effective public suffix selected by the rule set.
19 */
20 public val publicSuffix: String,
21 /**
22 * The public suffix plus one preceding label, or None if no such label exists.
23 */
24 public val registrableDomain: String?,
25 /**
26 * The selected exact, wildcard, or exception rule, when explicitly present.
27 */
28 public val rule: String?,
29 /**
30 * Whether the selected rule came from the private section.
31 */
32 public val isPrivate: Boolean
35/**
36 * An in-memory public-suffix rule set supporting exact, wildcard, exception, and optional
37 * private-section rules. Lookup performs no network update.
38 */
39public class PublicSuffixList internal constructor(
40 private val rules: Map<String, Int>,
41 /**
42 * The number of parsed suffix rules.
43 */
44 public val ruleCount: Long,
45 /**
46 * The version marker supplied by the rule-list text, if present.
47 */
48 public val version: String?,
49 /**
50 * The revision marker supplied by the rule-list text, if present.
51 */
52 public val revision: String?
53) {
54 /**
55 * Applies suffix rules to a normalized DNS name; URL-origin overloads return None for IP
56 * hosts. Private rules are included by default.
57 */
58 public fun lookup(domain: String, includePrivate: Boolean = true): PublicSuffixMatch =
59 lookupAscii(Idna.toAscii(domain), includePrivate)
60 /**
61 * Applies suffix rules to a normalized DNS name; URL-origin overloads return None for IP
62 * hosts. Private rules are included by default.
63 */
64 public fun lookup(name: IdnaName, includePrivate: Boolean = true): PublicSuffixMatch =
65 lookupAscii(name.ascii, includePrivate)
66 /**
67 * Applies suffix rules to a normalized DNS name; URL-origin overloads return None for IP
68 * hosts. Private rules are included by default.
69 */
70 public fun lookup(origin: UrlOrigin, includePrivate: Boolean = true): PublicSuffixMatch? = when (origin.host) {
71 case Dns(val name) -> Some(lookupAscii(name, includePrivate))
72 case Ipv4(_) -> None
73 case Ipv6(_) -> None
74 }
75 /**
76 * Returns None for invalid domain input, otherwise performs suffix lookup with the selected
77 * private-rule policy.
78 */
79 public fun lookupOrNone(domain: String, includePrivate: Boolean = true): PublicSuffixMatch? {
80 val ascii = Idna.toAsciiOrNone(domain)
81 if (ascii == None || ascii!! == ".") return None
82 return Some(lookupAscii(ascii!!, includePrivate))
83 }
85 private fun lookupAscii(input: String, includePrivate: Boolean): PublicSuffixMatch {
86 val domain = if (input.endsWith(".")) input.slice(0L, input.charLength - 1L) else input
87 if (domain.charLength == 0L) throw IllegalArgumentException(Some("public suffix lookup requires a host name"))
88 val labels = domain.split('.')
89 var selected = labels.size - 1L
90 var rule: String? = None
91 var privateRule = false
92 var exception = -1L
93 var exceptionPrivate = false
94 var suffix = ""
95 var index = labels.size - 1L
96 while (index >= 0L) {
97 suffix = labels[index] + (if (suffix.charLength == 0L) "" else "." + suffix)
98 val flags = rules.getOrDefault(suffix, 0) and (if (includePrivate) 63 else 7)
99 if ((flags and 36) != 0) { exception = index; exceptionPrivate = (flags and 4) == 0 }
100 if ((flags and 9) != 0 && index <= selected) {
101 selected = index; rule = Some(suffix); privateRule = (flags and 1) == 0
102 }
103 if ((flags and 18) != 0 && index > 0L && index - 1L < selected) {
104 selected = index - 1L; rule = Some("*." + suffix); privateRule = (flags and 2) == 0
105 }
106 index -= 1L
107 }
108 if (exception >= 0L) {
109 selected = exception + 1L
110 rule = Some("!" + publicSuffixTail(labels, exception))
111 privateRule = exceptionPrivate
112 } else if (rule == None) {
113 val flags = rules.getOrDefault("", 0) and (if (includePrivate) 18 else 2)
114 if (flags != 0) { rule = Some("*"); privateRule = (flags and 2) == 0 }
115 }
116 return PublicSuffixMatch(domain, publicSuffixTail(labels, selected),
117 if (selected == 0L) None else Some(publicSuffixTail(labels, selected - 1L)), rule, privateRule)
118 }
120 /**
121 * Rule-list parsing and access to the bundled snapshot.
122 */
123 public companion object {
124 /**
125 * Parses public-suffix list text, including section metadata and wildcard/exception rules.
126 */
127 public fun parse(text: String): PublicSuffixList = parsePublicSuffixList(text)
128 /**
129 * Parses the public-suffix snapshot bundled with the library; it does not fetch network
130 * updates.
131 */
132 public fun bundled(): PublicSuffixList {
133 @Unsafe { return parse(String.fromUtf8(httpBytes(httpPublicSuffixData()))) }
134 }
135 }
138private fun publicSuffixTail(labels: List<String>, start: Long): String {
139 val output = StringBuilder()
140 for (index in start..<labels.size) {
141 if (index > start) output.append(".")
142 output.append(labels[index])
143 }
144 return output.toString()
147@Extern(name = "scoop_http_public_suffix_data") internal fun httpPublicSuffixData(): NativeHttpBytes