From ffac0ebcac185944b0d209bb6390ef76336d0a20 Mon Sep 17 00:00:00 2001 From: vibr Date: Sun, 15 Aug 2004 11:49:15 +0000 Subject: [PATCH] simplified get-numeric-field-value (now uses string-trim-both), adapted packages.scm --- scheme/lib/sunet-utilities.scm | 33 +++++++++++++++------------------ scheme/packages.scm | 2 +- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/scheme/lib/sunet-utilities.scm b/scheme/lib/sunet-utilities.scm index 81a97bc..78b393b 100644 --- a/scheme/lib/sunet-utilities.scm +++ b/scheme/lib/sunet-utilities.scm @@ -80,29 +80,24 @@ ;; check wether a header-field with name field-name is contained in req; ;; if not, return #f, -;; if there is one, check wether its field-content conforms to +;; else, take the first such header field and check wether its field-content conforms to ;; field-content = *LWS 1*DIGIT *LWS -;; (i.e. optional leading whitespaces, at least one digit, optional trailing whitespace); ;; if so, return digit as a number (define (get-numeric-field-value req field-name) (let - ;;take first Content-length: header (RFC 2616 allows only one Content-length: header) + ;;try to get first "field-name" header ((field-content (get-header (request-headers req) field-name))) - (if field-content - (let* - ((field-value-start (string-skip field-content char-set:whitespace));; skip whitespace, ;;char-set:whitespace = LWS from RFC2616? - (field-value (if field-value-start ;;yes, field content contained non-whitespace chars - (string->number (substring field-content - field-value-start - (string-length field-content))) ;;trailing whitespace? RFC allows it! -> - ;; probably read-rfc822-headers in rfc822.scm should do the job of skipping leading and trailing whitespace?* - (http-error (status-code bad-request) req - (format #f "~A header contained only whitespace" field-name))))) - (if (and (integer? field-value) (>= field-value 0)) ;;yes, field value contained only digits + (if field-content ;; request contained "field-name" header + (let ;;see * below + ((field-value (string->number (string-trim-both field-content char-set:whitespace)))) ;;char-set:whitespace = LWS from RFC2616? + (if (and (integer? field-value) (>= field-value 0)) ;;yes, field value contained only digits, and at least one digit. field-value - (http-error (status-code bad-request) req - (format #f "~A header contained characters other than digits or whitespace between digits" field-name)))) + (http-error + (status-code bad-request) req + (format #f + "~A header contained only whitespace, or characters other than digits, or whitespace between digits" + field-name)))) #f))) @@ -112,12 +107,14 @@ ;;non-whitespace character of the field-value. Such leading or ;;trailing LWS MAY be removed without changing the semantics of the ;;field value. - +;;(probably read-rfc822-headers in rfc822.scm should do the job of skipping leading and trailing whitespace?) ;;get request's message-body length from Content-length: header or ;;throw http-error if no such header (define (get-body-length-from-content-length req) - (let ((maybe-length (get-numeric-field-value req 'content-length))) + (let + ;;try to get field value of first Content-length header (RFC 2616 allows only one Content-length: header) + ((maybe-length (get-numeric-field-value req 'content-length))) (or maybe-length (http-error (status-code bad-request) req "No Content-Length header in request")))) diff --git a/scheme/packages.scm b/scheme/packages.scm index c1a04d5..3c89a6b 100644 --- a/scheme/packages.scm +++ b/scheme/packages.scm @@ -604,7 +604,7 @@ (open scheme-with-scsh format-net sigevents - (subset srfi-13 (string-join string-skip)) + (subset srfi-13 (string-join string-skip string-trim-both)) dns let-opt ; :optional locks