2013-04-25 22:06:48 +02:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2022-03-03 20:38:36 +01:00
|
|
|
|
;;; Copyright © 2012-2018, 2020-2022 Ludovic Courtès <ludo@gnu.org>
|
2015-02-19 01:33:10 +01:00
|
|
|
|
;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
|
2015-03-03 22:09:30 +01:00
|
|
|
|
;;; Copyright © 2012, 2015 Free Software Foundation, Inc.
|
2017-08-28 15:46:10 +02:00
|
|
|
|
;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
|
2013-04-25 22:06:48 +02: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 16:35:37 +02:00
|
|
|
|
(define-module (guix http-client)
|
2013-04-25 22:06:48 +02:00
|
|
|
|
#:use-module (web uri)
|
2021-01-07 20:40:07 +01:00
|
|
|
|
#:use-module (web http)
|
2015-05-06 10:31:11 +02:00
|
|
|
|
#:use-module ((web client) #:hide (open-socket-for-uri))
|
2021-01-07 20:40:07 +01:00
|
|
|
|
#:use-module (web request)
|
2013-04-25 22:06:48 +02:00
|
|
|
|
#:use-module (web response)
|
2021-01-07 20:40:07 +01:00
|
|
|
|
#:use-module (srfi srfi-1)
|
2013-04-25 22:06:48 +02:00
|
|
|
|
#:use-module (srfi srfi-11)
|
2015-10-17 13:02:53 +02:00
|
|
|
|
#:use-module (srfi srfi-19)
|
|
|
|
|
#:use-module (srfi srfi-26)
|
2014-03-01 15:38:11 +01:00
|
|
|
|
#:use-module (srfi srfi-34)
|
|
|
|
|
#:use-module (srfi srfi-35)
|
2015-09-26 11:11:41 +02:00
|
|
|
|
#:use-module (ice-9 match)
|
2016-10-19 14:28:56 +02:00
|
|
|
|
#:use-module (ice-9 binary-ports)
|
2013-04-25 22:06:48 +02:00
|
|
|
|
#:use-module (rnrs bytevectors)
|
|
|
|
|
#:use-module (guix ui)
|
|
|
|
|
#:use-module (guix utils)
|
2015-12-16 11:12:46 +01:00
|
|
|
|
#:use-module (guix base64)
|
Switch to Guile-Gcrypt.
This removes (guix hash) and (guix pk-crypto), which now live as part of
Guile-Gcrypt (version 0.1.0.)
* guix/gcrypt.scm, guix/hash.scm, guix/pk-crypto.scm,
tests/hash.scm, tests/pk-crypto.scm: Remove.
* configure.ac: Test for Guile-Gcrypt. Remove LIBGCRYPT and
LIBGCRYPT_LIBDIR assignments.
* m4/guix.m4 (GUIX_ASSERT_LIBGCRYPT_USABLE): Remove.
* README: Add Guile-Gcrypt to the dependencies; move libgcrypt as
"required unless --disable-daemon".
* doc/guix.texi (Requirements): Likewise.
* gnu/packages/bash.scm, guix/derivations.scm, guix/docker.scm,
guix/git.scm, guix/http-client.scm, guix/import/cpan.scm,
guix/import/cran.scm, guix/import/crate.scm, guix/import/elpa.scm,
guix/import/gnu.scm, guix/import/hackage.scm,
guix/import/texlive.scm, guix/import/utils.scm, guix/nar.scm,
guix/pki.scm, guix/scripts/archive.scm,
guix/scripts/authenticate.scm, guix/scripts/download.scm,
guix/scripts/hash.scm, guix/scripts/pack.scm,
guix/scripts/publish.scm, guix/scripts/refresh.scm,
guix/scripts/substitute.scm, guix/store.scm,
guix/store/deduplication.scm, guix/tests.scm, tests/base32.scm,
tests/builders.scm, tests/challenge.scm, tests/cpan.scm,
tests/crate.scm, tests/derivations.scm, tests/gem.scm,
tests/nar.scm, tests/opam.scm, tests/pki.scm,
tests/publish.scm, tests/pypi.scm, tests/store-deduplication.scm,
tests/store.scm, tests/substitute.scm: Adjust imports.
* gnu/system/vm.scm: Likewise.
(guile-sqlite3&co): Rename to...
(gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT.
(expression->derivation-in-linux-vm)[config]: Remove.
(iso9660-image)[config]: Remove.
(qemu-image)[config]: Remove.
(system-docker-image)[config]: Remove.
* guix/scripts/pack.scm: Adjust imports.
(guile-sqlite3&co): Rename to...
(gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT.
(self-contained-tarball)[build]: Call 'make-config.scm' without
#:libgcrypt argument.
(squashfs-image)[libgcrypt]: Remove.
[build]: Call 'make-config.scm' without #:libgcrypt.
(docker-image)[config, json]: Remove.
[build]: Add GUILE-GCRYPT to the extensions Remove (guix config) from
the imported modules.
* guix/self.scm (specification->package): Remove "libgcrypt", add
"guile-gcrypt".
(compiled-guix): Remove #:libgcrypt.
[guile-gcrypt]: New variable.
[dependencies]: Add it.
[*core-modules*]: Remove #:libgcrypt from 'make-config.scm' call.
Add #:extensions.
[*config*]: Remove #:libgcrypt from 'make-config.scm' call.
(%dependency-variables): Remove %libgcrypt.
(make-config.scm): Remove #:libgcrypt.
* build-aux/build-self.scm (guile-gcrypt): New variable.
(make-config.scm): Remove #:libgcrypt.
(build-program)[fake-gcrypt-hash]: New variable.
Add (gcrypt hash) to the imported modules. Adjust load path
assignments.
* gnu/packages/package-management.scm (guix)[propagated-inputs]: Add
GUILE-GCRYPT.
[arguments]: In 'wrap-program' phase, add GUILE-GCRYPT to the search
path.
2018-08-31 17:07:07 +02:00
|
|
|
|
#:autoload (gcrypt hash) (sha256)
|
2021-04-24 17:59:14 +02:00
|
|
|
|
#:autoload (gnutls) (error/invalid-session error/again error/interrupted)
|
2015-10-17 13:02:53 +02:00
|
|
|
|
#:use-module ((guix build utils)
|
|
|
|
|
#:select (mkdir-p dump-port))
|
2015-05-06 10:31:11 +02:00
|
|
|
|
#:use-module ((guix build download)
|
2015-11-25 23:20:44 +01:00
|
|
|
|
#:select (open-socket-for-uri
|
2017-03-17 23:41:37 +01:00
|
|
|
|
(open-connection-for-uri
|
|
|
|
|
. guix:open-connection-for-uri)
|
|
|
|
|
resolve-uri-reference))
|
2015-05-06 10:31:11 +02:00
|
|
|
|
#:re-export (open-socket-for-uri)
|
2014-03-01 15:38:11 +01:00
|
|
|
|
#:export (&http-get-error
|
|
|
|
|
http-get-error?
|
|
|
|
|
http-get-error-uri
|
|
|
|
|
http-get-error-code
|
|
|
|
|
http-get-error-reason
|
2022-03-03 20:38:36 +01:00
|
|
|
|
http-get-error-headers
|
2014-03-01 15:38:11 +01:00
|
|
|
|
|
2015-10-17 13:02:53 +02:00
|
|
|
|
http-fetch
|
2021-01-07 20:40:07 +01:00
|
|
|
|
http-multiple-get
|
2015-10-17 13:02:53 +02:00
|
|
|
|
|
|
|
|
|
%http-cache-ttl
|
|
|
|
|
http-fetch/cached))
|
2013-04-25 22:06:48 +02:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
;;;
|
2014-03-01 15:38:11 +01:00
|
|
|
|
;;; HTTP client portable among Guile versions, and with proper error condition
|
|
|
|
|
;;; reporting.
|
2013-04-25 22:06:48 +02:00
|
|
|
|
;;;
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
2014-03-01 15:38:11 +01:00
|
|
|
|
;; HTTP GET error.
|
|
|
|
|
(define-condition-type &http-get-error &error
|
|
|
|
|
http-get-error?
|
2022-03-03 20:38:36 +01:00
|
|
|
|
(uri http-get-error-uri) ;URI
|
|
|
|
|
(code http-get-error-code) ;integer
|
|
|
|
|
(reason http-get-error-reason) ;string
|
|
|
|
|
(headers http-get-error-headers)) ;alist
|
2014-03-01 15:38:11 +01:00
|
|
|
|
|
|
|
|
|
|
2016-03-14 17:35:09 +01:00
|
|
|
|
(define* (http-fetch uri #:key port (text? #f) (buffered? #t)
|
2021-01-07 21:18:57 +01:00
|
|
|
|
(open-connection guix:open-connection-for-uri)
|
2020-12-02 22:49:39 +01:00
|
|
|
|
(keep-alive? #f)
|
2020-01-03 15:47:12 +01:00
|
|
|
|
(verify-certificate? #t)
|
2020-10-12 11:19:32 +02:00
|
|
|
|
(headers '((user-agent . "GNU Guile")))
|
2021-03-17 15:04:56 +01:00
|
|
|
|
(log-port (current-error-port))
|
2020-10-12 11:19:32 +02:00
|
|
|
|
timeout)
|
2013-04-25 22:06:48 +02: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 23:40:09 +02:00
|
|
|
|
textual. Follow any HTTP redirection. When BUFFERED? is #f, return an
|
2020-01-03 15:47:12 +01:00
|
|
|
|
unbuffered port, suitable for use in `filtered-port'. HEADERS is an alist of
|
|
|
|
|
extra HTTP headers.
|
2014-03-01 15:38:11 +01:00
|
|
|
|
|
2020-12-02 22:49:39 +01:00
|
|
|
|
When KEEP-ALIVE? is true, the connection is marked as 'keep-alive' and PORT is
|
|
|
|
|
not closed upon completion.
|
|
|
|
|
|
2016-11-12 12:53:45 +01:00
|
|
|
|
When VERIFY-CERTIFICATE? is true, verify HTTPS server certificates.
|
|
|
|
|
|
2020-10-12 11:19:32 +02:00
|
|
|
|
TIMEOUT specifies the timeout in seconds for connection establishment; when
|
|
|
|
|
TIMEOUT is #f, connection establishment never times out.
|
|
|
|
|
|
2021-03-17 15:04:56 +01:00
|
|
|
|
Write information about redirects to LOG-PORT.
|
|
|
|
|
|
2014-03-01 15:38:11 +01:00
|
|
|
|
Raise an '&http-get-error' condition if downloading fails."
|
2022-03-03 21:37:27 +01:00
|
|
|
|
(define uri*
|
|
|
|
|
(if (string? uri) (string->uri uri) uri))
|
|
|
|
|
|
|
|
|
|
(let loop ((uri uri*)
|
|
|
|
|
(port (or port (open-connection uri*
|
|
|
|
|
#:verify-certificate?
|
|
|
|
|
verify-certificate?
|
|
|
|
|
#:timeout timeout))))
|
|
|
|
|
(let ((headers (match (uri-userinfo uri)
|
2017-01-13 18:22:53 +01:00
|
|
|
|
((? string? str)
|
|
|
|
|
(cons (cons 'Authorization
|
|
|
|
|
(string-append "Basic "
|
|
|
|
|
(base64-encode
|
|
|
|
|
(string->utf8 str))))
|
|
|
|
|
headers))
|
|
|
|
|
(_ headers))))
|
2016-03-16 10:20:45 +01:00
|
|
|
|
(unless (or buffered? (not (file-port? port)))
|
2019-01-07 10:57:18 +01:00
|
|
|
|
(setvbuf port 'none))
|
2013-06-29 22:10:06 +02:00
|
|
|
|
(let*-values (((resp data)
|
2017-03-17 23:07:01 +01:00
|
|
|
|
(http-get uri #:streaming? #t #:port port
|
2020-12-02 22:49:39 +01:00
|
|
|
|
#:keep-alive? keep-alive?
|
2017-03-17 23:07:01 +01:00
|
|
|
|
#:headers headers))
|
2013-06-29 22:10:06 +02:00
|
|
|
|
((code)
|
|
|
|
|
(response-code resp)))
|
|
|
|
|
(case code
|
|
|
|
|
((200)
|
2015-05-07 21:51:12 +02:00
|
|
|
|
(values data (response-content-length resp)))
|
2013-06-29 22:10:06 +02:00
|
|
|
|
((301 ; moved permanently
|
2017-08-28 15:46:10 +02:00
|
|
|
|
302 ; found (redirection)
|
|
|
|
|
303 ; see other
|
|
|
|
|
307 ; temporary redirection
|
|
|
|
|
308) ; permanent redirection
|
2022-03-03 21:37:27 +01:00
|
|
|
|
(let ((host (uri-host uri))
|
|
|
|
|
(uri (resolve-uri-reference (response-location resp) uri)))
|
|
|
|
|
(if keep-alive?
|
|
|
|
|
(dump-port data (%make-void-port "w0")
|
|
|
|
|
(response-content-length resp))
|
|
|
|
|
(close-port port))
|
2021-03-17 15:04:56 +01:00
|
|
|
|
(format log-port (G_ "following redirection to `~a'...~%")
|
2013-06-29 22:10:06 +02:00
|
|
|
|
(uri->string uri))
|
2022-03-03 21:37:27 +01:00
|
|
|
|
(loop uri
|
|
|
|
|
(or (and keep-alive?
|
|
|
|
|
(or (not (uri-host uri))
|
|
|
|
|
(string=? host (uri-host uri)))
|
|
|
|
|
port)
|
2022-04-10 15:41:14 +02:00
|
|
|
|
(open-connection uri
|
2022-03-03 21:37:27 +01:00
|
|
|
|
#:verify-certificate?
|
|
|
|
|
verify-certificate?
|
|
|
|
|
#:timeout timeout)))))
|
2013-06-29 22:10:06 +02:00
|
|
|
|
(else
|
2014-03-01 15:38:11 +01:00
|
|
|
|
(raise (condition (&http-get-error
|
|
|
|
|
(uri uri)
|
|
|
|
|
(code code)
|
2022-03-03 20:38:36 +01:00
|
|
|
|
(reason (response-reason-phrase resp))
|
|
|
|
|
(headers (response-headers resp)))
|
2014-03-01 15:38:11 +01:00
|
|
|
|
(&message
|
2017-01-10 15:34:11 +01: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 15:57:02 +02:00
|
|
|
|
(G_ "~a: HTTP download failed: ~a (~s)")
|
2017-01-10 15:34:11 +01:00
|
|
|
|
(uri->string uri) code
|
|
|
|
|
(response-reason-phrase resp))))))))))))
|
2013-04-25 22:06:48 +02:00
|
|
|
|
|
2021-03-27 18:39:28 +01:00
|
|
|
|
(define-syntax-rule (false-if-networking-error exp)
|
|
|
|
|
"Return #f if EXP triggers a network related exception as can occur when
|
|
|
|
|
reusing stale cached connections."
|
|
|
|
|
;; FIXME: Duplicated from 'with-cached-connection'.
|
|
|
|
|
(catch #t
|
|
|
|
|
(lambda ()
|
|
|
|
|
exp)
|
|
|
|
|
(lambda (key . args)
|
|
|
|
|
;; If PORT was cached and the server closed the connection in the
|
|
|
|
|
;; meantime, we get EPIPE. In that case, open a fresh connection and
|
|
|
|
|
;; retry. We might also get 'bad-response or a similar exception from
|
|
|
|
|
;; (web response) later on, once we've sent the request, or a
|
|
|
|
|
;; ERROR/INVALID-SESSION from GnuTLS.
|
|
|
|
|
(if (or (and (eq? key 'system-error)
|
|
|
|
|
(= EPIPE (system-error-errno `(,key ,@args))))
|
|
|
|
|
(and (eq? key 'gnutls-error)
|
2021-04-24 17:59:14 +02:00
|
|
|
|
(memq (first args)
|
|
|
|
|
(list error/invalid-session
|
|
|
|
|
|
|
|
|
|
;; XXX: These two are not properly handled in
|
|
|
|
|
;; GnuTLS < 3.7.2, in
|
|
|
|
|
;; 'write_to_session_record_port'; see
|
|
|
|
|
;; <https://bugs.gnu.org/47867>.
|
|
|
|
|
error/again error/interrupted)))
|
2021-03-27 18:39:28 +01:00
|
|
|
|
(memq key
|
|
|
|
|
'(bad-response bad-header bad-header-component)))
|
|
|
|
|
#f
|
|
|
|
|
(apply throw key args)))))
|
|
|
|
|
|
2021-01-07 20:40:07 +01:00
|
|
|
|
(define* (http-multiple-get base-uri proc seed requests
|
|
|
|
|
#:key port (verify-certificate? #t)
|
|
|
|
|
(open-connection guix:open-connection-for-uri)
|
|
|
|
|
(keep-alive? #t)
|
|
|
|
|
(batch-size 1000))
|
|
|
|
|
"Send all of REQUESTS to the server at BASE-URI. Call PROC for each
|
|
|
|
|
response, passing it the request object, the response, a port from which to
|
|
|
|
|
read the response body, and the previous result, starting with SEED, à la
|
|
|
|
|
'fold'. Return the final result.
|
|
|
|
|
|
|
|
|
|
When PORT is specified, use it as the initial connection on which HTTP
|
|
|
|
|
requests are sent; otherwise call OPEN-CONNECTION to open a new connection for
|
|
|
|
|
a URI. When KEEP-ALIVE? is false, close the connection port before
|
|
|
|
|
returning."
|
|
|
|
|
(let connect ((port port)
|
|
|
|
|
(requests requests)
|
|
|
|
|
(result seed))
|
|
|
|
|
(define batch
|
|
|
|
|
(if (>= batch-size (length requests))
|
|
|
|
|
requests
|
|
|
|
|
(take requests batch-size)))
|
|
|
|
|
|
|
|
|
|
;; (format (current-error-port) "connecting (~a requests left)..."
|
|
|
|
|
;; (length requests))
|
|
|
|
|
(let ((p (or port (open-connection base-uri
|
|
|
|
|
#:verify-certificate?
|
|
|
|
|
verify-certificate?))))
|
|
|
|
|
;; For HTTPS, P is not a file port and does not support 'setvbuf'.
|
|
|
|
|
(when (file-port? p)
|
|
|
|
|
(setvbuf p 'block (expt 2 16)))
|
|
|
|
|
|
|
|
|
|
;; Send BATCH in a row.
|
|
|
|
|
;; XXX: Do our own caching to work around inefficiencies when
|
|
|
|
|
;; communicating over TLS: <http://bugs.gnu.org/22966>.
|
|
|
|
|
(let-values (((buffer get) (open-bytevector-output-port)))
|
|
|
|
|
;; Inherit the HTTP proxying property from P.
|
|
|
|
|
(set-http-proxy-port?! buffer (http-proxy-port? p))
|
|
|
|
|
|
2021-04-24 18:07:09 +02:00
|
|
|
|
;; Swallow networking errors that could occur due to connection reuse
|
|
|
|
|
;; and the like; they will be handled down the road when trying to
|
|
|
|
|
;; read responses.
|
|
|
|
|
(false-if-networking-error
|
|
|
|
|
(begin
|
|
|
|
|
(for-each (cut write-request <> buffer) batch)
|
|
|
|
|
(put-bytevector p (get))
|
|
|
|
|
(force-output p))))
|
2021-01-07 20:40:07 +01:00
|
|
|
|
|
|
|
|
|
;; Now start processing responses.
|
|
|
|
|
(let loop ((sent batch)
|
|
|
|
|
(processed 0)
|
|
|
|
|
(result result))
|
|
|
|
|
(match sent
|
|
|
|
|
(()
|
|
|
|
|
(match (drop requests processed)
|
|
|
|
|
(()
|
|
|
|
|
(unless keep-alive?
|
|
|
|
|
(close-port p))
|
|
|
|
|
(reverse result))
|
|
|
|
|
(remainder
|
|
|
|
|
(connect p remainder result))))
|
|
|
|
|
((head tail ...)
|
2021-03-27 18:39:28 +01:00
|
|
|
|
(match (false-if-networking-error (read-response p))
|
|
|
|
|
((? response? resp)
|
|
|
|
|
(let* ((body (response-body-port resp))
|
|
|
|
|
(result (proc head resp body result)))
|
|
|
|
|
;; The server can choose to stop responding at any time,
|
|
|
|
|
;; in which case we have to try again. Check whether
|
|
|
|
|
;; that is the case. Note that even upon "Connection:
|
|
|
|
|
;; close", we can read from BODY.
|
|
|
|
|
(match (assq 'connection (response-headers resp))
|
|
|
|
|
(('connection 'close)
|
|
|
|
|
(close-port p)
|
|
|
|
|
(connect #f ;try again
|
|
|
|
|
(drop requests (+ 1 processed))
|
|
|
|
|
result))
|
|
|
|
|
(_
|
|
|
|
|
(loop tail (+ 1 processed) result)))))
|
|
|
|
|
(#f
|
|
|
|
|
(close-port p)
|
|
|
|
|
(connect #f ; try again
|
2021-03-28 22:04:01 +02:00
|
|
|
|
(drop requests processed)
|
2021-03-27 18:39:28 +01:00
|
|
|
|
result)))))))))
|
2021-01-07 20:40:07 +01:00
|
|
|
|
|
2015-10-17 13:02:53 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Caching.
|
|
|
|
|
;;;
|
|
|
|
|
|
2015-10-21 11:48:45 +02:00
|
|
|
|
(define %http-cache-ttl
|
2015-10-17 13:02:53 +02: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 17:23:29 +01: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 10:23:38 +01:00
|
|
|
|
(define* (http-fetch/cached uri #:key (ttl (%http-cache-ttl)) text?
|
2022-05-18 20:10:53 +02:00
|
|
|
|
(headers '((user-agent . "GNU Guile")))
|
2017-11-15 10:23:38 +01:00
|
|
|
|
(write-cache dump-port)
|
2020-10-12 11:19:32 +02:00
|
|
|
|
(cache-miss (const #t))
|
2021-03-17 15:04:56 +01:00
|
|
|
|
(log-port (current-error-port))
|
2020-10-12 11:19:32 +02:00
|
|
|
|
(timeout 10))
|
2015-10-17 13:02:53 +02:00
|
|
|
|
"Like 'http-fetch', return an input port, but cache its contents in
|
2017-11-15 10:23:38 +01: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
|
2020-10-12 11:19:32 +02:00
|
|
|
|
URI.
|
|
|
|
|
|
2022-05-18 20:10:53 +02:00
|
|
|
|
HEADERS is an alist of extra HTTP headers, to which cache-related headers are
|
|
|
|
|
added automatically as appropriate.
|
|
|
|
|
|
2021-03-17 15:04:56 +01:00
|
|
|
|
TIMEOUT specifies the timeout in seconds for connection establishment.
|
|
|
|
|
|
|
|
|
|
Write information about redirects to LOG-PORT."
|
2022-05-18 20:10:54 +02:00
|
|
|
|
(let* ((uri (if (string? uri)
|
|
|
|
|
(string->uri uri)
|
|
|
|
|
uri))
|
|
|
|
|
(file (cache-file-for-uri uri)))
|
2017-09-19 11:49:29 +02:00
|
|
|
|
(define (update-cache cache-port)
|
|
|
|
|
(define cache-time
|
|
|
|
|
(and cache-port
|
|
|
|
|
(stat:mtime (stat cache-port))))
|
|
|
|
|
|
2022-05-18 20:10:53 +02:00
|
|
|
|
(define extended-headers
|
|
|
|
|
(if cache-time
|
|
|
|
|
`((if-modified-since
|
|
|
|
|
. ,(time-utc->date (make-time time-utc 0 cache-time)))
|
|
|
|
|
,@headers)
|
|
|
|
|
headers))
|
2017-09-19 11:49:29 +02:00
|
|
|
|
|
2015-10-17 13:02:53 +02:00
|
|
|
|
;; Update the cache and return an input port.
|
2017-09-19 11:49:29 +02:00
|
|
|
|
(guard (c ((http-get-error? c)
|
|
|
|
|
(if (= 304 (http-get-error-code c)) ;"Not Modified"
|
2017-09-26 00:32:12 +02:00
|
|
|
|
(begin
|
|
|
|
|
(utime file) ;update FILE's mtime
|
|
|
|
|
cache-port)
|
2017-09-19 11:49:29 +02:00
|
|
|
|
(raise c))))
|
|
|
|
|
(let ((port (http-fetch uri #:text? text?
|
2021-03-17 15:04:56 +01:00
|
|
|
|
#:log-port log-port
|
2022-05-18 20:10:53 +02:00
|
|
|
|
#:headers extended-headers
|
|
|
|
|
#:timeout timeout)))
|
2017-11-15 10:23:38 +01:00
|
|
|
|
(cache-miss uri)
|
2017-09-19 11:49:29 +02:00
|
|
|
|
(mkdir-p (dirname file))
|
|
|
|
|
(when cache-port
|
|
|
|
|
(close-port cache-port))
|
|
|
|
|
(with-atomic-file-output file
|
2017-11-15 10:23:38 +01:00
|
|
|
|
(cut write-cache port <>))
|
2017-09-19 11:49:29 +02:00
|
|
|
|
(close-port port)
|
|
|
|
|
(open-input-file file))))
|
2015-10-17 13:02:53 +02: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 11:49:29 +02:00
|
|
|
|
(update-cache port)
|
2015-10-17 13:02:53 +02:00
|
|
|
|
port)))
|
|
|
|
|
(lambda args
|
|
|
|
|
(if (= ENOENT (system-error-errno args))
|
2017-09-19 11:49:29 +02:00
|
|
|
|
(update-cache #f)
|
2015-10-17 13:02:53 +02:00
|
|
|
|
(apply throw args))))))
|
|
|
|
|
|
2013-07-14 16:35:37 +02:00
|
|
|
|
;;; http-client.scm ends here
|