scoop:scoop.http:0.1.0
src/http2_headers.scoop
Selected production source · src/http2_headers.scoop
1
package scoop.http3
internal class Http2Fields internal constructor() : Closeable {4
internal val handle: ULong = Http2Fields.create()5
internal var length: Long = -1L7
companion object {8
internal fun create(): ULong {9
@Unsafe {10
val value = http2FieldsNew()11
if (value == 0UL) throw http2Failure("prepare HTTP/2 fields", -901, "cannot allocate header storage")12
return value13
}14
}15
}17
internal fun add(name: Array<Byte>, value: Array<Byte>, neverIndex: Boolean = false) {18
@Unsafe {19
val code = name.withDataPointer { namePointer, nameSize ->20
@Unsafe {21
return@withDataPointer value.withDataPointer { valuePointer, valueSize ->22
@Unsafe { return@withDataPointer http2FieldAdd(handle, namePointer, nameSize,23
valuePointer, valueSize, if (neverIndex) 1 else 0) }24
}25
}26
}27
if (code < 0) throw http2Failure("prepare HTTP/2 fields", code, "invalid HTTP/2 field name or value")28
}29
}31
internal fun addRegular(fields: List<HttpField>, trailers: Boolean = false) {32
for (field in fields) {33
if (trailers) requireHttpTrailerName(field.name)34
val name = Array<Byte>(field.name.size) { index ->35
val byte = field.name[index].toUByte().toInt()36
(if (byte >= 65 && byte <= 90) byte + 32 else byte).toByte()37
}38
if (name.size == 0L || name[0L] == 58.toByte()) throw IllegalArgumentException(Some("ordinary HTTP/2 field cannot be a pseudo-header"))39
add(name, field.value, field.neverIndex)40
val token = String.fromUtf8(name)41
if (token == "connection" || token == "keep-alive" || token == "proxy-connection" || token == "upgrade" || token == "transfer-encoding")42
throw IllegalArgumentException(Some("connection-specific HTTP/2 field"))43
if (token == "te" && !httpAsciiEqual(field.value, "trailers".toByteArray()))44
throw IllegalArgumentException(Some("HTTP/2 TE must be trailers"))45
if (token == "content-length") {46
if (length >= 0L) throw IllegalArgumentException(Some("invalid repeated Content-Length"))47
length = httpContentLength(field.value)48
}49
}50
}52
public override fun close() { @Unsafe { http2FieldsFree(handle) } }53
}55
internal fun http2RequestFields(head: HttpRequestHead): Http2Fields {56
if (head.version != HttpVersion.Http2) throw IllegalArgumentException(Some("HTTP/2 request requires HTTP/2 version"))57
val fields = Http2Fields()58
try {59
fields.add(":method".toByteArray(), head.method, head.indexing.method)60
val connect = httpBytesEqual(head.method, "CONNECT".toByteArray())61
when (head.target) {62
case Http2(val scheme, val authority, val path) -> {63
if (connect || scheme.size == 0L || path.size == 0L)64
throw IllegalArgumentException(Some("invalid HTTP/2 request pseudo-headers"))65
requireHttp2Scheme(scheme)66
fields.add(":scheme".toByteArray(), scheme, head.indexing.scheme)67
if (authority.size != 0L) fields.add(":authority".toByteArray(), authority, head.indexing.authority)68
fields.add(":path".toByteArray(), path, head.indexing.path)69
}70
case Connect(val authority) -> {71
if (!connect || authority.size == 0L) throw IllegalArgumentException(Some("CONNECT requires authority"))72
fields.add(":authority".toByteArray(), authority, head.indexing.authority)73
}74
else -> throw IllegalArgumentException(Some("HTTP/2 requires pseudo-header request target"))75
}76
fields.addRegular(head.fields)77
return fields78
} catch (error: Throwable) { fields.close(); throw error }79
}81
private fun requireHttp2Scheme(scheme: Array<Byte>) {82
var index = 0L83
for (byte in scheme) {84
val c = byte.toUByte().toInt()85
val alpha = (c >= 65 && c <= 90) || (c >= 97 && c <= 122)86
if (!alpha && (index == 0L || !((c >= 48 && c <= 57) || c == 43 || c == 45 || c == 46)))87
throw IllegalArgumentException(Some("invalid HTTP/2 URI scheme"))88
index += 1L89
}90
}92
internal fun http2ResponseFields(head: HttpResponseHead): Http2Fields {93
val hasReason = when (head.reason) { case None -> false; case Some(_) -> true }94
if (head.version != HttpVersion.Http2 || head.status < 100U || head.status > 599U || head.status == 101U || hasReason)95
throw IllegalArgumentException(Some("invalid HTTP/2 response version status or reason"))96
val fields = Http2Fields()97
try {98
fields.add(":status".toByteArray(), head.status.toString().toByteArray(), head.neverIndexStatus)99
fields.addRegular(head.fields)100
return fields101
} catch (error: Throwable) { fields.close(); throw error }102
}