download: Export '%x509-certificate-directory'.
[jackhill/guix/guix.git] / guix / build / download.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2015 Steve Sprang <scs@stevesprang.com>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (guix build download)
22 #:use-module (web uri)
23 #:use-module (web http)
24 #:use-module ((web client) #:hide (open-socket-for-uri))
25 #:use-module (web response)
26 #:use-module (guix base64)
27 #:use-module (guix ftp-client)
28 #:use-module (guix build utils)
29 #:use-module (rnrs io ports)
30 #:use-module (rnrs bytevectors)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-11)
33 #:use-module (srfi srfi-19)
34 #:use-module (srfi srfi-26)
35 #:autoload (ice-9 ftw) (scandir)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 format)
38 #:export (open-socket-for-uri
39 open-connection-for-uri
40 %x509-certificate-directory
41 close-connection
42 resolve-uri-reference
43 maybe-expand-mirrors
44 url-fetch
45 byte-count->string
46 current-terminal-columns
47 progress-proc
48 uri-abbreviation
49 nar-uri-abbreviation
50 store-path-abbreviation))
51
52 ;;; Commentary:
53 ;;;
54 ;;; Fetch data such as tarballs over HTTP or FTP (builder-side code).
55 ;;;
56 ;;; Code:
57
58 (define %http-receive-buffer-size
59 ;; Size of the HTTP receive buffer.
60 65536)
61
62 (define current-terminal-columns
63 ;; Number of columns of the terminal.
64 (make-parameter 80))
65
66 (define (nearest-exact-integer x)
67 "Given a real number X, return the nearest exact integer, with ties going to
68 the nearest exact even integer."
69 (inexact->exact (round x)))
70
71 (define (duration->seconds duration)
72 "Return the number of seconds represented by DURATION, a 'time-duration'
73 object, as an inexact number."
74 (+ (time-second duration)
75 (/ (time-nanosecond duration) 1e9)))
76
77 (define (seconds->string duration)
78 "Given DURATION in seconds, return a string representing it in 'mm:ss' or
79 'hh:mm:ss' format, as needed."
80 (if (not (number? duration))
81 "00:00"
82 (let* ((total-seconds (nearest-exact-integer duration))
83 (extra-seconds (modulo total-seconds 3600))
84 (num-hours (quotient total-seconds 3600))
85 (hours (and (positive? num-hours) num-hours))
86 (mins (quotient extra-seconds 60))
87 (secs (modulo extra-seconds 60)))
88 (format #f "~@[~2,'0d:~]~2,'0d:~2,'0d" hours mins secs))))
89
90 (define (byte-count->string size)
91 "Given SIZE in bytes, return a string representing it in a human-readable
92 way."
93 (let ((KiB 1024.)
94 (MiB (expt 1024. 2))
95 (GiB (expt 1024. 3))
96 (TiB (expt 1024. 4)))
97 (cond
98 ((< size KiB) (format #f "~dB" (nearest-exact-integer size)))
99 ((< size MiB) (format #f "~dKiB" (nearest-exact-integer (/ size KiB))))
100 ((< size GiB) (format #f "~,1fMiB" (/ size MiB)))
101 ((< size TiB) (format #f "~,2fGiB" (/ size GiB)))
102 (else (format #f "~,3fTiB" (/ size TiB))))))
103
104 (define* (progress-bar % #:optional (bar-width 20))
105 "Return % as a string representing an ASCII-art progress bar. The total
106 width of the bar is BAR-WIDTH."
107 (let* ((fraction (/ % 100))
108 (filled (inexact->exact (floor (* fraction bar-width))))
109 (empty (- bar-width filled)))
110 (format #f "[~a~a]"
111 (make-string filled #\#)
112 (make-string empty #\space))))
113
114 (define (string-pad-middle left right len)
115 "Combine LEFT and RIGHT with enough padding in the middle so that the
116 resulting string has length at least LEN (it may overflow). If the string
117 does not overflow, the last char in RIGHT will be flush with the LEN
118 column."
119 (let* ((total-used (+ (string-length left)
120 (string-length right)))
121 (num-spaces (max 1 (- len total-used)))
122 (padding (make-string num-spaces #\space)))
123 (string-append left padding right)))
124
125 (define* (ellipsis #:optional (port (current-output-port)))
126 "Make a rough guess at whether Unicode's HORIZONTAL ELLIPSIS can be written
127 in PORT's encoding, and return either that or ASCII dots."
128 (if (equal? (port-encoding port) "UTF-8")
129 "…"
130 "..."))
131
132 (define* (store-path-abbreviation store-path #:optional (prefix-length 6))
133 "If STORE-PATH is the file name of a store entry, return an abbreviation of
134 STORE-PATH for display, showing PREFIX-LENGTH characters of the hash.
135 Otherwise return STORE-PATH."
136 (if (string-prefix? (%store-directory) store-path)
137 (let ((base (basename store-path)))
138 (string-append (string-take base prefix-length)
139 (ellipsis)
140 (string-drop base 32)))
141 store-path))
142
143 (define* (progress-proc file size
144 #:optional (log-port (current-output-port))
145 #:key (abbreviation basename))
146 "Return a procedure to show the progress of FILE's download, which is SIZE
147 bytes long. The returned procedure is suitable for use as an argument to
148 `dump-port'. The progress report is written to LOG-PORT, with ABBREVIATION
149 used to shorten FILE for display."
150 ;; XXX: Because of <http://bugs.gnu.org/19939> this procedure is often not
151 ;; called as frequently as we'd like too; this is especially bad with Nginx
152 ;; on hydra.gnu.org, which returns whole nars as a single chunk.
153 (let ((start-time #f))
154 (let-syntax ((with-elapsed-time
155 (syntax-rules ()
156 ((_ elapsed body ...)
157 (let* ((now (current-time time-monotonic))
158 (elapsed (and start-time
159 (duration->seconds
160 (time-difference now
161 start-time)))))
162 (unless start-time
163 (set! start-time now))
164 body ...)))))
165 (if (number? size)
166 (lambda (transferred cont)
167 (with-elapsed-time elapsed
168 (let* ((% (* 100.0 (/ transferred size)))
169 (throughput (if elapsed
170 (/ transferred elapsed)
171 0))
172 (left (format #f " ~a ~a"
173 (abbreviation file)
174 (byte-count->string size)))
175 (right (format #f "~a/s ~a ~a~6,1f%"
176 (byte-count->string throughput)
177 (seconds->string elapsed)
178 (progress-bar %) %)))
179 (display "\r\x1b[K" log-port)
180 (display (string-pad-middle left right
181 (current-terminal-columns))
182 log-port)
183 (flush-output-port log-port)
184 (cont))))
185 (lambda (transferred cont)
186 (with-elapsed-time elapsed
187 (let* ((throughput (if elapsed
188 (/ transferred elapsed)
189 0))
190 (left (format #f " ~a"
191 (abbreviation file)))
192 (right (format #f "~a/s ~a | ~a transferred"
193 (byte-count->string throughput)
194 (seconds->string elapsed)
195 (byte-count->string transferred))))
196 (display "\r\x1b[K" log-port)
197 (display (string-pad-middle left right
198 (current-terminal-columns))
199 log-port)
200 (flush-output-port log-port)
201 (cont))))))))
202
203 (define* (uri-abbreviation uri #:optional (max-length 42))
204 "If URI's string representation is larger than MAX-LENGTH, return an
205 abbreviation of URI showing the scheme, host, and basename of the file."
206 (define uri-as-string
207 (uri->string uri))
208
209 (define (elide-path)
210 (let* ((path (uri-path uri))
211 (base (basename path))
212 (prefix (string-append (symbol->string (uri-scheme uri)) "://"
213
214 ;; `file' URIs have no host part.
215 (or (uri-host uri) "")
216
217 (string-append "/" (ellipsis) "/"))))
218 (if (> (+ (string-length prefix) (string-length base)) max-length)
219 (string-append prefix (ellipsis)
220 (string-drop base (quotient (string-length base) 2)))
221 (string-append prefix base))))
222
223 (if (> (string-length uri-as-string) max-length)
224 (let ((short (elide-path)))
225 (if (< (string-length short) (string-length uri-as-string))
226 short
227 uri-as-string))
228 uri-as-string))
229
230 (define (nar-uri-abbreviation uri)
231 "Abbreviate URI, which is assumed to be the URI of a nar as served by Hydra
232 and 'guix publish', something like
233 \"http://example.org/nar/1ldrllwbna0aw5z8kpci4fsvbd2w8cw4-texlive-bin-2015\"."
234 (let* ((uri (if (string? uri) (string->uri uri) uri))
235 (path (basename (uri-path uri))))
236 (if (and (> (string-length path) 33)
237 (char=? (string-ref path 32) #\-))
238 (string-drop path 33)
239 path)))
240
241 (define* (ftp-fetch uri file #:key timeout)
242 "Fetch data from URI and write it to FILE. Return FILE on success. Bail
243 out if the connection could not be established in less than TIMEOUT seconds."
244 (let* ((conn (ftp-open (uri-host uri) #:timeout timeout))
245 (size (false-if-exception (ftp-size conn (uri-path uri))))
246 (in (ftp-retr conn (basename (uri-path uri))
247 (dirname (uri-path uri)))))
248 (call-with-output-file file
249 (lambda (out)
250 (dump-port in out
251 #:buffer-size %http-receive-buffer-size
252 #:progress (progress-proc (uri-abbreviation uri) size))))
253
254 (ftp-close conn))
255 (newline)
256 file)
257
258 ;; Autoload GnuTLS so that this module can be used even when GnuTLS is
259 ;; not available. At compile time, this yields "possibly unbound
260 ;; variable" warnings, but these are OK: we know that the variables will
261 ;; be bound if we need them, because (guix download) adds GnuTLS as an
262 ;; input in that case.
263
264 ;; XXX: Use this hack instead of #:autoload to avoid compilation errors.
265 ;; See <http://bugs.gnu.org/12202>.
266 (module-autoload! (current-module)
267 '(gnutls) '(make-session connection-end/client))
268
269 (define %tls-ports
270 ;; Mapping of session record ports to the underlying file port.
271 (make-weak-key-hash-table))
272
273 (define (register-tls-record-port record-port port)
274 "Hold a weak reference from RECORD-PORT to PORT, where RECORD-PORT is a TLS
275 session record port using PORT as its underlying communication port."
276 (hashq-set! %tls-ports record-port port))
277
278 (define %x509-certificate-directory
279 ;; The directory where X.509 authority PEM certificates are stored.
280 (make-parameter (or (getenv "GUIX_TLS_CERTIFICATE_DIRECTORY")
281 (getenv "SSL_CERT_DIR")))) ;like OpenSSL
282
283 (define (make-credendials-with-ca-trust-files directory)
284 "Return certificate credentials with X.509 authority certificates read from
285 DIRECTORY. Those authority certificates are checked when
286 'peer-certificate-status' is later called."
287 (let ((cred (make-certificate-credentials))
288 (files (or (scandir directory
289 (lambda (file)
290 (string-suffix? ".pem" file)))
291 '())))
292 (for-each (lambda (file)
293 (let ((file (string-append directory "/" file)))
294 ;; Protect against dangling symlinks.
295 (when (file-exists? file)
296 (set-certificate-credentials-x509-trust-file!
297 cred file
298 x509-certificate-format/pem))))
299 (or files '()))
300 cred))
301
302 (define (peer-certificate session)
303 "Return the certificate of the remote peer in SESSION."
304 (match (session-peer-certificate-chain session)
305 ((first _ ...)
306 (import-x509-certificate first x509-certificate-format/der))))
307
308 (define (assert-valid-server-certificate session server)
309 "Return #t if the certificate of the remote peer for SESSION is a valid
310 certificate for SERVER, where SERVER is the expected host name of peer."
311 (define cert
312 (peer-certificate session))
313
314 ;; First check whether the server's certificate matches SERVER.
315 (unless (x509-certificate-matches-hostname? cert server)
316 (throw 'tls-certificate-error 'host-mismatch cert server))
317
318 ;; Second check its validity and reachability from the set of authority
319 ;; certificates loaded via 'set-certificate-credentials-x509-trust-file!'.
320 (match (peer-certificate-status session)
321 (() ;certificate is valid
322 #t)
323 ((statuses ...)
324 (throw 'tls-certificate-error 'invalid-certificate cert server
325 statuses))))
326
327 (define (print-tls-certificate-error port key args default-printer)
328 "Print the TLS certificate error represented by ARGS in an intelligible
329 way."
330 (match args
331 (('host-mismatch cert server)
332 (format port
333 "X.509 server certificate for '~a' does not match: ~a~%"
334 server (x509-certificate-dn cert)))
335 (('invalid-certificate cert server statuses)
336 (format port
337 "X.509 certificate of '~a' could not be verified:~%~{ ~a~%~}"
338 server
339 (map certificate-status->string statuses)))))
340
341 (set-exception-printer! 'tls-certificate-error
342 print-tls-certificate-error)
343
344 (define* (tls-wrap port server #:key (verify-certificate? #t))
345 "Return PORT wrapped in a TLS connection to SERVER. SERVER must be a DNS
346 host name without trailing dot."
347 (define (log level str)
348 (format (current-error-port)
349 "gnutls: [~a|~a] ~a" (getpid) level str))
350
351 (let ((session (make-session connection-end/client))
352 (ca-certs (%x509-certificate-directory)))
353
354 ;; Some servers such as 'cloud.github.com' require the client to support
355 ;; the 'SERVER NAME' extension. However, 'set-session-server-name!' is
356 ;; not available in older GnuTLS releases. See
357 ;; <http://bugs.gnu.org/18526> for details.
358 (if (module-defined? (resolve-interface '(gnutls))
359 'set-session-server-name!)
360 (set-session-server-name! session server-name-type/dns server)
361 (format (current-error-port)
362 "warning: TLS 'SERVER NAME' extension not supported~%"))
363
364 (set-session-transport-fd! session (fileno port))
365 (set-session-default-priority! session)
366
367 ;; The "%COMPAT" bit allows us to work around firewall issues (info
368 ;; "(gnutls) Priority Strings"); see <http://bugs.gnu.org/23311>.
369 ;; Explicitly disable SSLv3, which is insecure:
370 ;; <https://tools.ietf.org/html/rfc7568>.
371 (set-session-priorities! session "NORMAL:%COMPAT:-VERS-SSL3.0")
372
373 (set-session-credentials! session
374 (if (and verify-certificate? ca-certs)
375 (make-credendials-with-ca-trust-files
376 ca-certs)
377 (make-certificate-credentials)))
378
379 ;; Uncomment the following lines in case of debugging emergency.
380 ;;(set-log-level! 10)
381 ;;(set-log-procedure! log)
382
383 (handshake session)
384
385 ;; Verify the server's certificate if needed.
386 (when verify-certificate?
387 (catch 'tls-certificate-error
388 (lambda ()
389 (assert-valid-server-certificate session server))
390 (lambda args
391 (close-port port)
392 (apply throw args))))
393
394 (let ((record (session-record-port session)))
395 ;; Since we use `fileno' above, the file descriptor behind PORT would be
396 ;; closed when PORT is GC'd. If we used `port->fdes', it would instead
397 ;; never be closed. So we use `fileno', but keep a weak reference to
398 ;; PORT, so the file descriptor gets closed when RECORD is GC'd.
399 (register-tls-record-port record port)
400 record)))
401
402 (define (ensure-uri uri-or-string) ;XXX: copied from (web http)
403 (cond
404 ((string? uri-or-string) (string->uri uri-or-string))
405 ((uri? uri-or-string) uri-or-string)
406 (else (error "Invalid URI" uri-or-string))))
407
408 (define current-http-proxy
409 ;; XXX: Add a dummy definition for Guile < 2.0.10; this is used in
410 ;; 'open-socket-for-uri'.
411 (or (and=> (module-variable (resolve-interface '(web client))
412 'current-http-proxy)
413 variable-ref)
414 (const #f)))
415
416 (define* (open-socket-for-uri uri-or-string #:key timeout)
417 "Return an open input/output port for a connection to URI. When TIMEOUT is
418 not #f, it must be a (possibly inexact) number denoting the maximum duration
419 in seconds to wait for the connection to complete; passed TIMEOUT, an
420 ETIMEDOUT error is raised."
421 ;; Includes a fix for <http://bugs.gnu.org/15368> which affects Guile's
422 ;; 'open-socket-for-uri' up to 2.0.11 included, uses 'connect*' instead
423 ;; of 'connect', and uses AI_ADDRCONFIG.
424
425 (define http-proxy (current-http-proxy))
426 (define uri (ensure-uri (or http-proxy uri-or-string)))
427 (define addresses
428 (let ((port (uri-port uri)))
429 (delete-duplicates
430 (getaddrinfo (uri-host uri)
431 (cond (port => number->string)
432 (else (symbol->string (uri-scheme uri))))
433 (if (number? port)
434 (logior AI_ADDRCONFIG AI_NUMERICSERV)
435 AI_ADDRCONFIG))
436 (lambda (ai1 ai2)
437 (equal? (addrinfo:addr ai1) (addrinfo:addr ai2))))))
438
439 (let loop ((addresses addresses))
440 (let* ((ai (car addresses))
441 (s (with-fluids ((%default-port-encoding #f))
442 ;; Restrict ourselves to TCP.
443 (socket (addrinfo:fam ai) SOCK_STREAM IPPROTO_IP))))
444 (catch 'system-error
445 (lambda ()
446 (connect* s (addrinfo:addr ai) timeout)
447
448 ;; Buffer input and output on this port.
449 (setvbuf s _IOFBF)
450 ;; If we're using a proxy, make a note of that.
451 (when http-proxy (set-http-proxy-port?! s #t))
452 s)
453 (lambda args
454 ;; Connection failed, so try one of the other addresses.
455 (close s)
456 (if (null? (cdr addresses))
457 (apply throw args)
458 (loop (cdr addresses))))))))
459
460 (define* (open-connection-for-uri uri
461 #:key
462 timeout
463 (verify-certificate? #t))
464 "Like 'open-socket-for-uri', but also handle HTTPS connections. The
465 resulting port must be closed with 'close-connection'. When
466 VERIFY-CERTIFICATE? is true, verify HTTPS server certificates."
467 (define https?
468 (eq? 'https (uri-scheme uri)))
469
470 (let-syntax ((with-https-proxy
471 (syntax-rules ()
472 ((_ exp)
473 ;; For HTTPS URIs, honor 'https_proxy', not 'http_proxy'.
474 ;; FIXME: Proxying is not supported for https.
475 (let ((thunk (lambda () exp)))
476 (if (and https?
477 (module-variable
478 (resolve-interface '(web client))
479 'current-http-proxy))
480 (parameterize ((current-http-proxy #f))
481 (when (and=> (getenv "https_proxy")
482 (negate string-null?))
483 (format (current-error-port)
484 "warning: 'https_proxy' is ignored~%"))
485 (thunk))
486 (thunk)))))))
487 (with-https-proxy
488 (let ((s (open-socket-for-uri uri #:timeout timeout)))
489 ;; Buffer input and output on this port.
490 (setvbuf s _IOFBF %http-receive-buffer-size)
491
492 (if https?
493 (tls-wrap s (uri-host uri)
494 #:verify-certificate? verify-certificate?)
495 s)))))
496
497 (define (close-connection port)
498 "Like 'close-port', but (1) idempotent, and (2) also closes the underlying
499 port if PORT is a TLS session record port."
500 ;; FIXME: This is a partial workaround for <http://bugs.gnu.org/20145>,
501 ;; because 'http-fetch' & co. may return a chunked input port whose 'close'
502 ;; method calls 'close-port', not 'close-connection'.
503 (unless (port-closed? port)
504 (close-port port))
505 (and=> (hashq-ref %tls-ports port)
506 close-connection))
507
508 ;; XXX: This is an awful hack to make sure the (set-port-encoding! p
509 ;; "ISO-8859-1") call in `read-response' passes, even during bootstrap
510 ;; where iconv is not available.
511 (module-define! (resolve-module '(web response))
512 'set-port-encoding!
513 (lambda (p e) #f))
514
515 ;; XXX: Work around <http://bugs.gnu.org/13095>, present in Guile
516 ;; up to 2.0.7.
517 (module-define! (resolve-module '(web client))
518 'shutdown (const #f))
519
520
521 ;; XXX: Work around <http://bugs.gnu.org/23421>, fixed in Guile commit
522 ;; 16050431f29d56f80c4a8253506fc851b8441840. Guile's date validation
523 ;; procedure rejects dates in which the hour is not padded with a zero but
524 ;; with whitespace.
525 (begin
526 (define-syntax string-match?
527 (lambda (x)
528 (syntax-case x ()
529 ((_ str pat) (string? (syntax->datum #'pat))
530 (let ((p (syntax->datum #'pat)))
531 #`(let ((s str))
532 (and
533 (= (string-length s) #,(string-length p))
534 #,@(let lp ((i 0) (tests '()))
535 (if (< i (string-length p))
536 (let ((c (string-ref p i)))
537 (lp (1+ i)
538 (case c
539 ((#\.) ; Whatever.
540 tests)
541 ((#\d) ; Digit.
542 (cons #`(char-numeric? (string-ref s #,i))
543 tests))
544 ((#\a) ; Alphabetic.
545 (cons #`(char-alphabetic? (string-ref s #,i))
546 tests))
547 (else ; Literal.
548 (cons #`(eqv? (string-ref s #,i) #,c)
549 tests)))))
550 tests)))))))))
551
552 (define (parse-rfc-822-date str space zone-offset)
553 (let ((parse-non-negative-integer (@@ (web http) parse-non-negative-integer))
554 (parse-month (@@ (web http) parse-month))
555 (bad-header (@@ (web http) bad-header)))
556 ;; We could verify the day of the week but we don't.
557 (cond ((string-match? (substring str 0 space) "aaa, dd aaa dddd dd:dd:dd")
558 (let ((date (parse-non-negative-integer str 5 7))
559 (month (parse-month str 8 11))
560 (year (parse-non-negative-integer str 12 16))
561 (hour (parse-non-negative-integer str 17 19))
562 (minute (parse-non-negative-integer str 20 22))
563 (second (parse-non-negative-integer str 23 25)))
564 (make-date 0 second minute hour date month year zone-offset)))
565 ((string-match? (substring str 0 space) "aaa, d aaa dddd dd:dd:dd")
566 (let ((date (parse-non-negative-integer str 5 6))
567 (month (parse-month str 7 10))
568 (year (parse-non-negative-integer str 11 15))
569 (hour (parse-non-negative-integer str 16 18))
570 (minute (parse-non-negative-integer str 19 21))
571 (second (parse-non-negative-integer str 22 24)))
572 (make-date 0 second minute hour date month year zone-offset)))
573
574 ;; The next two clauses match dates that have a space instead of
575 ;; a leading zero for hours, like " 8:49:37".
576 ((string-match? (substring str 0 space) "aaa, dd aaa dddd d:dd:dd")
577 (let ((date (parse-non-negative-integer str 5 7))
578 (month (parse-month str 8 11))
579 (year (parse-non-negative-integer str 12 16))
580 (hour (parse-non-negative-integer str 18 19))
581 (minute (parse-non-negative-integer str 20 22))
582 (second (parse-non-negative-integer str 23 25)))
583 (make-date 0 second minute hour date month year zone-offset)))
584 ((string-match? (substring str 0 space) "aaa, d aaa dddd d:dd:dd")
585 (let ((date (parse-non-negative-integer str 5 6))
586 (month (parse-month str 7 10))
587 (year (parse-non-negative-integer str 11 15))
588 (hour (parse-non-negative-integer str 17 18))
589 (minute (parse-non-negative-integer str 19 21))
590 (second (parse-non-negative-integer str 22 24)))
591 (make-date 0 second minute hour date month year zone-offset)))
592
593 (else
594 (bad-header 'date str) ; prevent tail call
595 #f))))
596 (module-set! (resolve-module '(web http))
597 'parse-rfc-822-date parse-rfc-822-date))
598
599 ;; XXX: Work around <http://bugs.gnu.org/19840>, present in Guile
600 ;; up to 2.0.11.
601 (unless (or (> (string->number (major-version)) 2)
602 (> (string->number (minor-version)) 0)
603 (> (string->number (micro-version)) 11))
604 (let ((var (module-variable (resolve-module '(web http))
605 'declare-relative-uri-header!)))
606 ;; If 'declare-relative-uri-header!' doesn't exist, forget it.
607 (when (and var (variable-bound? var))
608 (let ((declare-relative-uri-header! (variable-ref var)))
609 (declare-relative-uri-header! "Location")))))
610
611 (define (resolve-uri-reference ref base)
612 "Resolve the URI reference REF, interpreted relative to the BASE URI, into a
613 target URI, according to the algorithm specified in RFC 3986 section 5.2.2.
614 Return the resulting target URI."
615
616 (define (merge-paths base-path rel-path)
617 (let* ((base-components (string-split base-path #\/))
618 (base-directory-components (match base-components
619 ((components ... last) components)
620 (() '())))
621 (base-directory (string-join base-directory-components "/")))
622 (string-append base-directory "/" rel-path)))
623
624 (define (remove-dot-segments path)
625 (let loop ((in
626 ;; Drop leading "." and ".." components from a relative path.
627 ;; (absolute paths will start with a "" component)
628 (drop-while (match-lambda
629 ((or "." "..") #t)
630 (_ #f))
631 (string-split path #\/)))
632 (out '()))
633 (match in
634 (("." . rest)
635 (loop rest out))
636 ((".." . rest)
637 (match out
638 ((or () (""))
639 (error "remove-dot-segments: too many '..' components" path))
640 (_
641 (loop rest (cdr out)))))
642 ((component . rest)
643 (loop rest (cons component out)))
644 (()
645 (string-join (reverse out) "/")))))
646
647 (cond ((or (uri-scheme ref)
648 (uri-host ref))
649 (build-uri (or (uri-scheme ref)
650 (uri-scheme base))
651 #:userinfo (uri-userinfo ref)
652 #:host (uri-host ref)
653 #:port (uri-port ref)
654 #:path (remove-dot-segments (uri-path ref))
655 #:query (uri-query ref)
656 #:fragment (uri-fragment ref)))
657 ((string-null? (uri-path ref))
658 (build-uri (uri-scheme base)
659 #:userinfo (uri-userinfo base)
660 #:host (uri-host base)
661 #:port (uri-port base)
662 #:path (remove-dot-segments (uri-path base))
663 #:query (or (uri-query ref)
664 (uri-query base))
665 #:fragment (uri-fragment ref)))
666 (else
667 (build-uri (uri-scheme base)
668 #:userinfo (uri-userinfo base)
669 #:host (uri-host base)
670 #:port (uri-port base)
671 #:path (remove-dot-segments
672 (if (string-prefix? "/" (uri-path ref))
673 (uri-path ref)
674 (merge-paths (uri-path base)
675 (uri-path ref))))
676 #:query (uri-query ref)
677 #:fragment (uri-fragment ref)))))
678
679 (define* (http-fetch uri file #:key timeout (verify-certificate? #t))
680 "Fetch data from URI and write it to FILE; when TIMEOUT is true, bail out if
681 the connection could not be established in less than TIMEOUT seconds. Return
682 FILE on success. When VERIFY-CERTIFICATE? is true, verify HTTPS
683 certificates; otherwise simply ignore them."
684
685 (define post-2.0.7?
686 (or (> (string->number (major-version)) 2)
687 (> (string->number (minor-version)) 0)
688 (> (string->number (micro-version)) 7)
689 (string>? (version) "2.0.7")))
690
691 (define headers
692 `(;; Some web sites, such as http://dist.schmorp.de, would block you if
693 ;; there's no 'User-Agent' header, presumably on the assumption that
694 ;; you're a spammer. So work around that.
695 (User-Agent . "GNU Guile")
696
697 ;; Some servers, such as https://alioth.debian.org, return "406 Not
698 ;; Acceptable" when not explicitly told that everything is accepted.
699 (Accept . "*/*")
700
701 ;; Basic authentication, if needed.
702 ,@(match (uri-userinfo uri)
703 ((? string? str)
704 `((Authorization . ,(string-append "Basic "
705 (base64-encode
706 (string->utf8 str))))))
707 (_ '()))))
708
709 (let*-values (((connection)
710 (open-connection-for-uri uri
711 #:timeout timeout
712 #:verify-certificate?
713 verify-certificate?))
714 ((resp bv-or-port)
715 ;; XXX: `http-get*' was introduced in 2.0.7, and replaced by
716 ;; #:streaming? in 2.0.8. We know we're using it within the
717 ;; chroot, but `guix-download' might be using a different
718 ;; version. So keep this compatibility hack for now.
719 (if post-2.0.7?
720 (http-get uri #:port connection #:decode-body? #f
721 #:streaming? #t
722 #:headers headers)
723 (if (module-defined? (resolve-interface '(web client))
724 'http-get*)
725 (http-get* uri #:port connection #:decode-body? #f
726 #:headers headers)
727 (http-get uri #:port connection #:decode-body? #f
728 #:extra-headers headers))))
729 ((code)
730 (response-code resp))
731 ((size)
732 (response-content-length resp)))
733 (case code
734 ((200) ; OK
735 (begin
736 (call-with-output-file file
737 (lambda (p)
738 (if (port? bv-or-port)
739 (begin
740 (dump-port bv-or-port p
741 #:buffer-size %http-receive-buffer-size
742 #:progress (progress-proc (uri-abbreviation uri)
743 size))
744 (newline))
745 (put-bytevector p bv-or-port))))
746 file))
747 ((301 ; moved permanently
748 302 ; found (redirection)
749 307) ; temporary redirection
750 (let ((uri (resolve-uri-reference (response-location resp) uri)))
751 (format #t "following redirection to `~a'...~%"
752 (uri->string uri))
753 (close connection)
754 (http-fetch uri file
755 #:timeout timeout
756 #:verify-certificate? verify-certificate?)))
757 (else
758 (error "download failed" (uri->string uri)
759 code (response-reason-phrase resp))))))
760
761 \f
762 (define-syntax-rule (false-if-exception* body ...)
763 "Like `false-if-exception', but print the exception on the error port."
764 (catch #t
765 (lambda ()
766 body ...)
767 (lambda (key . args)
768 #f)
769 (lambda (key . args)
770 (print-exception (current-error-port) #f key args))))
771
772 (define (uri-vicinity dir file)
773 "Concatenate DIR, slash, and FILE, keeping only one slash in between.
774 This is required by some HTTP servers."
775 (string-append (string-trim-right dir #\/) "/"
776 (string-trim file #\/)))
777
778 (define (maybe-expand-mirrors uri mirrors)
779 "If URI uses the 'mirror' scheme, expand it according to the MIRRORS alist.
780 Return a list of URIs."
781 (case (uri-scheme uri)
782 ((mirror)
783 (let ((kind (string->symbol (uri-host uri)))
784 (path (uri-path uri)))
785 (match (assoc-ref mirrors kind)
786 ((mirrors ..1)
787 (map (compose string->uri (cut uri-vicinity <> path))
788 mirrors))
789 (_
790 (error "unsupported URL mirror kind" kind uri)))))
791 (else
792 (list uri))))
793
794 (define* (url-fetch url file
795 #:key
796 (timeout 10) (verify-certificate? #t)
797 (mirrors '()) (content-addressed-mirrors '())
798 (hashes '()))
799 "Fetch FILE from URL; URL may be either a single string, or a list of
800 string denoting alternate URLs for FILE. Return #f on failure, and FILE
801 on success.
802
803 When MIRRORS is defined, it must be an alist of mirrors; it is used to resolve
804 'mirror://' URIs.
805
806 HASHES must be a list of algorithm/hash pairs, where each algorithm is a
807 symbol such as 'sha256 and each hash is a bytevector.
808 CONTENT-ADDRESSED-MIRRORS must be a list of procedures that, given a hash
809 algorithm and a hash, return a URL where the specified data can be retrieved
810 or #f.
811
812 When VERIFY-CERTIFICATE? is true, validate HTTPS server certificates;
813 otherwise simply ignore them."
814 (define uri
815 (append-map (cut maybe-expand-mirrors <> mirrors)
816 (match url
817 ((_ ...) (map string->uri url))
818 (_ (list (string->uri url))))))
819
820 (define (fetch uri file)
821 (format #t "~%Starting download of ~a~%From ~a...~%"
822 file (uri->string uri))
823 (case (uri-scheme uri)
824 ((http https)
825 (false-if-exception* (http-fetch uri file
826 #:verify-certificate?
827 verify-certificate?
828 #:timeout timeout)))
829 ((ftp)
830 (false-if-exception* (ftp-fetch uri file
831 #:timeout timeout)))
832 (else
833 (format #t "skipping URI with unsupported scheme: ~s~%"
834 uri)
835 #f)))
836
837 (define content-addressed-uris
838 (append-map (lambda (make-url)
839 (filter-map (match-lambda
840 ((hash-algo . hash)
841 (let ((file (strip-store-file-name file)))
842 (string->uri (make-url file hash-algo hash)))))
843 hashes))
844 content-addressed-mirrors))
845
846 ;; Make this unbuffered so 'progress-proc' works as expected. _IOLBF means
847 ;; '\n', not '\r', so it's not appropriate here.
848 (setvbuf (current-output-port) _IONBF)
849
850 (setvbuf (current-error-port) _IOLBF)
851
852 (let try ((uri (append uri content-addressed-uris)))
853 (match uri
854 ((uri tail ...)
855 (or (fetch uri file)
856 (try tail)))
857 (()
858 (format (current-error-port) "failed to download ~s from ~s~%"
859 file url)
860 #f))))
861
862 ;;; Local Variables:
863 ;;; eval: (put 'with-elapsed-time 'scheme-indent-function 1)
864 ;;; End:
865
866 ;;; download.scm ends here