Changeset 15079 in project


Ignore:
Timestamp:
06/27/09 15:12:36 (10 years ago)
Author:
sjamaan
Message:

Add error handling capabilities for bad headers

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

Legend:

Unmodified
Added
Removed
  • release/4/intarweb/trunk/intarweb.scm

    r15023 r15079  
    5050   http-0.9-request-parser http-1.x-request-parser
    5151   http-0.9-request-unparser http-1.x-request-unparser
     52   header-parse-error-handler
    5253   
    5354   make-response response? response-major response-major-set!
     
    334335     )))
    335336
     337(define header-parse-error-handler ;; ignore errors
     338  (make-parameter (lambda (header-name contents headers exn) headers)))
     339
    336340;; The parser is supposed to return a list of header values for its header
    337341(define (parse-header name contents)
     
    345349              (header-name (http-name->symbol (string-take line colon-idx)))
    346350              (contents    (string-trim-both (string-drop line (add1 colon-idx)))))
    347              (update-header-contents!
    348               header-name (parse-header header-name contents) headers))
     351     (handle-exceptions
     352      exn
     353      ((header-parse-error-handler) header-name contents headers exn)
     354      (update-header-contents!
     355       header-name (parse-header header-name contents) headers)))
    349356   (signal-http-condition "Bad header line" 'header-error 'contents line)))
    350357
  • release/4/intarweb/trunk/tests/run.scm

    r15054 r15079  
    1515    ((_ ?error-type ?expr)
    1616     (test-error* (sprintf "~S" '?expr) ?error-type ?expr))))
     17
     18(header-parse-error-handler (lambda (header-name contents exn) (raise exn)))
    1719
    1820(define (test-read-headers str)
     
    6567           '("foo, bar" "blah") (header-values 'unknown headers))))
    6668  (test-group "Miscellaneous"
    67     (parameterize ((header-parsers `((foo . ,(multiple identity)))))
     69    (parameterize ((header-parsers `((foo . ,(multiple identity))
     70                                     (bar . ,(lambda x (error "bad header"))))))
    6871      (test-error "Missing header contents" (test-read-headers "foo\r\n\r\n"))
     72      (test-error "Bad header w/ handler" (test-read-headers "bar: x\r\n\r\n"))
     73      (parameterize ((parse-error-handler (lambda (n c h exn) h)))
     74       (test "Bad header w/o handler" #t (headers? (test-read-headers "bar: x\r\n\r\n"))))
    6975      ;; RFC 2616 2.2
    7076      ;; "The backslash character ("\") MAY be used as a single-character
     
    691697;; - Fix the parsing system so it's not so broken (more comfortable combinators)
    692698;; - Test malformed headers
    693 ;; - When headers are malformed, what to do? Return #f for value and let
    694 ;;    single/multiple discard them? Throw an exception?
    695699;; - Add parsing capability for quoted-pairs inside tokens and comments
    696700;; - Rethink the auto-chunking stuff. Maybe this should be done at a higher level
Note: See TracChangeset for help on using the changeset viewer.