3 ;; Copyright (C) 2011, 2012, 2013 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 ;;; (web client) is a simple HTTP URL fetcher for Guile.
24 ;;; In its current incarnation, (web client) is synchronous. If you
25 ;;; want to fetch a number of URLs at once, probably the best thing to
26 ;;; do is to write an event-driven URL fetcher, similar in structure to
29 ;;; Another option, good but not as performant, would be to use threads,
30 ;;; possibly via a thread pool.
34 (define-module (web client)
35 #:use-module (rnrs bytevectors)
36 #:use-module (ice-9 binary-ports)
37 #:use-module (ice-9 iconv)
38 #:use-module (ice-9 rdelim)
39 #:use-module (web request)
40 #:use-module (web response)
41 #:use-module (web uri)
42 #:use-module (srfi srfi-1)
43 #:export (open-socket-for-uri
53 (define (ensure-uri uri-or-string)
55 ((string? uri-or-string) (string->uri uri-or-string))
56 ((uri? uri-or-string) uri-or-string)
57 (else (error "Invalid URI" uri-or-string))))
59 (define (open-socket-for-uri uri-or-string)
60 "Return an open input/output port for a connection to URI."
61 (define uri (ensure-uri uri-or-string))
63 (let ((port (uri-port uri)))
65 (getaddrinfo (uri-host uri)
66 (cond (port => number->string)
67 (else (symbol->string (uri-scheme uri))))
72 (equal? (addrinfo:addr ai1) (addrinfo:addr ai2))))))
74 (let loop ((addresses addresses))
75 (let* ((ai (car addresses))
76 (s (with-fluids ((%default-port-encoding #f))
77 ;; Restrict ourselves to TCP.
78 (socket (addrinfo:fam ai) SOCK_STREAM IPPROTO_IP))))
81 (connect s (addrinfo:addr ai))
83 ;; Buffer input and output on this port.
85 ;; Enlarge the receive buffer.
86 (setsockopt s SOL_SOCKET SO_RCVBUF (* 12 1024))
89 ;; Connection failed, so try one of the other addresses.
91 (if (null? (cdr addresses))
93 (loop (cdr addresses))))))))
95 (define (extend-request r k v . additional)
96 (let ((r (build-request (request-uri r) #:version (request-version r)
98 (assoc-set! (copy-tree (request-headers r))
100 #:port (request-port r))))
101 (if (null? additional)
103 (apply extend-request r additional))))
106 (define (sanitize-request request body)
107 "\"Sanitize\" the given request and body, ensuring that they are
108 complete and coherent. This method is most useful for methods that send
109 data to the server, like POST, but can be used for any method. Return
110 two values: a request and a bytevector, possibly the same ones that were
113 If BODY is a string, encodes the string to a bytevector, in an encoding
114 appropriate for REQUEST. Adds a ‘content-length’ and ‘content-type’
115 header, as necessary.
117 If BODY is a procedure, it is called with a port as an argument, and the
118 output collected as a bytevector. In the future we might try to instead
119 use a compressing, chunk-encoded port, and call this procedure later.
120 Authors are advised not to rely on the procedure being called at any
123 Note that we rely on the request itself already having been validated,
124 as is the case by default with a request returned by `build-request'."
127 (let ((length (request-content-length request)))
129 (unless (zero? length)
130 (error "content-length, but no body"))
131 (when (assq 'transfer-encoding (request-headers request))
132 (error "transfer-encoding not allowed with no body")))
133 (values request #vu8())))
135 (let* ((type (request-content-type request '(text/plain)))
136 (declared-charset (assq-ref (cdr type) 'charset))
137 (charset (or declared-charset "utf-8")))
141 (extend-request request 'content-type
142 `(,@type (charset . ,charset))))
143 (string->bytevector body charset))))
145 (let* ((type (request-content-type request
147 (declared-charset (assq-ref (cdr type) 'charset))
148 (charset (or declared-charset "utf-8")))
152 (extend-request request 'content-type
153 `(,@type (charset . ,charset))))
154 (call-with-encoded-output-string charset body))))
155 ((not (bytevector? body))
156 (error "unexpected body type"))
158 (values (let ((rlen (request-content-length request))
159 (blen (bytevector-length body)))
161 (rlen (if (= rlen blen)
163 (error "bad content-length" rlen blen)))
164 ((zero? blen) request)
165 (else (extend-request request 'content-length blen))))
168 (define (decode-response-body response body)
169 ;; `body' is either #f or a bytevector.
173 (let ((rlen (response-content-length response))
174 (blen (bytevector-length body)))
176 ((and rlen (not (= rlen blen)))
177 (error "bad content-length" rlen blen))
178 ((response-content-type response)
181 ((text-content-type? (car type))
182 ;; RFC 2616 3.7.1: "When no explicit charset parameter is
183 ;; provided by the sender, media subtypes of the "text"
184 ;; type are defined to have a default charset value of
185 ;; "ISO-8859-1" when received via HTTP."
186 (bytevector->string body (or (assq-ref (cdr type) 'charset)
191 (error "unexpected body type" body))))
193 ;; We could expose this to user code if there is demand.
194 (define* (request uri #:key
196 (port (open-socket-for-uri uri))
208 #:headers (if keep-alive?
210 (cons '(connection close) headers))
212 (call-with-values (lambda () (sanitize-request request body))
213 (lambda (request body)
214 (let ((request (write-request request port)))
216 (write-request-body request body))
217 (force-output (request-port request))
218 (let ((response (read-response port)))
220 ((equal? (request-method request) "HEAD")
223 (values response #f))
226 (response-body-port response
227 #:keep-alive? keep-alive?
228 #:decode? decode-body?)))
230 (let ((body (read-response-body response)))
235 (decode-response-body response body)
238 (define* (http-get uri #:key
240 (port (open-socket-for-uri uri))
241 (version '(1 . 1)) (keep-alive? #f)
242 ;; #:headers is the new name of #:extra-headers.
243 (extra-headers #f) (headers (or extra-headers '()))
244 (decode-body? #t) (streaming? #f))
245 "Connect to the server corresponding to URI and ask for the
246 resource, using the ‘GET’ method. If you already have a port open,
247 pass it as PORT. The port will be closed at the end of the
248 request unless KEEP-ALIVE? is true. Any extra headers in the
249 alist HEADERS will be added to the request.
251 If BODY is not #f, a message body will also be sent with the HTTP
252 request. If BODY is a string, it is encoded according to the
253 content-type in HEADERS, defaulting to UTF-8. Otherwise BODY should be
254 a bytevector, or #f for no body. Although it's allowed to send a
255 message body along with any request, usually only POST and PUT requests
256 have bodies. See ‘http-put’ and ‘http-post’ documentation, for more.
258 If DECODE-BODY? is true, as is the default, the body of the
259 response will be decoded to string, if it is a textual content-type.
260 Otherwise it will be returned as a bytevector.
262 However, if STREAMING? is true, instead of eagerly reading the response
263 body from the server, this function only reads off the headers. The
264 response body will be returned as a port on which the data may be read.
265 Unless KEEP-ALIVE? is true, the port will be closed after the full
266 response body has been read.
268 Returns two values: the response read from the server, and the response
269 body as a string, bytevector, #f value, or as a port (if STREAMING? is
272 (issue-deprecation-warning
273 "The #:extra-headers argument to http-get has been renamed to #:headers. "
274 "Please update your code."))
275 (request uri #:method "GET" #:body body
276 #:port port #:version version #:keep-alive? keep-alive?
277 #:headers headers #:decode-body? decode-body?
278 #:streaming? streaming?))
280 (define* (http-get* uri #:key
282 (port (open-socket-for-uri uri))
283 (version '(1 . 1)) (keep-alive? #f)
284 ;; #:headers is the new name of #:extra-headers.
285 (extra-headers #f) (headers (or extra-headers '()))
287 "Deprecated in favor of (http-get #:streaming? #t)."
289 (issue-deprecation-warning
290 "`http-get*' has been deprecated. "
291 "Instead, use `http-get' with the #:streaming? #t keyword argument."))
292 (http-get uri #:body body
293 #:port port #:version version #:keep-alive? keep-alive?
294 #:headers headers #:decode-body? #t #:streaming? #t))
296 (define-syntax-rule (define-http-verb http-verb method doc)
297 (define* (http-verb uri #:key
299 (port (open-socket-for-uri uri))
307 #:body body #:method method
308 #:port port #:version version #:keep-alive? keep-alive?
309 #:headers headers #:decode-body? decode-body?
310 #:streaming? streaming?)))
312 (define-http-verb http-head
314 "Fetch message headers for the given URI using the HTTP \"HEAD\"
317 This function is similar to ‘http-get’, except it uses the \"HEAD\"
318 method. See ‘http-get’ for full documentation on the various keyword
319 arguments that are accepted by this function.
321 Returns two values: the resulting response, and #f. Responses to HEAD
322 requests do not have a body. The second value is only returned so that
323 other procedures can treat all of the http-foo verbs identically.")
325 (define-http-verb http-post
327 "Post data to the given URI using the HTTP \"POST\" method.
329 This function is similar to ‘http-get’, except it uses the \"POST\"
330 method. See ‘http-get’ for full documentation on the various keyword
331 arguments that are accepted by this function.
333 Returns two values: the resulting response, and the response body.")
335 (define-http-verb http-put
337 "Put data at the given URI using the HTTP \"PUT\" method.
339 This function is similar to ‘http-get’, except it uses the \"PUT\"
340 method. See ‘http-get’ for full documentation on the various keyword
341 arguments that are accepted by this function.
343 Returns two values: the resulting response, and the response body.")
345 (define-http-verb http-delete
347 "Delete data at the given URI using the HTTP \"DELETE\" method.
349 This function is similar to ‘http-get’, except it uses the \"DELETE\"
350 method. See ‘http-get’ for full documentation on the various keyword
351 arguments that are accepted by this function.
353 Returns two values: the resulting response, and the response body.")
355 (define-http-verb http-trace
357 "Send an HTTP \"TRACE\" request.
359 This function is similar to ‘http-get’, except it uses the \"TRACE\"
360 method. See ‘http-get’ for full documentation on the various keyword
361 arguments that are accepted by this function.
363 Returns two values: the resulting response, and the response body.")
365 (define-http-verb http-options
367 "Query characteristics of an HTTP resource using the HTTP \"OPTIONS\"
370 This function is similar to ‘http-get’, except it uses the \"OPTIONS\"
371 method. See ‘http-get’ for full documentation on the various keyword
372 arguments that are accepted by this function.
374 Returns two values: the resulting response, and the response body.")