-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-common.scm
414 lines (367 loc) · 12.4 KB
/
http-common.scm
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
;;; Routines common to both a HTTP client and server.
;;;
;;; Copyright (c) 2008-2009 Per Eckerdal, 2005-2007 Marc Feeley, All
;;; Rights Reserved.
(import ../string/util
../srfi/1
../srfi/13
../srfi/19
x-www-form-urlencoded)
(export http-status-code
make-cookie
cookie?
cookie-name
cookie-name-set!
cookie-value
cookie-value-set!
cookie-expires
cookie-expires-set!
cookie-domain
cookie-domain-set!
cookie-path
cookie-path-set!
cookie-port
cookie-port-set!
cookie-secure
cookie-secure-set!
cookie-http-only
cookie-http-only-set!
cookie-to-http
cookie-parse
cookie-headers
date-in-the-past
date->rfc1123
display-crlf
display-header
display-headers
split-attribute-line
permissive-read-line-reached-eof
permissive-read-line
permissive-read-lines
read-header
read-content-chars)
(declare (block)
(mostly-fixnum)
(standard-bindings)
(extended-bindings))
;==============================================================================
; Status codes.
(define http-status-code #f)
(let ((http-status-codes (make-table)))
(for-each (lambda (x)
(table-set! http-status-codes
(car x)
(cdr x)))
'((100 . "Continue")
(101 . "Switching Protocols")
(200 . "OK")
(201 . "Created")
(202 . "Accepted")
(203 . "Non-Authoritative Information")
(204 . "No Content")
(205 . "Reset Content")
(206 . "Partial Content")
(300 . "Multiple Choices")
(301 . "Moved Permanently")
(302 . "Found")
(303 . "See Other")
(304 . "Not Modified")
(305 . "Use Proxy")
(307 . "Temporary Redirect")
(400 . "Bad Request")
(401 . "Unauthorized")
(402 . "Payment Required")
(403 . "Forbidden")
(404 . "Not Found")
(405 . "Method Not Allowed")
(406 . "Not Acceptable")
(407 . "Proxy Authentication Required")
(408 . "Request Timeout")
(409 . "Conflict")
(410 . "Gone")
(411 . "Length Required")
(412 . "Precondition Failed")
(413 . "Request Entity Too Large")
(414 . "Request-URI Too Long")
(415 . "Unsupported Media Type")
(416 . "Requested Range Not Satisfiable")
(417 . "Expectation Failed")
(500 . "Internal Server Error")
(501 . "Not Implemented")
(502 . "Bad Gateway")
(503 . "Service Unavailable")
(504 . "Gateway Timeout")
(505 . "HTTP Version Not Supported")))
(set! http-status-code (lambda (num)
(table-ref http-status-codes num))))
;==============================================================================
; Cookies.
(define-type cookie
id: D00E6EE6-5E55-47F2-B901-4DECBB3AA011
constructor: make-cookie/no-check
name
value
expires
domain
path
port
secure
http-only)
(define http-separators
(let ((lst '()))
(string-for-each (lambda (x)
(set! lst (cons x lst)))
"()<>@,;:\\\"/[]?={} \t")
lst))
;; One optimization might be to simply not call this function
(define (valid-cookie-name? name)
(call/cc
(lambda (ret)
(string-for-each
(lambda (chr)
(let ((int (char->integer chr)))
(if (or (<= int 31)
(>= int 127)
(find (lambda (x) (eq? x chr))
http-separators))
(ret #f))))
name)
(ret #t))))
(define (make-cookie name value #!key expires domain path port secure http-only)
(if (not (valid-cookie-name? name))
(error "Invalid cookie name:" name))
(make-cookie/no-check name value expires domain path port secure http-only))
(set! cookie-name-set!
(lambda (c v)
(error "Cookie names are read-only")))
(define (cookie-to-http c)
(let ((name (cookie-name c))
(value (cookie-value c))
(expires (cookie-expires c))
(domain (cookie-domain c))
(path (cookie-path c))
(port (cookie-port c))
(secure (cookie-secure c))
(http-only (cookie-http-only c)))
(apply string-append
`(,name
"="
,(or value "")
,@(if expires
`("; expires=" ,(date->rfc1123 expires))
'())
,@(if domain
`("; domain=" ,domain)
'())
,@(if path
`("; path=" ,path)
'())
,@(if port
`("; port="
,@(cond
((number? port)
`("\"" ,(number->string port) "\""))
((pair? port)
`("\""
,@(join "," (map number->string port))
"\""))
(else `(,port)))))
,(if secure "; secure" "")
,(if http-only "; HttpOnly" "")
"; Version=1"))))
;; Takes the raw Cookie: field data and splits it into a list
;; of key/value pairs.
(define (cookie-parse-split data)
(let ((cookies (make-table)))
(map (lambda (s)
(let ((sp (string-split #\= s)))
(if (or (null? sp)
(null? (cdr sp)))
(cons "" "")
(cons (urldecode (car sp))
(urldecode (cadr sp))))))
(map string-strip (string-split #\; data)))))
(define (cookie-parse-to-list data)
(let ((ps (cookie-parse-split data))
(current-cookie #f)
(default-prefs '()) ;; The special attributes specified before any other cookie
(cookies '())) ;; List of processed cookies
(define (set-pref name val)
(let ((name (string-downcase name)))
(if current-cookie
(cond
((equal? name "$path")
(cookie-path-set! current-cookie val))
((equal? name "$domain")
(cookie-domain-set! current-cookie val))
((equal? name "$port")
(cookie-port-set! current-cookie val)))
(set! default-prefs (cons (cons name val)
default-prefs)))))
(define (new-cookie name val)
(if current-cookie
(set! cookies (cons current-cookie cookies)))
(set! current-cookie (make-cookie name val))
(for-each (lambda (x) (set-pref (car x) (cdr x)))
default-prefs))
(for-each
(lambda (pair)
(let ((name (car pair))
(val (cdr pair)))
(if (not (= 0 (string-length name)))
(if (eq? #\$ (string-ref name 0))
(set-pref name val)
(new-cookie name val)))))
ps)
(if current-cookie
(cons current-cookie cookies)
'())))
;; Parses the value of a Cookie: header and returns it as a
;; table with the cookies, where the keys are the cookie names.
(define (cookie-parse data)
(let ((tbl (make-table)))
(for-each (lambda (c)
(table-set! tbl (cookie-name c) c))
(cookie-parse-to-list data))
tbl))
(define (cookie-headers tbl port)
(table-for-each (lambda (key val)
(display-crlf port "Set-Cookie: " (cookie-to-http val)))
tbl))
;==============================================================================
; Header writing functions.
(define (write-u8vector vec #!optional port)
(write-subu8vector vec
0
(u8vector-length vec)
(or port (current-output-port))))
(define date-in-the-past (make-date 0 0 0 0 1 1 1990 0))
(define (date->rfc1123 d)
(date->string d "~a, ~d ~b ~Y ~T GMT"))
(define (display-crlf port . msgs)
(if (not (null? msgs))
(for-each (lambda (msg)
(display msg port))
msgs))
(display "\r\n" port))
(define (display-header port pair)
(display-crlf port (car pair) ": " (cdr pair)))
(define (display-headers port hs)
(for-each (lambda (x)
(display-header port x))
hs))
;==============================================================================
; Http reading functions.
(define (find-char-pos str char)
(let loop ((i 0))
(if (< i (string-length str))
(if (char=? char (string-ref str i))
i
(loop (+ i 1)))
#f)))
(define (split-attribute-line line)
(let ((pos (find-char-pos line #\:)))
(and pos
(< pos (string-length line))
(cons (let ((str (substring line 0 pos)))
(string-downcase! str)
str)
(string-strip
(substring line (+ pos 1) (string-length line)))))))
(define permissive-read-line-reached-eof
(list 'permissive-read-line-reached-eof))
;; Reads one line from port, using the byte reading functions and not
;; read-line. Safe to US ASCII only.
(define (permissive-read-line port #!optional exception-on-eof?)
(let loop ((lst '()))
(let ((x (read-u8 port)))
(cond
((and (eq? x 10)
(pair? lst)
(eq? (car lst) #\return))
(reverse-list->string (cdr lst)))
((eq? #!eof x)
(if exception-on-eof?
(raise permissive-read-line-reached-eof)
(reverse-list->string lst)))
(else
(loop (cons (integer->char x) lst)))))))
(define (permissive-read-lines port)
(let loop ()
(let ((line (permissive-read-line port #t)))
(if (zero? (string-length line))
(loop)
line))))
;; Reads one line from port, using the byte reading functions and not
;; read-line. Safe to US ASCII only. This function also treats a CRLF
;; followed by a space or a tab as whitespace, not a newline.
;;
;; Used when parsing headers.
(define (permissive-read-line-skip-lws port peek-char)
(let loop ((lst '())
(current-char peek-char)
(peek-byte (read-u8 port)))
(cond
((and (null? lst)
(eq? 10 peek-byte) ;; #\newline
(eq? #\return current-char))
(cons 'done ""))
((eq? #!eof peek-byte)
(cons #!eof
(reverse-list->string
(if (and (eq? #\newline current-char)
(pair? lst)
(eq? #\return (car lst)))
(cdr lst)
(cons current-char lst)))))
((and (eq? current-char #\newline)
(pair? lst)
(eq? (car lst) #\return)
(not (or (eq? 9 peek-byte) ;; #\tab
(eq? 32 peek-byte)))) ;; #\space
(cons peek-byte
(reverse-list->string (cdr lst))))
(else
(let ((chr (integer->char peek-byte)))
(loop (cons current-char lst)
chr
(read-u8 port)))))))
(define (read-header port)
(let loop ((attributes '())
(peek-byte (read-u8 port)))
(and (not (eq? #!eof peek-byte))
(let* ((peek-byte/line-pair
(permissive-read-line-skip-lws
port
(integer->char peek-byte)))
(new-peek-byte
(car peek-byte/line-pair))
(line
(cdr peek-byte/line-pair)))
(cond
((eq? 'done new-peek-byte)
attributes)
((eq? #!eof new-peek-byte)
#f)
(else
(let ((attribute (split-attribute-line line)))
(if attribute
(loop (cons attribute attributes)
new-peek-byte)
#f))))))))
(define (read-content-chars port attributes)
(let ((cl
(cond ((assoc "content-length" attributes)
=>
(lambda (x)
(let ((n (string->number (cdr x))))
(and n (integer? n) (exact? n) n))))
(else
#f))))
(if cl
(let ((str (make-string cl)))
(let ((n (read-substring str 0 cl port)))
(if (= n cl)
str
"")))
"")))