3 ;; Copyright (C) 2011, 2012 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 rdelim)
38 #:use-module (web request)
39 #:use-module (web response)
40 #:use-module (web uri)
41 #:export (open-socket-for-uri
45 (define (open-socket-for-uri uri)
46 "Return an open input/output port for a connection to URI."
48 (let ((port (uri-port uri)))
49 (getaddrinfo (uri-host uri)
50 (cond (port => number->string)
51 (else (symbol->string (uri-scheme uri))))
56 (let loop ((addresses addresses))
57 (let* ((ai (car addresses))
58 (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)
59 (addrinfo:protocol ai))))
60 (set-port-encoding! s "ISO-8859-1")
64 (connect s (addrinfo:addr ai))
66 ;; Buffer input and output on this port.
68 ;; Enlarge the receive buffer.
69 (setsockopt s SOL_SOCKET SO_RCVBUF (* 12 1024))
72 ;; Connection failed, so try one of the other addresses.
76 (loop (cdr addresses))))))))
78 (define (decode-string bv encoding)
79 (if (string-ci=? encoding "utf-8")
81 (let ((p (open-bytevector-input-port bv)))
82 (set-port-encoding! p encoding)
83 (let ((res (read-delimited "" p)))
87 ;; Logically the inverse of (web server)'s `sanitize-response'.
89 (define (decode-response-body response body)
90 ;; `body' is either #f or a bytevector.
94 (let ((rlen (response-content-length response))
95 (blen (bytevector-length body)))
97 ((and rlen (not (= rlen blen)))
98 (error "bad content-length" rlen blen))
99 ((response-content-type response)
102 ((text-content-type? (car type))
103 (decode-string body (or (assq-ref (cdr type) 'charset)
108 (error "unexpected body type" body))))
110 (define* (http-get uri #:key (port (open-socket-for-uri uri))
111 (version '(1 . 1)) (keep-alive? #f) (extra-headers '())
113 "Connect to the server corresponding to URI and ask for the
114 resource, using the ‘GET’ method. If you already have a port open,
115 pass it as PORT. The port will be closed at the end of the
116 request unless KEEP-ALIVE? is true. Any extra headers in the
117 alist EXTRA-HEADERS will be added to the request.
119 If DECODE-BODY? is true, as is the default, the body of the
120 response will be decoded to string, if it is a textual content-type.
121 Otherwise it will be returned as a bytevector."
122 (let ((req (build-request uri #:version version
123 #:headers (if keep-alive?
125 (cons '(connection close)
127 (write-request req port)
129 (if (not keep-alive?)
131 (let* ((res (read-response port))
132 (body (read-response-body res)))
133 (if (not keep-alive?)
137 (decode-response-body res body)
140 (define* (http-get* uri #:key (port (open-socket-for-uri uri))
141 (version '(1 . 1)) (keep-alive? #f) (extra-headers '())
143 "Like ‘http-get’, but return an input port from which to read. When
144 DECODE-BODY? is true, as is the default, the returned port has its
145 encoding set appropriately if the data at URI is textual. Closing the
146 returned port closes PORT, unless KEEP-ALIVE? is true."
147 (let ((req (build-request uri #:version version
148 #:headers (if keep-alive?
150 (cons '(connection close)
152 (write-request req port)
156 (let* ((res (read-response port))
157 (body (response-body-port res
158 #:keep-alive? keep-alive?
159 #:decode? decode-body?)))