Scoop API

scoop:scoop.http:0.1.0

src/fields.scoop

Selected production source · src/fields.scoop

1package scoop.http
3internal fun httpBytesEqual(left: List<Byte>, right: List<Byte>): Boolean {
4 if (left.size != right.size) return false
5 var index = 0L
6 while (index < left.size) {
7 if (left[index] != right[index]) return false
8 index += 1L
9 }
10 return true
13internal fun httpToken(bytes: List<Byte>): Boolean {
14 if (bytes.size == 0L) return false
15 for (byte in bytes) {
16 if (!httpTokenByte(byte.toUByte().toInt())) return false
17 }
18 return true
21internal fun requireHttpValue(bytes: List<Byte>) {
22 for (byte in bytes) {
23 val c = byte.toUByte().toInt()
24 if ((c < 32 && c != 9) || c == 127)
25 throw IllegalArgumentException(Some("HTTP output contains an invalid field or reason byte"))
26 }
29internal fun httpTrim(bytes: List<Byte>, start: Long, end: Long): Array<Byte> {
30 var first = start
31 var last = end
32 while (first < last && (bytes[first] == 32.toByte() || bytes[first] == 9.toByte())) first += 1L
33 while (last > first && (bytes[last - 1L] == 32.toByte() || bytes[last - 1L] == 9.toByte())) last -= 1L
34 val offset = first
35 return Array<Byte>(last - first) { index -> bytes[offset + index] }
38internal fun httpCommaParts(bytes: List<Byte>): Array<Array<Byte>> {
39 val parts = ArrayList<Array<Byte>>()
40 var start = 0L
41 var index = 0L
42 while (index <= bytes.size) {
43 if (index == bytes.size || bytes[index] == 44.toByte()) {
44 parts.add(httpTrim(bytes, start, index))
45 start = index + 1L
46 }
47 index += 1L
48 }
49 return parts.toArray()
52internal fun httpHasToken(fields: List<HttpField>, name: String, value: String): Boolean {
53 val expected = value.toByteArray()
54 for (field in fields.findAll(name.toByteArray())) {
55 for (part in httpCommaParts(field.value)) if (httpAsciiEqual(part, expected)) return true
56 }
57 return false
60internal fun httpContentLength(bytes: List<Byte>): Long {
61 val digits = httpTrim(bytes, 0L, bytes.size)
62 if (digits.size == 0L) throw IllegalArgumentException(Some("empty HTTP Content-Length"))
63 var value = 0L
64 for (byte in digits) {
65 val digit = byte.toUByte().toLong() - 48L
66 if (digit < 0L || digit > 9L || value > (Long.MAX_VALUE - digit) / 10L)
67 throw IllegalArgumentException(Some("invalid HTTP Content-Length"))
68 value = value * 10L + digit
69 }
70 return value