-
Notifications
You must be signed in to change notification settings - Fork 1k
/
URLBuilder.kt
184 lines (160 loc) · 5.05 KB
/
URLBuilder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.http
/**
* Select default port value from protocol.
*/
const val DEFAULT_PORT: Int = 0
/**
* A URL builder with all mutable components
*
* @property protocol URL protocol (scheme)
* @property host name without port (domain)
* @property port port number
* @property user username part (optional)
* @property password password part (optional)
* @property encodedPath encoded URL path without query
* @property parameters URL query parameters
* @property fragment URL fragment (anchor name)
* @property trailingQuery keep a trailing question character even if there are no query parameters
*/
class URLBuilder(
var protocol: URLProtocol = URLProtocol.HTTP,
var host: String = originHost,
var port: Int = DEFAULT_PORT,
var user: String? = null,
var password: String? = null,
var encodedPath: String = "/",
val parameters: ParametersBuilder = ParametersBuilder(),
var fragment: String = "",
var trailingQuery: Boolean = false
) {
/**
* Encode [components] to [encodedPath]
*/
fun path(vararg components: String): URLBuilder {
path(components.asList())
return this
}
/**
* Encode [components] to [encodedPath]
*/
fun path(components: List<String>): URLBuilder {
encodedPath = components.joinToString("/", prefix = "/") { it.encodeURLQueryComponent() }
return this
}
private fun <A : Appendable> appendTo(out: A): A {
out.append(protocol.name)
out.append("://")
out.append(authority)
out.appendUrlFullPath(encodedPath, parameters.build(), trailingQuery)
if (fragment.isNotEmpty()) {
out.append('#')
out.append(fragment.encodeURLQueryComponent())
}
return out
}
/**
* Build a URL string
*/
// note: 256 should fit 99.5% of all urls according to http://www.supermind.org/blog/740/average-length-of-a-url-part-2
fun buildString(): String = appendTo(StringBuilder(256)).toString()
/**
* Build a [Url] instance (everything is copied to a new instance)
*/
fun build(): Url = Url(
protocol, host, port, encodedPath, parameters.build(), fragment, user, password, trailingQuery
)
// Required to write external extension function
companion object
}
/**
* Hostname of current origin.
*
* It uses "localhost" for all platforms except js.
*/
expect val URLBuilder.Companion.originHost: String
/**
* Create a copy of this builder. Modifications in a copy is not reflected in the original instance and vise-versa.
*/
fun URLBuilder.clone(): URLBuilder = URLBuilder().takeFrom(this)
/**
* Represents an immutable URL
*
* @property protocol
* @property host name without port (domain)
* @property port the specified port or protocol default port
* @property specifiedPort port number that was specified to override protocol's default
* @property encodedPath encoded path without query string
* @property parameters URL query parameters
* @property fragment URL fragment (anchor name)
* @property user username part of URL
* @property password password part of URL
* @property trailingQuery keep trailing question character even if there are no query parameters
*/
data class Url(
val protocol: URLProtocol,
val host: String,
val specifiedPort: Int,
val encodedPath: String,
val parameters: Parameters,
val fragment: String,
val user: String?,
val password: String?,
val trailingQuery: Boolean
) {
init {
require(specifiedPort in 1..65536 || specifiedPort == DEFAULT_PORT) { "port must be between 1 and 65536, or $DEFAULT_PORT if not set" }
}
val port: Int get() = specifiedPort.takeUnless { it == DEFAULT_PORT } ?: protocol.defaultPort
override fun toString(): String = buildString {
append(protocol.name)
append("://")
append(authority)
append(fullPath)
if (fragment.isNotEmpty()) {
append('#')
append(fragment)
}
}
companion object
}
/**
* [Url] authority.
*/
val Url.authority: String
get() = buildString {
if (user != null) {
append(user.encodeURLParameter())
if (password != null) {
append(':')
append(password.encodeURLParameter())
}
append('@')
}
if (specifiedPort == DEFAULT_PORT) {
append(host)
} else {
append(hostWithPort)
}
}
/**
* [URLBuilder] authority.
*/
val URLBuilder.authority: String
get() = buildString {
user?.let { user ->
append(user.encodeURLParameter())
password?.let { password ->
append(":")
append(password.encodeURLParameter())
}
append("@")
}
append(host)
if (port != DEFAULT_PORT && port != protocol.defaultPort) {
append(":")
append(port.toString())
}
}