21 Cookie: Legacy HTTP Client Storage
NOTE: This library is deprecated; use the net-cookies package, instead. That package (source on GitHub) implements RFC 6265 [RFC6265] (which supersedes RFC 2109) and supports creating cookies on the server in an idiom more typical of Racket.
(require net/cookie) | package: net-lib |
21.1 Functions
procedure
(valid-domain? v) → boolean?
v : any/c
procedure
(cookie-name? v) → boolean?
v : any/c
procedure
(cookie-value? v) → boolean?
v : any/c
procedure
(set-cookie name value) → cookie?
name : cookie-name? value : cookie-value?
procedure
(cookie:add-comment cookie comment) → cookie?
cookie : cookie? comment : string?
procedure
(cookie:add-domain cookie domain) → cookie?
cookie : cookie? domain : valid-domain?
procedure
(cookie:add-max-age cookie seconds) → cookie?
cookie : cookie? seconds : exact-nonnegative-integer?
procedure
(cookie:add-path cookie path) → cookie?
cookie : cookie? path : valid-path?
procedure
(cookie:add-expires cookie path) → cookie?
cookie : cookie? path : string
procedure
(cookie:secure cookie secure) → cookie?
cookie : cookie? secure : boolean?
procedure
(cookie:version cookie version) → cookie?
cookie : cookie? version : exact-nonnegative-integer?
procedure
(print-cookie cookie) → string?
cookie : cookie?
procedure
(get-cookie name cookies) → (listof cookie-value?)
name : cookie-name? cookies : string?
The method used to obtain the "Cookie" header depends on the web server. It may be an environment variable (CGI), or you may have to read it from the input port (FastCGI), or maybe it comes in an initial-request structure, etc. The get-cookie and get-cookie/single procedure can be used to extract fields from a "Cookie" field value.
procedure
(get-cookie/single name cookies) → (or/c cookie-value? false/c)
name : cookie-name? cookies : string?
struct
(struct cookie-error exn:fail () #:extra-constructor-name make-cookie-error)
21.2 Examples
21.2.1 Creating a cookie
(let ([c (cookie:add-max-age (cookie:add-path (set-cookie "foo" "bar") "/servlets") 3600)]) (print-cookie c))
Produces
"foo=bar; Max-Age=3600; Path=/servlets; Version=1"
To use this output in a “regular” CGI, instead of the last line use:
(display (format "Set-Cookie: ~a" (print-cookie c)))
and to use with the PLT Web Server, use:
(make-response/full code message (current-seconds) mime (list (make-header #"Set-Cookie" (string->bytes/utf-8 (print-cookie c)))) body)
21.2.2 Parsing a cookie
Imagine your Cookie header looks like this:
> (define cookies "test2=2; test3=3; xfcTheme=theme6; xfcTheme=theme2")
Then, to get the values of the xfcTheme cookie, use
> (get-cookie "xfcTheme" cookies) '("theme6" "theme2")
> (get-cookie/single "xfcTheme" cookies) "theme6"
If you try to get a cookie that simply is not there:
> (get-cookie/single "foo" cookies) #f
> (get-cookie "foo" cookies) '()
Note that not having a cookie is normally not an error. Most clients won’t have a cookie set then first arrive at your site.
21.3 Cookie Unit
cookie@ and cookie^ are deprecated. They exist for backward-compatibility and will likely be removed in the future. New code should use the net-cookies package.
(require net/cookie-unit) | package: compatibility-lib |
value
cookie@ : unit?
21.4 Cookie Signature
(require net/cookie-sig) | package: compatibility-lib |
signature
cookie^ : signature
Includes everything exported by the net/cookie module.