From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Daniel Hartwig Newsgroups: gmane.lisp.guile.bugs Subject: bug#13544: (web http) fails to parse numeric timezones in Date header Date: Fri, 15 Mar 2013 22:40:17 +0800 Message-ID: <877gl8ohf2.fsf@gmail.com> References: <8738xqjkks.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1363358517 11449 80.91.229.3 (15 Mar 2013 14:41:57 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 15 Mar 2013 14:41:57 +0000 (UTC) To: 13544@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Fri Mar 15 15:42:20 2013 Return-path: Envelope-to: guile-bugs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1UGVpq-0007j3-Nh for guile-bugs@m.gmane.org; Fri, 15 Mar 2013 15:42:18 +0100 Original-Received: from localhost ([::1]:57850 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGVpT-00071h-Tm for guile-bugs@m.gmane.org; Fri, 15 Mar 2013 10:41:55 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:34802) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGVpM-0006x1-N7 for bug-guile@gnu.org; Fri, 15 Mar 2013 10:41:53 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UGVpJ-00027U-JK for bug-guile@gnu.org; Fri, 15 Mar 2013 10:41:48 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:52394) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGVpJ-00027M-Ft for bug-guile@gnu.org; Fri, 15 Mar 2013 10:41:45 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.72) (envelope-from ) id 1UGVqY-0006tB-4V for bug-guile@gnu.org; Fri, 15 Mar 2013 10:43:02 -0400 X-Loop: help-debbugs@gnu.org In-Reply-To: <8738xqjkks.fsf@gnu.org> Resent-From: Daniel Hartwig Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-CC: bug-guile@gnu.org Resent-Date: Fri, 15 Mar 2013 14:43:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 13544 X-GNU-PR-Package: guile X-GNU-PR-Keywords: Original-Received: via spool by 13544-submit@debbugs.gnu.org id=B13544.136335852126397 (code B ref 13544); Fri, 15 Mar 2013 14:43:01 +0000 Original-Received: (at 13544) by debbugs.gnu.org; 15 Mar 2013 14:42:01 +0000 Original-Received: from localhost ([127.0.0.1]:56503 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1UGVpX-0006ra-Tt for submit@debbugs.gnu.org; Fri, 15 Mar 2013 10:42:01 -0400 Original-Received: from mail-pb0-f53.google.com ([209.85.160.53]:64634) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1UGVpU-0006rF-4E for 13544@debbugs.gnu.org; Fri, 15 Mar 2013 10:41:57 -0400 Original-Received: by mail-pb0-f53.google.com with SMTP id un1so3875838pbc.40 for <13544@debbugs.gnu.org>; Fri, 15 Mar 2013 07:40:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:subject:date:message-id:mime-version :content-type; bh=jHGzvA4ZZhg0gam8Z0TF8pHkNSK6AS3O6u0uwMRfMKc=; b=BgiNyMY0Sx7fNdddW1Pwx4c09Mw8u5l+E+vzR3/SfI2OUfKeJUpFLstgWT+Teq+i1j Tx2U8K04tEvTbLSdMxjaAZxXUN/FjNNwXXd4hG/6YRFICEYqRxejNFDEyYS5fDQrqZ2F ihXOy8yK6C1jSB/1EWMreh8VDAwfO3nbYyz+Dazpj5DfO8PD30WKNyVAWUN8B/e0nJmp SIEP+3vPPYnrKRU4gtsxt3c+hCwWA95z/L/N5D2hagpNZZpqOOFCKFlU8B8t+fM1naQm PCAYDqutAK08ymk/vqmkoue6SQnUHzVBpb3yMUDQnFttRQ0OGa9htd2WnDSPEJLwl4Z1 GPqg== X-Received: by 10.68.222.73 with SMTP id qk9mr16635296pbc.115.1363358432690; Fri, 15 Mar 2013 07:40:32 -0700 (PDT) Original-Received: from io ([120.156.22.79]) by mx.google.com with ESMTPS id qb10sm9067546pbb.43.2013.03.15.07.40.29 (version=TLSv1.1 cipher=RC4-SHA bits=128/128); Fri, 15 Mar 2013 07:40:31 -0700 (PDT) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 140.186.70.43 X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Original-Sender: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.bugs:6973 Archived-At: --=-=-= See attached for handling of numeric time zones that may or may not be GMT. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-web-http-parse-numeric-time-zones-in-headers.patch >From 430fc9498ee08f6d06b5ec494a5d65e395c6c067 Mon Sep 17 00:00:00 2001 From: Daniel Hartwig Date: Fri, 15 Mar 2013 22:25:10 +0800 Subject: [PATCH] web http: parse numeric time zones in headers * module/web/http.scm (parse-zone-offset, normalize-date): New procedures. (parse-rfc-822-date, parse-rfc-850-date, parse-date): Update. * test-suite/tests/web-http.test ("general headers"): Add test. --- module/web/http.scm | 61 ++++++++++++++++++++++++++++++---------- test-suite/tests/web-http.test | 3 ++ 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/module/web/http.scm b/module/web/http.scm index c79d57d..975eb8e 100644 --- a/module/web/http.scm +++ b/module/web/http.scm @@ -702,29 +702,50 @@ as an ordered alist." (else (bad)))) (else (bad)))))) +;; "GMT" | "+" 4DIGIT | "-" 4DIGIT +;; +;; RFC 2616 requires date values to use "GMT", but recommends accepting +;; the others as they are commonly generated by e.g. RFC 822 sources. +(define (parse-zone-offset str start) + (let ((s (substring str start))) + (define (bad) + (bad-header-component 'zone-offset s)) + (cond + ((string=? s "GMT") + 0) + ((string-match? s ".dddd") + (let ((sign (case (string-ref s 0) + ((#\+) +1) + ((#\-) -1) + (else (bad)))) + (hours (parse-non-negative-integer s 1 3)) + (minutes (parse-non-negative-integer s 3 5))) + (* sign 60 (+ (* 60 hours) minutes)))) ; seconds east of Greenwich + (else (bad))))) + ;; RFC 822, updated by RFC 1123 ;; ;; Sun, 06 Nov 1994 08:49:37 GMT ;; 01234567890123456789012345678 ;; 0 1 2 -(define (parse-rfc-822-date str) +(define (parse-rfc-822-date str space zone-offset) ;; We could verify the day of the week but we don't. - (cond ((string-match? str "aaa, dd aaa dddd dd:dd:dd GMT") + (cond ((string-match? (substring str 0 space) "aaa, dd aaa dddd dd:dd:dd") (let ((date (parse-non-negative-integer str 5 7)) (month (parse-month str 8 11)) (year (parse-non-negative-integer str 12 16)) (hour (parse-non-negative-integer str 17 19)) (minute (parse-non-negative-integer str 20 22)) (second (parse-non-negative-integer str 23 25))) - (make-date 0 second minute hour date month year 0))) - ((string-match? str "aaa, d aaa dddd dd:dd:dd GMT") + (make-date 0 second minute hour date month year zone-offset))) + ((string-match? (substring str 0 space) "aaa, d aaa dddd dd:dd:dd") (let ((date (parse-non-negative-integer str 5 6)) (month (parse-month str 7 10)) (year (parse-non-negative-integer str 11 15)) (hour (parse-non-negative-integer str 16 18)) (minute (parse-non-negative-integer str 19 21)) (second (parse-non-negative-integer str 22 24))) - (make-date 0 second minute hour date month year 0))) + (make-date 0 second minute hour date month year zone-offset))) (else (bad-header 'date str) ; prevent tail call #f))) @@ -733,10 +754,10 @@ as an ordered alist." ;; Sunday, 06-Nov-94 08:49:37 GMT ;; 0123456789012345678901 ;; 0 1 2 -(define (parse-rfc-850-date str comma) +(define (parse-rfc-850-date str comma space zone-offset) ;; We could verify the day of the week but we don't. - (let ((tail (substring str (1+ comma)))) - (if (not (string-match? tail " dd-aaa-dd dd:dd:dd GMT")) + (let ((tail (substring str (1+ comma) space))) + (if (not (string-match? tail " dd-aaa-dd dd:dd:dd")) (bad-header 'date str)) (let ((date (parse-non-negative-integer tail 1 3)) (month (parse-month tail 4 7)) @@ -750,7 +771,7 @@ as an ordered alist." (cond ((< (+ then 50) now) (+ then 100)) ((< (+ now 50) then) (- then 100)) (else then))) - 0)))) + zone-offset)))) ;; ANSI C's asctime() format ;; Sun Nov 6 08:49:37 1994 @@ -770,13 +791,23 @@ as an ordered alist." (second (parse-non-negative-integer str 17 19))) (make-date 0 second minute hour date month year 0))) +;; Convert all date values to GMT time zone, as per RFC 2616 appendix C. +(define (normalize-date date) + (if (zero? (date-zone-offset date)) + date + (time-utc->date (date->time-utc date) 0))) + (define (parse-date str) - (if (string-suffix? " GMT" str) - (let ((comma (string-index str #\,))) - (cond ((not comma) (bad-header 'date str)) - ((= comma 3) (parse-rfc-822-date str)) - (else (parse-rfc-850-date str comma)))) - (parse-asctime-date str))) + (let* ((space (string-rindex str #\space)) + (zone-offset (and space (false-if-exception + (parse-zone-offset str (1+ space)))))) + (normalize-date + (if zone-offset + (let ((comma (string-index str #\,))) + (cond ((not comma) (bad-header 'date str)) + ((= comma 3) (parse-rfc-822-date str space zone-offset)) + (else (parse-rfc-850-date str comma space zone-offset)))) + (parse-asctime-date str))))) (define (write-date date port) (define (display-digits n digits port) diff --git a/test-suite/tests/web-http.test b/test-suite/tests/web-http.test index 97f5559..0baa6ab 100644 --- a/test-suite/tests/web-http.test +++ b/test-suite/tests/web-http.test @@ -109,6 +109,9 @@ (pass-if-parse date "Tue, 15 Nov 1994 08:12:31 GMT" (string->date "Tue, 15 Nov 1994 08:12:31 +0000" "~a, ~d ~b ~Y ~H:~M:~S ~z")) + (pass-if-parse date "Tue, 15 Nov 1994 16:12:31 +0800" + (string->date "Tue, 15 Nov 1994 08:12:31 +0000" + "~a, ~d ~b ~Y ~H:~M:~S ~z")) (pass-if-parse date "Wed, 7 Sep 2011 11:25:00 GMT" (string->date "Wed, 7 Sep 2011 11:25:00 +0000" "~a,~e ~b ~Y ~H:~M:~S ~z")) -- 1.7.10.4 --=-=-=--