/
Request.kt
335 lines (286 loc) · 10.5 KB
/
Request.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Copyright (C) 2013 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3
import java.net.URL
import kotlin.reflect.KClass
import kotlin.reflect.cast
import okhttp3.Headers.Companion.headersOf
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.internal.canonicalUrl
import okhttp3.internal.commonAddHeader
import okhttp3.internal.commonCacheControl
import okhttp3.internal.commonDelete
import okhttp3.internal.commonEmptyRequestBody
import okhttp3.internal.commonGet
import okhttp3.internal.commonHead
import okhttp3.internal.commonHeader
import okhttp3.internal.commonHeaders
import okhttp3.internal.commonMethod
import okhttp3.internal.commonPatch
import okhttp3.internal.commonPost
import okhttp3.internal.commonPut
import okhttp3.internal.commonRemoveHeader
import okhttp3.internal.commonTag
import okhttp3.internal.commonToString
/**
* An HTTP request. Instances of this class are immutable if their [body] is null or itself
* immutable.
*/
class Request internal constructor(builder: Builder) {
@get:JvmName("url")
val url: HttpUrl = checkNotNull(builder.url) { "url == null" }
@get:JvmName("method")
val method: String = builder.method
@get:JvmName("headers")
val headers: Headers = builder.headers.build()
@get:JvmName("body")
val body: RequestBody? = builder.body
@get:JvmName("cacheUrlOverride")
val cacheUrlOverride: HttpUrl? = builder.cacheUrlOverride
internal val tags: Map<KClass<*>, Any> = builder.tags.toMap()
internal var lazyCacheControl: CacheControl? = null
val isHttps: Boolean
get() = url.isHttps
/**
* Constructs a new request.
*
* Use [Builder] for more fluent construction, including helper methods for various HTTP methods.
*
* @param method defaults to "GET" if [body] is null, and "POST" otherwise.
*/
constructor(
url: HttpUrl,
headers: Headers = headersOf(),
// '\u0000' is a sentinel value that'll choose based on what the body is:
method: String = "\u0000",
body: RequestBody? = null,
) : this(
Builder()
.url(url)
.headers(headers)
.method(
when {
method != "\u0000" -> method
body != null -> "POST"
else -> "GET"
},
body,
),
)
fun header(name: String): String? = commonHeader(name)
fun headers(name: String): List<String> = commonHeaders(name)
/** Returns the tag attached with [T] as a key, or null if no tag is attached with that key. */
@JvmName("reifiedTag")
inline fun <reified T : Any> tag(): T? = tag(T::class)
/** Returns the tag attached with [type] as a key, or null if no tag is attached with that key. */
fun <T : Any> tag(type: KClass<T>): T? = type.java.cast(tags[type])
/**
* Returns the tag attached with `Object.class` as a key, or null if no tag is attached with
* that key.
*
* Prior to OkHttp 3.11, this method never returned null if no tag was attached. Instead it
* returned either this request, or the request upon which this request was derived with
* [newBuilder].
*
* @suppress this method breaks Dokka! https://github.com/Kotlin/dokka/issues/2473
*/
fun tag(): Any? = tag<Any>()
/**
* Returns the tag attached with [type] as a key, or null if no tag is attached with that
* key.
*/
fun <T> tag(type: Class<out T>): T? = tag(type.kotlin)
fun newBuilder(): Builder = Builder(this)
/**
* Returns the cache control directives for this response. This is never null, even if this
* response contains no `Cache-Control` header.
*/
@get:JvmName("cacheControl")
val cacheControl: CacheControl
get() {
var result = lazyCacheControl
if (result == null) {
result = CacheControl.parse(headers)
lazyCacheControl = result
}
return result
}
@JvmName("-deprecated_url")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "url"),
level = DeprecationLevel.ERROR,
)
fun url(): HttpUrl = url
@JvmName("-deprecated_method")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "method"),
level = DeprecationLevel.ERROR,
)
fun method(): String = method
@JvmName("-deprecated_headers")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "headers"),
level = DeprecationLevel.ERROR,
)
fun headers(): Headers = headers
@JvmName("-deprecated_body")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "body"),
level = DeprecationLevel.ERROR,
)
fun body(): RequestBody? = body
@JvmName("-deprecated_cacheControl")
@Deprecated(
message = "moved to val",
replaceWith = ReplaceWith(expression = "cacheControl"),
level = DeprecationLevel.ERROR,
)
fun cacheControl(): CacheControl = cacheControl
override fun toString(): String = commonToString()
open class Builder {
internal var url: HttpUrl? = null
internal var method: String
internal var headers: Headers.Builder
internal var body: RequestBody? = null
internal var cacheUrlOverride: HttpUrl? = null
/** A mutable map of tags, or an immutable empty map if we don't have any. */
internal var tags = mapOf<KClass<*>, Any>()
constructor() {
this.method = "GET"
this.headers = Headers.Builder()
}
internal constructor(request: Request) {
this.url = request.url
this.method = request.method
this.body = request.body
this.tags =
when {
request.tags.isEmpty() -> mapOf()
else -> request.tags.toMutableMap()
}
this.headers = request.headers.newBuilder()
this.cacheUrlOverride = request.cacheUrlOverride
}
open fun url(url: HttpUrl): Builder =
apply {
this.url = url
}
/**
* Sets the URL target of this request.
*
* @throws IllegalArgumentException if [url] is not a valid HTTP or HTTPS URL. Avoid this
* exception by calling [HttpUrl.parse]; it returns null for invalid URLs.
*/
open fun url(url: String): Builder {
return url(canonicalUrl(url).toHttpUrl())
}
/**
* Sets the URL target of this request.
*
* @throws IllegalArgumentException if the scheme of [url] is not `http` or `https`.
*/
open fun url(url: URL) = url(url.toString().toHttpUrl())
/**
* Sets the header named [name] to [value]. If this request already has any headers
* with that name, they are all replaced.
*/
open fun header(
name: String,
value: String,
) = commonHeader(name, value)
/**
* Adds a header with [name] and [value]. Prefer this method for multiply-valued
* headers like "Cookie".
*
* Note that for some headers including `Content-Length` and `Content-Encoding`,
* OkHttp may replace [value] with a header derived from the request body.
*/
open fun addHeader(
name: String,
value: String,
) = commonAddHeader(name, value)
/** Removes all headers named [name] on this builder. */
open fun removeHeader(name: String) = commonRemoveHeader(name)
/** Removes all headers on this builder and adds [headers]. */
open fun headers(headers: Headers) = commonHeaders(headers)
/**
* Sets this request's `Cache-Control` header, replacing any cache control headers already
* present. If [cacheControl] doesn't define any directives, this clears this request's
* cache-control headers.
*/
open fun cacheControl(cacheControl: CacheControl): Builder = commonCacheControl(cacheControl)
open fun get(): Builder = commonGet()
open fun head(): Builder = commonHead()
open fun post(body: RequestBody): Builder = commonPost(body)
@JvmOverloads
open fun delete(body: RequestBody? = commonEmptyRequestBody): Builder = commonDelete(body)
open fun put(body: RequestBody): Builder = commonPut(body)
open fun patch(body: RequestBody): Builder = commonPatch(body)
open fun method(
method: String,
body: RequestBody?,
): Builder = commonMethod(method, body)
/**
* Attaches [tag] to the request using [T] as a key. Tags can be read from a request using
* [Request.tag]. Use null to remove any existing tag assigned for [T].
*
* Use this API to attach timing, debugging, or other application data to a request so that
* you may read it in interceptors, event listeners, or callbacks.
*/
@JvmName("reifiedTag")
inline fun <reified T : Any> tag(tag: T?): Builder = tag(T::class, tag)
/**
* Attaches [tag] to the request using [type] as a key. Tags can be read from a request using
* [Request.tag]. Use null to remove any existing tag assigned for [type].
*
* Use this API to attach timing, debugging, or other application data to a request so that
* you may read it in interceptors, event listeners, or callbacks.
*/
fun <T : Any> tag(
type: KClass<T>,
tag: T?,
): Builder = commonTag(type, type.cast(tag))
/** Attaches [tag] to the request using `Object.class` as a key. */
open fun tag(tag: Any?): Builder = commonTag(Any::class, tag)
/**
* Attaches [tag] to the request using [type] as a key. Tags can be read from a
* request using [Request.tag]. Use null to remove any existing tag assigned for [type].
*
* Use this API to attach timing, debugging, or other application data to a request so that
* you may read it in interceptors, event listeners, or callbacks.
*/
open fun <T> tag(
type: Class<in T>,
tag: T?,
) = commonTag(type.kotlin, tag)
/**
* Override the [Request.url] for caching, if it is either polluted with
* transient query params, or has a canonical URL possibly for a CDN.
*
* Note that POST requests will not be sent to the server if this URL is set
* and matches a cached response.
*/
fun cacheUrlOverride(cacheUrlOverride: HttpUrl?) =
apply {
this.cacheUrlOverride = cacheUrlOverride
}
open fun build(): Request = Request(this)
}
}