Scoop API

scoop:scoop.http.client:0.1.0

src/transport/connect.scoop

Selected production source · src/transport/connect.scoop

1package scoop.http.client
3import scoop.asyncio.*
4import scoop.http.*
5import scoop.http.asyncio.*
6import scoop.platform.net.*
7import scoop.platform.time.MonotonicTimeSource
8import scoop.tls.*
9import scoop.url.UrlHost
11internal enum ClientConnection {
12 Http1(AsyncHttp1Connection), Http2(AsyncHttp2Connection)
15internal enum ClientConnectResult<T> {
16 Connected(T), Authenticate(ClientAuthenticationSession)
19internal suspend fun closeClientConnection(connection: ClientConnection) {
20 when (connection) {
21 case Http1(val value) -> value.close();
22 case Http2(val value) -> value.close()
23 }
26internal suspend fun awaitClientConnection(call: ClientCall): ClientConnectResult<
27 ClientConnection
28> =
29 suspendCoroutine<ClientConnectResult<ClientConnection>> {
30 continuation ->
31 call.lock.withLock {
32 if (call.ending)
33 throw (call.failure ?: CancellationException())
34 call.dialWaiter = Some(continuation)
35 }
36 observeClientPoolQueued(call)
37 call.client.lock.withLock { call.client.waiting.addLast(call) }
38 wakeClient(call.client)
39 }
41internal fun startClientDial(state: ClientState, call: ClientCall, entry: ClientPoolEntry) {
42 val scope = call.lock.withLock { call.scope }
43 if (!clientPresent(scope) || call.lock.withLock { call.ending }) {
44 removeClientPoolEntry(state, entry)
45 return
46 }
47 try {
48 launchClientTask(
49 scope!!,
50 {
51 error ->
52 cancelClientCall(call, error)
53 removeClientPoolEntry(state, entry)
54 }
55 ) {
56 try {
57 when (connectClient(call, entry)) {
58 case Authenticate(val session) -> {
59 val waiter =
60 call.lock.withLock {
61 val saved = call.dialWaiter;
62 call.dialWaiter = None;
63 saved
64 }
65 removeClientPoolEntry(state, entry)
66 if (clientPresent(waiter))
67 waiter!!.resume(ClientConnectResult.Authenticate(session))
68 }
69 case Connected(val _connection) -> {
70 val connection = _connection
71 if (
72 call.lock.withLock { call.ending } ||
73 state.lock.withLock { state.closing }
74 ) {
75 try {
76 closeClientConnection(connection)
77 }
78 finally {
79 removeClientPoolEntry(state, entry)
80 }
81 } else {
82 state.lock.withLock {
83 entry.connection = Some(connection)
84 entry.connecting = false
85 entry.idle = entry.created
86 }
87 wakeClient(state)
88 }
89 }
90 }
91 } catch (error: Throwable) {
92 val waiter =
93 call.lock.withLock {
94 val saved = call.dialWaiter;
95 call.dialWaiter = None;
96 saved
97 }
98 removeClientPoolEntry(state, entry)
99 if (clientPresent(waiter))
100 waiter!!.resumeWithException(error)
101 else
102 if (!(error is CancellationException))
103 failClientCall(call, error)
104 }
105 }
106 } catch (error: Throwable) {
107 failClientCall(call, error)
108 removeClientPoolEntry(state, entry)
109 }
112private suspend fun connectClient(call: ClientCall, entry: ClientPoolEntry): ClientConnectResult<
113 ClientConnection
114> {
115 val state = call.client
116 val(host, port) = clientRouteAddress(call)
117 val tcp = connectClientSocket(call, host, port)
118 try {
119 val connected = MonotonicTimeSource.markNow()
120 state.lock.withLock { entry.created = connected }
121 requireClientCall(call)
122 return when (call.route) {
123 case Direct -> ClientConnectResult.Connected(connectClientOrigin(call, entry, tcp))
124 case HttpForward(val endpoint) -> connectClientHttpProxy(call, entry, tcp, endpoint, true)
125 case HttpTunnel(val endpoint) -> connectClientHttpProxy(call, entry, tcp, endpoint, false)
126 case Socks5(val endpoint) -> ClientConnectResult.Connected(
127 connectClientSocks5(call, entry, tcp, endpoint)
128 )
129 }
130 } catch (error: Throwable) {
131 try {
132 tcp.close()
133 } catch (cleanup: Throwable) {
134 throw ResourceUseException(error, cleanup)
135 }
136 throw error
137 }
140internal suspend fun <T> connectClientOrigin(
141 call: ClientCall,
142 entry: ClientPoolEntry,
143 transport: T
144): ClientConnection
145where T: AsyncByteStream, T: AsyncCloseable {
146 val config = call.client.config
147 try {
148 requireClientCall(call)
149 if (call.request.origin.scheme == "http") {
150 val protocol =
151 if (
152 !clientPresent(call.sequence.webSocket) &&
153 config.cleartext ==
154 HttpCleartextProtocol.Http2PriorKnowledge
155 )
156 HttpProtocol.Http2
157 else
158 HttpProtocol.Http1
159 return adoptClientConnection(call, entry, transport, protocol)
160 }
161 val context =
162 if (clientPresent(call.sequence.webSocket))
163 clientWebSocketTls(call.client)
164 else
165 call.client.tls
166 requireClientCall(call)
167 val tls =
168 connectClientTls(call, transport, clientTlsPeer(call.request.origin.host), context)
169 try {
170 val allowed: Array<HttpProtocol> =
171 if (clientPresent(call.sequence.webSocket))
172 [HttpProtocol.Http1]
173 else
174 when (config.versionPolicy) {
175 case Auto -> [HttpProtocol.Http2, HttpProtocol.Http1]
176 case Http1Only -> [HttpProtocol.Http1]
177 case Http2Only -> [HttpProtocol.Http2]
178 }
179 val selected: List<Byte>? =
180 when (tls.connectionInfo.alpn) {
181 case Some(val bytes) -> Some(bytes);
182 case None -> None
183 }
184 val protocol =
185 selectHttpAlpn(
186 selected,
187 allowed,
188 clientPresent(call.sequence.webSocket) ||
189 config.versionPolicy !=
190 HttpVersionPolicy.Http2Only
191 )
192 requireClientCall(call)
193 return adoptClientConnection(call, entry, tls, protocol)
194 } catch (error: Throwable) {
195 try {
196 tls.close()
197 } catch (cleanup: Throwable) {
198 throw ResourceUseException(error, cleanup)
199 }
200 throw error
201 }
202 } catch (error: Throwable) {
203 try {
204 transport.close()
205 } catch (cleanup: Throwable) {
206 throw ResourceUseException(error, cleanup)
207 }
208 throw error
209 }
212internal fun clientTlsPeer(host: UrlHost): TlsPeerName =
213 when (host) {
214 case Dns(val name) -> TlsPeerName.Dns(name)
215 case Ipv4(val bytes) -> TlsPeerName.Ip(IpAddress.fromByteArray(bytes))
216 case Ipv6(val bytes) -> TlsPeerName.Ip(IpAddress.fromByteArray(bytes))
217 }
219internal suspend fun <T> adoptClientConnection(
220 call: ClientCall,
221 entry: ClientPoolEntry,
222 transport: T,
223 protocol: HttpProtocol
224): ClientConnection
225where T: AsyncByteStream, T: AsyncCloseable {
226 val state = call.client
227 val scope =
228 state.lock.withLock {
229 if (state.closing)
230 throw HttpClientClosedException()
231 state.scope!!
232 }
233 val config = state.config
234 return when (protocol) {
235 case Http1 -> ClientConnection.Http1(
236 AsyncHttp1Connection.takeOwnershipAsClient(
237 transport,
238 scope,
239 config.http1Receive,
240 config.http1Limits,
241 captureTimings = clientPresent(config.observer)
242 )
243 )
244 case Http2 -> ClientConnection.Http2(
245 AsyncHttp2Connection.takeOwnershipAsClient(
246 transport,
247 scope,
248 config.http2Receive,
249 config.http2Limits,
250 config.http2Settings,
251 onEvent = { notice -> clientPoolNotice(state, entry, notice) },
252 captureTimings = clientPresent(config.observer)
253 )
254 )
255 }