deploy: Add '--verbosity' and properly interpret build log.
[jackhill/guix/guix.git] / guix / http-client.scm
CommitLineData
1c9e7d65 1;;; GNU Guix --- Functional package management for GNU
1d84d7bf 2;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
04dec194 3;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
c28606bd 4;;; Copyright © 2012, 2015 Free Software Foundation, Inc.
57d28987 5;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
1c9e7d65
LC
6;;;
7;;; This file is part of GNU Guix.
8;;;
9;;; GNU Guix is free software; you can redistribute it and/or modify it
10;;; under the terms of the GNU General Public License as published by
11;;; the Free Software Foundation; either version 3 of the License, or (at
12;;; your option) any later version.
13;;;
14;;; GNU Guix is distributed in the hope that it will be useful, but
15;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;;; GNU General Public License for more details.
18;;;
19;;; You should have received a copy of the GNU General Public License
20;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
3b8258c5 22(define-module (guix http-client)
1c9e7d65 23 #:use-module (web uri)
76238483 24 #:use-module ((web client) #:hide (open-socket-for-uri))
1c9e7d65
LC
25 #:use-module (web response)
26 #:use-module (srfi srfi-11)
739ab68b
LC
27 #:use-module (srfi srfi-19)
28 #:use-module (srfi srfi-26)
706e9e57
LC
29 #:use-module (srfi srfi-34)
30 #:use-module (srfi srfi-35)
15d5ca13 31 #:use-module (ice-9 match)
2535635f 32 #:use-module (ice-9 binary-ports)
1c9e7d65
LC
33 #:use-module (rnrs bytevectors)
34 #:use-module (guix ui)
35 #:use-module (guix utils)
0cb5bc2c 36 #:use-module (guix base64)
ca719424 37 #:autoload (gcrypt hash) (sha256)
739ab68b
LC
38 #:use-module ((guix build utils)
39 #:select (mkdir-p dump-port))
76238483 40 #:use-module ((guix build download)
8a5063f7 41 #:select (open-socket-for-uri
4fd06a4d
LC
42 (open-connection-for-uri
43 . guix:open-connection-for-uri)
44 resolve-uri-reference))
76238483 45 #:re-export (open-socket-for-uri)
706e9e57
LC
46 #:export (&http-get-error
47 http-get-error?
48 http-get-error-uri
49 http-get-error-code
50 http-get-error-reason
51
739ab68b
LC
52 http-fetch
53
54 %http-cache-ttl
55 http-fetch/cached))
1c9e7d65
LC
56
57;;; Commentary:
58;;;
706e9e57
LC
59;;; HTTP client portable among Guile versions, and with proper error condition
60;;; reporting.
1c9e7d65
LC
61;;;
62;;; Code:
63
706e9e57
LC
64;; HTTP GET error.
65(define-condition-type &http-get-error &error
66 http-get-error?
67 (uri http-get-error-uri) ; URI
68 (code http-get-error-code) ; integer
69 (reason http-get-error-reason)) ; string
70
71
d262a0f3 72(define* (http-fetch uri #:key port (text? #f) (buffered? #t)
608a50b6
LC
73 keep-alive? (verify-certificate? #t)
74 (headers '((user-agent . "GNU Guile"))))
1c9e7d65
LC
75 "Return an input port containing the data at URI, and the expected number of
76bytes available or #f. If TEXT? is true, the data at URI is considered to be
101d9f3f 77textual. Follow any HTTP redirection. When BUFFERED? is #f, return an
d262a0f3
LC
78unbuffered port, suitable for use in `filtered-port'. When KEEP-ALIVE? is
79true, send a 'Connection: keep-alive' HTTP header, in which case PORT may be
608a50b6 80reused for future HTTP requests. HEADERS is an alist of extra HTTP headers.
706e9e57 81
17cff9c6
LC
82When VERIFY-CERTIFICATE? is true, verify HTTPS server certificates.
83
706e9e57 84Raise an '&http-get-error' condition if downloading fails."
25d188ce
LC
85 (let loop ((uri (if (string? uri)
86 (string->uri uri)
87 uri)))
4fd06a4d
LC
88 (let ((port (or port (guix:open-connection-for-uri uri
89 #:verify-certificate?
90 verify-certificate?)))
608a50b6
LC
91 (headers (match (uri-userinfo uri)
92 ((? string? str)
93 (cons (cons 'Authorization
94 (string-append "Basic "
95 (base64-encode
96 (string->utf8 str))))
97 headers))
98 (_ headers))))
409e4ac6 99 (unless (or buffered? (not (file-port? port)))
76832d34 100 (setvbuf port 'none))
bb7dcaea 101 (let*-values (((resp data)
36626c55
LC
102 (http-get uri #:streaming? #t #:port port
103 #:keep-alive? #t
104 #:headers headers))
bb7dcaea
LC
105 ((code)
106 (response-code resp)))
107 (case code
108 ((200)
005c8fc6 109 (values data (response-content-length resp)))
bb7dcaea 110 ((301 ; moved permanently
57d28987
TGR
111 302 ; found (redirection)
112 303 ; see other
113 307 ; temporary redirection
114 308) ; permanent redirection
04dec194 115 (let ((uri (resolve-uri-reference (response-location resp) uri)))
bb7dcaea 116 (close-port port)
9572d2b4 117 (format (current-error-port) (G_ "following redirection to `~a'...~%")
bb7dcaea
LC
118 (uri->string uri))
119 (loop uri)))
120 (else
706e9e57
LC
121 (raise (condition (&http-get-error
122 (uri uri)
123 (code code)
124 (reason (response-reason-phrase resp)))
125 (&message
dd1141eb
LC
126 (message
127 (format
128 #f
69daee23 129 (G_ "~a: HTTP download failed: ~a (~s)")
dd1141eb
LC
130 (uri->string uri) code
131 (response-reason-phrase resp))))))))))))
1c9e7d65 132
739ab68b
LC
133\f
134;;;
135;;; Caching.
136;;;
137
cbaf0f11 138(define %http-cache-ttl
739ab68b
LC
139 ;; Time-to-live in seconds of the HTTP cache of in ~/.cache/guix.
140 (make-parameter
141 (* 3600 (or (and=> (getenv "GUIX_HTTP_CACHE_TTL")
142 string->number*)
143 36))))
144
a4e7083d
LC
145(define (cache-file-for-uri uri)
146 "Return the name of the file in the cache corresponding to URI."
147 (let ((digest (sha256 (string->utf8 (uri->string uri)))))
148 ;; Use the "URL" alphabet because it does not contain "/".
149 (string-append (cache-directory) "/http/"
150 (base64-encode digest 0 (bytevector-length digest)
151 #f #f base64url-alphabet))))
152
7482b981
LC
153(define* (http-fetch/cached uri #:key (ttl (%http-cache-ttl)) text?
154 (write-cache dump-port)
155 (cache-miss (const #t)))
739ab68b 156 "Like 'http-fetch', return an input port, but cache its contents in
7482b981
LC
157~/.cache/guix. The cache remains valid for TTL seconds.
158
159Call WRITE-CACHE with the HTTP input port and the cache output port to write
160the data to cache. Call CACHE-MISS with URI just before fetching data from
161URI."
a4e7083d 162 (let ((file (cache-file-for-uri uri)))
3ce1b902
LC
163 (define (update-cache cache-port)
164 (define cache-time
165 (and cache-port
166 (stat:mtime (stat cache-port))))
167
168 (define headers
169 `((user-agent . "GNU Guile")
170 ,@(if cache-time
171 `((if-modified-since
172 . ,(time-utc->date (make-time time-utc 0 cache-time))))
173 '())))
174
739ab68b 175 ;; Update the cache and return an input port.
3ce1b902
LC
176 (guard (c ((http-get-error? c)
177 (if (= 304 (http-get-error-code c)) ;"Not Modified"
06acf6b5
LC
178 (begin
179 (utime file) ;update FILE's mtime
180 cache-port)
3ce1b902
LC
181 (raise c))))
182 (let ((port (http-fetch uri #:text? text?
183 #:headers headers)))
7482b981 184 (cache-miss uri)
3ce1b902
LC
185 (mkdir-p (dirname file))
186 (when cache-port
187 (close-port cache-port))
188 (with-atomic-file-output file
7482b981 189 (cut write-cache port <>))
3ce1b902
LC
190 (close-port port)
191 (open-input-file file))))
739ab68b
LC
192
193 (define (old? port)
194 ;; Return true if PORT has passed TTL.
195 (let* ((s (stat port))
196 (now (current-time time-utc)))
197 (< (+ (stat:mtime s) ttl) (time-second now))))
198
199 (catch 'system-error
200 (lambda ()
201 (let ((port (open-input-file file)))
202 (if (old? port)
3ce1b902 203 (update-cache port)
739ab68b
LC
204 port)))
205 (lambda args
206 (if (= ENOENT (system-error-errno args))
3ce1b902 207 (update-cache #f)
739ab68b
LC
208 (apply throw args))))))
209
3b8258c5 210;;; http-client.scm ends here