http-client.scm 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2012, 2015 Free Software Foundation, Inc.
  5. ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
  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. (define-module (guix http-client)
  22. #:use-module (web uri)
  23. #:use-module ((web client) #:hide (open-socket-for-uri))
  24. #:use-module (web response)
  25. #:use-module (srfi srfi-11)
  26. #:use-module (srfi srfi-19)
  27. #:use-module (srfi srfi-26)
  28. #:use-module (srfi srfi-34)
  29. #:use-module (srfi srfi-35)
  30. #:use-module (ice-9 match)
  31. #:use-module (ice-9 binary-ports)
  32. #:use-module (rnrs bytevectors)
  33. #:use-module (guix ui)
  34. #:use-module (guix utils)
  35. #:use-module (guix base64)
  36. #:autoload (gcrypt hash) (sha256)
  37. #:use-module ((guix build utils)
  38. #:select (mkdir-p dump-port))
  39. #:use-module ((guix build download)
  40. #:select (open-socket-for-uri
  41. (open-connection-for-uri
  42. . guix:open-connection-for-uri)
  43. resolve-uri-reference))
  44. #:re-export (open-socket-for-uri)
  45. #:export (&http-get-error
  46. http-get-error?
  47. http-get-error-uri
  48. http-get-error-code
  49. http-get-error-reason
  50. http-fetch
  51. %http-cache-ttl
  52. http-fetch/cached))
  53. ;;; Commentary:
  54. ;;;
  55. ;;; HTTP client portable among Guile versions, and with proper error condition
  56. ;;; reporting.
  57. ;;;
  58. ;;; Code:
  59. ;; HTTP GET error.
  60. (define-condition-type &http-get-error &error
  61. http-get-error?
  62. (uri http-get-error-uri) ; URI
  63. (code http-get-error-code) ; integer
  64. (reason http-get-error-reason)) ; string
  65. (define* (http-fetch uri #:key port (text? #f) (buffered? #t)
  66. (verify-certificate? #t)
  67. (headers '((user-agent . "GNU Guile"))))
  68. "Return an input port containing the data at URI, and the expected number of
  69. bytes available or #f. If TEXT? is true, the data at URI is considered to be
  70. textual. Follow any HTTP redirection. When BUFFERED? is #f, return an
  71. unbuffered port, suitable for use in `filtered-port'. HEADERS is an alist of
  72. extra HTTP headers.
  73. When VERIFY-CERTIFICATE? is true, verify HTTPS server certificates.
  74. Raise an '&http-get-error' condition if downloading fails."
  75. (let loop ((uri (if (string? uri)
  76. (string->uri uri)
  77. uri)))
  78. (let ((port (or port (guix:open-connection-for-uri uri
  79. #:verify-certificate?
  80. verify-certificate?)))
  81. (headers (match (uri-userinfo uri)
  82. ((? string? str)
  83. (cons (cons 'Authorization
  84. (string-append "Basic "
  85. (base64-encode
  86. (string->utf8 str))))
  87. headers))
  88. (_ headers))))
  89. (unless (or buffered? (not (file-port? port)))
  90. (setvbuf port 'none))
  91. (let*-values (((resp data)
  92. (http-get uri #:streaming? #t #:port port
  93. ;; XXX: When #:keep-alive? is true, if DATA is
  94. ;; a chunked-encoding port, closing DATA won't
  95. ;; close PORT, leading to a file descriptor
  96. ;; leak.
  97. #:keep-alive? #f
  98. #:headers headers))
  99. ((code)
  100. (response-code resp)))
  101. (case code
  102. ((200)
  103. (values data (response-content-length resp)))
  104. ((301 ; moved permanently
  105. 302 ; found (redirection)
  106. 303 ; see other
  107. 307 ; temporary redirection
  108. 308) ; permanent redirection
  109. (let ((uri (resolve-uri-reference (response-location resp) uri)))
  110. (close-port port)
  111. (format (current-error-port) (G_ "following redirection to `~a'...~%")
  112. (uri->string uri))
  113. (loop uri)))
  114. (else
  115. (raise (condition (&http-get-error
  116. (uri uri)
  117. (code code)
  118. (reason (response-reason-phrase resp)))
  119. (&message
  120. (message
  121. (format
  122. #f
  123. (G_ "~a: HTTP download failed: ~a (~s)")
  124. (uri->string uri) code
  125. (response-reason-phrase resp))))))))))))
  126. ;;;
  127. ;;; Caching.
  128. ;;;
  129. (define %http-cache-ttl
  130. ;; Time-to-live in seconds of the HTTP cache of in ~/.cache/guix.
  131. (make-parameter
  132. (* 3600 (or (and=> (getenv "GUIX_HTTP_CACHE_TTL")
  133. string->number*)
  134. 36))))
  135. (define (cache-file-for-uri uri)
  136. "Return the name of the file in the cache corresponding to URI."
  137. (let ((digest (sha256 (string->utf8 (uri->string uri)))))
  138. ;; Use the "URL" alphabet because it does not contain "/".
  139. (string-append (cache-directory) "/http/"
  140. (base64-encode digest 0 (bytevector-length digest)
  141. #f #f base64url-alphabet))))
  142. (define* (http-fetch/cached uri #:key (ttl (%http-cache-ttl)) text?
  143. (write-cache dump-port)
  144. (cache-miss (const #t)))
  145. "Like 'http-fetch', return an input port, but cache its contents in
  146. ~/.cache/guix. The cache remains valid for TTL seconds.
  147. Call WRITE-CACHE with the HTTP input port and the cache output port to write
  148. the data to cache. Call CACHE-MISS with URI just before fetching data from
  149. URI."
  150. (let ((file (cache-file-for-uri uri)))
  151. (define (update-cache cache-port)
  152. (define cache-time
  153. (and cache-port
  154. (stat:mtime (stat cache-port))))
  155. (define headers
  156. `((user-agent . "GNU Guile")
  157. ,@(if cache-time
  158. `((if-modified-since
  159. . ,(time-utc->date (make-time time-utc 0 cache-time))))
  160. '())))
  161. ;; Update the cache and return an input port.
  162. (guard (c ((http-get-error? c)
  163. (if (= 304 (http-get-error-code c)) ;"Not Modified"
  164. (begin
  165. (utime file) ;update FILE's mtime
  166. cache-port)
  167. (raise c))))
  168. (let ((port (http-fetch uri #:text? text?
  169. #:headers headers)))
  170. (cache-miss uri)
  171. (mkdir-p (dirname file))
  172. (when cache-port
  173. (close-port cache-port))
  174. (with-atomic-file-output file
  175. (cut write-cache port <>))
  176. (close-port port)
  177. (open-input-file file))))
  178. (define (old? port)
  179. ;; Return true if PORT has passed TTL.
  180. (let* ((s (stat port))
  181. (now (current-time time-utc)))
  182. (< (+ (stat:mtime s) ttl) (time-second now))))
  183. (catch 'system-error
  184. (lambda ()
  185. (let ((port (open-input-file file)))
  186. (if (old? port)
  187. (update-cache port)
  188. port)))
  189. (lambda args
  190. (if (= ENOENT (system-error-errno args))
  191. (update-cache #f)
  192. (apply throw args))))))
  193. ;;; http-client.scm ends here