Scoop API

scoop:scoop.http:0.1.0

src/http1_output.scoop

Selected production source · src/http1_output.scoop

1package scoop.http
3import scoop.bytes.ByteArrayBuilder
5internal enum Http1HeadKind { Request, Response, Metadata, Forbidden, Switch }
7internal fun http1VersionText(version: HttpVersion): String = when (version) {
8 case Http10 -> "HTTP/1.0"
9 case Http11 -> "HTTP/1.1"
10 case Http2 -> throw IllegalArgumentException(Some("HTTP/2 cannot use a textual HTTP/1 encoder"))
13internal fun requireHttp1Method(method: Array<Byte>) {
14 if (!httpToken(method)) throw IllegalArgumentException(Some("invalid HTTP method token"))
17internal fun appendHttp1Field(output: ByteArrayBuilder, field: HttpField) {
18 if (!httpToken(field.name)) throw IllegalArgumentException(Some("invalid HTTP output field name"))
19 requireHttpValue(field.value)
20 output.append(field.name).append(": ".toByteArray()).append(field.value).append("\r\n".toByteArray())
23internal fun http1ResponseKind(head: HttpResponseHead, method: Array<Byte>): Http1HeadKind {
24 if (head.status < 100U || head.status > 999U) throw IllegalArgumentException(Some("HTTP status requires three digits"))
25 if (head.status == 101U || (httpBytesEqual(method, "CONNECT".toByteArray()) && head.status >= 200U && head.status < 300U))
26 return Http1HeadKind.Switch
27 if (head.status < 200U || head.status == 204U) return Http1HeadKind.Forbidden
28 if (head.status == 304U || httpBytesEqual(method, "HEAD".toByteArray())) return Http1HeadKind.Metadata
29 return Http1HeadKind.Response
32internal fun appendHttp1Headers(
33 output: ByteArrayBuilder, fields: Array<HttpField>, version: HttpVersion,
34 framing: Http1BodyFraming, kind: Http1HeadKind
35): Boolean {
36 val lengths = fields.findAll("content-length".toByteArray())
37 val encodings = fields.findAll("transfer-encoding".toByteArray())
38 if (lengths.size > 1L || encodings.size > 1L || (lengths.size != 0L && encodings.size != 0L))
39 throw IllegalArgumentException(Some("conflicting or repeated HTTP body framing fields"))
40 val length: Long? = if (lengths.size == 0L) None else Some(httpContentLength(lengths[0L].value))
41 val bodyless = kind == Http1HeadKind.Metadata || kind == Http1HeadKind.Forbidden || kind == Http1HeadKind.Switch
42 if (bodyless && framing != Http1BodyFraming.Empty)
43 throw IllegalArgumentException(Some("this HTTP response cannot carry a body"))
44 if ((kind == Http1HeadKind.Forbidden || kind == Http1HeadKind.Switch) && lengths.size != 0L)
45 throw IllegalArgumentException(Some("this HTTP response cannot carry Content-Length"))
47 for (field in fields) appendHttp1Field(output, field)
48 when (framing) {
49 case Empty -> {
50 if (encodings.size != 0L) throw IllegalArgumentException(Some("empty HTTP body cannot use Transfer-Encoding"))
51 if (kind != Http1HeadKind.Metadata && length != None && length!! != 0L)
52 throw IllegalArgumentException(Some("Content-Length disagrees with the empty HTTP body"))
53 if (kind == Http1HeadKind.Response && length == None) output.append("Content-Length: 0\r\n".toByteArray())
54 }
55 case Length(val count) -> {
56 if (count < 0L || encodings.size != 0L || (length != None && length!! != count))
57 throw IllegalArgumentException(Some("Content-Length disagrees with HTTP body length"))
58 if (length == None) output.append(("Content-Length: " + count.toString() + "\r\n").toByteArray())
59 }
60 case Chunked -> {
61 if (version != HttpVersion.Http11 || length != None)
62 throw IllegalArgumentException(Some("chunked output requires HTTP/1.1 without Content-Length"))
63 if (encodings.size == 0L) output.append("Transfer-Encoding: chunked\r\n".toByteArray())
64 else requireFinalChunked(encodings[0L].value)
65 }
66 case Eof -> {
67 if (kind != Http1HeadKind.Response || length != None || httpHasToken(fields, "transfer-encoding", "chunked"))
68 throw IllegalArgumentException(Some("EOF framing requires a response without a length or chunked encoding"))
69 if (!httpHasToken(fields, "connection", "close")) output.append("Connection: close\r\n".toByteArray())
70 }
71 }
72 output.append("\r\n".toByteArray())
73 return kind == Http1HeadKind.Switch || framing == Http1BodyFraming.Eof || httpHasToken(fields, "connection", "close") ||
74 (version == HttpVersion.Http10 && !httpHasToken(fields, "connection", "keep-alive"))
77private fun requireFinalChunked(value: Array<Byte>) {
78 val parts = httpCommaParts(value)
79 val chunked = "chunked".toByteArray()
80 if (parts.size == 0L || !httpAsciiEqual(parts[parts.size - 1L], chunked))
81 throw IllegalArgumentException(Some("chunked must be the final HTTP transfer coding"))
82 var index = 0L
83 while (index < parts.size - 1L) {
84 if (parts[index].size == 0L || httpAsciiEqual(parts[index], chunked))
85 throw IllegalArgumentException(Some("invalid HTTP transfer coding order"))
86 index += 1L
87 }