scoop:scoop.http.client:0.1.0
src/request.scoop
Selected production source · src/request.scoop
1
package scoop.http.client3
import scoop.collections.emptyList4
import scoop.http.*5
import scoop.url.Url6
import scoop.url.UrlOrigin8
/**9
* An immutable HTTP request with validated method/fields, body plan, and call options. Host and10
* transfer framing are generated by the client; proxy credentials belong to proxy configuration.11
*/12
public 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.fields40
internal val contentLength: Long? get() = metadata.contentLength41
internal val expectsContinue: Boolean get() = metadata.expectsContinue42
}44
private 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 origin49
}51
private class ClientRequestMetadata(52
val fields: Array<HttpField>,53
val contentLength: Long?,54
val expectsContinue: Boolean55
)57
private fun prepareRequestFields(58
method: Array<Byte>,59
body: RequestBody,60
fields: List<HttpField>,61
expectContinue: Boolean62
): 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
length81
else82
body.contentLength83
val present = httpExpectsContinue(snapshot)84
val adding = expectContinue && actualLength != Some(0L) && !present85
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
} else93
snapshot94
return ClientRequestMetadata(95
prepared,96
actualLength,97
(present || adding) && actualLength != Some(0L)98
)99
}101
internal fun validateClientRequest(config: HttpClientConfig, request: HttpRequest) {102
if (request.origin.scheme != "http")103
return104
if (105
config.versionPolicy ==106
HttpVersionPolicy.Http2Only &&107
config.cleartext ==108
HttpCleartextProtocol.Http1109
)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)115
}117
internal 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
return122
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"))128
}