Scoop API

scoop:scoop.http.client:0.1.0

src/request.scoop

Selected production source · src/request.scoop

1package scoop.http.client
3import scoop.collections.emptyList
4import scoop.http.*
5import scoop.url.Url
6import scoop.url.UrlOrigin
8/**
9 * An immutable HTTP request with validated method/fields, body plan, and call options. Host and
10 * transfer framing are generated by the client; proxy credentials belong to proxy configuration.
11 */
12public class HttpRequest internal constructor(
13 /**
14 * The validated http or https request URL.
15 */
16 public val url: Url,
17 /**
18 * The case-sensitive HTTP method token.
19 */
20 public val method: String = "GET",
21 fields: List<HttpField> = emptyList<HttpField>(),
22 /**
23 * The body plan, including replayability and metadata.
24 */
25 public val body: RequestBody = RequestBody.Empty,
26 /**
27 * Call-specific overrides and callbacks.
28 */
29 public val options: HttpCallOptions = httpCallOptions()
30) {
31 internal val methodBytes: Array<Byte> = method.toByteArray()
32 internal val origin: UrlOrigin = requestOrigin(url)
33 internal val suppliedFields: Array<HttpField> = fields.toArray()
34 private val metadata: ClientRequestMetadata =
35 prepareRequestFields(methodBytes, body, suppliedFields, options.expectContinue)
36 /**
37 * Prepared regular fields, including metadata derived from the body.
38 */
39 public val fields: Array<HttpField> get() = metadata.fields
40 internal val contentLength: Long? get() = metadata.contentLength
41 internal val expectsContinue: Boolean get() = metadata.expectsContinue
44private fun requestOrigin(url: Url): UrlOrigin {
45 val origin = url.httpOrigin()
46 if (origin.scheme != "http" && origin.scheme != "https")
47 throw IllegalArgumentException(Some("HTTP request URL must use http or https"))
48 return origin
51private class ClientRequestMetadata(
52 val fields: Array<HttpField>,
53 val contentLength: Long?,
54 val expectsContinue: Boolean
57private fun prepareRequestFields(
58 method: Array<Byte>,
59 body: RequestBody,
60 fields: List<HttpField>,
61 expectContinue: Boolean
62): ClientRequestMetadata {
63 if (!isHttpToken(method))
64 throw IllegalArgumentException(Some("invalid HTTP method token"))
65 val snapshot = requestBodyFields(body, fields)
66 for (field in snapshot) {
67 field.validate()
68 if (field.hasName("Host".toByteArray()) || field.hasName("Transfer-Encoding".toByteArray()))
69 throw IllegalArgumentException(
70 Some("HTTP client generates authority and transfer framing")
71 )
72 if (field.hasName("Proxy-Authorization".toByteArray()))
73 throw IllegalArgumentException(Some("proxy credentials belong to the proxy endpoint"))
74 }
75 val length = parseHttpContentLength(snapshot)
76 if (length != None && body.contentLength != None && length!! != body.contentLength!!)
77 throw IllegalArgumentException(Some("Content-Length conflicts with request body length"))
78 val actualLength =
79 if (length != None)
80 length
81 else
82 body.contentLength
83 val present = httpExpectsContinue(snapshot)
84 val adding = expectContinue && actualLength != Some(0L) && !present
85 val prepared =
86 if (adding) {
87 val result = ArrayList<HttpField>()
88 for (field in snapshot)
89 result.add(field)
90 result.add(HttpField("Expect".toByteArray(), "100-continue".toByteArray()))
91 result.toArray()
92 } else
93 snapshot
94 return ClientRequestMetadata(
95 prepared,
96 actualLength,
97 (present || adding) && actualLength != Some(0L)
98 )
101internal fun validateClientRequest(config: HttpClientConfig, request: HttpRequest) {
102 if (request.origin.scheme != "http")
103 return
104 if (
105 config.versionPolicy ==
106 HttpVersionPolicy.Http2Only &&
107 config.cleartext ==
108 HttpCleartextProtocol.Http1
109 )
110 throw IllegalArgumentException(
111 Some("cleartext HTTP/2-only requests require prior knowledge or an explicit upgrade")
112 )
113 if (config.cleartext == HttpCleartextProtocol.Http1)
114 validateClientHttp1Request(request, config.http1RequestVersion)
117internal fun validateClientHttp1Request(request: HttpRequest, version: HttpVersion) {
118 if (request.options.duplex)
119 throw IllegalArgumentException(Some("HTTP/1 cannot perform a duplex upload"))
120 if (version != HttpVersion.Http10)
121 return
122 if (request.body.trailerNames.size != 0L)
123 throw IllegalArgumentException(Some("HTTP/1.0 cannot send request trailers"))
124 if (request.contentLength == None)
125 throw IllegalArgumentException(Some("HTTP/1.0 request body requires a known length"))
126 if (request.expectsContinue)
127 throw IllegalArgumentException(Some("Continue waiting requires HTTP/1.1 or HTTP/2"))