Changeset 11853 in project


Ignore:
Timestamp:
09/01/08 21:51:29 (13 years ago)
Author:
sjamaan
Message:

Add tests for quoting special chars (need to fix reader+parser to accept them too. At least they're generated properly now)

Location:
release/4/intarweb/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • release/4/intarweb/trunk/header-parsers.scm

    r11815 r11853  
    316316          (loop (cdr params) (if str (cons str results) results))))))
    317317
    318 (define (unparse-token token)
    319   (if (string-any (char-set #\" #\; #\,) token)
    320       (string-append "\"" (string-translate* token '(("\"" . "\\\"")
    321                                                      ("\\" . "\\\\"))) "\"")
    322       token))
     318(define unparse-token
     319  (let* ((must-be-quoted-chars (char-set-adjoin char-set:iso-control #\"))
     320         (trigger-quoting-chars (char-set-adjoin must-be-quoted-chars #\, #\;))
     321         (smap (map (lambda (c)
     322                      (cons (string c)
     323                            (string-append "\\" (string c))))
     324                    (char-set->list must-be-quoted-chars))))
     325   (lambda (token)
     326     (if (string-any trigger-quoting-chars token)
     327         (string-append "\"" (string-translate* token smap) "\"")
     328         token))))
    323329
    324330;; There's no need to make a specific header unparser for every header type.
     
    335341               (value (cond
    336342                       ((pair? contents) ; alist?
     343                        ;; XXX Do something with =, comma, semicolon and newline
    337344                        (if (eq? (cdr contents) #t)
    338345                            (->string (car contents))
  • release/4/intarweb/trunk/tests/run.scm

    r11832 r11853  
    6565           '("foo, bar" "blah") (header-values 'unknown headers))))
    6666  (test-group "Miscellaneous"
    67     (test-error "Missing header contents" (test-read-headers "foo\r\n\r\n"))))
     67    (test-error "Missing header contents" (test-read-headers "foo\r\n\r\n"))
     68    ;; RFC 2616 2.2
     69    ;; "The backslash character ("\") MAY be used as a single-character
     70    ;; quoting mechanism only within quoted-string and comment constructs."
     71    ;;     quoted-pair = "\" CHAR
     72    ;; CHAR implies any char, *including* CR/LF. This is clarified by RFC 822,
     73    ;; on which RFC 2616 is based.
     74    ;; Apparently, even \CRLF is allowed (as opposed to \CR\LF)
     75    (test "Embedded newlines"
     76          "bar\r\nqux"
     77          (test-read-headers "Foo: \"bar\\\r\\\nqux\""))))
    6878
    6979(test-group "Specialized header parsers"
     
    286296          "Foo: \"bar, qux\"\r\n"
    287297          (test-unparse-headers `((foo "bar, qux"))))
    288     (test "Escaping"
     298    (test "Escaping quotes"
    289299          "Foo: \"bar \\\" qux\", mooh\r\n"
    290300          (test-unparse-headers `((foo "bar \" qux" "mooh"))))
     301    (test "Escaping control characters"
     302          "Foo: \"bar\\\r\\\x01qux\"\r\n"
     303          (test-unparse-headers `((foo "bar\r\x01qux"))))
    291304    (test "Alist"
    292305          "Foo: bar=qux, mooh=mumble\r\n"
Note: See TracChangeset for help on using the changeset viewer.