scoop:scoop.url:0.1.0
src/url.scoop
Selected production source · src/url.scoop
1
package scoop.url3
import scoop.collections.emptyList5
/**6
* An immutable URI/IRI reference. Parsing accepts relative references and non-HTTP schemes without7
* network I/O. Equality and ordering use normalized ASCII serialization; parsing follows a strict8
* profile without browser-style recovery.9
*/10
public class Url internal constructor(11
/**12
* The normalized scheme, or None for a relative reference.13
*/14
public val scheme: String?,15
/**16
* The parsed authority when the reference contains one.17
*/18
public val authority: UrlAuthority?,19
/**20
* The percent-encoded path, retaining reserved characters as structural data.21
*/22
public val encodedPath: String,23
/**24
* The encoded query without the leading question mark; None differs from an empty query.25
*/26
public val encodedQuery: String?,27
/**28
* The encoded fragment without the leading hash; None differs from an empty fragment.29
*/30
public val encodedFragment: String?,31
private val serialized: String32
): Equals<Url>, Compare<Url>, Hash, ToString {33
/**34
* Returns the normalized ASCII URI serialization.35
*/36
public override fun toString(): String = serialized37
/**38
* Compares normalized ASCII serializations.39
*/40
public override operator fun equals(other: Url): Boolean = serialized == other.serialized41
/**42
* Orders normalized ASCII serializations lexicographically.43
*/44
public override operator fun compareTo(other: Url): Long =45
serialized.compareTo(other.serialized)46
/**47
* Hashes the same normalized serialization used by equality.48
*/49
public override fun hash(): Long = serialized.hash()51
/**52
* Resolves a relative reference against this URL using URI reference-resolution rules.53
*/54
public fun resolve(reference: String): Url = resolve(Url.parse(reference))55
/**56
* Resolves a relative reference against this URL using URI reference-resolution rules.57
*/58
public fun resolve(reference: Url): Url = resolveNativeUrl(this, reference)59
/**60
* Configures a copy of these components and returns an independent immutable URL.61
*/62
public fun copy(configure: UrlBuilder.() -> Unit = {}): Url {63
val builder = UrlBuilder.from(this)64
builder.configure()65
return builder.snapshot()66
}68
/**69
* Validates an HTTP or HTTPS network target and computes its normalized host and effective70
* port. Invalid DNS names, compatibility numeric addresses, port zero, and IPvFuture are71
* rejected.72
*/73
public fun httpOrigin(): UrlOrigin {74
if (!isNetworkScheme(scheme))75
urlError(UrlComponent.Scheme, "HTTP target requires http, https, ws or wss")76
if (authority == None)77
urlError(UrlComponent.Authority, "HTTP target requires an authority")78
val value = authority!!79
val port =80
if (value.port == None)81
defaultUrlPort(scheme!!)82
else83
value.port!!84
if (port == 0)85
urlError(UrlComponent.Port, "HTTP target port cannot be zero")86
return UrlOrigin(scheme!!, value.networkHost(), port)87
}89
/**90
* Returns origin-form path and query, excluding user information and fragment.91
*/92
public fun httpRequestTarget(): String {93
httpOrigin()94
return (95
if (encodedPath.charLength == 0L)96
"/"97
else98
encodedPath99
) +100
(101
if (encodedQuery == None)102
""103
else104
"?" + encodedQuery!!105
)106
}108
/**109
* Returns the absolute HTTP request target for a proxy, excluding user information and110
* fragment.111
*/112
public fun httpAbsoluteTarget(): String {113
val target = httpRequestTarget()114
return scheme!! + "://" + authority!!.render(false, false) + target115
}117
/**118
* Parses the encoded query as ordered parameters, with ordinary URI plus semantics.119
*/120
public fun queryParameters(): UrlQuery =121
UrlQuery.parse(122
if (encodedQuery == None)123
""124
else125
encodedQuery!!126
)128
/**129
* Returns a display-oriented IRI, retaining escapes for reserved characters such as encoded130
* slashes.131
*/132
public fun toIriString(): String {133
val output = StringBuilder()134
if (scheme != None)135
output.append(scheme!!).append(":")136
if (authority != None)137
output.append("//").append(authority!!.render(true, true))138
output.append(iriDisplay(encodedPath))139
if (encodedQuery != None)140
output.append("?").append(iriDisplay(encodedQuery!!, true))141
if (encodedFragment != None)142
output.append("#").append(iriDisplay(encodedFragment!!))143
return output.toString()144
}146
/**147
* Removes user information and masks values of the specified query names for presentation.148
*/149
public fun redacted(queryNames: List<String> = emptyList<String>()): String {150
val redactedQuery =151
if (encodedQuery != None && queryNames.size > 0L) {152
val entries = ArrayList<UrlQueryParameter>()153
for (entry in queryParameters().entries) {154
var hide = false155
for (name in queryNames) {156
if (urlBytesEqual(entry.nameBytes, name.toByteArray()))157
hide = true158
}159
entries.add(160
if (hide && hasUrlValue(entry.valueBytes))161
UrlQueryParameter(162
entry.nameBytes,163
Some<List<Byte>>("[redacted]".toByteArray())164
)165
else166
entry167
)168
}169
Some(UrlQuery(entries).toString())170
} else171
encodedQuery172
return copy {173
encodedUserInfo = None174
encodedQuery = redactedQuery175
}.toString()176
}178
/**179
* Strict URI/IRI parsing and parser-backend version information.180
*/181
public companion object: ValueEncoder<Url>, ValueDecoder<Url> {182
/** Encodes the existing normalized ASCII URI, preserving all of its components. */183
public override fun encode(value: Url, encoder: Encoder) {184
encoder.singleValue().writeString(value.toString())185
}187
/** Decodes a URI reference without imposing network or scheme-specific restrictions. */188
public override fun decode(decoder: Decoder): Url =189
when (parseOrNone(decoder.singleValue().readString())) {190
case Some(val value) -> value191
case None -> throw DecodingException(decoder.path, "invalid URL")192
}194
/**195
* Parses a complete URI/IRI reference. Relative input stays relative; no scheme is196
* guessed.197
*/198
public fun parse(text: String): Url = parseNativeUrl(prepareIri(text))199
/**200
* Returns None only for an ordinary URL format failure. Backend failures still propagate.201
*/202
public fun parseOrNone(text: String): Url? {203
try {204
return Some(parse(text))205
}206
catch (_: UrlParseException) {207
return None208
}209
}210
/**211
* The version reported by the linked URI parser backend.212
*/213
public val backendVersion: String get() {214
@Unsafe215
{216
return String.fromCString(urlVersion())217
}218
}219
}220
}