Scoop API

scoop:scoop.http.client:0.1.0

src/lifetime/call_owner.scoop

Selected production source · src/lifetime/call_owner.scoop

1package scoop.http.client
3internal suspend fun executeClientAttempt(
4 state: ClientState,
5 request: HttpRequest,
6 caller: Job,
7 sequence: ClientRequestSequence
8): ClientCallResult {
9 if (!clientPresent(sequence.webSocket))
10 validateClientRequest(state.config, request)
11 return suspendCoroutine<ClientCallResult> {
12 continuation ->
13 val call = ClientCall(state, request, caller, sequence, continuation)
14 val scope =
15 state.lock.withLock {
16 if (state.closing)
17 throw HttpClientClosedException()
18 state.calls.add(call)
19 state.scope!!
20 }
21 try {
22 launchClientTask(scope, { error -> finishUnstartedClientCall(call, error) }) {
23 suspendCoroutine<Unit> {
24 hold ->
25 call.lock.withLock { call.hold = Some(hold) }
26 startCoroutine<Unit>(
27 suspend { runClientCall(call) },
28 ClientCallCompletion(call)
29 )
30 }
31 }
32 } catch (error: Throwable) {
33 finishUnstartedClientCall(call, error)
34 }
35 }
38private suspend fun runClientCall(call: ClientCall) {
39 if (clientPresent(call.observation))
40 runObservedClientCall(call)
41 else
42 runClientCallWork(call)
45internal suspend fun runClientCallWork(call: ClientCall) {
46 try {
47 runClientCallScope(call)
48 }
49 catch (error: Throwable) {
50 if (clientPresent(call.openedWebSocket))
51 failClientCall(call, error)
52 throw error
53 } finally {
54 closeFailedClientWebSocket(call)
55 }
58private suspend fun runClientCallScope(call: ClientCall) {
59 coroutineScope {
60 val scope = this
62 call.lock.withLock { call.scope = Some(scope) }
63 val jobListener = scope.job.invokeOnCancellation { error -> cancelClientCall(call, error) }
64 val caller =
65 call.lock.withLock {
66 val saved = call.caller!!;
67 call.caller = None;
68 saved
69 }
70 val callerListener =
71 caller.invokeOnCancellation { error -> cancelClientCall(call, error, true) }
72 call.lock.withLock {
73 call.jobListener = Some(jobListener);
74 call.callerListener = Some(callerListener)
75 }
76 try {
77 if (!clientPresent(call.observation))
78 startClientDeadline(
79 call,
80 scope,
81 HttpTimeoutPhase.Total,
82 call.timeouts.total,
83 call.started
84 )
85 requireClientCall(call)
86 emitClientEvent(call, HttpCallEventKind.AttemptStarted)
87 try {
88 selectClientRoute(call)
89 val proxy = call.proxyAuthentication
90 if (clientPresent(proxy))
91 prepareClientAuthentication(call, proxy!!)
92 prepareClientAuthentication(call, call.authentication)
93 prepareClientCookies(call)
94 prepareClientWebSocket(call)
95 prepareClientAttempt(call)
96 }
97 catch (error: Throwable) {
98 failClientCall(call, error, true)
99 }
100 requireClientCall(call)
101 val cached =
102 try {
103 prepareClientCache(call)
104 }
105 catch (error: Throwable) {
106 failClientCall(call, error, true);
107 None
108 }
109 requireClientCall(call)
110 if (clientPresent(cached)) {
111 call.lock.withLock {
112 call.uploadDone = true;
113 call.uploadComplete = true
114 }
115 observeClientResponseHeaders(call, cached!!, true)
116 emitClientEvent(call, HttpCallEventKind.ResponseHeaders)
117 deliverClientResponse(call, cached!!)
118 awaitClientCallEnd(call)
119 return@coroutineScope
120 }
121 startClientDeadline(
122 call,
123 scope,
124 HttpTimeoutPhase.PoolAcquire,
125 call.timeouts.poolAcquire
126 )
127 val connection =
128 when (awaitClientConnection(call)) {
129 case Connected(val _connection) -> _connection
130 case Authenticate(val session) -> {
131 requireClientCall(call)
132 selectClientFollowUp(call, ClientFollowUp.Authenticate(session))
133 return@coroutineScope
134 }
135 }
136 emitClientEvent(call, HttpCallEventKind.ConnectionAcquired)
137 markClientCacheSent(call)
138 val exchange = openClientExchange(call, connection)
139 call.lock.withLock {
140 call.exchange = Some(exchange);
141 call.responseStream = Some<ClientResponseStream>(exchange)
142 }
143 observeClientRequestHeaders(call, exchange.version)
144 emitClientEvent(call, HttpCallEventKind.RequestHeaders)
145 val headersDeadline =
146 startClientDeadline(
147 call,
148 scope,
149 HttpTimeoutPhase.ResponseHeaders,
150 call.timeouts.responseHeaders
151 )
152 val uploading = scope.launch suspend { sendClientBody(call, exchange) }
153 call.lock.withLock { call.upload = Some(uploading) }
154 while (true) {
155 val head = exchange.receiveResponse()
156 observeClientResponseHeaders(call, head)
157 if (head.status < 200U && head.status != 101U) {
158 if (head.status == 100U)
159 settleClientContinue(call, true)
160 emitClientEvent(call, HttpCallEventKind.ResponseHeaders)
161 try {
162 call.request.options.onInformational(head)
163 }
164 catch (error: Throwable) {
165 failClientUserCallback(call, error);
166 throw error
167 }
168 continue
169 }
170 requireClientCall(call)
171 finishClientDeadline(headersDeadline)
172 requireClientCall(call)
173 markClientCacheReceived(call)
174 prepareClientDigestResponse(call, head)
175 saveClientCookies(call, head)
176 val prepared =
177 try {
178 prepareClientCacheResponse(call, head)
179 }
180 catch (error: Throwable) {
181 failClientCall(call, error, true);
182 None
183 }
184 requireClientCall(call)
185 emitClientEvent(call, HttpCallEventKind.ResponseHeaders)
186 if (clientPresent(prepared))
187 deliverClientResponse(call, prepared!!)
188 else
189 selectClientFollowUp(call, ClientFollowUp.CacheRefresh)
190 break
191 }
192 awaitClientCallEnd(call)
193 } catch (error: Throwable) {
194 failClientCall(call, error)
195 }
196 finally {
197 val uploading = call.lock.withLock { call.upload }
198 if (clientPresent(uploading))
199 uploading!!.cancel()
200 try {
201 cleanupClientExchange(call)
202 }
203 finally {
204 stopClientDeadlines(call, clientPresent(call.observation))
205 }
206 }
207 }
210private suspend fun cleanupClientExchange(call: ClientCall) {
211 val input = call.lock.withLock { call.responseInput }
212 if (clientPresent(input)) {
213 try {
214 input!!.closeCodecs()
215 }
216 catch (error: Throwable) {
217 call.lock.withLock {
218 call.cleanupFailure = Some(clientCombined(call.cleanupFailure, error))
219 }
220 }
221 }
222 try {
223 cleanupClientCache(call)
224 }
225 catch (error: Throwable) {
226 call.lock.withLock {
227 call.cleanupFailure = Some(clientCombined(call.cleanupFailure, error))
228 }
229 }
230 val exchange = call.lock.withLock { call.exchange }
231 if (clientPresent(exchange)) {
232 try {
233 exchange!!.close()
234 }
235 catch (error: Throwable) {
236 call.lock.withLock {
237 call.cleanupFailure = Some(clientCombined(call.cleanupFailure, error))
238 }
239 }
240 }
241 if (clientPresent(input))
242 input!!.awaitReadCompletion()
245private fun finishUnstartedClientCall(call: ClientCall, error: Throwable) {
246 val unstarted =
247 call.lock.withLock {
248 if (call.completed || clientPresent(call.hold))
249 return@withLock false
250 call.failure = Some(error)
251 true
252 }
253 if (unstarted)
254 ClientCallCompletion(call).finish(None)