scoop:scoop.http.client:0.1.0
src/transport/connect.scoop
Selected production source · src/transport/connect.scoop
1
package scoop.http.client3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.http.asyncio.*6
import scoop.platform.net.*7
import scoop.platform.time.MonotonicTimeSource8
import scoop.tls.*9
import scoop.url.UrlHost11
internal enum ClientConnection {12
Http1(AsyncHttp1Connection), Http2(AsyncHttp2Connection)13
}15
internal enum ClientConnectResult<T> {16
Connected(T), Authenticate(ClientAuthenticationSession)17
}19
internal suspend fun closeClientConnection(connection: ClientConnection) {20
when (connection) {21
case Http1(val value) -> value.close();22
case Http2(val value) -> value.close()23
}24
}26
internal suspend fun awaitClientConnection(call: ClientCall): ClientConnectResult<27
ClientConnection28
> =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
}41
internal 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
return46
}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
saved64
}65
removeClientPoolEntry(state, entry)66
if (clientPresent(waiter))67
waiter!!.resume(ClientConnectResult.Authenticate(session))68
}69
case Connected(val _connection) -> {70
val connection = _connection71
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 = false85
entry.idle = entry.created86
}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
saved97
}98
removeClientPoolEntry(state, entry)99
if (clientPresent(waiter))100
waiter!!.resumeWithException(error)101
else102
if (!(error is CancellationException))103
failClientCall(call, error)104
}105
}106
} catch (error: Throwable) {107
failClientCall(call, error)108
removeClientPoolEntry(state, entry)109
}110
}112
private suspend fun connectClient(call: ClientCall, entry: ClientPoolEntry): ClientConnectResult<113
ClientConnection114
> {115
val state = call.client116
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 error137
}138
}140
internal suspend fun <T> connectClientOrigin(141
call: ClientCall,142
entry: ClientPoolEntry,143
transport: T144
): ClientConnection145
where T: AsyncByteStream, T: AsyncCloseable {146
val config = call.client.config147
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.Http2PriorKnowledge155
)156
HttpProtocol.Http2157
else158
HttpProtocol.Http1159
return adoptClientConnection(call, entry, transport, protocol)160
}161
val context =162
if (clientPresent(call.sequence.webSocket))163
clientWebSocketTls(call.client)164
else165
call.client.tls166
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
else174
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 -> None183
}184
val protocol =185
selectHttpAlpn(186
selected,187
allowed,188
clientPresent(call.sequence.webSocket) ||189
config.versionPolicy !=190
HttpVersionPolicy.Http2Only191
)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 error201
}202
} catch (error: Throwable) {203
try {204
transport.close()205
} catch (cleanup: Throwable) {206
throw ResourceUseException(error, cleanup)207
}208
throw error209
}210
}212
internal 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
}219
internal suspend fun <T> adoptClientConnection(220
call: ClientCall,221
entry: ClientPoolEntry,222
transport: T,223
protocol: HttpProtocol224
): ClientConnection225
where T: AsyncByteStream, T: AsyncCloseable {226
val state = call.client227
val scope =228
state.lock.withLock {229
if (state.closing)230
throw HttpClientClosedException()231
state.scope!!232
}233
val config = state.config234
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
}256
}