~ chicken-core (chicken-5) 6a33a2463241171284d99ed07c13ca1738cb1362


commit 6a33a2463241171284d99ed07c13ca1738cb1362
Author:     Peter Bex <peter@more-magic.net>
AuthorDate: Sun Sep 15 15:52:01 2019 +0200
Commit:     Evan Hanson <evhan@foldling.org>
CommitDate: Sun Sep 29 11:44:23 2019 +1300

    Fix egg-download response handling
    
    There were two issues:
    
    - The check for 200 "OK" was nested inside the 407 code handling,
      which meant we would misinterpret error HTML pages as egg contents.
    - The code which handled 407 "Proxy Authentication Required" would
      simply send a new request without reading the response headers
      correctly.
    
    Signed-off-by: Evan Hanson <evhan@foldling.org>

diff --git a/NEWS b/NEWS
index 5642c85b..9b71bd35 100644
--- a/NEWS
+++ b/NEWS
@@ -42,6 +42,8 @@
   - The new "-module-registration" options causes module registration
     code to always be included in the program, even when it has also
     been emitted as a separate file (for example with "-J").
+  - chicken-install now correctly checks server response code to avoid
+    interpreting error response bodies (like 404, 500) as Scheme code.
 
 
 5.1.0
diff --git a/egg-download.scm b/egg-download.scm
index 0839ad83..15dddbbd 100644
--- a/egg-download.scm
+++ b/egg-download.scm
@@ -66,10 +66,10 @@
          ""))
   (let-values (((in out)
                   (tcp-connect (or proxy-host host) (or proxy-port port))))
-    (d "requesting ~s ...~%" locn)
-    (let ((req (make-HTTP-GET/1.1 locn user-agent host 
-                        port: port accept: "*/*"
-			proxy-host: proxy-host proxy-port: proxy-port)))
+    (let next-req ((req (make-HTTP-GET/1.1 locn user-agent host
+                         port: port accept: "*/*"
+			 proxy-host: proxy-host proxy-port: proxy-port)))
+      (d "requesting ~s ...~%" locn)
       (display req out)
       (flush-output out)
       (d "reading response ...~%")
@@ -79,32 +79,31 @@
              (response-match (match-http-response h1)))
         (d "~a~%" h1)
         ;;XXX handle redirects here
-        (if (response-match-code? response-match 407)
-            (let-values (((inpx outpx) (tcp-connect proxy-host proxy-port)))
-	      (set! in inpx) (set! out outpx)
-	      (display
-	        (make-HTTP-GET/1.1 
-		  locn user-agent host port: port 
-                  accept: "*/*"
-		  proxy-host: proxy-host proxy-port: proxy-port 
-		  proxy-user-pass: proxy-user-pass)
-		out)
-	      (unless (response-match-code? response-match 200)
-		(network-failure "invalid response from server" h1)))
-	    (let loop ()
-    	      (let ((ln (read-line in)))
-	        (unless (equal? ln "")
-		  (cond ((match-chunked-transfer-encoding ln)
-                         (set! chunked #t))
-                        ((match-content-length ln) =>
-                         (lambda (sz) (set! datalen sz))))
-		  (d "~a~%" ln)
-		  (loop) ) ) ) )
-	(when chunked
-	  (d "reading chunks ")
-	  (let ((data (read-chunks in)))
-	    (close-input-port in)
-	    (set! in (open-input-string data))) )
+        (cond
+	 ((response-match-code? response-match 407)
+          (let-values (((inpx outpx) (tcp-connect proxy-host proxy-port)))
+	    (set! in inpx) (set! out outpx)
+	    (next-req (make-HTTP-GET/1.1
+		       locn user-agent host port: port
+		       accept: "*/*"
+		       proxy-host: proxy-host proxy-port: proxy-port
+		       proxy-user-pass: proxy-user-pass))))
+	 ((response-match-code? response-match 200)
+	  (let loop ()
+    	    (let ((ln (read-line in)))
+	      (unless (equal? ln "")
+		(cond ((match-chunked-transfer-encoding ln)
+                       (set! chunked #t))
+                      ((match-content-length ln) =>
+                       (lambda (sz) (set! datalen sz))))
+		(d "~a~%" ln)
+		(loop) ) ) )
+	  (when chunked
+	    (d "reading chunks ")
+	    (let ((data (read-chunks in)))
+	      (close-input-port in)
+	      (set! in (open-input-string data))) ))
+	 (else (network-failure "invalid response from server" h1)))
         (values in out datalen)))))
 
 (define (http-retrieve-files in out dest)
Trap