original commit: d34ff0a7930e4e32a623e326e79bec087e3db531
This commit is contained in:
Matthew Flatt 2004-10-20 13:13:33 +00:00
parent c48724f181
commit a2494b1726
2 changed files with 19 additions and 9 deletions

View File

@ -8,4 +8,5 @@
form-urlencoded-encode form-urlencoded-encode
form-urlencoded-decode form-urlencoded-decode
alist->form-urlencoded alist->form-urlencoded
form-urlencoded->alist))) form-urlencoded->alist
current-alist-separator-mode)))

View File

@ -208,8 +208,9 @@
;; http://www.w3.org/TR/html401/appendix/notes.html#ampersands-in-uris ;; http://www.w3.org/TR/html401/appendix/notes.html#ampersands-in-uris
;; listof (cons symbol string) -> string ;; listof (cons symbol string) -> string
(define alist->form-urlencoded (define alist->form-urlencoded
(opt-lambda (args [mode 'semi]) (opt-lambda (args)
(let* ([format-one (let* ([mode (current-alist-separator-mode)]
[format-one
(lambda (arg) (lambda (arg)
(let* ([name (car arg)] (let* ([name (car arg)]
[value (cdr arg)]) [value (cdr arg)])
@ -222,20 +223,20 @@
[(null? args) null] [(null? args) null]
[(null? (cdr args)) (list (format-one (car args)))] [(null? (cdr args)) (list (format-one (car args)))]
[else (list* (format-one (car args)) [else (list* (format-one (car args))
(if (eq? mode 'semi) (if (eq? mode 'amp)
";" "&"
"&") ";")
(loop (cdr args)))]))]) (loop (cdr args)))]))])
(apply string-append strs)))) (apply string-append strs))))
;; string -> listof (cons string string) ;; string -> listof (cons string string)
;; http://www.w3.org/TR/html401/appendix/notes.html#ampersands-in-uris ;; http://www.w3.org/TR/html401/appendix/notes.html#ampersands-in-uris
(define form-urlencoded->alist (define form-urlencoded->alist
(opt-lambda (str [mode 'both]) (opt-lambda (str)
(define key-regexp (regexp "[^=]*")) (define key-regexp (regexp "[^=]*"))
(define value-regexp (case mode (define value-regexp (case (current-alist-separator-mode)
[(semi) (regexp "[^;]*")] [(semi) (regexp "[^;]*")]
[(ampm) (regexp "[^&]*")] [(amp) (regexp "[^&]*")]
[else (regexp "[^&;]*")])) [else (regexp "[^&;]*")]))
(define (next-key str start) (define (next-key str start)
(if (>= start (string-length str)) (if (>= start (string-length str))
@ -275,6 +276,14 @@
(loop next-start end (lambda (x) (make-alist (cons pair x))))] (loop next-start end (lambda (x) (make-alist (cons pair x))))]
[#f (make-alist '())])])))) [#f (make-alist '())])]))))
(define current-alist-separator-mode
(make-parameter 'amp-or-semi (lambda (s)
(unless (memq s '(amp semi amp-or-semi))
(raise-type-error 'current-alist-separator-mode
"'amp, 'semi, or 'amp-or-semi"
s))
s)))
)) ))
) )