Scoop API

scoop:scoop.http.asyncio:0.1.0

src/http2_connection.scoop

Selected production source · src/http2_connection.scoop

1package scoop.http.asyncio
3import scoop.asyncio.*
4import scoop.http.*
6/**
7 * A received request head paired with its independently controlled HTTP/2 stream.
8 */
9public class Http2IncomingRequest internal constructor(
10 /**
11 * The received protocol head, preserving the original ordered fields.
12 */
13 public val head: HttpRequestHead,
14 /**
15 * The stream used to read the request body and send its response.
16 */
17 public val stream: AsyncHttp2Stream)
19/**
20 * No new stream was submitted because the HTTP/2 connection is draining.
21 */
22public class Http2ConnectionDrainingException public constructor() :
23 Exception(Some("HTTP/2 connection is draining; no new request was submitted"))
25/**
26 * An owned multiplexed HTTP/2 connection driven by structured tasks in the supplied scope.
27 * Per-stream cancellation resets only that stream; connection or scope closure ends shared
28 * transport I/O.
29 */
30public class AsyncHttp2Connection internal constructor(internal val state: H2State) : AsyncCloseable {
31 /**
32 * Factories that transfer an established transport to structured protocol tasks.
33 */
34 public companion object {
35 /**
36 * Takes an already-established transport as an HTTP client. The supplied scope owns
37 * connection tasks; TCP connection and TLS handshake must already be complete.
38 */
39 public suspend fun <T> takeOwnershipAsClient(transport: T, scope: CoroutineScope,
40 options: Http2ReceiveOptions = Http2ReceiveOptions(), limits: Http2Limits = Http2Limits(),
41 settings: Http2Settings = Http2Settings(), ioTimeouts: HttpIoTimeoutOptions = HttpIoTimeoutOptions(),
42 onEvent: (Http2Notice) -> Unit = { _ -> }, captureTimings: Boolean = false): AsyncHttp2Connection
43 where T : AsyncByteStream, T : AsyncCloseable =
44 adoptH2(transport, transport, scope, { Http2Session.client(options, limits, settings) }, settings, ioTimeouts, onEvent, false, captureTimings)
46 /**
47 * Takes an already-established transport as an HTTP server. The supplied scope owns
48 * connection tasks; TCP connection and TLS handshake must already be complete.
49 */
50 public suspend fun <T> takeOwnershipAsServer(transport: T, scope: CoroutineScope,
51 options: Http2ReceiveOptions = Http2ReceiveOptions(), limits: Http2Limits = Http2Limits(),
52 settings: Http2Settings = Http2Settings(), ioTimeouts: HttpIoTimeoutOptions = HttpIoTimeoutOptions(),
53 onEvent: (Http2Notice) -> Unit = { _ -> }): AsyncHttp2Connection
54 where T : AsyncByteStream, T : AsyncCloseable =
55 adoptH2(transport, transport, scope, { Http2Session.server(options, limits, settings) }, settings, ioTimeouts, onEvent, false)
57 /**
58 * Takes transport ownership after a completed HTTP/1 h2c switch, preserving stream 1 and
59 * using the actual upgrade SETTINGS. New client streams begin at 3.
60 */
61 public suspend fun <T> takeOwnershipAsUpgradedClient(transport: T, scope: CoroutineScope, requestMethod: List<Byte>,
62 settings: Http2Settings = Http2Settings(), options: Http2ReceiveOptions = Http2ReceiveOptions(),
63 limits: Http2Limits = Http2Limits(), ioTimeouts: HttpIoTimeoutOptions = HttpIoTimeoutOptions(),
64 onEvent: (Http2Notice) -> Unit = { _ -> }, captureTimings: Boolean = false): AsyncHttp2Connection
65 where T : AsyncByteStream, T : AsyncCloseable =
66 adoptH2(transport, transport, scope, { Http2Session.upgradedClient(requestMethod, settings, options, limits) },
67 settings, ioTimeouts, onEvent, true, captureTimings)
69 /**
70 * Takes transport ownership after a completed HTTP/1 h2c switch, preserving stream 1 and
71 * using the actual upgrade SETTINGS. New client streams begin at 3.
72 */
73 public suspend fun <T> takeOwnershipAsUpgradedServer(transport: T, scope: CoroutineScope, requestMethod: List<Byte>,
74 settingsPayload: List<Byte>, options: Http2ReceiveOptions = Http2ReceiveOptions(), limits: Http2Limits = Http2Limits(),
75 settings: Http2Settings = Http2Settings(), ioTimeouts: HttpIoTimeoutOptions = HttpIoTimeoutOptions(),
76 onEvent: (Http2Notice) -> Unit = { _ -> }): AsyncHttp2Connection
77 where T : AsyncByteStream, T : AsyncCloseable =
78 adoptH2(transport, transport, scope,
79 { Http2Session.upgradedServer(requestMethod, settingsPayload, options, limits, settings) },
80 settings, ioTimeouts, onEvent, true)
81 }
83 /**
84 * The local HTTP client or server role.
85 */
86 public val role: HttpRole get() = state.session.role
87 /**
88 * The default I/O idle deadlines used by this connection.
89 */
90 public val ioTimeouts: HttpIoTimeoutOptions get() = state.ioTimeouts
91 /**
92 * Whether connection shutdown has begun, before cleanup completes.
93 */
94 public val isClosing: Boolean get() = state.lock.withLock { state.closing }
95 /**
96 * Whether connection cleanup has completed.
97 */
98 public val isClosed: Boolean get() = state.lock.withLock { state.closed }
99 /**
100 * Whether new requests are disallowed while existing streams drain.
101 */
102 public val isDraining: Boolean get() = state.lock.withLock { state.acceptingEnded }
103 /**
104 * Stream 1 associated with a successful h2c upgrade, or None for a connection established
105 * directly as HTTP/2.
106 */
107 public val upgradedStream: AsyncHttp2Stream? get() = state.lock.withLock {
108 when (state.upgrade) { case Some(val stream) -> Some(AsyncHttp2Stream(state, stream)); case None -> None }
109 }
111 /**
112 * Submits a request and returns its stream. Set endStream false when an upload body or
113 * trailers will follow; draining connections reject before submission.
114 */
115 public suspend fun openRequest(head: HttpRequestHead, endStream: Boolean = true,
116 ioTimeouts: HttpIoTimeoutOptions = this.ioTimeouts): AsyncHttp2Stream {
117 val job = httpJob(state.loop, true)
118 requireHttpIoTimeouts(ioTimeouts)
119 val stream = state.lock.withLock {
120 h2Open(state)
121 if (state.acceptingEnded) throw Http2ConnectionDrainingException()
122 val value = H2Stream(state.session.submitRequest(head, endStream), ioTimeouts)
123 value.sendStarted = true
124 value.sendEnded = endStream
125 state.streams[value.id] = value
126 value
127 }
128 listenH2Stream(state, stream, job)
129 val result = AsyncHttp2Stream(state, stream)
130 try { result.flush(); return result }
131 catch (error: Throwable) { result.close(); throw error }
132 }
134 /**
135 * Waits for a server-side incoming request, or None after the connection stops accepting
136 * requests.
137 */
138 public suspend fun acceptRequest(): Http2IncomingRequest? = acceptH2(state)
140 /**
141 * Sends an explicit GOAWAY notification with selected code, debug data, and optional
142 * last-stream boundary.
143 */
144 public suspend fun goAway(code: UInt = 0U, debugData: List<Byte> = ArrayList<Byte>(), lastStreamId: Int? = None) {
145 flushH2Connection(state) {
146 if (state.goAwayPending) throw IllegalStateException(Some("HTTP/2 GOAWAY send is busy"))
147 state.session.goAway(code, debugData, lastStreamId)
148 state.goAwayPending = true
149 state.goAwaySerialized = false
150 state.acceptingEnded = true
151 }
152 }
154 /**
155 * Cancels and joins shared connection I/O and closes the owned transport. This is local
156 * cleanup, distinct from explicitly sending GOAWAY.
157 */
158 public override suspend fun close() { closeH2(state) }