Stricter cookie contracts

svn: r18629
This commit is contained in:
Jay McCarthy 2010-03-26 16:07:35 +00:00
parent 302555cf01
commit e69a18eefd
5 changed files with 23 additions and 12 deletions

View File

@ -1,5 +1,7 @@
#lang scheme/signature #lang scheme/signature
cookie-name?
cookie-value?
cookie? cookie?
valid-domain? valid-domain?
set-cookie set-cookie

View File

@ -294,13 +294,14 @@
;; ;;
;; Returns whether this is a valid string to use as the value or the ;; Returns whether this is a valid string to use as the value or the
;; name (depending on value?) of an HTTP cookie. ;; name (depending on value?) of an HTTP cookie.
(define (cookie-string? s [value? #t]) (define (cookie-value? s)
(unless (string? s) (and (string? s)
(error* "string expected, received: ~a" s)) (rfc2109:value? s)))
(if value?
(rfc2109:value? s) (define (cookie-name? s)
;; name: token (and (string? s)
(rfc2068:token? s))) ;; name: token
(rfc2068:token? s)))
;; Host names as per RFC 1123 and RFC952, more or less, anyway. :-) ;; Host names as per RFC 1123 and RFC952, more or less, anyway. :-)
(define char-set:hostname (define char-set:hostname

View File

@ -25,7 +25,15 @@ otherwise.}
Returns @scheme[#t] if @scheme[v] represents a valid domain, @scheme[#f] otherwise. Returns @scheme[#t] if @scheme[v] represents a valid domain, @scheme[#f] otherwise.
} }
@defproc[(set-cookie [name string?] [value string?]) cookie?]{ @defproc[(cookie-name? [v any/c]) boolean?]{
Returns @scheme[#t] if @scheme[v] is a valid cookie name string, @scheme[#f] otherwise.
}
@defproc[(cookie-value? [v any/c]) boolean?]{
Returns @scheme[#t] if @scheme[v] is a valid cookie value string, @scheme[#f] otherwise.
}
@defproc[(set-cookie [name cookie-name?] [value cookie-value?]) cookie?]{
Creates a new cookie, with default values for required fields.} Creates a new cookie, with default values for required fields.}
@ -74,7 +82,7 @@ Prints @scheme[cookie] to a string. Empty fields do not appear in the
output except when there is a required default.} output except when there is a required default.}
@defproc[(get-cookie [name string?] [cookies string?]) (listof string?)]{ @defproc[(get-cookie [name cookie-name?] [cookies string?]) (listof cookie-value?)]{
Returns a list with all the values (strings) associated with @scheme[name]. Returns a list with all the values (strings) associated with @scheme[name].
@ -86,7 +94,7 @@ initial-request structure, etc. The @scheme[get-cookie] and
from a @scheme["Cookie"] field value.} from a @scheme["Cookie"] field value.}
@defproc[(get-cookie/single [name string?] [cookies string?]) (or/c string? false/c)]{ @defproc[(get-cookie/single [name cookie-name?] [cookies string?]) (or/c cookie-value? false/c)]{
Like @scheme[get-cookie], but returns the just first value string Like @scheme[get-cookie], but returns the just first value string
associated to @scheme[name], or #f if no association is found.} associated to @scheme[name], or #f if no association is found.}

View File

@ -7,7 +7,7 @@
scheme/contract) scheme/contract)
(provide/contract (provide/contract
[make-cookie ((string? string?) (#:comment (or/c false/c string?) [make-cookie ((cookie-name? cookie-value?) (#:comment (or/c false/c string?)
#:domain (or/c false/c valid-domain?) #:domain (or/c false/c valid-domain?)
#:max-age (or/c false/c exact-nonnegative-integer?) #:max-age (or/c false/c exact-nonnegative-integer?)
#:path (or/c false/c string?) #:path (or/c false/c string?)

View File

@ -271,7 +271,7 @@ transmission that the server @bold{will not catch}.}
@defmodule[web-server/http/cookie]{ @defmodule[web-server/http/cookie]{
This module provides functions to create cookies and responses that set them. This module provides functions to create cookies and responses that set them.
@defproc[(make-cookie [name string?] [value string?] @defproc[(make-cookie [name cookie-name?] [value cookie-value?]
[#:comment comment (or/c false/c string?) #f] [#:comment comment (or/c false/c string?) #f]
[#:domain domain (or/c false/c valid-domain?) #f] [#:domain domain (or/c false/c valid-domain?) #f]
[#:max-age max-age (or/c false/c exact-nonnegative-integer?) #f] [#:max-age max-age (or/c false/c exact-nonnegative-integer?) #f]