-
Notifications
You must be signed in to change notification settings - Fork 1
/
fourth.rkt
291 lines (256 loc) · 8.14 KB
/
fourth.rkt
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
#lang racket/base
(provide start)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Imports
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require racket/list
racket/function
racket/string
web-server/http/request-structs
web-server/http/response-structs
web-server/servlet-env
web-server/dispatch
web-server/templates
net/cookies/server
txexpr
css-expr
racket/match
net/url
racket/contract
racket/promise
(file "./respond.rkt")) ;; convenient HTTP responses
(module+ test
(require rackunit))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; HTTP request handlers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define/contract (cookie-header? header)
(header? . -> . boolean?)
(bytes=? #"Cookie" (header-field header)))
(define/contract (cookies req)
(request? . -> . list?)
(define headers (request-headers/raw req))
(define cookie-headers
(filter cookie-header? headers))
(define cookie-values
(map header-value cookie-headers))
(define cookie-alists
(map cookie-header->alist cookie-values))
(apply append cookie-alists))
(define/contract (cookie->header cookie)
(cookie? . -> . header?)
(make-header #"Cookie"
(cookie->set-cookie-header cookie)))
(module+ test
(define empty-request
(make-request
#"GET" ; method
(string->url "http://example.com") ; uri
empty ; headers
(delay (lambda () empty)) ; body (as a promise)
#f ; bindings
"127.0.0.1" ; host IP
80 ; host PORT
"172.10.0.1" ; client IP
))
(define (empty-request/headers . headers)
(struct-copy request
empty-request
[headers/raw headers])))
(module+ test
(define-simple-check (check-pair-represents-cookie pair cookie)
(check-true (pair? pair))
(check-true (cookie? cookie))
(check-true (string? (cookie-name cookie)))
(check-true (string? (cookie-value cookie)))
(let ([n (car pair)]
[v (cdr pair)])
(check-true (bytes? n))
(check-true (bytes? v))
(check-true (bytes=? n
(string->bytes/utf-8
(cookie-name cookie))))
(check-true (bytes=? v
(string->bytes/utf-8
(cookie-value cookie)))))))
(module+ test
;; a request containing a single cookie header
(let* ([cookie (make-cookie "jam" "rhubarb")]
[header (cookie->header cookie)]
[req (empty-request/headers header)]
[cookies (cookies req)])
(check-false (empty? cookies))
(check-= 1 (length cookies) 0)
(check-pair-represents-cookie (first cookies) cookie)))
(module+ test
;; a request that has no cookies (indeed, no headers) at all:
(let* ([req (empty-request/headers)]
[cookies (cookies req)])
(check-true (empty? cookies))))
(module+ test
;; a request that has a single non-cookie header:
(let* ([header (make-header #"Foo" #"bar")]
[req (empty-request/headers header)]
[cookies (cookies req)])
(check-true (empty? cookies))))
(module+ test
;; a request that has two cookie headers
(let* ([cookie-1 (make-cookie "jam" "rhubarb")]
[cookie-2 (make-cookie "pumpkin" "pie")]
[header-1 (cookie->header cookie-1)]
[header-2 (cookie->header cookie-2)]
[req (empty-request/headers header-1 header-2)]
[cookies (cookies req)])
(check-false (empty? cookies))
(check-= 2 (length cookies) 0)
(check-pair-represents-cookie (first cookies) cookie-1)
(check-pair-represents-cookie (second cookies) cookie-2)))
(module+ test
;; a request that has repeated cookie headers
(let* ([cookie (make-cookie "jam" "rhubarb")]
[header (cookie->header cookie)]
[req (empty-request/headers header header)]
[cookies (cookies req)])
(check-false (empty? cookies))
(check-= 2 (length cookies) 0)
(check-pair-represents-cookie (first cookies) cookie)
(check-pair-represents-cookie (second cookies) cookie)))
(define (view-homepage req)
(respond/html
#:body (include-template "fourth/homepage.html")))
(define (hi req)
(respond/html
#:body (include-template "fourth/hi.html")))
(define default-theme #"black-on-white")
(define/contract (make-theme bg-color text-color)
(string? string? . -> . string?)
(define bg (string->symbol bg-color))
(define text (string->symbol text-color))
(css-expr->css
(css-expr
[body
#:background-color ,bg
#:color ,text])))
(define themes
(hash
#"black-on-white"
(make-theme "white" "black")
#"white-on-black"
(make-theme "black" "white")
#"white-on-red"
(make-theme "red" "white")
#"black-on-green"
(make-theme "green" "black")
#"yellow-on-red"
(make-theme "red" "yellow")))
(define default-theme/css
(hash-ref themes default-theme))
(define cookie-for-default-style
(make-cookie "theme"
default-theme
#:path #f))
(define/contract (known-theme? theme)
(bytes? . -> . boolean?)
(hash-has-key? themes theme))
(define (css-for-theme theme)
(if (known-theme? theme)
(values (hash-ref themes theme)
#f)
(values default-theme/css
#t)))
(module+ test
(let-values ([(css default?) (css-for-theme #"")])
(check-true (string? css))
(check-false (string=? "" css))
(check-true default?)))
(define (style.css req)
(respond/css
#:body (include-template "fourth/style.css")))
(define/contract (find-cookie key req)
(bytes? request? . -> . (or/c false/c bytes?))
(define cs (cookies req))
(define c (findf (lambda (p)
(bytes=? key (car p)))
cs))
(cond ((pair? c)
(cdr c))
(else
#f)))
(define/contract (theme.css req)
(request? . -> . response?)
(define theme (find-cookie theme-key req))
(cond ((bytes? theme)
(define-values (css using-fallback?)
(css-for-theme theme))
(define c (if using-fallback?
cookie-for-default-style
(make-cookie theme-key
theme)))
(respond/css
#:body css
#:cookies (list c)))
(else
(respond/css
#:cookies (list cookie-for-default-style)))))
(define theme-key #"theme")
(define/contract (extract-theme req)
(request? . -> . (or/c false/c bytes?))
(define data (request-bindings/raw req))
(define b (bindings-assq theme-key
data))
(cond ((binding:form? b)
(binding:form-value b))
(else
#f)))
(define/contract (bytes->string bstr)
(bytes? . -> . (or/c false/c string?))
(define fail (const #f))
(with-handlers ([exn:fail:contract? fail])
(bytes->string/utf-8 bstr)))
(define/contract (referrer req fallback)
(request? string? . -> . string?)
(define r (headers-assq* #"Referer"
(request-headers/raw req)))
(cond ((bytes? r)
(let ([s (bytes->string r)])
(if (string? s)
s
fallback)))
(else
fallback)))
(define/contract (change-theme req)
(request? . -> . response?)
(define ref (referrer req "/"))
(define t (extract-theme req))
(define theme
(cond ((and (bytes? t)
(known-theme? t))
t)
((bytes? t)
default-theme)
(else
default-theme)))
(define c (make-cookie theme-key theme))
(respond #:code 303
#:headers (list (cons 'Location ref))
#:cookies (list c)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dispatcher and its associated URL generator
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-values (start url-generator)
(dispatch-rules
[("")
#:method "get"
view-homepage]
[("hi")
#:method "get"
hi]
[("style.css")
#:method "get"
style.css]
[("theme.css")
#:method "get"
theme.css]
[("change-theme")
#:method "post"
change-theme]))