From fbccbd8ad3183477c74ebb685457f42cfe78099e Mon Sep 17 00:00:00 2001 From: Jay McCarthy Date: Tue, 27 Aug 2013 08:51:43 -0600 Subject: [PATCH] Parse HTTP responses more generously --- .../racket-test/tests/net/http-client.rkt | 12 ++++++++++++ racket/collects/net/http-client.rkt | 9 ++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/pkgs/racket-pkgs/racket-test/tests/net/http-client.rkt b/pkgs/racket-pkgs/racket-test/tests/net/http-client.rkt index 38778baab2..d2135180a5 100644 --- a/pkgs/racket-pkgs/racket-test/tests/net/http-client.rkt +++ b/pkgs/racket-pkgs/racket-test/tests/net/http-client.rkt @@ -78,6 +78,18 @@ '(#"Content-Type: text/plain") #"This is the data in the first chunk and this is the second one"] + ["HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 62\r\n\r\nThis is the data in the first chunk and this is the second one" + #"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n" + #"HTTP/1.0 200 OK" + '(#"Content-Type: text/plain" #"Content-Length: 62") + #"This is the data in the first chunk and this is the second one"] + + ["HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\ncontent-length: 62\r\n\r\nThis is the data in the first chunk and this is the second one" + #"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n" + #"HTTP/1.0 200 OK" + '(#"Content-Type: text/plain" #"content-length: 62") + #"This is the data in the first chunk and this is the second one"] + ["HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n20\r\nThis is the data in the first ch\r\n21\r\nand this is the second oneXXXXXXX\r\n0\r\n" #"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n" diff --git a/racket/collects/net/http-client.rkt b/racket/collects/net/http-client.rkt index a81dcfa2bc..d1f6105616 100644 --- a/racket/collects/net/http-client.rkt +++ b/racket/collects/net/http-client.rkt @@ -22,6 +22,9 @@ (error 'http-client "Connection ended early")) bs) +(define (regexp-member rx l) + (ormap (λ (h) (regexp-match rx h)) l)) + ;; Core (struct http-conn (host to from abandon-p) #:mutable) @@ -160,13 +163,13 @@ (define headers (http-conn-headers! hc)) (define close? (or iclose? - (member #"Connection: close" headers))) + (regexp-member #rx#"^(?i:Connection: +close)$" headers))) (define response-port (cond - [(member #"Transfer-Encoding: chunked" headers) + [(regexp-member #rx#"^(?i:Transfer-Encoding: +chunked)$" headers) (http-conn-response-port/chunked! hc #:close? #t)] [(ormap (λ (h) - (match (regexp-match #rx#"^Content-Length: (.+)$" h) + (match (regexp-match #rx#"^(?i:Content-Length:) +(.+)$" h) [#f #f] [(list _ cl-bs) (string->number