add (ice-9 binary-ports)
[bpt/guile.git] / module / web / response.scm
CommitLineData
a9eeb2f4
AW
1;;; HTTP response objects
2
be1be3e5 3;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
a9eeb2f4
AW
4
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.
9;;
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.
14;;
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
18;; 02110-1301 USA
19
20;;; Code:
21
22(define-module (web response)
23 #:use-module (rnrs bytevectors)
24 #:use-module (rnrs io ports)
25 #:use-module (ice-9 rdelim)
26 #:use-module (srfi srfi-9)
27 #:use-module (web http)
28 #:export (response?
29 response-version
30 response-code
31 response-reason-phrase
32 response-headers
33 response-port
34 read-response
35 build-response
c6371902 36 adapt-response-version
a9eeb2f4
AW
37 write-response
38
3475fbb5
AW
39 read-response-body
40 write-response-body
a9eeb2f4
AW
41
42 ;; General headers
43 ;;
44 response-cache-control
45 response-connection
46 response-date
47 response-pragma
48 response-trailer
49 response-transfer-encoding
50 response-upgrade
51 response-via
52 response-warning
53
54 ;; Entity headers
55 ;;
56 response-allow
57 response-content-encoding
58 response-content-language
59 response-content-length
60 response-content-location
61 response-content-md5
62 response-content-range
63 response-content-type
64 response-expires
65 response-last-modified
66
67 ;; Response headers
68 ;;
69 response-accept-ranges
70 response-age
71 response-etag
72 response-location
73 response-proxy-authenticate
74 response-retry-after
75 response-server
76 response-vary
77 response-www-authenticate))
78
79
80(define-record-type <response>
81 (make-response version code reason-phrase headers port)
82 response?
83 (version response-version)
84 (code response-code)
85 (reason-phrase %response-reason-phrase)
86 (headers response-headers)
87 (port response-port))
88
89(define (bad-response message . args)
90 (throw 'bad-response message args))
91
e2d4bfea
AW
92(define (non-negative-integer? n)
93 (and (number? n) (>= n 0) (exact? n) (integer? n)))
94
95(define (validate-headers headers)
96 (if (pair? headers)
97 (let ((h (car headers)))
98 (if (pair? h)
99 (let ((k (car h)) (v (cdr h)))
be1be3e5
AW
100 (if (valid-header? k v)
101 (validate-headers (cdr headers))
102 (bad-response "Bad value for header ~a: ~s" k v)))
e2d4bfea
AW
103 (bad-response "Header not a pair: ~a" h)))
104 (if (not (null? headers))
105 (bad-response "Headers not a list: ~a" headers))))
106
a9eeb2f4 107(define* (build-response #:key (version '(1 . 1)) (code 200) reason-phrase
e2d4bfea 108 (headers '()) port (validate-headers? #t))
cc1e26c2
AW
109 "Construct an HTTP response object. If @var{validate-headers?} is true,
110the headers are each run through their respective validators."
e2d4bfea
AW
111 (cond
112 ((not (and (pair? version)
113 (non-negative-integer? (car version))
114 (non-negative-integer? (cdr version))))
115 (bad-response "Bad version: ~a" version))
116 ((not (and (non-negative-integer? code) (< code 600)))
117 (bad-response "Bad code: ~a" code))
118 ((and reason-phrase (not (string? reason-phrase)))
119 (bad-response "Bad reason phrase" reason-phrase))
120 (else
121 (if validate-headers?
122 (validate-headers headers))))
a9eeb2f4
AW
123 (make-response version code reason-phrase headers port))
124
125(define *reason-phrases*
126 '((100 . "Continue")
127 (101 . "Switching Protocols")
128 (200 . "OK")
129 (201 . "Created")
130 (202 . "Accepted")
131 (203 . "Non-Authoritative Information")
132 (204 . "No Content")
133 (205 . "Reset Content")
134 (206 . "Partial Content")
135 (300 . "Multiple Choices")
136 (301 . "Moved Permanently")
137 (302 . "Found")
138 (303 . "See Other")
139 (304 . "Not Modified")
140 (305 . "Use Proxy")
141 (307 . "Temporary Redirect")
142 (400 . "Bad Request")
143 (401 . "Unauthorized")
144 (402 . "Payment Required")
145 (403 . "Forbidden")
146 (404 . "Not Found")
147 (405 . "Method Not Allowed")
148 (406 . "Not Acceptable")
149 (407 . "Proxy Authentication Required")
150 (408 . "Request Timeout")
151 (409 . "Conflict")
152 (410 . "Gone")
153 (411 . "Length Required")
154 (412 . "Precondition Failed")
155 (413 . "Request Entity Too Large")
156 (414 . "Request-URI Too Long")
157 (415 . "Unsupported Media Type")
158 (416 . "Requested Range Not Satisfiable")
159 (417 . "Expectation Failed")
160 (500 . "Internal Server Error")
161 (501 . "Not Implemented")
162 (502 . "Bad Gateway")
163 (503 . "Service Unavailable")
164 (504 . "Gateway Timeout")
165 (505 . "HTTP Version Not Supported")))
166
167(define (code->reason-phrase code)
168 (or (assv-ref *reason-phrases* code)
169 "(Unknown)"))
170
171(define (response-reason-phrase response)
cc1e26c2
AW
172 "Return the reason phrase given in @var{response}, or the standard
173reason phrase for the response's code."
a9eeb2f4
AW
174 (or (%response-reason-phrase response)
175 (code->reason-phrase (response-code response))))
176
177(define (read-response port)
f944ee8f 178 "Read an HTTP response from @var{port}.
cc1e26c2
AW
179
180As a side effect, sets the encoding on @var{port} to
181ISO-8859-1 (latin-1), so that reading one character reads one byte. See
182the discussion of character sets in \"HTTP Responses\" in the manual,
183for more information."
a9eeb2f4
AW
184 (set-port-encoding! port "ISO-8859-1")
185 (call-with-values (lambda () (read-response-line port))
186 (lambda (version code reason-phrase)
187 (make-response version code reason-phrase (read-headers port) port))))
188
c6371902 189(define (adapt-response-version response version)
cc1e26c2
AW
190 "Adapt the given response to a different HTTP version. Returns a new
191HTTP response.
192
193The idea is that many applications might just build a response for the
194default HTTP version, and this method could handle a number of
195programmatic transformations to respond to older HTTP versions (0.9 and
1961.0). But currently this function is a bit heavy-handed, just updating
197the version field."
c6371902
AW
198 (build-response #:code (response-code response)
199 #:version version
200 #:headers (response-headers response)
201 #:port (response-port response)))
202
a9eeb2f4 203(define (write-response r port)
cc1e26c2
AW
204 "Write the given HTTP response to @var{port}.
205
206Returns a new response, whose @code{response-port} will continue writing
207on @var{port}, perhaps using some transfer encoding."
a9eeb2f4
AW
208 (write-response-line (response-version r) (response-code r)
209 (response-reason-phrase r) port)
210 (write-headers (response-headers r) port)
211 (display "\r\n" port)
212 (if (eq? port (response-port r))
213 r
214 (make-response (response-version r) (response-code r)
215 (response-reason-phrase r) (response-headers r) port)))
216
3475fbb5 217(define (read-response-body r)
cc1e26c2
AW
218 "Reads the response body from @var{r}, as a bytevector. Returns
219@code{#f} if there was no response body."
a9eeb2f4
AW
220 (let ((nbytes (response-content-length r)))
221 (and nbytes
222 (let ((bv (get-bytevector-n (response-port r) nbytes)))
223 (if (= (bytevector-length bv) nbytes)
224 bv
225 (bad-response "EOF while reading response body: ~a bytes of ~a"
226 (bytevector-length bv) nbytes))))))
227
3475fbb5 228(define (write-response-body r bv)
cc1e26c2
AW
229 "Write @var{body}, a bytevector, to the port corresponding to the HTTP
230response @var{r}."
a9eeb2f4
AW
231 (put-bytevector (response-port r) bv))
232
233(define-syntax define-response-accessor
234 (lambda (x)
235 (syntax-case x ()
236 ((_ field)
237 #'(define-response-accessor field #f))
238 ((_ field def) (identifier? #'field)
239 #`(define* (#,(datum->syntax
240 #'field
241 (symbol-append 'response- (syntax->datum #'field)))
242 response
243 #:optional (default def))
244 (cond
245 ((assq 'field (response-headers response)) => cdr)
246 (else default)))))))
247
248;; General headers
249;;
250(define-response-accessor cache-control '())
251(define-response-accessor connection '())
252(define-response-accessor date #f)
253(define-response-accessor pragma '())
254(define-response-accessor trailer '())
255(define-response-accessor transfer-encoding '())
256(define-response-accessor upgrade '())
257(define-response-accessor via '())
258(define-response-accessor warning '())
259
260;; Entity headers
261;;
262(define-response-accessor allow '())
263(define-response-accessor content-encoding '())
264(define-response-accessor content-language '())
265(define-response-accessor content-length #f)
266(define-response-accessor content-location #f)
267(define-response-accessor content-md5 #f)
268(define-response-accessor content-range #f)
269(define-response-accessor content-type #f)
270(define-response-accessor expires #f)
271(define-response-accessor last-modified #f)
272
273;; Response headers
274;;
275(define-response-accessor accept-ranges #f)
276(define-response-accessor age #f)
277(define-response-accessor etag #f)
278(define-response-accessor location #f)
279(define-response-accessor proxy-authenticate #f)
280(define-response-accessor retry-after #f)
281(define-response-accessor server #f)
282(define-response-accessor vary '())
283(define-response-accessor www-authenticate #f)