Scoop API

scoop:scoop.http.asyncio:0.1.0

src/http2_events.scoop

Selected production source · src/http2_events.scoop

1package scoop.http.asyncio
3import scoop.http.*
5internal fun notifyH2(state: H2State, notice: Http2Notice, actions: ArrayList<() -> Unit>) {
6 actions.add {
7 try { state.onEvent(notice) }
8 catch (error: Throwable) {
9 state.lock.withLock { terminateH2(state, error) }
10 driveH2(state)
11 }
12 }
15internal fun receiveH2Events(state: H2State, actions: ArrayList<() -> Unit>): Boolean {
16 var changed = false
17 while (true) {
18 val event = state.session.nextEvent()
19 if (!present(event)) return changed
20 changed = true
21 when (event!!) {
22 case RequestHeadStarted(val id) -> notifyH2(state, Http2Notice.RequestHeadStarted(id), actions)
23 case Request(val id, val head) -> {
24 if (state.acceptingEnded || state.requests.size >= state.requestCapacity) state.session.reset(id, 7U)
25 else {
26 val stream = H2Stream(id, state.ioTimeouts)
27 state.streams[id] = stream
28 state.requests.add(Http2IncomingRequest(head, AsyncHttp2Stream(state, stream)))
29 notifyH2(state, Http2Notice.RequestHeadReceived(id), actions)
30 }
31 }
32 case Response(val id, val head) -> {
33 val stream = state.streams[id]
34 if (present(stream)) {
35 if (stream!!.responses.size >= 16L)
36 resetH2Stream(state, stream!!, h2StreamFailure(id, 11U, "too many pending HTTP/2 response heads"), 11U, actions)
37 else stream!!.responses.addLast(head)
38 }
39 }
40 case Data(val id, val bytes) -> {
41 val stream = state.streams[id]
42 if (!present(stream)) state.session.reset(id)
43 else if (bytes.size != 0L) stream!!.data.addLast(bytes)
44 }
45 case Trailers(val id, val fields) -> {
46 val stream = state.streams[id]
47 if (present(stream)) stream!!.trailers = Some(fields)
48 }
49 case End(val id) -> {
50 val stream = state.streams[id]
51 if (present(stream)) stream!!.inputEnded = true
52 }
53 case StreamClosed(val id, val code) -> {
54 val stream = state.streams[id]
55 if (present(stream)) {
56 if (code != 0U || !stream!!.inputEnded) {
57 state.session.reset(id, code)
58 releaseH2Stream(state, stream!!, h2StreamFailure(id, code, "HTTP/2 stream closed before normal completion"), actions)
59 } else stream!!.closed = true
60 }
61 notifyH2(state, Http2Notice.StreamClosed(id, code), actions)
62 }
63 case GoAway(val last, val code, val data, val local) -> {
64 state.acceptingEnded = true
65 if (local) state.goAwaySerialized = true
66 notifyH2(state, Http2Notice.GoAway(last, code, data, local), actions)
67 }
68 case Settings(val values, val ack) -> notifyH2(state, Http2Notice.Settings(values, ack), actions)
69 case IgnoredField(val id, val field) -> notifyH2(state, Http2Notice.IgnoredField(id, field), actions)
70 case Failure(val error) -> {
71 val stream = state.streams[error.streamId]
72 if (error.scope == HttpFailureScope.Connection) terminateH2(state, error)
73 else if (present(stream)) resetH2Stream(state, stream!!, error, 2U, actions)
74 notifyH2(state, Http2Notice.Failure(error), actions)
75 }
76 }
77 }