scoop:scoop.http.client:0.1.0
src/transport/h2c_upgrade.scoop
Selected production source · src/transport/h2c_upgrade.scoop
1
package scoop.http.client3
import scoop.asyncio.IoClosedException4
import scoop.http.asyncio.*6
internal class ClientUpgrade(7
val call: ClientCall,8
val entry: ClientPoolEntry,9
val source: AsyncHttp1Connection10
)12
internal suspend fun awaitClientUpgrade(13
call: ClientCall,14
entry: ClientPoolEntry,15
source: AsyncHttp1Connection16
): AsyncHttp2Connection =17
suspendCoroutine<AsyncHttp2Connection> {18
continuation ->19
call.lock.withLock {20
if (call.ending)21
throw (call.failure ?: CancellationException())22
call.upgradeWaiter = Some(continuation)23
}24
val queued =25
call.client.lock.withLock {26
if (call.client.closing)27
false28
else {29
call.client.upgrades.addLast(ClientUpgrade(call, entry, source));30
true31
}32
}33
if (queued)34
wakeClient(call.client)35
else36
cancelClientCall(call, HttpClientClosedException())37
}39
internal fun startClientUpgrade(work: ClientUpgrade) {40
val call = work.call41
val scope = call.lock.withLock { call.scope }42
if (!clientPresent(scope) || call.lock.withLock { call.ending }) {43
finishClientH2cOffer(call.client, work.entry, true)44
return45
}46
try {47
launchClientTask(48
scope!!,49
{50
error ->51
cancelClientCall(call, error)52
finishClientH2cOffer(call.client, work.entry, true)53
}54
) {55
try {56
val connection =57
context(scope!!.job) {58
adoptClientUpgrade(work)59
}60
deliverClientUpgrade(work, connection)61
} catch (error: Throwable) {62
val waiter =63
call.lock.withLock {64
val saved = call.upgradeWaiter;65
call.upgradeWaiter = None;66
saved67
}68
finishClientH2cOffer(call.client, work.entry, true)69
if (clientPresent(waiter))70
waiter!!.resumeWithException(error)71
else72
if (!(error is CancellationException))73
failClientCall(call, error)74
}75
}76
} catch (error: Throwable) {77
failClientCall(call, error)78
finishClientH2cOffer(call.client, work.entry, true)79
}80
}82
private suspend fun adoptClientUpgrade(work: ClientUpgrade): AsyncHttp2Connection {83
val call = work.call84
val state = call.client85
val owner =86
state.lock.withLock {87
if (state.closing)88
throw HttpClientClosedException()89
state.scope!!90
}91
requireClientCall(call)92
val transport = work.source.takeTransport()93
try {94
val config = state.config95
return AsyncHttp2Connection.takeOwnershipAsUpgradedClient(96
transport,97
owner,98
call.request.methodBytes,99
config.http2Settings,100
config.http2Receive,101
config.http2Limits,102
ioTimeouts = clientIoTimeouts(call),103
onEvent = { notice -> clientPoolNotice(state, work.entry, notice) },104
captureTimings = clientPresent(config.observer)105
)106
} catch (error: Throwable) {107
try {108
transport.close()109
} catch (cleanup: Throwable) {110
throw ResourceUseException(error, cleanup)111
}112
throw error113
}114
}116
private suspend fun deliverClientUpgrade(work: ClientUpgrade, connection: AsyncHttp2Connection) {117
val call = work.call118
val waiter =119
call.lock.withLock {120
val saved = call.upgradeWaiter;121
call.upgradeWaiter = None;122
saved123
}124
val failure: Throwable? =125
call.client.lock.withLock {126
if (call.client.closing)127
Some(HttpClientClosedException())128
else129
if (!clientPresent(waiter))130
Some(IoClosedException())131
else {132
val error =133
call.lock.withLock {134
if (call.ending)135
Some(call.failure ?: IoClosedException())136
else137
None138
}139
if (!clientPresent(error)) {140
work.entry.connection = Some(ClientConnection.Http2(connection))141
work.entry.h2cPending = false142
}143
error144
}145
}146
if (clientPresent(failure)) {147
var error = failure!!148
try {149
connection.close()150
} catch (cleanup: Throwable) {151
error = clientCombined(Some(error), cleanup)152
}153
finishClientH2cOffer(call.client, work.entry, true)154
if (clientPresent(waiter))155
waiter!!.resumeWithException(error)156
else157
if (error is ResourceUseException)158
failClientCall(call, error)159
} else {160
waiter!!.resume(connection)161
wakeClient(call.client)162
}163
}