aboutsummaryrefslogtreecommitdiff
path: root/guix/build/http.scm
diff options
context:
space:
mode:
Diffstat (limited to 'guix/build/http.scm')
-rw-r--r--guix/build/http.scm52
1 files changed, 50 insertions, 2 deletions
diff --git a/guix/build/http.scm b/guix/build/http.scm
index f6f797f9cf..a3f9f9a870 100644
--- a/guix/build/http.scm
+++ b/guix/build/http.scm
@@ -29,13 +29,61 @@
;;;
;;; Code:
+(define (open-connection-for-uri uri)
+ "Return an open input/output port for a connection to URI.
+
+This is the same as Guile's `open-socket-for-uri', except that we always
+use a numeric port argument, to avoid the need to go through libc's NSS,
+which is not available during bootstrap."
+ (define addresses
+ (let ((port (or (uri-port uri)
+ (case (uri-scheme uri)
+ ((http) 80) ; /etc/services, not for me!
+ (else
+ (error "unsupported URI scheme" uri))))))
+ (getaddrinfo (uri-host uri)
+ (number->string port)
+ AI_NUMERICSERV)))
+
+ (let loop ((addresses addresses))
+ (let* ((ai (car addresses))
+ (s (with-fluids ((%default-port-encoding #f))
+ (socket (addrinfo:fam ai) (addrinfo:socktype ai)
+ (addrinfo:protocol ai)))))
+ (catch 'system-error
+ (lambda ()
+ (connect s (addrinfo:addr ai))
+
+ ;; Buffer input and output on this port.
+ (setvbuf s _IOFBF)
+ ;; Enlarge the receive buffer.
+ (setsockopt s SOL_SOCKET SO_RCVBUF (* 12 1024))
+ s)
+ (lambda args
+ ;; Connection failed, so try one of the other addresses.
+ (close s)
+ (if (null? addresses)
+ (apply throw args)
+ (loop (cdr addresses))))))))
+
+;; XXX: This is an awful hack to make sure the (set-port-encoding! p
+;; "ISO-8859-1") call in `read-response' passes, even during bootstrap
+;; where iconv is not available.
+(module-define! (resolve-module '(web response))
+ 'set-port-encoding!
+ (lambda (p e) #f))
+
(define (http-fetch url file)
"Fetch data from URL and write it to FILE. Return FILE on success."
;; FIXME: Use a variant of `http-get' that returns a port instead of
;; loading everything in memory.
- (let-values (((resp bv)
- (http-get (string->uri url) #:decode-body? #f)))
+ (let*-values (((uri)
+ (string->uri url))
+ ((connection)
+ (open-connection-for-uri uri))
+ ((resp bv)
+ (http-get uri #:port connection #:decode-body? #f)))
(call-with-output-file file
(lambda (p)
(put-bytevector p bv))))