2013-04-25 16:06:48 -04:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2017-01-13 12:22:53 -05:00
|
|
|
|
;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
|
2015-02-18 19:33:10 -05:00
|
|
|
|
;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
|
2015-03-03 16:09:30 -05:00
|
|
|
|
;;; Copyright © 2012, 2015 Free Software Foundation, Inc.
|
2017-08-28 09:46:10 -04:00
|
|
|
|
;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
|
2013-04-25 16:06:48 -04:00
|
|
|
|
;;;
|
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2013-07-14 10:35:37 -04:00
|
|
|
|
(define-module (guix http-client)
|
2013-04-25 16:06:48 -04:00
|
|
|
|
#:use-module (web uri)
|
2015-05-06 04:31:11 -04:00
|
|
|
|
#:use-module ((web client) #:hide (open-socket-for-uri))
|
2013-04-25 16:06:48 -04:00
|
|
|
|
#:use-module (web response)
|
|
|
|
|
#:use-module (srfi srfi-11)
|
2015-10-17 07:02:53 -04:00
|
|
|
|
#:use-module (srfi srfi-19)
|
|
|
|
|
#:use-module (srfi srfi-26)
|
2014-03-01 09:38:11 -05:00
|
|
|
|
#:use-module (srfi srfi-34)
|
|
|
|
|
#:use-module (srfi srfi-35)
|
2015-09-26 05:11:41 -04:00
|
|
|
|
#:use-module (ice-9 match)
|
2016-10-19 08:28:56 -04:00
|
|
|
|
#:use-module (ice-9 binary-ports)
|
2013-04-25 16:06:48 -04:00
|
|
|
|
#:use-module (rnrs bytevectors)
|
|
|
|
|
#:use-module (guix ui)
|
|
|
|
|
#:use-module (guix utils)
|
2015-12-16 05:12:46 -05:00
|
|
|
|
#:use-module (guix base64)
|
2016-02-25 11:23:29 -05:00
|
|
|
|
#:autoload (guix hash) (sha256)
|
2015-10-17 07:02:53 -04:00
|
|
|
|
#:use-module ((guix build utils)
|
|
|
|
|
#:select (mkdir-p dump-port))
|
2015-05-06 04:31:11 -04:00
|
|
|
|
#:use-module ((guix build download)
|
2015-11-25 17:20:44 -05:00
|
|
|
|
#:select (open-socket-for-uri
|
2017-03-17 18:41:37 -04:00
|
|
|
|
(open-connection-for-uri
|
|
|
|
|
. guix:open-connection-for-uri)
|
|
|
|
|
resolve-uri-reference))
|
2015-05-06 04:31:11 -04:00
|
|
|
|
#:re-export (open-socket-for-uri)
|
2014-03-01 09:38:11 -05:00
|
|
|
|
#:export (&http-get-error
|
|
|
|
|
http-get-error?
|
|
|
|
|
http-get-error-uri
|
|
|
|
|
http-get-error-code
|
|
|
|
|
http-get-error-reason
|
|
|
|
|
|
2015-10-17 07:02:53 -04:00
|
|
|
|
http-fetch
|
|
|
|
|
|
|
|
|
|
%http-cache-ttl
|
|
|
|
|
http-fetch/cached))
|
2013-04-25 16:06:48 -04:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
;;;
|
2014-03-01 09:38:11 -05:00
|
|
|
|
;;; HTTP client portable among Guile versions, and with proper error condition
|
|
|
|
|
;;; reporting.
|
2013-04-25 16:06:48 -04:00
|
|
|
|
;;;
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
2014-03-01 09:38:11 -05:00
|
|
|
|
;; HTTP GET error.
|
|
|
|
|
(define-condition-type &http-get-error &error
|
|
|
|
|
http-get-error?
|
|
|
|
|
(uri http-get-error-uri) ; URI
|
|
|
|
|
(code http-get-error-code) ; integer
|
|
|
|
|
(reason http-get-error-reason)) ; string
|
|
|
|
|
|
|
|
|
|
|
2015-03-03 16:26:52 -05:00
|
|
|
|
(define-syntax when-guile<=2.0.5-or-otherwise-broken
|
2013-04-27 05:37:31 -04:00
|
|
|
|
(lambda (s)
|
|
|
|
|
(syntax-case s ()
|
|
|
|
|
((_ body ...)
|
|
|
|
|
;; Always emit BODY, regardless of VERSION, because sometimes this code
|
|
|
|
|
;; might be compiled with a recent Guile and run with 2.0.5---e.g.,
|
|
|
|
|
;; when using "guix pull".
|
|
|
|
|
#'(begin body ...)))))
|
|
|
|
|
|
2015-03-03 16:26:52 -05:00
|
|
|
|
(when-guile<=2.0.5-or-otherwise-broken
|
2015-03-03 16:09:30 -05:00
|
|
|
|
;; Backport of Guile commits 312e79f8 ("Add HTTP Chunked Encoding support to
|
2015-09-26 05:11:41 -04:00
|
|
|
|
;; web modules."), 00d3ecf2 ("http: Do not buffer HTTP chunks."), and 53b8d5f
|
|
|
|
|
;; ("web: Gracefully handle premature EOF when reading chunk header.")
|
2013-04-27 05:37:31 -04:00
|
|
|
|
|
|
|
|
|
(use-modules (ice-9 rdelim))
|
|
|
|
|
|
2015-03-03 16:26:52 -05:00
|
|
|
|
(define %web-http
|
|
|
|
|
(resolve-module '(web http)))
|
|
|
|
|
|
2013-04-27 05:37:31 -04:00
|
|
|
|
;; Chunked Responses
|
|
|
|
|
(define (read-chunk-header port)
|
2015-09-26 05:11:41 -04:00
|
|
|
|
"Read a chunk header from PORT and return the size in bytes of the
|
|
|
|
|
upcoming chunk."
|
|
|
|
|
(match (read-line port)
|
|
|
|
|
((? eof-object?)
|
|
|
|
|
;; Connection closed prematurely: there's nothing left to read.
|
|
|
|
|
0)
|
|
|
|
|
(str
|
|
|
|
|
(let ((extension-start (string-index str
|
|
|
|
|
(lambda (c)
|
|
|
|
|
(or (char=? c #\;)
|
|
|
|
|
(char=? c #\return))))))
|
|
|
|
|
(string->number (if extension-start ; unnecessary?
|
|
|
|
|
(substring str 0 extension-start)
|
|
|
|
|
str)
|
|
|
|
|
16)))))
|
2013-04-27 05:37:31 -04:00
|
|
|
|
|
|
|
|
|
(define* (make-chunked-input-port port #:key (keep-alive? #f))
|
|
|
|
|
"Returns a new port which translates HTTP chunked transfer encoded
|
|
|
|
|
data from PORT into a non-encoded format. Returns eof when it has
|
|
|
|
|
read the final chunk from PORT. This does not necessarily mean
|
|
|
|
|
that there is no more data on PORT. When the returned port is
|
|
|
|
|
closed it will also close PORT, unless the KEEP-ALIVE? is true."
|
|
|
|
|
(define (close)
|
|
|
|
|
(unless keep-alive?
|
|
|
|
|
(close-port port)))
|
2015-03-03 16:09:30 -05:00
|
|
|
|
|
|
|
|
|
(define chunk-size 0) ;size of the current chunk
|
|
|
|
|
(define remaining 0) ;number of bytes left from the current chunk
|
|
|
|
|
(define finished? #f) ;did we get all the chunks?
|
|
|
|
|
|
2013-04-27 05:37:31 -04:00
|
|
|
|
(define (read! bv idx to-read)
|
|
|
|
|
(define (loop to-read num-read)
|
|
|
|
|
(cond ((or finished? (zero? to-read))
|
|
|
|
|
num-read)
|
2015-03-03 16:09:30 -05:00
|
|
|
|
((zero? remaining) ;get a new chunk
|
|
|
|
|
(let ((size (read-chunk-header port)))
|
|
|
|
|
(set! chunk-size size)
|
|
|
|
|
(set! remaining size)
|
|
|
|
|
(if (zero? size)
|
|
|
|
|
(begin
|
|
|
|
|
(set! finished? #t)
|
|
|
|
|
num-read)
|
|
|
|
|
(loop to-read num-read))))
|
|
|
|
|
(else ;read from the current chunk
|
|
|
|
|
(let* ((ask-for (min to-read remaining))
|
|
|
|
|
(read (get-bytevector-n! port bv (+ idx num-read)
|
|
|
|
|
ask-for)))
|
|
|
|
|
(if (eof-object? read)
|
|
|
|
|
(begin ;premature termination
|
|
|
|
|
(set! finished? #t)
|
|
|
|
|
num-read)
|
|
|
|
|
(let ((left (- remaining read)))
|
|
|
|
|
(set! remaining left)
|
|
|
|
|
(when (zero? left)
|
|
|
|
|
;; We're done with this chunk; read CR and LF.
|
|
|
|
|
(get-u8 port) (get-u8 port))
|
|
|
|
|
(loop (- to-read read)
|
|
|
|
|
(+ num-read read))))))))
|
2013-04-27 05:37:31 -04:00
|
|
|
|
(loop to-read 0))
|
2015-03-03 16:09:30 -05:00
|
|
|
|
|
2013-04-27 05:37:31 -04:00
|
|
|
|
(make-custom-binary-input-port "chunked input port" read! #f #f close))
|
|
|
|
|
|
2015-03-03 16:26:52 -05:00
|
|
|
|
;; Chunked encoding support in Guile <= 2.0.11 would load whole chunks in
|
|
|
|
|
;; memory---see <http://bugs.gnu.org/19939>.
|
|
|
|
|
(when (module-variable %web-http 'read-chunk-body)
|
|
|
|
|
(module-set! %web-http 'make-chunked-input-port make-chunked-input-port))
|
|
|
|
|
|
2015-04-08 15:38:52 -04:00
|
|
|
|
(define (make-delimited-input-port port len keep-alive?)
|
|
|
|
|
"Return an input port that reads from PORT, and makes sure that
|
|
|
|
|
exactly LEN bytes are available from PORT. Closing the returned port
|
|
|
|
|
closes PORT, unless KEEP-ALIVE? is true."
|
|
|
|
|
(define bytes-read 0)
|
|
|
|
|
|
|
|
|
|
(define (fail)
|
|
|
|
|
((@@ (web response) bad-response)
|
|
|
|
|
"EOF while reading response body: ~a bytes of ~a"
|
|
|
|
|
bytes-read len))
|
|
|
|
|
|
|
|
|
|
(define (read! bv start count)
|
|
|
|
|
;; Read at most LEN bytes in total. HTTP/1.1 doesn't say what to do
|
|
|
|
|
;; when a server provides more than the Content-Length, but it seems
|
|
|
|
|
;; wise to just stop reading at LEN.
|
|
|
|
|
(let ((count (min count (- len bytes-read))))
|
|
|
|
|
(let loop ((ret (get-bytevector-n! port bv start count)))
|
|
|
|
|
(cond ((eof-object? ret)
|
|
|
|
|
(if (= bytes-read len)
|
|
|
|
|
0 ; EOF
|
|
|
|
|
(fail)))
|
|
|
|
|
((and (zero? ret) (> count 0))
|
|
|
|
|
;; Do not return zero since zero means EOF, so try again.
|
|
|
|
|
(loop (get-bytevector-n! port bv start count)))
|
|
|
|
|
(else
|
|
|
|
|
(set! bytes-read (+ bytes-read ret))
|
|
|
|
|
ret)))))
|
|
|
|
|
|
|
|
|
|
(define close
|
|
|
|
|
(and (not keep-alive?)
|
|
|
|
|
(lambda ()
|
2015-09-10 16:35:45 -04:00
|
|
|
|
(close-port port))))
|
2015-04-08 15:38:52 -04:00
|
|
|
|
|
|
|
|
|
(make-custom-binary-input-port "delimited input port" read! #f #f close))
|
|
|
|
|
|
2016-01-06 15:36:15 -05:00
|
|
|
|
(define (read-header-line port)
|
|
|
|
|
"Read an HTTP header line and return it without its final CRLF or LF.
|
|
|
|
|
Raise a 'bad-header' exception if the line does not end in CRLF or LF,
|
|
|
|
|
or if EOF is reached."
|
|
|
|
|
(match (%read-line port)
|
|
|
|
|
(((? string? line) . #\newline)
|
|
|
|
|
;; '%read-line' does not consider #\return a delimiter; so if it's
|
|
|
|
|
;; there, remove it. We are more tolerant than the RFC in that we
|
|
|
|
|
;; tolerate LF-only endings.
|
|
|
|
|
(if (string-suffix? "\r" line)
|
|
|
|
|
(string-drop-right line 1)
|
|
|
|
|
line))
|
|
|
|
|
((line . _) ;EOF or missing delimiter
|
|
|
|
|
((@@ (web http) bad-header) 'read-header-line line))))
|
|
|
|
|
|
2015-09-10 16:35:45 -04:00
|
|
|
|
(unless (guile-version>? "2.0.11")
|
2015-04-08 15:38:52 -04:00
|
|
|
|
;; Guile <= 2.0.9 had a bug whereby 'response-body-port' would read more
|
|
|
|
|
;; than what 'content-length' says. See Guile commit 802a25b.
|
2016-01-06 12:04:36 -05:00
|
|
|
|
;; Guile <= 2.0.11 had a bug whereby the 'close' method of the response
|
2015-09-10 16:35:45 -04:00
|
|
|
|
;; body port would fail with wrong-arg-num. See Guile commit 5a10e41.
|
2015-04-08 15:38:52 -04:00
|
|
|
|
(module-set! (resolve-module '(web response))
|
2016-01-06 15:36:15 -05:00
|
|
|
|
'make-delimited-input-port make-delimited-input-port)
|
|
|
|
|
|
|
|
|
|
;; Guile <= 2.0.11 was affected by <http://bugs.gnu.org/22273>. See Guile
|
|
|
|
|
;; commit 4c7732c.
|
|
|
|
|
(when (module-variable %web-http 'read-line*)
|
|
|
|
|
(module-set! %web-http 'read-line* read-header-line))))
|
2013-04-27 05:37:31 -04:00
|
|
|
|
|
|
|
|
|
|
2016-03-14 12:35:09 -04:00
|
|
|
|
(define* (http-fetch uri #:key port (text? #f) (buffered? #t)
|
2017-01-13 12:22:53 -05:00
|
|
|
|
keep-alive? (verify-certificate? #t)
|
|
|
|
|
(headers '((user-agent . "GNU Guile"))))
|
2013-04-25 16:06:48 -04:00
|
|
|
|
"Return an input port containing the data at URI, and the expected number of
|
|
|
|
|
bytes available or #f. If TEXT? is true, the data at URI is considered to be
|
2013-05-15 17:40:09 -04:00
|
|
|
|
textual. Follow any HTTP redirection. When BUFFERED? is #f, return an
|
2016-03-14 12:35:09 -04:00
|
|
|
|
unbuffered port, suitable for use in `filtered-port'. When KEEP-ALIVE? is
|
|
|
|
|
true, send a 'Connection: keep-alive' HTTP header, in which case PORT may be
|
2017-01-13 12:22:53 -05:00
|
|
|
|
reused for future HTTP requests. HEADERS is an alist of extra HTTP headers.
|
2014-03-01 09:38:11 -05:00
|
|
|
|
|
2016-11-12 06:53:45 -05:00
|
|
|
|
When VERIFY-CERTIFICATE? is true, verify HTTPS server certificates.
|
|
|
|
|
|
2014-03-01 09:38:11 -05:00
|
|
|
|
Raise an '&http-get-error' condition if downloading fails."
|
2015-11-29 11:46:11 -05:00
|
|
|
|
(let loop ((uri (if (string? uri)
|
|
|
|
|
(string->uri uri)
|
|
|
|
|
uri)))
|
2017-03-17 18:41:37 -04:00
|
|
|
|
(let ((port (or port (guix:open-connection-for-uri uri
|
|
|
|
|
#:verify-certificate?
|
|
|
|
|
verify-certificate?)))
|
2017-01-13 12:22:53 -05:00
|
|
|
|
(headers (match (uri-userinfo uri)
|
|
|
|
|
((? string? str)
|
|
|
|
|
(cons (cons 'Authorization
|
|
|
|
|
(string-append "Basic "
|
|
|
|
|
(base64-encode
|
|
|
|
|
(string->utf8 str))))
|
|
|
|
|
headers))
|
|
|
|
|
(_ headers))))
|
2016-03-16 05:20:45 -04:00
|
|
|
|
(unless (or buffered? (not (file-port? port)))
|
2015-05-06 04:31:11 -04:00
|
|
|
|
(setvbuf port _IONBF))
|
2013-06-29 16:10:06 -04:00
|
|
|
|
(let*-values (((resp data)
|
2017-03-17 18:07:01 -04:00
|
|
|
|
(http-get uri #:streaming? #t #:port port
|
|
|
|
|
#:keep-alive? #t
|
|
|
|
|
#:headers headers))
|
2013-06-29 16:10:06 -04:00
|
|
|
|
((code)
|
|
|
|
|
(response-code resp)))
|
|
|
|
|
(case code
|
|
|
|
|
((200)
|
2015-05-07 15:51:12 -04:00
|
|
|
|
(values data (response-content-length resp)))
|
2013-06-29 16:10:06 -04:00
|
|
|
|
((301 ; moved permanently
|
2017-08-28 09:46:10 -04:00
|
|
|
|
302 ; found (redirection)
|
|
|
|
|
303 ; see other
|
|
|
|
|
307 ; temporary redirection
|
|
|
|
|
308) ; permanent redirection
|
2015-02-18 19:33:10 -05:00
|
|
|
|
(let ((uri (resolve-uri-reference (response-location resp) uri)))
|
2013-06-29 16:10:06 -04:00
|
|
|
|
(close-port port)
|
ui: Rename '_' to 'G_'.
This avoids collisions with '_' when the latter is used as a 'match'
pattern for instance. See
<https://lists.gnu.org/archive/html/guix-devel/2017-04/msg00464.html>.
* guix/ui.scm: Rename '_' to 'G_'.
* po/guix/Makevars (XGETTEXT_OPTIONS): Adjust accordingly.
* build-aux/compile-all.scm (warnings): Remove 'format'.
* gnu/packages.scm,
gnu/services.scm,
gnu/services/shepherd.scm,
gnu/system.scm,
gnu/system/shadow.scm,
guix/gnupg.scm,
guix/http-client.scm,
guix/import/cpan.scm,
guix/import/elpa.scm,
guix/import/pypi.scm,
guix/nar.scm,
guix/scripts.scm,
guix/scripts/archive.scm,
guix/scripts/authenticate.scm,
guix/scripts/build.scm,
guix/scripts/challenge.scm,
guix/scripts/container.scm,
guix/scripts/container/exec.scm,
guix/scripts/copy.scm,
guix/scripts/download.scm,
guix/scripts/edit.scm,
guix/scripts/environment.scm,
guix/scripts/gc.scm,
guix/scripts/graph.scm,
guix/scripts/hash.scm,
guix/scripts/import.scm,
guix/scripts/import/cpan.scm,
guix/scripts/import/cran.scm,
guix/scripts/import/crate.scm,
guix/scripts/import/elpa.scm,
guix/scripts/import/gem.scm,
guix/scripts/import/gnu.scm,
guix/scripts/import/hackage.scm,
guix/scripts/import/nix.scm,
guix/scripts/import/pypi.scm,
guix/scripts/import/stackage.scm,
guix/scripts/lint.scm,
guix/scripts/offload.scm,
guix/scripts/pack.scm,
guix/scripts/package.scm,
guix/scripts/perform-download.scm,
guix/scripts/publish.scm,
guix/scripts/pull.scm,
guix/scripts/refresh.scm,
guix/scripts/size.scm,
guix/scripts/substitute.scm,
guix/scripts/system.scm,
guix/ssh.scm,
guix/upstream.scm: Use 'G_' instead of '_'. Most of this change was
obtained by running: "sed -i -e's/(_ "/(G_ "/g' `find -name \*.scm`".
2017-05-03 09:57:02 -04:00
|
|
|
|
(format #t (G_ "following redirection to `~a'...~%")
|
2013-06-29 16:10:06 -04:00
|
|
|
|
(uri->string uri))
|
|
|
|
|
(loop uri)))
|
|
|
|
|
(else
|
2014-03-01 09:38:11 -05:00
|
|
|
|
(raise (condition (&http-get-error
|
|
|
|
|
(uri uri)
|
|
|
|
|
(code code)
|
|
|
|
|
(reason (response-reason-phrase resp)))
|
|
|
|
|
(&message
|
2017-01-10 09:34:11 -05:00
|
|
|
|
(message
|
|
|
|
|
(format
|
|
|
|
|
#f
|
ui: Rename '_' to 'G_'.
This avoids collisions with '_' when the latter is used as a 'match'
pattern for instance. See
<https://lists.gnu.org/archive/html/guix-devel/2017-04/msg00464.html>.
* guix/ui.scm: Rename '_' to 'G_'.
* po/guix/Makevars (XGETTEXT_OPTIONS): Adjust accordingly.
* build-aux/compile-all.scm (warnings): Remove 'format'.
* gnu/packages.scm,
gnu/services.scm,
gnu/services/shepherd.scm,
gnu/system.scm,
gnu/system/shadow.scm,
guix/gnupg.scm,
guix/http-client.scm,
guix/import/cpan.scm,
guix/import/elpa.scm,
guix/import/pypi.scm,
guix/nar.scm,
guix/scripts.scm,
guix/scripts/archive.scm,
guix/scripts/authenticate.scm,
guix/scripts/build.scm,
guix/scripts/challenge.scm,
guix/scripts/container.scm,
guix/scripts/container/exec.scm,
guix/scripts/copy.scm,
guix/scripts/download.scm,
guix/scripts/edit.scm,
guix/scripts/environment.scm,
guix/scripts/gc.scm,
guix/scripts/graph.scm,
guix/scripts/hash.scm,
guix/scripts/import.scm,
guix/scripts/import/cpan.scm,
guix/scripts/import/cran.scm,
guix/scripts/import/crate.scm,
guix/scripts/import/elpa.scm,
guix/scripts/import/gem.scm,
guix/scripts/import/gnu.scm,
guix/scripts/import/hackage.scm,
guix/scripts/import/nix.scm,
guix/scripts/import/pypi.scm,
guix/scripts/import/stackage.scm,
guix/scripts/lint.scm,
guix/scripts/offload.scm,
guix/scripts/pack.scm,
guix/scripts/package.scm,
guix/scripts/perform-download.scm,
guix/scripts/publish.scm,
guix/scripts/pull.scm,
guix/scripts/refresh.scm,
guix/scripts/size.scm,
guix/scripts/substitute.scm,
guix/scripts/system.scm,
guix/ssh.scm,
guix/upstream.scm: Use 'G_' instead of '_'. Most of this change was
obtained by running: "sed -i -e's/(_ "/(G_ "/g' `find -name \*.scm`".
2017-05-03 09:57:02 -04:00
|
|
|
|
(G_ "~a: HTTP download failed: ~a (~s)")
|
2017-01-10 09:34:11 -05:00
|
|
|
|
(uri->string uri) code
|
|
|
|
|
(response-reason-phrase resp))))))))))))
|
2013-04-25 16:06:48 -04:00
|
|
|
|
|
2015-10-17 07:02:53 -04:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Caching.
|
|
|
|
|
;;;
|
|
|
|
|
|
2015-10-21 05:48:45 -04:00
|
|
|
|
(define %http-cache-ttl
|
2015-10-17 07:02:53 -04:00
|
|
|
|
;; Time-to-live in seconds of the HTTP cache of in ~/.cache/guix.
|
|
|
|
|
(make-parameter
|
|
|
|
|
(* 3600 (or (and=> (getenv "GUIX_HTTP_CACHE_TTL")
|
|
|
|
|
string->number*)
|
|
|
|
|
36))))
|
|
|
|
|
|
2016-02-25 11:23:29 -05:00
|
|
|
|
(define (cache-file-for-uri uri)
|
|
|
|
|
"Return the name of the file in the cache corresponding to URI."
|
|
|
|
|
(let ((digest (sha256 (string->utf8 (uri->string uri)))))
|
|
|
|
|
;; Use the "URL" alphabet because it does not contain "/".
|
|
|
|
|
(string-append (cache-directory) "/http/"
|
|
|
|
|
(base64-encode digest 0 (bytevector-length digest)
|
|
|
|
|
#f #f base64url-alphabet))))
|
|
|
|
|
|
2017-11-15 04:23:38 -05:00
|
|
|
|
(define* (http-fetch/cached uri #:key (ttl (%http-cache-ttl)) text?
|
|
|
|
|
(write-cache dump-port)
|
|
|
|
|
(cache-miss (const #t)))
|
2015-10-17 07:02:53 -04:00
|
|
|
|
"Like 'http-fetch', return an input port, but cache its contents in
|
2017-11-15 04:23:38 -05:00
|
|
|
|
~/.cache/guix. The cache remains valid for TTL seconds.
|
|
|
|
|
|
|
|
|
|
Call WRITE-CACHE with the HTTP input port and the cache output port to write
|
|
|
|
|
the data to cache. Call CACHE-MISS with URI just before fetching data from
|
|
|
|
|
URI."
|
2016-02-25 11:23:29 -05:00
|
|
|
|
(let ((file (cache-file-for-uri uri)))
|
2017-09-19 05:49:29 -04:00
|
|
|
|
(define (update-cache cache-port)
|
|
|
|
|
(define cache-time
|
|
|
|
|
(and cache-port
|
|
|
|
|
(stat:mtime (stat cache-port))))
|
|
|
|
|
|
|
|
|
|
(define headers
|
|
|
|
|
`((user-agent . "GNU Guile")
|
|
|
|
|
,@(if cache-time
|
|
|
|
|
`((if-modified-since
|
|
|
|
|
. ,(time-utc->date (make-time time-utc 0 cache-time))))
|
|
|
|
|
'())))
|
|
|
|
|
|
2015-10-17 07:02:53 -04:00
|
|
|
|
;; Update the cache and return an input port.
|
2017-09-19 05:49:29 -04:00
|
|
|
|
(guard (c ((http-get-error? c)
|
|
|
|
|
(if (= 304 (http-get-error-code c)) ;"Not Modified"
|
2017-09-25 18:32:12 -04:00
|
|
|
|
(begin
|
|
|
|
|
(utime file) ;update FILE's mtime
|
|
|
|
|
cache-port)
|
2017-09-19 05:49:29 -04:00
|
|
|
|
(raise c))))
|
|
|
|
|
(let ((port (http-fetch uri #:text? text?
|
|
|
|
|
#:headers headers)))
|
2017-11-15 04:23:38 -05:00
|
|
|
|
(cache-miss uri)
|
2017-09-19 05:49:29 -04:00
|
|
|
|
(mkdir-p (dirname file))
|
|
|
|
|
(when cache-port
|
|
|
|
|
(close-port cache-port))
|
|
|
|
|
(with-atomic-file-output file
|
2017-11-15 04:23:38 -05:00
|
|
|
|
(cut write-cache port <>))
|
2017-09-19 05:49:29 -04:00
|
|
|
|
(close-port port)
|
|
|
|
|
(open-input-file file))))
|
2015-10-17 07:02:53 -04:00
|
|
|
|
|
|
|
|
|
(define (old? port)
|
|
|
|
|
;; Return true if PORT has passed TTL.
|
|
|
|
|
(let* ((s (stat port))
|
|
|
|
|
(now (current-time time-utc)))
|
|
|
|
|
(< (+ (stat:mtime s) ttl) (time-second now))))
|
|
|
|
|
|
|
|
|
|
(catch 'system-error
|
|
|
|
|
(lambda ()
|
|
|
|
|
(let ((port (open-input-file file)))
|
|
|
|
|
(if (old? port)
|
2017-09-19 05:49:29 -04:00
|
|
|
|
(update-cache port)
|
2015-10-17 07:02:53 -04:00
|
|
|
|
port)))
|
|
|
|
|
(lambda args
|
|
|
|
|
(if (= ENOENT (system-error-errno args))
|
2017-09-19 05:49:29 -04:00
|
|
|
|
(update-cache #f)
|
2015-10-17 07:02:53 -04:00
|
|
|
|
(apply throw args))))))
|
|
|
|
|
|
2013-07-14 10:35:37 -04:00
|
|
|
|
;;; http-client.scm ends here
|