Scoop API

scoop:scoop.http:0.1.0

src/http2_events.scoop

Selected production source · src/http2_events.scoop

1package scoop.http
3/**
4 * An HTTP/2 stream or connection event. Data consumption and outgoing bytes must be driven
5 * separately through the session.
6 */
7public enum Http2Event {
8 /** A server began decoding a new request header block, before the head is complete. */
9 RequestHeadStarted(Int),
10 /**
11 * A stream ID and its initial request head.
12 */
13 Request(Int, HttpRequestHead),
14 /**
15 * A stream ID and response head, including informational responses.
16 */
17 Response(Int, HttpResponseHead),
18 /**
19 * A stream ID and owned DATA payload; acknowledge actual application consumption with consume.
20 */
21 Data(Int, Array<Byte>),
22 /**
23 * A stream ID and trailing regular fields.
24 */
25 Trailers(Int, Array<HttpField>),
26 /**
27 * The peer ended its sending direction for this stream.
28 */
29 End(Int),
30 /**
31 * A stream fully closed with the supplied HTTP/2 error code.
32 */
33 StreamClosed(Int, UInt),
34 /**
35 * The last stream ID, error code, debug bytes, and whether this GOAWAY was sent locally.
36 */
37 GoAway(Int, UInt, Array<Byte>, Boolean),
38 /**
39 * Received settings entries and whether the frame is an acknowledgement.
40 */
41 Settings(Array<Http2Setting>, Boolean),
42 /**
43 * A stream ID and regular field ignored under the receive compatibility policy.
44 */
45 IgnoredField(Int, HttpField),
46 /**
47 * An asynchronous send-side protocol failure with its affected scope.
48 */
49 Failure(HttpException)
52@Unsafe internal fun http2EventRequest(event: ULong, info: NativeHttpEventInfo): HttpRequestHead {
53 val method = httpBytes(httpEventBytes(event, 0))
54 val authority = httpBytes(httpEventBytes(event, 2))
55 val target = if (httpBytesEqual(method, "CONNECT".toByteArray())) HttpRequestTarget.Connect(authority)
56 else HttpRequestTarget.Http2(httpBytes(httpEventBytes(event, 1)), authority, httpBytes(httpEventBytes(event, 3)))
57 val indexing = Http2RequestIndexing((info.flags and 1U) != 0U, (info.flags and 2U) != 0U,
58 (info.flags and 4U) != 0U, (info.flags and 8U) != 0U)
59 return HttpRequestHead(HttpVersion.Http2, method, target, httpEventFields(event), indexing)
62internal fun http2EventSettings(bytes: Array<Byte>): Array<Http2Setting> =
63 Array<Http2Setting>(bytes.size / 6L) { index ->
64 val at = index * 6L
65 val id = (bytes[at].toUByte().toUInt() * 256U + bytes[at + 1L].toUByte().toUInt()).toUShort()
66 var value = 0U
67 var offset = 2L
68 while (offset < 6L) { value = value * 256U + bytes[at + offset].toUByte().toUInt(); offset += 1L }
69 Http2Setting(id, value)
70 }
72@Unsafe internal fun http2ReadEvent(event: ULong): Http2Event {
73 val info = httpEventInfo(event)
74 return when (info.kind) {
75 case 1 -> Http2Event.Request(info.streamId, http2EventRequest(event, info))
76 case 2 -> Http2Event.Response(info.streamId, HttpResponseHead(HttpVersion.Http2, info.status.toUInt(),
77 None, httpEventFields(event), (info.flags and 16U) != 0U))
78 case 3 -> Http2Event.Data(info.streamId, httpBytes(httpEventBytes(event, 0)))
79 case 4 -> Http2Event.Trailers(info.streamId, httpEventFields(event))
80 case 5 -> Http2Event.End(info.streamId)
81 case 7 -> Http2Event.StreamClosed(info.streamId, info.flags)
82 case 8 -> Http2Event.GoAway(info.streamId, info.flags, httpBytes(httpEventBytes(event, 0)), info.status != 0)
83 case 9 -> Http2Event.Settings(http2EventSettings(httpBytes(httpEventBytes(event, 0))), info.flags != 0U)
84 case 10 -> Http2Event.IgnoredField(info.streamId, httpEventFields(event)[0L])
85 case 11 -> Http2Event.Failure(http2Failure("send HTTP/2 frame", info.code, String.fromUtf8(httpBytes(httpEventBytes(event, 0))),
86 info.streamId, if (info.streamId == 0) HttpFailureScope.Connection else HttpFailureScope.Stream))
87 case 12 -> Http2Event.RequestHeadStarted(info.streamId)
88 else -> throw IllegalStateException(Some("invalid native HTTP/2 event"))
89 }