[plt-scheme] Cookies in the PLT Web Server
>From http://jay-mccarthy.blogspot.com/2009/02/cookies-in-plt-web-server.html:
It has always been possible to use cookies in PLT Web Server
applications, but a lot of the work was not done for you.
Now it is!
The PLT Web Server now provides two modules as part of web-server/http
for cookies: web-server/http/cookie [1] for installing cookies and
web-server/http/cookie-parse [2] for extracting them.
Here's a simple example:
#lang web-server/insta
(require net/url)
(define (start req)
(define cookies (request-cookies req))
(define id-cookie
(findf (lambda (c)
(string=? "id" (client-cookie-name c)))
cookies))
(define who
(if id-cookie
(client-cookie-value id-cookie)
#f))
(define new-req
(send/suspend
(lambda (k-url)
`(html (head (title "Hello!"))
(body (h1 "Hello " ,(if who who ""))
(form ([action ,k-url])
(input ([name "who"]))))))))
(define binds
(request-bindings/raw new-req))
(match (bindings-assq #"who" binds)
[(? binding:form? b)
(define new-who
(bytes->string/utf-8 (binding:form-value b)))
(redirect-to (url->string (request-uri req))
see-other
#:headers
(list
(cookie->header (make-cookie "id" new-who))))]
[else
(redirect-to
(url->string (request-uri req))
see-other)]))
This addition is based on code written for the PLaneT server by Jacob
Matthews and the existing net/cookie client-side cookie library.
1. http://faculty.cs.byu.edu/~jay/plt-doc/web-server/cookie.html
2. http://faculty.cs.byu.edu/~jay/plt-doc/web-server/cookie-parse.html
--
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://teammccarthy.org/jay
"The glory of God is Intelligence" - D&C 93