-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.ss
233 lines (206 loc) · 7.94 KB
/
cookie.ss
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
#lang scheme/base
; This library implements Cookies as per the Netscape spec.
; Later specs are not supported by popular but retarded
; browsers such as Internet Explorer
(require (lib "unitsig.ss")
mzlib/etc
mzlib/list
srfi/13/string
srfi/14/char-set
srfi/19/time)
(define-struct cookie (name value expires path domain secure) #:transparent #:mutable)
(define-struct (cookie-error exn) ())
; Netscape Spec:
; Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
(define set-cookie
(lambda (name value)
(unless (and (cookie-string? name #f)
(cookie-string? value))
(raise (make-cookie-error (format "Invalid NAME/VALUE pair: ~a / ~a" name value) (current-continuation-marks))))
(make-cookie name value
#f ; session cookie
#f ; current path
#f ; current domain
#f; normal (non SSL)
)))
;!
;
; (function (print-cookie cookie))
;
; (param cookie Cookie-structure "The cookie to return as a string")
;
; Formats the cookie contents in a string ready to be appended to a
; "Set-Cookie: " header, and sent to a client (browser).
(define print-cookie
(lambda (cookie)
(unless (cookie? cookie)
(raise (make-cookie-error (format "Cookie expected, received: ~a" cookie) (current-continuation-marks))))
(string-join
(filter (lambda (s)
(not (string-null? s)))
(list (format "~a=~a" (cookie-name cookie) (cookie-value cookie))
(let ((e (cookie-expires cookie)))
(if e
(format "expires=~a" (expires->rfc822-string e))
""))
(let ((p (cookie-path cookie))) (if p (format "path=~a" p) ""))
(let ((d (cookie-domain cookie))) (if d (format "domain=~a" d) ""))
(let ((s (cookie-secure cookie))) (if s "secure" ""))))
"; ")))
(define cookie:add-domain
(lambda (cookie domain)
(unless (valid-domain? domain)
(raise (make-cookie-error (format "Invalid domain: ~a" domain) (current-continuation-marks))))
(unless (cookie? cookie)
(raise (make-cookie-error (format "Cookie expected, received: ~a" cookie) (current-continuation-marks))))
(set-cookie-domain! cookie domain)
cookie))
(define cookie:add-expires
(lambda (cookie seconds)
(unless (and (integer? seconds) (not (negative? seconds)))
(raise (make-cookie-error (format "Invalid Expires for cookie: ~a" seconds) (current-continuation-marks))))
(unless (cookie? cookie)
(raise (make-cookie-error (format "Cookie expected, received: ~a" cookie) (current-continuation-marks))))
(set-cookie-expires! cookie seconds)
cookie))
(define cookie:add-path
(lambda (cookie path)
(unless (string? path)
(raise (make-cookie-error (format "Invalid path: ~a" path) (current-continuation-marks))))
(unless (cookie? cookie)
(raise (make-cookie-error (format "Cookie expected, received: ~a" cookie) (current-continuation-marks))))
(set-cookie-path! cookie path)
cookie))
(define cookie:secure
(lambda (cookie secure?)
(unless (boolean? secure?)
(raise (make-cookie-error (format "Invalid argument (boolean expected), received: ~a" secure?) (current-continuation-marks))))
(unless (cookie? cookie)
(raise (make-cookie-error (format "Cookie expected, received: ~a" cookie) (current-continuation-marks))))
(set-cookie-secure! cookie secure?)
cookie))
; Parsing the Cookie header:
(define char-set:all-but=
(char-set-difference char-set:full (string->char-set "=")))
(define char-set:all-but-semicolon
(char-set-difference char-set:full (string->char-set ";")))
;!
;
; (function (get-all-results name cookies))
;
; Auxiliar procedure that returns all values associated with
; `name' in the association list (cookies).
(define get-all-results
(lambda (name cookies)
(let loop ((c cookies))
(cond ((null? c) null)
(else
(let ((pair (car c)))
(if (string=? name (car pair))
; found an instance of cookie named `name'
(cons (cadr pair) (loop (cdr c)))
(loop (cdr c)))))))))
; which tipically looks like: (cookie . "test5=\"5\"; test1=\"1\"; test0=\"0\"; test1=\"20\"")
; note that it can be multi-valued: `test1' has values: "1", and "20".
; Of course, in the same spirit, we only receive the "string content".
(define get-cookie
(lambda (name cookies)
(let ((cookies (map (lambda (p)
(map string-trim-both
(string-tokenize p char-set:all-but=)))
(string-tokenize cookies char-set:all-but-semicolon))))
(get-all-results name cookies))))
;!
;
; (function (get-cookie/single name cookies))
;
; (param name String "The name of the cookie we are looking for")
; (param cookies String "The string (from the environment) with the content of the cookie header.")
;
; Returns the first name associated with the cookie named `name', if any, or #f.
(define get-cookie/single
(lambda (name cookies)
(let ((cookies (get-cookie name cookies)))
(and (not (null? cookies))
(car cookies)))))
;;;
; Auxiliar procedures
;;;
; token = 1*<any CHAR except CTLs or tspecials>
;
; tspecials = "(" | ")" | "<" | ">" | "@"
; | "," | ";" | ":" | "\" | <">
; | "/" | "[" | "]" | "?" | "="
; | "{" | "}" | SP | HT
(define char-set:tspecials
(char-set-union
(char-set-difference char-set:punctuation (string->char-set "_."))
char-set:whitespace))
(define char-set:control (char-set-union char-set:iso-control
(char-set (integer->char 127)))); DEL
(define char-set:token (char-set-difference char-set:ascii char-set:tspecials char-set:control))
;!
;
; (function (quoted-string? s))
;
; (param s String "The string to check")
;
; Returns #t only if the string is surrounded by double quotes. As in:
; quoted-string = ( <"> *(qdtext) <"> )
; qdtext = <any TEXT except <">>
(define quoted-string?
(lambda (s)
(and (string=? (string-take s 1) "\"")
(string=? (string-take-right s 1) "\""))))
;!
;
; (function (cookie-string? s))
;
; (param s String "String to check")
;
; Returns whether this is a valid string to use as the value or the
; name (depending on value?) of an HTTP cookie.
(define cookie-string?
(opt-lambda (s (value? #t))
(unless (string? s)
(raise (make-cookie-error (format "String expected, received: ~a" s) (current-continuation-marks))))
(if value?
; value: token | quoted-string
(or (string-every char-set:token s)
(quoted-string? s))
; name: token
(string-every char-set:token s))))
; Host names as per RFC 1123 and RFC952, more or less, anyway. :-)
(define char-set:hostname
(let ((a-z-lowercase (ucs-range->char-set #x61 #x7B))
(a-z-uppercase (ucs-range->char-set #x41 #x5B)))
(char-set-adjoin!
(char-set-union char-set:digit a-z-lowercase a-z-uppercase)
#\. )))
(define valid-domain?
(lambda (dom)
(and
; Domain must start with a dot (.)
(string=? (string-take dom 1) ".")
; The rest are tokens-like strings separated by dots
(string-every char-set:hostname dom)
(<= (string-length dom) 76))))
;; Utility
(define (expires->rfc822-string seconds)
(date->string (time-utc->date
(make-time time-utc 0 seconds))
"~a, ~d-~b-~Y ~H:~M:~S GMT"))
; Provide statements -----------------------------
(provide set-cookie
cookie:add-domain
cookie:add-expires
cookie:add-path
cookie:secure
; To actually return a cookie (string formated as a cookie):
print-cookie
; To parse the Cookies header:
get-cookie
get-cookie/single
; exceptions
(struct-out cookie-error)
expires->rfc822-string)