unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Daniel Hartwig <mandyke@gmail.com>
To: "R. P. Dillon" <rpdillon@gmail.com>
Cc: bug-guile@gnu.org, guile-user@gnu.org
Subject: HTTP "Expires" header should handle non-date values
Date: Sun, 27 Nov 2011 18:39:12 +0800	[thread overview]
Message-ID: <CAN3veRcj__twTi8nXKaEqNFrEmwOa2rhBEwmXLq-BV5dpXzApA@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1951 bytes --]

Package: guile
Version: 2.0.3
Tags: patch

On 6 November 2011 13:49, R. P. Dillon <rpdillon@gmail.com> wrote:
> (use-modules (web request) (web response) (web uri) (rnrs bytevectors))
> (define port (socket PF_INET SOCK_STREAM 0))
> (define address (addrinfo:addr (car (getaddrinfo "www.google.com" "http"))))
> (connect port address)
> (define request (build-request (build-uri 'http #:host "www.google.com")))
> (write-request request port)
> (define response (read-response port))
> (read-response ...) consistently fails with Google:
> web/http.scm:754:6: In procedure parse-asctime-date:
> web/http.scm:754:6: Throw to key `bad-header' with args `(date "-1")'.
> The expiration is set to -1 in the headers, and this seems to cause a
> problem for the web libraries in Guile.
> This same request seems to work well for my own domain (killring.org).

This is definitely a bug on Guile's part, HTTP/1.1 permits such values
for "Expires" headers [1], treating them as though they were a date in
the past:

   HTTP/1.1 clients and caches MUST treat other invalid date formats,
   especially including the value "0", as in the past (i.e., "already
   expired").

[1] http://tools.ietf.org/html/rfc2616#section-14.21

Attached patch permits non-date values for "Expires", leaving them as
strings (preferable, as such responses can be transparently forwarded
to other clients). The staleness of a response could be determined
quite crudely, e.g.

(define (response-stale? r)
  (let ((expires (response-expires r)))
    (and expires
         (or (not (date? expires)) ;; Indicates already expired.
             (time<=? (date->time-utc expires)
                      (current-time))))))

This approach completely ignores the recommended way of determining
whether a response has expired.  See section 13.2.4 of the RFC for
calculations involving various factors such as the time that a request
was sent, "Cache-Control" directives, etc.


Regards

Daniel

[-- Attachment #2: 0001-Permit-non-date-values-for-Expires-header.patch --]
[-- Type: text/x-patch, Size: 1833 bytes --]

From dcbcd94db89a8e73ebc3089a860575bbeb8d1708 Mon Sep 17 00:00:00 2001
From: Daniel Hartwig <mandyke@gmail.com>
Date: Sun, 27 Nov 2011 18:25:55 +0800
Subject: [PATCH] Permit non-date values for "Expires" header.

* module/web/http.scm ("Expires"): Permit non-date values.
---
 module/web/http.scm            |   20 +++++++++++++++++++-
 test-suite/tests/web-http.test |    1 +
 2 files changed, 20 insertions(+), 1 deletions(-)

diff --git a/module/web/http.scm b/module/web/http.scm
index dc742a1..5eb2e90 100644
--- a/module/web/http.scm
+++ b/module/web/http.scm
@@ -1500,7 +1500,25 @@ phrase\"."
 
 ;; Expires = HTTP-date
 ;;
-(declare-date-header! "Expires")
+;; ...
+;; HTTP/1.1 clients and caches MUST treat other invalid date formats,
+;; especially including the value "0", as in the past (i.e., "already
+;; expired").
+;;
+(declare-header! "Expires"
+  (lambda (str)
+    (or (false-if-exception (parse-date str))
+        str))
+  (lambda (val)
+    (or (string? val) (date? val)))
+  (lambda (val port)
+    (cond
+     ((string? val)
+      (display val port))
+     ((date? val)
+      (write-date val port))
+     (else
+      (bad-header-component 'expires val)))))
 
 ;; Last-Modified = HTTP-date
 ;;
diff --git a/test-suite/tests/web-http.test b/test-suite/tests/web-http.test
index b6abbf3..c881223 100644
--- a/test-suite/tests/web-http.test
+++ b/test-suite/tests/web-http.test
@@ -134,6 +134,7 @@
   (pass-if-parse expires "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 expires "0" "0")
   (pass-if-parse last-modified "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")))
-- 
1.7.2.5


             reply	other threads:[~2011-11-27 10:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-27 10:39 Daniel Hartwig [this message]
2011-12-22  2:51 ` bug#10147: HTTP "Expires" header should handle non-date values Andy Wingo
2011-12-22  4:28   ` Daniel Hartwig
2011-12-22 12:35     ` Andy Wingo
2011-12-27 15:49       ` Daniel Hartwig
     [not found]       ` <CAN3veRfgg+dDzjMy1L8xaAcaZ82dAuFM1dnNpGbzq-5ckoVsAA@mail.gmail.com>
2012-01-09 22:36         ` Andy Wingo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAN3veRcj__twTi8nXKaEqNFrEmwOa2rhBEwmXLq-BV5dpXzApA@mail.gmail.com \
    --to=mandyke@gmail.com \
    --cc=bug-guile@gnu.org \
    --cc=guile-user@gnu.org \
    --cc=rpdillon@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).