Scoop API

scoop:scoop.http.server:0.1.0

src/server_connection.scoop

Selected production source · src/server_connection.scoop

1package scoop.http.server
3import scoop.asyncio.*
4import scoop.http.*
5import scoop.http.asyncio.*
6import scoop.tls.asyncio.AsyncTlsStream
7import scoop.platform.time.MonotonicTimeSource
9internal suspend fun dispatchServerConnection(
10 state: ServerState,
11 entry: ServerConnection,
12 executor: CoroutineExecutor
13) {
14 val entered = AtomicBoolean(false)
15 try {
16 withExecutor<Unit>(executor) {
17 entered.store(true)
18 runServerConnection(state, entry)
19 }
20 } finally {
21 // A queued worker task can be cancelled before entering its body.
22 if (!entered.load()) {
23 try {
24 entry.transport.close()
25 } finally {
26 state.unregister(entry)
27 }
28 }
29 }
32internal suspend fun runServerConnection(state: ServerState, entry: ServerConnection) {
33 try {
34 coroutineScope<Unit> {
35 val scope = this
37 val stop =
38 state.lock.withLock {
39 entry.job = Some(scope.job)
40 state.phase != ServerPhase.Running
41 }
42 if (stop) {
43 scope.job.cancel();
44 scope.job.ensureActive()
45 }
46 if (present(state.options.tls)) {
47 val tls =
48 serverDeadline<AsyncTlsStream>(
49 state.options.tlsHandshakeTimeout,
50 "TLS handshake",
51 suspend {
52 AsyncTlsStream.takeOwnershipAsServer(
53 entry.transport,
54 state.options.tls!!
55 )
56 }
57 )
58 var failure: Throwable? = None
59 try {
60 val info = tls.connectionInfo
61 val alpn: List<Byte>? =
62 when (info.alpn) {
63 case None -> None;
64 case Some(val value) -> Some(value)
65 }
66 val allowed =
67 if (state.options.allowTlsHttp2)
68 [HttpProtocol.Http1, HttpProtocol.Http2]
69 else
70 [HttpProtocol.Http1]
71 val protocol = selectHttpAlpn(alpn, allowed, state.options.allowHttp1Fallback)
72 entry.info =
73 HttpConnectionInfo(
74 entry.transport.localAddress,
75 entry.transport.peerAddress,
76 protocol,
77 Some(info)
78 )
79 serveSelectedHttp(
80 state,
81 entry,
82 tls,
83 scope,
84 protocol,
85 MonotonicTimeSource.markNow()
86 )
87 } catch (error: Throwable) {
88 failure = Some(error);
89 throw error
90 }
91 finally {
92 closeServerResource(state, tls, failure)
93 }
94 } else
95 serveCleartextHttp(state, entry, scope)
96 }
97 } catch (error: Throwable) {
98 if (!(error is CancellationException))
99 observeConnectionFailure(state, entry.info, error)
100 } finally {
101 try {
102 entry.transport.close()
103 }
104 catch (error: Throwable) {
105 observeConnectionFailure(state, entry.info, error);
106 state.recordFailure(error)
107 }
108 state.unregister(entry)
109 }
112private fun observeConnectionFailure(
113 state: ServerState,
114 info: HttpConnectionInfo,
115 error: Throwable
116) {
117 if (!present(state.options.onConnectionError))
118 return
119 try {
120 state.options.onConnectionError!!(info, error)
121 }
122 catch (_: Throwable) {
123 return
124 }
127internal suspend fun closeServerResource(
128 state: ServerState,
129 resource: AsyncCloseable,
130 primary: Throwable?
131) {
132 try {
133 resource.close()
134 }
135 catch (cleanup: Throwable) {
136 state.recordFailure(cleanup)
137 if (present(primary))
138 throw ResourceUseException(primary!!, cleanup)
139 throw cleanup
140 }