Scoop API

scoop:scoop.url:0.1.0

src/authority.scoop

Selected production source · src/authority.scoop

1package scoop.url
3/**
4 * A parsed authority preserving encoded user information, host, and an explicitly supplied port.
5 */
6public class UrlAuthority internal constructor(
7 /**
8 * Encoded user information, excluding the trailing at sign.
9 */
10 public val encodedUserInfo: String?,
11 /**
12 * The serialized host component, including brackets for an IP literal when applicable.
13 */
14 public val encodedHost: String,
15 /**
16 * The explicit port, or None when omitted.
17 */
18 public val port: Int?,
19 internal val hostKind: Int,
20 internal val resolution: HostResolution
21) : Equals<UrlAuthority>, Hash, ToString {
22 /**
23 * Serializes this authority without the leading double slash.
24 */
25 public override fun toString(): String = render(true, false)
26 /**
27 * Compares the authority's serialized representation.
28 */
29 public override operator fun equals(other: UrlAuthority): Boolean = toString() == other.toString()
30 /**
31 * Hashes the serialized representation used by equality.
32 */
33 public override fun hash(): Long = toString().hash()
35 internal fun render(userInfo: Boolean, iri: Boolean): String {
36 val output = StringBuilder()
37 if (userInfo && encodedUserInfo != None) {
38 output.append(if (iri) iriDisplay(encodedUserInfo!!) else encodedUserInfo!!).append("@")
39 }
40 var host = encodedHost
41 if (iri) {
42 host = when (resolution) {
43 case HostResolution.Dns(val name) -> name.unicode
44 else -> iriDisplay(encodedHost)
45 }
46 }
47 if (hostKind == 2 || hostKind == 3) output.append("[").append(host).append("]")
48 else output.append(host)
49 if (port != None) output.append(":").append(port!!.toString())
50 return output.toString()
51 }
53 /**
54 * Validates and returns the DNS or IP network-host representation.
55 */
56 public fun networkHost(): UrlHost = when (resolution) {
57 case HostResolution.Dns(val name) -> UrlHost.Dns(stripRootDot(name.ascii))
58 case HostResolution.Ipv4(val bytes) -> UrlHost.Ipv4(bytes)
59 case HostResolution.Ipv6(val bytes) -> UrlHost.Ipv6(bytes)
60 case HostResolution.Invalid(val detail, None) -> throw UrlParseException(UrlComponent.Host, None, detail)
61 case HostResolution.Invalid(val detail, Some(val error)) -> throw UrlParseException(UrlComponent.Host, None, detail, Some(error))
62 }
65/**
66 * An HTTP origin identified by normalized scheme, DNS name or IP bytes, and effective port.
67 */
68public class UrlOrigin internal constructor(
69 /**
70 * The normalized HTTP or HTTPS scheme.
71 */
72 public val scheme: String,
73 /**
74 * The normalized DNS or IP host used for origin equality.
75 */
76 public val host: UrlHost,
77 /**
78 * The effective network port, including the scheme default when omitted.
79 */
80 public val port: Int
81) : Equals<UrlOrigin>, Hash, ToString {
82 /**
83 * Compares scheme, normalized host, and effective port.
84 */
85 public override operator fun equals(other: UrlOrigin): Boolean = scheme == other.scheme && host == other.host && port == other.port
86 /**
87 * Hashes the same scheme, host, and port used by equality.
88 */
89 public override fun hash(): Long = (scheme.hash() * 31L + host.hash()) * 31L + port.hash()
90 /**
91 * Serializes the host and port, omitting the scheme default unless explicitly requested.
92 */
93 public fun authority(includeDefaultPort: Boolean = false): String =
94 host.authorityText() + (if (includeDefaultPort || port != defaultUrlPort(scheme)) ":" + port.toString() else "")
95 /**
96 * Serializes the scheme and normalized origin authority.
97 */
98 public override fun toString(): String = scheme + "://" + authority()
101internal fun isNetworkScheme(scheme: String?): Boolean =
102 scheme != None && (scheme!! == "http" || scheme!! == "https" || scheme!! == "ws" || scheme!! == "wss")
104internal fun defaultUrlPort(scheme: String): Int = if (scheme == "https" || scheme == "wss") 443 else 80
106internal fun urlPort(text: String?): Int? {
107 if (text == None) return None
108 if (text!!.charLength == 0L) urlError(UrlComponent.Port, "explicit port cannot be empty")
109 var value = 0
110 for (character in text!!) {
111 val digit = character.code - 48
112 if (digit < 0 || digit > 9 || value > 6553 || (value == 6553 && digit > 5)) {
113 urlError(UrlComponent.Port, "port must be decimal in 0..65535")
114 }
115 value = value * 10 + digit
116 }
117 return Some(value)