-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.rkt
More file actions
663 lines (547 loc) · 21.2 KB
/
main.rkt
File metadata and controls
663 lines (547 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
#lang racket
;; Notice
;; To install (from within the package directory):
;; $ raco pkg install
;; To install (once uploaded to pkgs.racket-lang.org):
;; $ raco pkg install <<name>>
;; To uninstall:
;; $ raco pkg remove <<name>>
;; To view documentation:
;; $ raco docs <<name>>
(require racket/string
html-parsing
json
net/base64
net/http-client
net/uri-codec
xml)
(provide JSON-HEADERS
HTML-HEADERS
XML-HEADERS
TEXT-HEADERS
POST-FORM
html-requester
json-requester
xml-requester
text-requester
update-host
update-headers
update-port
update-ssl
authenticate-basic
get
post
put
patch
delete
get-status
get-status-code
get-response-type
get-headers
http-error?
http-redirect?
http-success?
http-exn-of-type?
(struct-out exn:fail:network:http:read)
(struct-out exn:fail:network:http:error)
(struct-out requester)
(struct-out text-response)
(struct-out html-response)
(struct-out json-response)
(struct-out xml-response))
(define JSON-HEADERS '("Accept: application/json"
"Content-Type: application/json"))
(define HTML-HEADERS '("Accept: text/html; charset=utf-8"))
(define XML-HEADERS '("Accept: application/xml"))
(define TEXT-HEADERS '("Accept: text/plain; charset=utf-8"))
(define POST-FORM '("Content-Type: application/x-www-formurlencoded"))
;; Responses are packed into structs to allow easy matching
(struct text-response (status headers body) #:transparent)
(struct html-response (status headers body) #:transparent)
(struct json-response (status headers body) #:transparent)
(struct xml-response (status headers body) #:transparent)
;; Requesters carry context for HTTP calls (host, ssl?, default headers...)
(struct requester (host headers port ssl type) #:transparent)
;; TODO: Maybe add a form-post-requester
(define html-requester (requester "" HTML-HEADERS #f #f 'html))
(define json-requester (requester "" JSON-HEADERS #f #f 'json))
(define xml-requester (requester "" XML-HEADERS #f #f 'xml))
(define text-requester (requester "" TEXT-HEADERS #f #f 'text))
;; Header utilities
;; ================
(define (get-headers resp)
(cond
[(json-response? resp) (json-response-headers resp)]
[(html-response? resp) (html-response-headers resp)]
[(xml-response? resp) (xml-response-headers resp)]
[(text-response? resp) (text-response-headers resp)]))
;; Convert header list into immutable hash
(define (map-headers headers)
(define (make-pairs s)
(let* ([key (car (regexp-match #rx"[A-Za-z-]*:" s))]
[val (string-replace s key "")])
(list (string->symbol
(string-titlecase (string-trim (string-replace key ":" ""))))
(string-trim val))))
(make-immutable-hasheq
(map (λ (s)
(make-pairs
(if (bytes? s)
(bytes->string/utf-8 s)
s)))
headers)))
;; Merges header list onto successive lists.
;; Removes duplicate headers (first set in arg position wins) and sorts list
(define (merge-headers . hs)
(let* ([headers (apply append (reverse hs))]
[hm (map-headers headers)]
[hl (hash->list hm)])
(sort (map (λ (s) (format "~a: ~a" (car s) (cadr s))) hl) string<?)))
;; Requester updaters
;; ==================
(define (update-host req nhost)
(struct-copy requester req [host nhost]))
(define (update-headers req nheaders)
(struct-copy requester req
[headers (merge-headers
nheaders
(requester-headers req))]))
(define (update-port req nport)
(struct-copy requester req [port nport]))
(define (update-ssl req nssl)
(struct-copy requester req [ssl nssl]))
(define (params->string ps)
(if (not (empty? ps))
(format "?~a" (alist->form-urlencoded ps))
""))
;; TODO: rename this?
(define (make-uri uri [params '()])
(format "~a~a" uri (params->string params)))
;; Constructs a basic auth header and adds it
(define (authenticate-basic req username password)
(let ([auth-header (string-append
"Authorization: Basic "
(bytes->string/utf-8
(base64-encode
(string->bytes/utf-8
(string-append username ":" password))
"")))])
(update-headers req (list auth-header))))
;; Status Checking
;; ===============
(define (get-status resp)
(cond
[(json-response? resp) (json-response-status resp)]
[(html-response? resp) (html-response-status resp)]
[(xml-response? resp) (xml-response-status resp)]
[(text-response? resp) (text-response-status resp)]))
(define (get-status-code resp)
(let ([status (get-status resp)])
(and status (extract-status-code status))))
(define (get-response-type resp)
(cond
[(json-response? resp) "json"]
[(html-response? resp) "html"]
[(xml-response? resp) "xml"]
[(text-response? resp) "text"]))
;; HTTP Status Predicates
;; https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
;; Return HTTP code as a number from response bytes
(define (extract-status-code status-bytes)
(let ([status (if (bytes? status-bytes)
(bytes->string/utf-8 status-bytes)
status-bytes)])
(string->number
(car (regexp-match
#px"\\d{3}"
status)))))
(define (http-error? resp)
(let ([status (get-status resp)])
(regexp-match? #rx"4[0-9]*|5[0-9]*" status)))
(define (http-success? resp)
(let ([status (get-status resp)])
(regexp-match? #rx"20[0-8]|226" status)))
(define (http-redirect? resp)
(let ([status (get-status resp)])
(regexp-match? #rx"30[0-8]" status)))
;; Response handling
;; =================
;; Exception for response read errors
(struct exn:fail:network:http:read exn:fail (type) #:transparent)
;; Exception for HTTP errors - response is parsed
(struct exn:fail:network:http:error exn:fail:network (code type) #:transparent)
;; Regex Content-Type header to figure out what we have
(define (get-content-type headers-hash)
(match (car (hash-ref headers-hash 'Content-Type))
[(regexp #rx"application/json") 'json]
[(regexp #rx"text/html") 'html]
[(regexp #rx"application/xml") 'xml]
[else 'text]))
;; Raise an error with the response
(define (make-http-read-exn headers response)
(let ([type (get-content-type headers)]
[body (port->string response)])
(raise
(exn:fail:network:http:read
(~a body) (current-continuation-marks) type))))
;; Raise an error code, and inclue the response type
(define (make-http-error-exn code headers response)
(let* ([type (get-content-type headers)]
[body (cond
[(eq? type 'json) (read-json response)]
[(eq? type 'html) (html->xexp response)]
[(eq? type 'xml) (read-xml response)]
[else (port->string response)])])
(raise
(exn:fail:network:http:error
(~a body) (current-continuation-marks) code type))))
;; Get the type of exception
(define (http-exn-of-type? type v)
(and (exn:fail:network:http:read? v)
(eq? type (exn:fail:network:http:read-type v))))
;; Try to read the error response
(define (parse-http-error-body exn)
(let ([type (exn:fail:network:http:error-type exn)]
[body (exn-message exn)])
(cond
[(eq? type 'json) (read-json body)]
[(eq? type 'html) (html->xexp body)]
[(eq? type 'xml) (read-xml body)]
[else (port->string body)])))
;; Uses Content-Type to determine how to parse response data
(define (create-response status headers response)
(with-handlers ([exn:fail? (λ (e) (make-http-read-exn headers response))])
(let ([type (get-content-type headers)])
(cond
[(eq? type 'json)
(json-response status headers (read-json response))]
[(eq? type 'html)
(html-response status headers (html->xexp response))]
[(eq? type 'xml)
(xml-response status headers (read-xml response))]
[else
(text-response status headers (port->string response))]))))
;; Make HTTP Requests
;; ==================
(define (requester-accept-header requester)
(let ([type (requester-type requester)])
(cond
[(eq? type 'html) "text/html"]
[(eq? type 'json) "application/json"]
[(eq? type 'xml) "text/xml"]
[(eq? type 'text) "text/plain"]
[else "*"])))
; Compare the Accept header from the request to the Content-Type header in the
; response (using a regex) to try and figure out if we have the correct response
; type
(define (correct-content-type? requester resp-headers)
(let* ([resh (map-headers resp-headers)]
[accept (requester-accept-header requester)]
[ctype (hash-ref resh 'Content-Type)])
(regexp-match (regexp accept) (car ctype))))
;; define-http-method macro defines the interface for request functions
;; (define-http-method get '"GET") -> (get requester "/get")
(define-syntax (define-http-method stx)
(syntax-case stx ()
[(_ method verb)
#'(define (method req uri #:data [data #f] #:params [params '()])
(let* ([nuri (make-uri uri params)]
[host (requester-host req)]
[headers (requester-headers req)]
[ssl (requester-ssl req)]
[port (cond [(requester-port req)]
[ssl 443]
[else 80])])
(let-values ([(status resp-headers response)
(http-sendrecv host nuri #:ssl? ssl #:port port
#:method verb #:headers headers
#:data data)])
(define response-code (extract-status-code status))
;; Raise HTTP exn if we get an HTTP error code
(when (> response-code 399)
(make-http-error-exn
response-code
(map-headers resp-headers)
response))
;; Raise read exn if the requested content type doesn't match
;; the response content type and isn't a redirect
(when
(and
(or (< response-code 300) (> response-code 309))
(false? (correct-content-type? req resp-headers)))
(make-http-read-exn (map-headers resp-headers) response))
(create-response
(bytes->string/utf-8 status)
(map-headers resp-headers)
response))))]))
;; Sets up functions named after HTTP verbs
(define-http-method get '"GET")
(define-http-method post '"POST")
(define-http-method put '"PUT")
(define-http-method patch '"PATCH")
(define-http-method delete '"DELETE")
;; =====
;; Tests
;; =====
(module+ test
(require rackunit)
; Requesters
(check-pred requester? json-requester)
(check-pred requester? html-requester)
(check-pred requester? xml-requester)
; Headers
(define headers (map-headers (map string->bytes/utf-8 JSON-HEADERS)))
(check-pred hash-eq? headers)
(for ([h (hash-keys headers)])
(check-true (hash-has-key? headers h)))
(define h0 (merge-headers '() '(#"foo: a")))
(define h1 (merge-headers '(#"foo: a") '()))
(define h2 (merge-headers '(#"foo: a" #"bar: b") '(#"baz: c")))
(define h3 (merge-headers '(#"foo: a") '(#"foo: a" #"baz: c")))
(define h4 (merge-headers '(#"foo: a") '(#"foo: b" #"baz: c")))
(define h5 (merge-headers '(#"foo: b") '(#"foo: a") '(#"baz: c" #"foo: c")))
(define h6 (merge-headers '("foo: b") '(#"foo: a") '("baz: c" #"foo: c")))
(define h7 (merge-headers '("Content-Type: b") '(#"Content-type: a")
'("content-type: c" #"foo: c")))
(check-equal? h0 '("Foo: a"))
(check-equal? h1 '("Foo: a"))
(check-equal? h2 '("Bar: b" "Baz: c" "Foo: a"))
(check-equal? h3 '("Baz: c" "Foo: a"))
; first set of dupe headers wins: "foo: a"
(check-equal? h4 '("Baz: c" "Foo: a"))
; first set of dupe headers wins: "foo: b"
(check-equal? h5 '("Baz: c" "Foo: b"))
; can take bytes or strings
(check-equal? h6 '("Baz: c" "Foo: b"))
; can deal with kludgy casing in header names
(check-equal? h7 '("Content-Type: b" "Foo: c"))
(define host0 (update-host html-requester "groundwork.com"))
(check-equal? (requester-host html-requester) "")
(check-equal? (requester-host host0) "groundwork.com")
(define hdrs0 (update-headers html-requester '("X-Foo: Foo" "X-Bar: Bar")))
(define hdrs1
(update-headers
html-requester
'("Accept: text/html; charset=latin-1" "X-Bar: Bar")))
(check-equal?
(requester-headers hdrs0)
'("Accept: text/html; charset=utf-8"
"X-Bar: Bar"
"X-Foo: Foo"))
(check-equal?
(requester-headers hdrs1)
'("Accept: text/html; charset=latin-1"
"X-Bar: Bar"))
(define hdrs2
(authenticate-basic
html-requester
"foo" "bar"))
(check-equal?
(requester-headers hdrs2)
'("Accept: text/html; charset=utf-8"
"Authorization: Basic Zm9vOmJhcg=="))
(define port0 (update-port json-requester 8080))
(check-equal? (requester-port port0) 8080)
(define ssl0 (update-ssl json-requester #t))
(check-equal? (requester-ssl ssl0) #t)
; Params
(check-equal? (params->string '()) "")
(check-equal? (params->string '((foo . "12"))) "?foo=12")
(check-equal? (params->string '((foo . "12") (bar . "bar"))) "?foo=12&bar=bar")
; URI
(check-equal? (make-uri "groundwork.com") "groundwork.com")
(check-equal? (make-uri "groundwork.com" '((foo . "12") (bar . "baz")))
"groundwork.com?foo=12&bar=baz")
; Status checking
(define hds (make-hash '((Access-Control-Allow-Credentials . ("true")))))
(define body "body")
(define jresp (json-response "HTTP/1.1 200 OK" hds body))
(define hresp (html-response "HTTP/1.1 200 OK" hds body))
(define xresp (xml-response "HTTP/1.1 200 OK" hds body))
(define tresp (text-response "HTTP/1.1 200 OK" hds body))
(check-equal? (get-status jresp) "HTTP/1.1 200 OK")
(check-equal? (get-status hresp) "HTTP/1.1 200 OK")
(check-equal? (get-status xresp) "HTTP/1.1 200 OK")
(check-equal? (get-status tresp) "HTTP/1.1 200 OK")
(check-equal? (get-status-code jresp) 200)
(check-equal? (get-status-code hresp) 200)
(check-equal? (get-status-code xresp) 200)
(check-equal? (get-status-code tresp) 200)
; Headers checking
(check-equal? (get-headers jresp) hds)
(check-equal? (get-headers hresp) hds)
(check-equal? (get-headers xresp) hds)
(check-equal? (get-headers tresp) hds)
(define r200 (json-response "HTTP/1.1 200 OK" hds body))
(define r201 (json-response "HTTP/1.1 201 Created" hds body))
(define r209 (json-response "HTTP/1.1 209 Unassigned" hds body))
(define r226 (json-response "HTTP/1.1 226 IM Used" hds body))
(define r301 (json-response "HTTP/1.1 301 Moved Permanently" hds body))
(define r400 (json-response "HTTP/1.1 400 Bad Request" hds body))
(define r404 (json-response "HTTP/1.1 404 Not Found" hds body))
(define r500 (json-response "HTTP/1.1 500 Server Error" hds body))
(define r504 (json-response "HTTP/1.1 504 Gateway Timeout" hds body))
(check-pred http-success? r200)
(check-pred http-success? r201)
(check-false (http-success? r209))
(check-pred http-success? r226)
(check-pred http-redirect? r301)
(check-false (http-success? r301))
(check-pred http-error? r400)
(check-pred http-error? r404)
(check-pred http-error? r500)
(check-pred http-error? r504)
; Check responses
(check-pred json-response?
(call-with-values
(λ () (values
"HTTP 1.1/200 OK"
(map-headers '("Content-Type: application/json"))
(open-input-bytes #"{\"foo\": 1}")))
create-response))
(check-pred html-response?
(call-with-values
(λ () (values
"HTTP 1.1/200 OK"
(map-headers '("Content-Type: text/html; charset=utf-8"))
(open-input-bytes
#"<html><head><title>Foo</title></head></html>")))
create-response))
(check-pred
xml-response?
(call-with-values
(λ () (values
"HTTP 1.1/200 OK"
(map-headers '("Content-Type: application/xml; charset=utf-8"))
(open-input-bytes
#"<?xml version=\"1.0\" ?><foo>Foo</foo>")))
create-response))
(check-pred text-response?
(call-with-values
(λ () (values
"HTTP 1.1/200 OK"
(map-headers '("Content-Type: application/scheme"))
(open-input-bytes
#"'(foo bar baz quux)")))
create-response))
; Check exceptions
(check-exn exn:fail:network:http:read?
(λ () (call-with-values
(λ () (values
"HTTP 1.1/200 OK"
(map-headers '("Content-Type: application/json"))
(open-input-bytes #"blurg")))
create-response)))
(check-exn exn:fail:network:http:read?
(λ () (call-with-values
(λ () (values
"HTTP 1.1/200 OK"
(map-headers '("Content-Type: application/xml"))
(open-input-bytes #"blarg")))
create-response)))
;; HTML Parsing is very permissive, so it won't really fail
(check-not-exn
(λ () (call-with-values
(λ () (values
"HTTP 1.1/200 OK"
(map-headers '("Content-Type: text/html"))
(open-input-bytes #"<hurgle\n")))
create-response)))
;; Check that the exception carries the request type, so we know what it was
;; we were asking for, ex: an exception on a JSON request should have
;; an http-exn-of-type? of 'json
;; Unreadable JSON response
(check-true
(http-exn-of-type?
'json
(with-handlers ([exn:fail:network:http:read? (λ (exn) exn)])
(call-with-values
(λ () (values
"HTTP 1.1/200 OK"
(map-headers '("Content-Type: application/json"))
(open-input-bytes #"blurg")))
create-response))))
;; Unreadable XML response
(check-true
(http-exn-of-type?
'xml
(with-handlers ([exn:fail:network:http:read? (λ (exn) exn)])
(call-with-values
(λ () (values
"HTTP 1.1/200 OK"
(map-headers '("Content-Type: application/xml"))
(open-input-bytes #"blurg")))
create-response)))))
;; Integrations tests
(module+ integration-test
(require rackunit
json)
;; JSON GET requests
(define httpbin-json (update-host json-requester "httpbin.org"))
(define httpbin-html (update-host html-requester "httpbin.org"))
(define json-get (get httpbin-json "/get"))
(define json-get-body (json-response-body json-get))
(check-pred http-success? json-get)
(check-equal? (json-response-status json-get) "HTTP/1.1 200 OK")
(check-equal? (get-status-code json-get) 200)
(check-pred jsexpr? json-get-body)
(define https-json (update-ssl httpbin-json #t))
(define json-ssl-get (get https-json "/get"))
(check-equal?
(hash-ref (json-response-body json-ssl-get) 'url)
"https://httpbin.org/get")
(define json-ssl-params (get https-json "/get" #:params
'((query . "huevos"))))
(check-equal?
(hash-ref (hash-ref (json-response-body json-ssl-params) 'args) 'query)
"huevos")
(check-exn
exn:fail:network:http:read?
(λ () (get httpbin-json "/html")))
;; Error codes have a Content-Type
(check-equal?
(with-handlers
([exn:fail:network:http:error?
(λ (e) (exn:fail:network:http:error-type e))])
(get httpbin-json "/code/405")) 'html)
;; JSON POST/PUT/PATCH requests
(define json-data
(jsexpr->string (make-hasheq '((colossal-squid . "drumbones")))))
(define json-post (post httpbin-json "/post" #:data json-data))
(check-equal?
(hash-ref (json-response-body json-post) 'data)
json-data)
(define json-put (put httpbin-json "/put" #:data json-data))
(check-equal?
(hash-ref (json-response-body json-put) 'data)
json-data)
(define json-patch (patch httpbin-json "/patch" #:data json-data))
(check-equal?
(hash-ref (json-response-body json-patch) 'data)
json-data)
;; Error Handling
; 405
(check-exn
exn:fail:network:http:error?
(λ () (post httpbin-json "/put" #:data json-data)))
; 404
(check-exn
exn:fail:network:http:error?
(λ () (get httpbin-json "/status/404")))
; 503
(check-exn
exn:fail:network:http:error?
(λ () (get httpbin-json "/status/503")))
; Error response is parsed
(check-pred
xexpr?
(with-handlers ([exn:fail:network:http:error? (λ (e) (exn-message e))])
(get httpbin-json "/status/503")))
;; Redirect Handling
(check-pred http-redirect? (get httpbin-json "/redirect/1"))
(check-pred http-redirect? (get httpbin-json "/redirect/5"))
(check-pred http-redirect?
(get httpbin-json "/redirect-to" #:params '((url . "foo"))))
;; Success Handling
(check-pred http-success? (get httpbin-html "/status/201"))
(check-pred http-success? (get httpbin-html "/status/200")))