Scoop API

scoop:scoop.http.client:0.1.0

src/interceptors/chain.scoop

Selected production source · src/interceptors/chain.scoop

1package scoop.http.client
3import scoop.http.*
4import scoop.url.Url
6/**
7 * A suspend interceptor around a logical call. It may invoke the chain once while active and is
8 * responsible for returning a compatible owned result.
9 */
10public class HttpInterceptor public constructor(
11 internal val intercept: suspend (HttpCallChain) -> HttpCallResult
14/**
15 * The current logical request and one-shot continuation into later interceptors or transport.
16 */
17public class HttpCallChain internal constructor(
18 internal val invocation: ClientInvocation,
19 internal val next: Long,
20 /**
21 * The current request passed to this interceptor.
22 */
23 public val request: HttpRequest
24) {
25 /**
26 * The caller-supplied fields, before client-managed framing and authority.
27 */
28 public val suppliedFields: Array<HttpField> = request.suppliedFields.toArray()
29 /**
30 * Upgrade options when this chain belongs to a WebSocket handshake.
31 */
32 public val webSocketOptions: HttpWebSocketOptions? = invocation.sequence.webSocket
33 internal var active: Boolean = true
34 internal var proceeded: Boolean = false
36 /**
37 * Invokes the remainder of the chain once while active, optionally replacing URL, method,
38 * fields, and body. WebSocket chains require a replayable empty GET.
39 */
40 public suspend fun proceed(
41 url: Url = request.url,
42 method: String = request.method,
43 fields: List<HttpField> = suppliedFields,
44 body: RequestBody = request.body
45 ): HttpCallResult {
46 invocation.client.lock.withLock {
47 if (!active || proceeded)
48 throw IllegalStateException(
49 Some("HTTP interceptor chain can proceed only once while active")
50 )
51 proceeded = true
52 }
53 invocation.requireActive()
54 val selected =
55 httpRequest(url) {
56 this.method = method
57 this.fields = fields
58 this.body = body
59 this.options = this @HttpCallChain.request.options
60 }
61 if (clientPresent(webSocketOptions)) {
62 if (
63 method !=
64 "GET" ||
65 body.contentLength !=
66 Some(0L) ||
67 !body.isReplayable ||
68 body.trailerNames.size !=
69 0L
70 )
71 throw IllegalArgumentException(
72 Some("WebSocket interceptor requires GET with a replayable empty body")
73 )
74 val websocket =
75 httpWebSocketRequest(clientWebSocketUrl(url)) {
76 this.fields = fields
77 this.options = this @HttpCallChain.request.options
78 }
79 validateClientWebSocket(websocket, webSocketOptions!!)
80 }
81 return invokeClientInterceptor(invocation, next, selected)
82 }
85internal suspend fun invokeClientInterceptor(
86 invocation: ClientInvocation,
87 index: Long,
88 request: HttpRequest
89): HttpCallResult {
90 invocation.requireActive()
91 val interceptors = invocation.client.config.interceptors
92 if (index == interceptors.size) {
93 val result = runClientSequence(invocation.client, request, invocation.sequence)
94 rememberClientInvocationResult(invocation, result)
95 return result
96 }
97 val chain = HttpCallChain(invocation, index + 1L, request)
98 try {
99 val result = interceptors[index].intercept(chain)
100 rememberClientInvocationResult(invocation, result)
101 return result
102 } finally {
103 invocation.client.lock.withLock { chain.active = false }
104 }