Scoop API

scoop:scoop.http.client:0.1.0

src/interceptors/attempt.scoop

Selected production source · src/interceptors/attempt.scoop

1package scoop.http.client
3import scoop.collections.HashSet
4import scoop.http.*
5import scoop.text.lowercaseAscii
6import scoop.text.startsWith
7import scoop.url.Url
9/**
10 * The reason another concrete HTTP attempt is being performed.
11 */
12public enum HttpAttemptReason {
13 /**
14 * The initial network attempt.
15 */
16 Initial,
17 /**
18 * An attempt following a redirect.
19 */
20 Redirect,
21 /**
22 * An attempt after a retryable failure or configured status.
23 */
24 Retry,
25 /**
26 * An origin authentication retry.
27 */
28 Authentication,
29 /**
30 * A proxy authentication retry.
31 */
32 ProxyAuthentication,
33 /**
34 * An attempt validating or refreshing a cached representation.
35 */
36 CacheRefresh
39/**
40 * A snapshot of the concrete request target, prepared fields, route, and attempt number supplied
41 * to onAttempt.
42 */
43public class HttpAttempt internal constructor(call: ClientCall) {
44 /**
45 * The request sequence's current attempt number.
46 */
47 public val number: Long = call.sequence.attemptNumber
48 /**
49 * Why this attempt was started.
50 */
51 public val reason: HttpAttemptReason = call.sequence.attemptReason
52 /**
53 * The request selected for this concrete attempt.
54 */
55 public val request: HttpRequest = call.request
56 /**
57 * The exact prepared request-target bytes.
58 */
59 public val requestTarget: Array<Byte> = clientRequestTargetBytes(call)
60 /**
61 * A snapshot of fields prepared before onAttempt additions.
62 */
63 public val fields: Array<HttpField> =
64 when (call.webSocketHandshake) {
65 case Some(val handshake) -> handshake.request.fields.toArray()
66 case None -> clientRequestSnapshotFields(call)
67 }
68 /**
69 * The selected proxy endpoint, or None for a direct route.
70 */
71 public val proxyUrl: Url? =
72 when (call.route) {
73 case Direct -> None
74 case HttpForward(val endpoint) -> Some(endpoint.url)
75 case HttpTunnel(val endpoint) -> Some(endpoint.url)
76 case Socks5(val endpoint) -> Some(endpoint.url)
77 }
80private class ClientAttemptCallback(val call: ClientCall) {
81 var active: Boolean = true
84internal suspend fun prepareClientAttempt(call: ClientCall) {
85 val callback = call.client.config.onAttempt ?: return
86 val request = call.request
87 val supplied = ArrayList<HttpField>()
88 for (field in request.suppliedFields)
89 supplied.add(field)
90 addClientAcceptEncoding(supplied, request, call.client.config)
91 call.request =
92 httpRequest(request.url) {
93 this.method = request.method
94 this.fields = supplied
95 this.body = request.body
96 this.options = request.options
97 }
98 val snapshot = HttpAttempt(call)
99 val active = ClientAttemptCallback(call)
100 val added =
101 try {
102 context(active) {
103 callback(snapshot).toArray()
104 }
105 }
106 catch (error: Throwable) {
107 failClientUserCallback(call, error);
108 throw error
109 }
110 finally {
111 call.client.lock.withLock { active.active = false }
112 }
113 requireClientCall(call)
114 val names = HashSet<String>()
115 for (field in snapshot.fields)
116 names.add(String.fromUtf8(field.name).lowercaseAscii())
117 for (field in added) {
118 field.validate()
119 val name = String.fromUtf8(field.name).lowercaseAscii()
120 if (
121 name ==
122 "host" ||
123 name ==
124 "content-length" ||
125 name ==
126 "transfer-encoding" ||
127 name ==
128 "connection" ||
129 name ==
130 "upgrade" ||
131 name ==
132 "expect" ||
133 name ==
134 "trailer" ||
135 name ==
136 "proxy-authorization" ||
137 (clientPresent(call.sequence.webSocket) && name.startsWith("sec-websocket-"))
138 )
139 throw IllegalArgumentException(
140 Some("HTTP attempt callback cannot supply managed field " + name)
141 )
142 if (!names.add(name))
143 throw IllegalArgumentException(
144 Some("HTTP attempt callback cannot replace field " + name)
145 )
146 if (name == "authorization")
147 call.attemptAuthorization = true
148 supplied.add(field)
149 }
150 call.attemptFields = added
151 call.request =
152 httpRequest(request.url) {
153 this.method = request.method
154 this.fields = supplied
155 this.body = request.body
156 this.options = request.options
157 }
160internal fun checkClientAttemptClose(client: ClientState) {
161 val callback = contextOrNone<ClientAttemptCallback>()
162 if (
163 clientPresent(callback) &&
164 callback!!.call.client ===
165 client &&
166 client.lock.withLock { callback!!.active }
167 )
168 throw IllegalStateException(
169 Some("cannot await client.close from its active attempt callback")
170 )