unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH] Support HTTP/2 headers from web servers.
@ 2017-07-04 16:15 Derek Upham
  0 siblings, 0 replies; 2+ messages in thread
From: Derek Upham @ 2017-07-04 16:15 UTC (permalink / raw)
  To: guile-devel

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

The web client module doesn’t currently support HTTPS, so I have been calling out to curl and parsing the result with the web client code.  This is breaking for HTTP/2 servers, as the code expects a MAJOR.MINOR format, and the HTTP/2 working group is explicitly not using a minor version in the HTTP/2 spec (https://http2.github.io/faq/#is-it-http20-or-http2).  For example,

  curl --silent --head https://www.google.com

returns the first line

  HTTP/2 200

This patch adds parsing and printing support that handles HTTP/2, representing it as a consistent ‘'(2 . 0)’ internally.

I’m tempted to drop the whole “find the dot index” approach for HTTP/1.x and just explicitly match on “HTTP/1.0” and “HTTP/1.1” as well.

Derek


[-- Attachment #2: Git patch file --]
[-- Type: text/x-diff, Size: 3351 bytes --]

From f957bd2b496a2aea92d1184e57c18215acc3ed22 Mon Sep 17 00:00:00 2001
From: Derek Upham <sand@blarg.net>
Date: Tue, 4 Jul 2017 08:54:06 -0700
Subject: [PATCH] Support HTTP/2 headers from web servers.

For example,

  curl --silent --head https://www.google.com

returns the first line

  HTTP/2 200

which does not match the MAJOR.MINOR format expected by the old code.
---
 module/web/http.scm | 41 ++++++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/module/web/http.scm b/module/web/http.scm
index 993b50ef4..db0dff76d 100644
--- a/module/web/http.scm
+++ b/module/web/http.scm
@@ -1059,7 +1059,11 @@ as an ordered alist."
 (define* (parse-http-version str #:optional (start 0) (end (string-length str)))
   "Parse an HTTP version from STR, returning it as a major–minor
 pair. For example, ‘HTTP/1.1’ parses as the pair of integers,
-‘(1 . 1)’."
+‘(1 . 1)’.
+
+The HTTP/2 working group has explicitly dropped the ‘.0’ minor
+version.  For internal consistency, track the the missing minor
+version as zero."
   (let lp ((known *known-versions*))
     (match known
       (((version-str . version-val) . known)
@@ -1067,28 +1071,35 @@ pair. For example, ‘HTTP/1.1’ parses as the pair of integers,
            version-val
            (lp known)))
       (()
-       (let ((dot-idx (string-index str #\. start end)))
-         (unless (and (string-prefix? "HTTP/" str 0 5 start end)
-                      dot-idx
-                      (= dot-idx (string-rindex str #\. start end)))
-           
-           (bad-header-component 'http-version (substring str start end)))
-         (cons (parse-non-negative-integer str (+ start 5) dot-idx)
-               (parse-non-negative-integer str (1+ dot-idx) end)))))))
+       (if (string=? "HTTP/2" (substring str start end))
+           '(2 . 0)
+           (let ((dot-idx (string-index str #\. start end)))
+             (unless (and (string-prefix? "HTTP/" str 0 5 start end)
+                          dot-idx
+                          (= dot-idx (string-rindex str #\. start end)))
+               (bad-header-component 'http-version (substring str start end)))
+             (cons (parse-non-negative-integer str (+ start 5) dot-idx)
+                   (parse-non-negative-integer str (1+ dot-idx) end))))))))
 
 (define (write-http-version val port)
-  "Write the given major-minor version pair to PORT."
-  (put-string port "HTTP/")
-  (put-non-negative-integer port (car val))
-  (put-char port #\.)
-  (put-non-negative-integer port (cdr val)))
+  "Write the given major-minor version pair to PORT.
+
+For consistency with HTTP/2 standards, print version ‘(2 . 0)’ as
+“HTTP/2”."
+  (if (equal? val '(2 . 0))
+      (put-string port "HTTP/2")
+      (begin
+        (put-string port "HTTP/")
+        (put-non-negative-integer port (car val))
+        (put-char port #\.)
+        (put-non-negative-integer port (cdr val)))))
 
 (for-each
  (lambda (v)
    (set! *known-versions*
          (acons v (parse-http-version v 0 (string-length v))
                 *known-versions*)))
- '("HTTP/1.0" "HTTP/1.1"))
+ '("HTTP/1.0" "HTTP/1.1" "HTTP/2"))
 
 
 ;; Request-URI = "*" | absoluteURI | abs_path | authority
-- 
2.13.1


[-- Attachment #3: Type: text/plain, Size: 32 bytes --]


-- 
Derek Upham
sand@blarg.net

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH] Support HTTP/2 headers from web servers.
@ 2018-10-30 13:42 Derek Upham
  0 siblings, 0 replies; 2+ messages in thread
From: Derek Upham @ 2018-10-30 13:42 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: patch file --]
[-- Type: text/x-diff, Size: 3351 bytes --]

From f957bd2b496a2aea92d1184e57c18215acc3ed22 Mon Sep 17 00:00:00 2001
From: Derek Upham <sand@blarg.net>
Date: Tue, 4 Jul 2017 08:54:06 -0700
Subject: [PATCH] Support HTTP/2 headers from web servers.

For example,

  curl --silent --head https://www.google.com

returns the first line

  HTTP/2 200

which does not match the MAJOR.MINOR format expected by the old code.
---
 module/web/http.scm | 41 ++++++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/module/web/http.scm b/module/web/http.scm
index 993b50ef4..db0dff76d 100644
--- a/module/web/http.scm
+++ b/module/web/http.scm
@@ -1059,7 +1059,11 @@ as an ordered alist."
 (define* (parse-http-version str #:optional (start 0) (end (string-length str)))
   "Parse an HTTP version from STR, returning it as a major–minor
 pair. For example, ‘HTTP/1.1’ parses as the pair of integers,
-‘(1 . 1)’."
+‘(1 . 1)’.
+
+The HTTP/2 working group has explicitly dropped the ‘.0’ minor
+version.  For internal consistency, track the the missing minor
+version as zero."
   (let lp ((known *known-versions*))
     (match known
       (((version-str . version-val) . known)
@@ -1067,28 +1071,35 @@ pair. For example, ‘HTTP/1.1’ parses as the pair of integers,
            version-val
            (lp known)))
       (()
-       (let ((dot-idx (string-index str #\. start end)))
-         (unless (and (string-prefix? "HTTP/" str 0 5 start end)
-                      dot-idx
-                      (= dot-idx (string-rindex str #\. start end)))
-           
-           (bad-header-component 'http-version (substring str start end)))
-         (cons (parse-non-negative-integer str (+ start 5) dot-idx)
-               (parse-non-negative-integer str (1+ dot-idx) end)))))))
+       (if (string=? "HTTP/2" (substring str start end))
+           '(2 . 0)
+           (let ((dot-idx (string-index str #\. start end)))
+             (unless (and (string-prefix? "HTTP/" str 0 5 start end)
+                          dot-idx
+                          (= dot-idx (string-rindex str #\. start end)))
+               (bad-header-component 'http-version (substring str start end)))
+             (cons (parse-non-negative-integer str (+ start 5) dot-idx)
+                   (parse-non-negative-integer str (1+ dot-idx) end))))))))
 
 (define (write-http-version val port)
-  "Write the given major-minor version pair to PORT."
-  (put-string port "HTTP/")
-  (put-non-negative-integer port (car val))
-  (put-char port #\.)
-  (put-non-negative-integer port (cdr val)))
+  "Write the given major-minor version pair to PORT.
+
+For consistency with HTTP/2 standards, print version ‘(2 . 0)’ as
+“HTTP/2”."
+  (if (equal? val '(2 . 0))
+      (put-string port "HTTP/2")
+      (begin
+        (put-string port "HTTP/")
+        (put-non-negative-integer port (car val))
+        (put-char port #\.)
+        (put-non-negative-integer port (cdr val)))))
 
 (for-each
  (lambda (v)
    (set! *known-versions*
          (acons v (parse-http-version v 0 (string-length v))
                 *known-versions*)))
- '("HTTP/1.0" "HTTP/1.1"))
+ '("HTTP/1.0" "HTTP/1.1" "HTTP/2"))
 
 
 ;; Request-URI = "*" | absoluteURI | abs_path | authority
-- 
2.13.1


[-- Attachment #2: Type: text/plain, Size: 881 bytes --]


The web client module doesn’t currently support HTTPS, so I have been calling out to curl and parsing the result with the web client code.  This is breaking for HTTP/2 servers, as the code expects a MAJOR.MINOR format, and the HTTP/2 working group is explicitly not using a minor version in the HTTP/2 spec (https://http2.github.io/faq/#is-it-http20-or-http2).  For example,

  curl --silent --head https://www.google.com

returns the first line

  HTTP/2 200

This patch adds parsing and printing support that handles HTTP/2, representing it as a consistent ‘'(2 . 0)’ internally.

At this point we can consider dropping the whole “find the dot index” approach for HTTP/1.x headers, and just explicitly match on “HTTP/1.0” and “HTTP/1.1” as well.  There won’t be an HTTP/1.2 to worry about.

Derek

-- 
Derek Upham
derek_upham@mailfence.com

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-10-30 13:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-30 13:42 [PATCH] Support HTTP/2 headers from web servers Derek Upham
  -- strict thread matches above, loose matches on Subject: below --
2017-07-04 16:15 Derek Upham

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).