scoop:scoop.http:0.1.0
src/fields.scoop
Selected production source · src/fields.scoop
1
package scoop.http3
internal fun httpBytesEqual(left: List<Byte>, right: List<Byte>): Boolean {4
if (left.size != right.size) return false5
var index = 0L6
while (index < left.size) {7
if (left[index] != right[index]) return false8
index += 1L9
}10
return true11
}13
internal fun httpToken(bytes: List<Byte>): Boolean {14
if (bytes.size == 0L) return false15
for (byte in bytes) {16
if (!httpTokenByte(byte.toUByte().toInt())) return false17
}18
return true19
}21
internal 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
}27
}29
internal fun httpTrim(bytes: List<Byte>, start: Long, end: Long): Array<Byte> {30
var first = start31
var last = end32
while (first < last && (bytes[first] == 32.toByte() || bytes[first] == 9.toByte())) first += 1L33
while (last > first && (bytes[last - 1L] == 32.toByte() || bytes[last - 1L] == 9.toByte())) last -= 1L34
val offset = first35
return Array<Byte>(last - first) { index -> bytes[offset + index] }36
}38
internal fun httpCommaParts(bytes: List<Byte>): Array<Array<Byte>> {39
val parts = ArrayList<Array<Byte>>()40
var start = 0L41
var index = 0L42
while (index <= bytes.size) {43
if (index == bytes.size || bytes[index] == 44.toByte()) {44
parts.add(httpTrim(bytes, start, index))45
start = index + 1L46
}47
index += 1L48
}49
return parts.toArray()50
}52
internal 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 true56
}57
return false58
}60
internal 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 = 0L64
for (byte in digits) {65
val digit = byte.toUByte().toLong() - 48L66
if (digit < 0L || digit > 9L || value > (Long.MAX_VALUE - digit) / 10L)67
throw IllegalArgumentException(Some("invalid HTTP Content-Length"))68
value = value * 10L + digit69
}70
return value71
}