2013-04-25 22:06:48 +02:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2015-05-06 10:31:11 +02:00
|
|
|
;;; Copyright © 2012, 2013, 2014, 2015 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.
|
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-27 11:37:31 +02:00
|
|
|
#:use-module (guix utils)
|
2013-04-25 22:06:48 +02:00
|
|
|
#:use-module (web uri)
|
2015-05-06 10:31:11 +02:00
|
|
|
#:use-module ((web client) #:hide (open-socket-for-uri))
|
2013-04-25 22:06:48 +02:00
|
|
|
#:use-module (web response)
|
|
|
|
#:use-module (srfi srfi-11)
|
2014-03-01 15:38:11 +01:00
|
|
|
#:use-module (srfi srfi-34)
|
|
|
|
#:use-module (srfi srfi-35)
|
2013-04-25 22:06:48 +02:00
|
|
|
#:use-module (rnrs io ports)
|
|
|
|
#:use-module (rnrs bytevectors)
|
|
|
|
#:use-module (guix ui)
|
|
|
|
#:use-module (guix utils)
|
2015-05-06 10:31:11 +02:00
|
|
|
#:use-module ((guix build download)
|
|
|
|
#:select (open-socket-for-uri resolve-uri-reference))
|
|
|
|
#: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
|
|
|
|
|
2013-06-29 22:10:06 +02:00
|
|
|
http-fetch))
|
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?
|
|
|
|
(uri http-get-error-uri) ; URI
|
|
|
|
(code http-get-error-code) ; integer
|
|
|
|
(reason http-get-error-reason)) ; string
|
|
|
|
|
|
|
|
|
2015-03-03 22:26:52 +01:00
|
|
|
(define-syntax when-guile<=2.0.5-or-otherwise-broken
|
2013-04-27 11:37:31 +02: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 22:26:52 +01:00
|
|
|
(when-guile<=2.0.5-or-otherwise-broken
|
2015-03-03 22:09:30 +01:00
|
|
|
;; Backport of Guile commits 312e79f8 ("Add HTTP Chunked Encoding support to
|
|
|
|
;; web modules.") and 00d3ecf2 ("http: Do not buffer HTTP chunks.")
|
2013-04-27 11:37:31 +02:00
|
|
|
|
|
|
|
(use-modules (ice-9 rdelim))
|
|
|
|
|
2015-03-03 22:26:52 +01:00
|
|
|
(define %web-http
|
|
|
|
(resolve-module '(web http)))
|
|
|
|
|
2013-04-27 11:37:31 +02:00
|
|
|
;; Chunked Responses
|
|
|
|
(define (read-chunk-header port)
|
|
|
|
(let* ((str (read-line port))
|
|
|
|
(extension-start (string-index str (lambda (c) (or (char=? c #\;)
|
|
|
|
(char=? c #\return)))))
|
|
|
|
(size (string->number (if extension-start ; unnecessary?
|
|
|
|
(substring str 0 extension-start)
|
|
|
|
str)
|
|
|
|
16)))
|
|
|
|
size))
|
|
|
|
|
|
|
|
(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 22:09:30 +01: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 11:37:31 +02:00
|
|
|
(define (read! bv idx to-read)
|
|
|
|
(define (loop to-read num-read)
|
|
|
|
(cond ((or finished? (zero? to-read))
|
|
|
|
num-read)
|
2015-03-03 22:09:30 +01: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 11:37:31 +02:00
|
|
|
(loop to-read 0))
|
2015-03-03 22:09:30 +01:00
|
|
|
|
2013-04-27 11:37:31 +02:00
|
|
|
(make-custom-binary-input-port "chunked input port" read! #f #f close))
|
|
|
|
|
2015-03-03 22:26:52 +01: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 21:38:52 +02: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 ()
|
|
|
|
(close port))))
|
|
|
|
|
|
|
|
(make-custom-binary-input-port "delimited input port" read! #f #f close))
|
|
|
|
|
|
|
|
(unless (guile-version>? "2.0.9")
|
|
|
|
;; Guile <= 2.0.9 had a bug whereby 'response-body-port' would read more
|
|
|
|
;; than what 'content-length' says. See Guile commit 802a25b.
|
|
|
|
(module-set! (resolve-module '(web response))
|
|
|
|
'make-delimited-input-port make-delimited-input-port))
|
|
|
|
|
2013-04-27 11:37:31 +02:00
|
|
|
(define (read-response-body* r)
|
|
|
|
"Reads the response body from @var{r}, as a bytevector. Returns
|
|
|
|
@code{#f} if there was no response body."
|
|
|
|
(define bad-response
|
|
|
|
(@@ (web response) bad-response))
|
|
|
|
|
|
|
|
(if (member '(chunked) (response-transfer-encoding r))
|
|
|
|
(let ((chunk-port (make-chunked-input-port (response-port r)
|
|
|
|
#:keep-alive? #t)))
|
|
|
|
(get-bytevector-all chunk-port))
|
|
|
|
(let ((nbytes (response-content-length r)))
|
2013-04-27 23:00:21 +02:00
|
|
|
;; Backport of Guile commit 84dfde82ae8f6ec247c1c147c1e2ae50b207bad9
|
|
|
|
;; ("fix response-body-port for responses without content-length").
|
|
|
|
(if nbytes
|
|
|
|
(let ((bv (get-bytevector-n (response-port r) nbytes)))
|
|
|
|
(if (= (bytevector-length bv) nbytes)
|
|
|
|
bv
|
|
|
|
(bad-response "EOF while reading response body: ~a bytes of ~a"
|
|
|
|
(bytevector-length bv) nbytes)))
|
|
|
|
(get-bytevector-all (response-port r))))))
|
2013-04-27 11:37:31 +02:00
|
|
|
|
|
|
|
;; Install this patch only on Guile 2.0.5.
|
2013-08-23 15:51:36 +02:00
|
|
|
(unless (guile-version>? "2.0.5")
|
2013-04-27 11:37:31 +02:00
|
|
|
(module-set! (resolve-module '(web response))
|
|
|
|
'read-response-body read-response-body*)))
|
|
|
|
|
2013-04-27 23:00:51 +02:00
|
|
|
;; XXX: Work around <http://bugs.gnu.org/13095>, present in Guile
|
|
|
|
;; up to 2.0.7.
|
|
|
|
(module-define! (resolve-module '(web client))
|
|
|
|
'shutdown (const #f))
|
2013-04-27 11:37:31 +02:00
|
|
|
|
2013-06-29 22:10:06 +02:00
|
|
|
(define* (http-fetch uri #:key port (text? #f) (buffered? #t))
|
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
|
2014-03-01 15:38:11 +01:00
|
|
|
unbuffered port, suitable for use in `filtered-port'.
|
|
|
|
|
|
|
|
Raise an '&http-get-error' condition if downloading fails."
|
2013-04-25 22:06:48 +02:00
|
|
|
(let loop ((uri uri))
|
2015-05-06 10:31:11 +02:00
|
|
|
(let ((port (or port (open-socket-for-uri uri))))
|
|
|
|
(unless buffered?
|
|
|
|
(setvbuf port _IONBF))
|
2013-06-29 22:10:06 +02:00
|
|
|
(let*-values (((resp data)
|
|
|
|
;; Try hard to use the API du jour to get an input port.
|
|
|
|
;; On Guile 2.0.5 and before, we can only get a string or
|
|
|
|
;; bytevector, and not an input port. Work around that.
|
2013-08-23 15:51:36 +02:00
|
|
|
(if (guile-version>? "2.0.7")
|
2013-06-29 22:10:06 +02:00
|
|
|
(http-get uri #:streaming? #t #:port port) ; 2.0.9+
|
|
|
|
(if (defined? 'http-get*)
|
|
|
|
(http-get* uri #:decode-body? text?
|
|
|
|
#:port port) ; 2.0.7
|
|
|
|
(http-get uri #:decode-body? text?
|
|
|
|
#:port port)))) ; 2.0.5-
|
|
|
|
((code)
|
|
|
|
(response-code resp)))
|
|
|
|
(case code
|
|
|
|
((200)
|
|
|
|
(let ((len (response-content-length resp)))
|
|
|
|
(cond ((not data)
|
|
|
|
(begin
|
|
|
|
;; Guile 2.0.5 and earlier did not support chunked
|
|
|
|
;; transfer encoding, which is required for instance when
|
|
|
|
;; fetching %PACKAGE-LIST-URL (see
|
|
|
|
;; <http://lists.gnu.org/archive/html/guile-devel/2011-09/msg00089.html>).
|
|
|
|
;; Normally the `when-guile<=2.0.5' block above fixes
|
|
|
|
;; that, but who knows what could happen.
|
|
|
|
(warning (_ "using Guile ~a, which does not support ~s encoding~%")
|
|
|
|
(version)
|
|
|
|
(response-transfer-encoding resp))
|
|
|
|
(leave (_ "download failed; use a newer Guile~%")
|
|
|
|
uri resp)))
|
|
|
|
((string? data) ; `http-get' from 2.0.5-
|
|
|
|
(values (open-input-string data) len))
|
|
|
|
((bytevector? data) ; likewise
|
|
|
|
(values (open-bytevector-input-port data) len))
|
|
|
|
(else ; input port
|
|
|
|
(values data len)))))
|
|
|
|
((301 ; moved permanently
|
|
|
|
302) ; found (redirection)
|
2015-02-19 01:33:10 +01:00
|
|
|
(let ((uri (resolve-uri-reference (response-location resp) uri)))
|
2013-06-29 22:10:06 +02:00
|
|
|
(close-port port)
|
|
|
|
(format #t (_ "following redirection to `~a'...~%")
|
|
|
|
(uri->string uri))
|
|
|
|
(loop uri)))
|
|
|
|
(else
|
2014-03-01 15:38:11 +01:00
|
|
|
(raise (condition (&http-get-error
|
|
|
|
(uri uri)
|
|
|
|
(code code)
|
|
|
|
(reason (response-reason-phrase resp)))
|
|
|
|
(&message
|
|
|
|
(message "download failed"))))))))))
|
2013-04-25 22:06:48 +02:00
|
|
|
|
2013-07-14 16:35:37 +02:00
|
|
|
;;; http-client.scm ends here
|