|
14 | 14 | "contract.rkt" |
15 | 15 | "error.rkt" |
16 | 16 | "logger.rkt" |
| 17 | + "middleware.rkt" |
17 | 18 | "payload.rkt" |
18 | 19 | "pool.rkt" |
19 | 20 | "proxy.rkt" |
|
36 | 37 | [#:pool-config pool-config? |
37 | 38 | #:ssl-context (or/c #f ssl-client-context? (promise/c ssl-client-context?)) |
38 | 39 | #:cookie-jar (or/c #f (is-a?/c cookie-jar<%>)) |
39 | | - #:proxies (listof proxy?)] |
| 40 | + #:proxies (listof proxy?) |
| 41 | + #:middleware (or/c #f middleware/c)] |
40 | 42 | session?)] |
41 | 43 | [session? |
42 | 44 | (-> any/c boolean?)] |
|
75 | 77 | ssl-ctx |
76 | 78 | cookies |
77 | 79 | proxies |
| 80 | + middleware |
78 | 81 | [closed? #:mutable]) |
79 | 82 | #:transparent) |
80 | 83 |
|
81 | 84 | (define (make-session |
82 | 85 | #:pool-config [conf (make-pool-config)] |
83 | 86 | #:ssl-context [ssl-ctx (delay/sync (ssl-secure-client-context))] |
84 | 87 | #:cookie-jar [cookies #f] |
85 | | - #:proxies [proxies null]) |
| 88 | + #:proxies [proxies null] |
| 89 | + #:middleware [middleware #f]) |
86 | 90 | (define the-session |
87 | 91 | (session |
88 | 92 | #;cust (make-custodian) |
|
92 | 96 | #;ssql-ctx ssl-ctx |
93 | 97 | #;cookies cookies |
94 | 98 | #;proxies proxies |
| 99 | + #;middleware middleware |
95 | 100 | #;closed? #f)) |
96 | 101 | (will-register executor the-session session-close!) |
97 | 102 | (log-http-easy-debug "session opened") |
|
170 | 175 | [(supplied? json) (json-payload json)] |
171 | 176 | [else data])) |
172 | 177 |
|
173 | | - (define (go u |
174 | | - #:method [method method] ;; noqa |
175 | | - #:headers [headers headers] ;; noqa |
176 | | - #:params [params params] ;; noqa |
177 | | - #:auth [auth auth] ;; noqa |
178 | | - #:data [data the-data] ;; noqa |
179 | | - #:history [history null] |
180 | | - #:attempts [attempts-remaining max-attempts] |
181 | | - #:redirects [redirects-remaining max-redirects]) |
| 178 | + (define (go |
| 179 | + #:method method ;; noqa |
| 180 | + #:headers headers ;; noqa |
| 181 | + #:params params ;; noqa |
| 182 | + #:auth auth ;; noqa |
| 183 | + #:data data ;; noqa |
| 184 | + #:history history |
| 185 | + #:attempts attempts-remaining |
| 186 | + #:redirects redirects-remaining |
| 187 | + u) |
182 | 188 | (let*-values ([(headers) (hash-set headers 'user-agent user-agent)] |
183 | 189 | [(headers) (maybe-add-cookie-header sess u headers)] |
184 | 190 | [(headers params) |
|
209 | 215 | [(positive? attempts-remaining) |
210 | 216 | (log-http-easy-debug "retrying~n attempts remaining: ~a" (sub1 attempts-remaining)) |
211 | 217 | (parameterize-break enable-breaks? |
212 | | - (go u #:attempts (sub1 attempts-remaining) #:history history))] |
| 218 | + (go+middleware |
| 219 | + u |
| 220 | + #:method method |
| 221 | + #:headers headers |
| 222 | + #:params params |
| 223 | + #:auth auth |
| 224 | + #:data data |
| 225 | + #:history history |
| 226 | + #:attempts (sub1 attempts-remaining) |
| 227 | + #:redirects redirects-remaining))] |
213 | 228 | [else |
214 | 229 | (log-http-easy-warning "out of retries; bubbling up exception") |
215 | 230 | (raise e)]))]) |
|
280 | 295 | (response-drain! resp (timeout-config-request timeouts)) |
281 | 296 | (response-close! resp) |
282 | 297 | (parameterize-break enable-breaks? |
283 | | - (go dest-url |
284 | | - #:method (case (response-status-code resp) |
285 | | - [(301 302 303) 'get] |
286 | | - [(307 308) method]) |
287 | | - #:headers (hash-remove headers 'authorization) |
288 | | - #:auth (and (same-origin? dest-url u) auth) |
289 | | - #:history (cons resp history) |
290 | | - #:redirects (sub1 redirects-remaining)))] |
| 298 | + (go+middleware |
| 299 | + dest-url |
| 300 | + #:method (case (response-status-code resp) |
| 301 | + [(301 302 303) 'get] |
| 302 | + [(307 308) method]) |
| 303 | + #:headers (hash-remove headers 'authorization) |
| 304 | + #:params params |
| 305 | + #:auth (and (same-origin? dest-url u) auth) |
| 306 | + #:data data |
| 307 | + #:history (cons resp history) |
| 308 | + #:attempts attempts-remaining |
| 309 | + #:redirects (sub1 redirects-remaining)))] |
291 | 310 | [(or close? (not stream?)) |
292 | 311 | (response-drain! resp (timeout-config-request timeouts)) |
293 | 312 | (response-close! resp) |
|
298 | 317 | (will-register executor resp response-close!) |
299 | 318 | resp]))))) |
300 | 319 |
|
301 | | - (go (->url urlish))) |
| 320 | + (define go+middleware |
| 321 | + (cond |
| 322 | + [(session-middleware sess) |
| 323 | + => (lambda (m) |
| 324 | + (make-keyword-procedure |
| 325 | + (lambda (kws kw-args u . args) |
| 326 | + (keyword-apply m kws kw-args u go args))))] |
| 327 | + [else go])) |
| 328 | + |
| 329 | + (go+middleware |
| 330 | + (->url urlish) |
| 331 | + #:method method |
| 332 | + #:headers headers |
| 333 | + #:params params |
| 334 | + #:auth auth |
| 335 | + #:data the-data |
| 336 | + #:history null |
| 337 | + #:attempts max-attempts |
| 338 | + #:redirects max-redirects)) |
302 | 339 |
|
303 | 340 | ;; https://www.rfc-editor.org/rfc/rfc2616#section-14.30 |
304 | 341 | (define (ensure-absolute-url orig location) |
|
0 commit comments