Scoop API

scoop:scoop.url:0.1.0

src/builder.scoop

Selected production source · src/builder.scoop

1package scoop.url
3import scoop.text.*
5/** Configures an empty URI reference and returns an independent immutable URL. */
6public fun url(configure: UrlBuilder.() -> Unit = {}): Url {
7 val builder = UrlBuilder()
8 builder.configure()
9 return builder.snapshot()
12/**
13 * Mutable URI components used by url and Url.copy. Encoded properties preserve escaped data;
14 * raw-text operations encode their input. Each entry point validates the assembled reference.
15 */
16public class UrlBuilder internal constructor() {
17 private var configuredScheme: String? = None
18 private var configuredHost: String? = None
19 private var configuredUserInfo: String? = None
20 private var configuredPort: Int? = None
21 private var configuredPath: String = ""
22 private var configuredQuery: String? = None
23 private var configuredFragment: String? = None
24 private val reusable: ArrayList<UrlAuthority> = ArrayList<UrlAuthority>()
26 /** The scheme, normalized to ASCII lowercase; None produces a relative reference. */
27 public var scheme: String?
28 get() = configuredScheme
29 set(value) {
30 configuredScheme =
31 if (value == None)
32 None
33 else
34 Some(canonicalEncoded(value!!, UrlComponent.Scheme).lowercaseAscii())
35 }
37 /** The encoded host, including brackets for IP literals; None removes the host. */
38 public var encodedHost: String?
39 get() = configuredHost
40 set(value) {
41 configuredHost = encodedOptional(value, UrlComponent.Host)
42 }
44 /** Encoded user information, or None to clear it. */
45 public var encodedUserInfo: String?
46 get() = configuredUserInfo
47 set(value) {
48 configuredUserInfo = encodedOptional(value, UrlComponent.UserInfo)
49 }
51 /** An explicit port in 0..65535, or None to omit it. */
52 public var port: Int?
53 get() = configuredPort
54 set(value) {
55 if (value != None && (value!! < 0 || value!! > 65535))
56 urlError(UrlComponent.Port, "port must be in 0..65535")
57 configuredPort = value
58 }
60 /** The complete encoded path, retaining escaped reserved characters as data. */
61 public var encodedPath: String
62 get() = configuredPath
63 set(value) {
64 configuredPath = canonicalEncoded(value, UrlComponent.Path)
65 }
67 /** The encoded query without a leading question mark; None differs from an empty query. */
68 public var encodedQuery: String?
69 get() = configuredQuery
70 set(value) {
71 configuredQuery = encodedOptional(value, UrlComponent.Query)
72 }
74 /** The encoded fragment without a leading hash; None differs from an empty fragment. */
75 public var encodedFragment: String?
76 get() = configuredFragment
77 set(value) {
78 configuredFragment = encodedOptional(value, UrlComponent.Fragment)
79 }
81 /** Encodes a raw Unicode host, adding brackets around a bare IPv6 address. */
82 public fun host(value: String) {
83 val encoded = encodeUrlBytes(value.toByteArray(), false, "[]:")
84 configuredHost =
85 Some(
86 if (encoded.contains(':') && !encoded.startsWith('['))
87 "[" + encoded + "]"
88 else
89 encoded
90 )
91 }
93 /** Replaces all authority components, or clears host, user information and port together. */
94 public fun encodedAuthority(value: String?) {
95 if (value == None) {
96 configuredHost = None
97 configuredUserInfo = None
98 configuredPort = None
99 return
100 }
101 val parsed = Url.parse("//" + value!!)
102 if (
103 parsed.encodedPath.charLength !=
104 0L ||
105 parsed.encodedQuery !=
106 None ||
107 parsed.encodedFragment !=
108 None
109 )
110 urlError(UrlComponent.Authority, "authority must not contain a path, query or fragment")
111 copyAuthority(parsed.authority!!)
112 }
114 /** Encodes raw user information, or clears it when value is None. */
115 public fun userInfo(value: String?) {
116 encodedUserInfo =
117 if (value == None)
118 None
119 else
120 Some(PercentEncoding.encode(value!!))
121 }
123 /** Encodes a complete raw path, preserving slashes as path separators. */
124 public fun path(value: String) {
125 encodedPath = encodeUrlBytes(value.toByteArray(), false, "/!$&'()*+,;=:@")
126 }
128 /** Appends one raw segment, encoding separators and dot or dot-dot segments as data. */
129 public fun appendPathSegment(value: String) {
130 val encoded =
131 if (value == ".")
132 "%2E"
133 else
134 if (value == "..")
135 "%2E%2E"
136 else
137 PercentEncoding.encode(value)
138 appendEncodedPathSegment(encoded)
139 }
141 /** Appends an encoded segment without accepting literal structural separators. */
142 public fun appendEncodedPathSegment(value: String) {
143 val segment = canonicalEncoded(value, UrlComponent.Path)
144 if (segment.contains('/') || segment.contains('?') || segment.contains('#'))
145 urlError(
146 UrlComponent.Path,
147 "an encoded path segment cannot contain structural separators"
148 )
149 if (
150 (configuredPath.charLength == 0L && configuredHost != None) ||
151 (configuredPath.charLength > 0L && !configuredPath.endsWith('/'))
152 )
153 configuredPath += "/"
154 configuredPath += segment
155 }
157 /** Encodes whole raw query text without interpreting name/value pairs; None clears it. */
158 public fun query(value: String?) {
159 encodedQuery =
160 if (value == None)
161 None
162 else
163 Some(PercentEncoding.encode(value!!))
164 }
166 /** Replaces the query using ordinary URI parameter serialization. */
167 public fun queryParameters(value: UrlQuery) {
168 encodedQuery = Some(value.toString())
169 }
171 /** Encodes a raw fragment, or clears it when value is None. */
172 public fun fragment(value: String?) {
173 encodedFragment =
174 if (value == None)
175 None
176 else
177 Some(PercentEncoding.encode(value!!))
178 }
180 internal fun snapshot(): Url {
181 @Unsafe
182 {
183 val result = urlEmpty()
184 checkUriCode(result.code, UrlComponent.Reference)
185 val handle = result.value!!
186 try {
187 if (configuredScheme != None)
188 setNativeUrlPart(handle, 0, configuredScheme, UrlComponent.Scheme)
189 if (configuredHost != None)
190 setNativeUrlPart(handle, 2, configuredHost, UrlComponent.Host)
191 if (configuredUserInfo != None)
192 setNativeUrlPart(handle, 1, configuredUserInfo, UrlComponent.UserInfo)
193 if (configuredPort != None)
194 setNativeUrlPart(
195 handle,
196 3,
197 Some(configuredPort!!.toString()),
198 UrlComponent.Port
199 )
200 setNativeUrlPart(handle, 4, Some(configuredPath), UrlComponent.Path)
201 if (configuredQuery != None)
202 setNativeUrlPart(handle, 5, configuredQuery, UrlComponent.Query)
203 if (configuredFragment != None)
204 setNativeUrlPart(handle, 6, configuredFragment, UrlComponent.Fragment)
205 return snapshotUrl(handle, reusable)
206 } finally {
207 urlFree(handle)
208 }
209 }
210 }
212 private fun copyAuthority(value: UrlAuthority) {
213 configuredHost =
214 Some(
215 if (value.hostKind == 2 || value.hostKind == 3)
216 "[" + value.encodedHost + "]"
217 else
218 value.encodedHost
219 )
220 configuredUserInfo = value.encodedUserInfo
221 configuredPort = value.port
222 reusable.add(value)
223 }
225 internal companion object {
226 internal fun from(value: Url): UrlBuilder {
227 val builder = UrlBuilder()
228 builder.configuredScheme = value.scheme
229 builder.configuredPath = value.encodedPath
230 builder.configuredQuery = value.encodedQuery
231 builder.configuredFragment = value.encodedFragment
232 if (value.authority != None)
233 builder.copyAuthority(value.authority!!)
234 return builder
235 }
236 }
239private fun encodedOptional(value: String?, component: UrlComponent): String? =
240 if (value == None)
241 None
242 else
243 Some(canonicalEncoded(value!!, component))