Improve correctness and consistency of 'eval-when' usage.
[bpt/guile.git] / module / web / server.scm
1 ;;; Web server
2
3 ;; Copyright (C) 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
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 ;;; Commentary:
21 ;;;
22 ;;; (web server) is a generic web server interface, along with a main
23 ;;; loop implementation for web servers controlled by Guile.
24 ;;;
25 ;;; The lowest layer is the <server-impl> object, which defines a set of
26 ;;; hooks to open a server, read a request from a client, write a
27 ;;; response to a client, and close a server. These hooks -- open,
28 ;;; read, write, and close, respectively -- are bound together in a
29 ;;; <server-impl> object. Procedures in this module take a
30 ;;; <server-impl> object, if needed.
31 ;;;
32 ;;; A <server-impl> may also be looked up by name. If you pass the
33 ;;; `http' symbol to `run-server', Guile looks for a variable named
34 ;;; `http' in the `(web server http)' module, which should be bound to a
35 ;;; <server-impl> object. Such a binding is made by instantiation of
36 ;;; the `define-server-impl' syntax. In this way the run-server loop can
37 ;;; automatically load other backends if available.
38 ;;;
39 ;;; The life cycle of a server goes as follows:
40 ;;;
41 ;;; * The `open' hook is called, to open the server. `open' takes 0 or
42 ;;; more arguments, depending on the backend, and returns an opaque
43 ;;; server socket object, or signals an error.
44 ;;;
45 ;;; * The `read' hook is called, to read a request from a new client.
46 ;;; The `read' hook takes one arguments, the server socket. It
47 ;;; should return three values: an opaque client socket, the
48 ;;; request, and the request body. The request should be a
49 ;;; `<request>' object, from `(web request)'. The body should be a
50 ;;; string or a bytevector, or `#f' if there is no body.
51 ;;;
52 ;;; If the read failed, the `read' hook may return #f for the client
53 ;;; socket, request, and body.
54 ;;;
55 ;;; * A user-provided handler procedure is called, with the request
56 ;;; and body as its arguments. The handler should return two
57 ;;; values: the response, as a `<response>' record from `(web
58 ;;; response)', and the response body as a string, bytevector, or
59 ;;; `#f' if not present. We also allow the reponse to be simply an
60 ;;; alist of headers, in which case a default response object is
61 ;;; constructed with those headers.
62 ;;;
63 ;;; * The `write' hook is called with three arguments: the client
64 ;;; socket, the response, and the body. The `write' hook returns no
65 ;;; values.
66 ;;;
67 ;;; * At this point the request handling is complete. For a loop, we
68 ;;; loop back and try to read a new request.
69 ;;;
70 ;;; * If the user interrupts the loop, the `close' hook is called on
71 ;;; the server socket.
72 ;;;
73 ;;; Code:
74
75 (define-module (web server)
76 #:use-module (srfi srfi-9)
77 #:use-module (srfi srfi-9 gnu)
78 #:use-module (rnrs bytevectors)
79 #:use-module (ice-9 binary-ports)
80 #:use-module (web request)
81 #:use-module (web response)
82 #:use-module (system repl error-handling)
83 #:use-module (ice-9 control)
84 #:use-module (ice-9 iconv)
85 #:export (define-server-impl
86 lookup-server-impl
87 open-server
88 read-client
89 handle-request
90 sanitize-response
91 write-client
92 close-server
93 serve-one-client
94 run-server))
95
96 (define *timer* (gettimeofday))
97 (define (print-elapsed who)
98 (let ((t (gettimeofday)))
99 (pk who (+ (* (- (car t) (car *timer*)) 1000000)
100 (- (cdr t) (cdr *timer*))))
101 (set! *timer* t)))
102
103 (eval-when (expand)
104 (define *time-debug?* #f))
105
106 (define-syntax debug-elapsed
107 (lambda (x)
108 (syntax-case x ()
109 ((_ who)
110 (if *time-debug?*
111 #'(print-elapsed who)
112 #'*unspecified*)))))
113
114 (define-record-type server-impl
115 (make-server-impl name open read write close)
116 server-impl?
117 (name server-impl-name)
118 (open server-impl-open)
119 (read server-impl-read)
120 (write server-impl-write)
121 (close server-impl-close))
122
123 (define-syntax-rule (define-server-impl name open read write close)
124 (define name
125 (make-server-impl 'name open read write close)))
126
127 (define (lookup-server-impl impl)
128 "Look up a server implementation. If IMPL is a server
129 implementation already, it is returned directly. If it is a symbol, the
130 binding named IMPL in the ‘(web server IMPL)’ module is
131 looked up. Otherwise an error is signaled.
132
133 Currently a server implementation is a somewhat opaque type, useful only
134 for passing to other procedures in this module, like
135 ‘read-client’."
136 (cond
137 ((server-impl? impl) impl)
138 ((symbol? impl)
139 (let ((impl (module-ref (resolve-module `(web server ,impl)) impl)))
140 (if (server-impl? impl)
141 impl
142 (error "expected a server impl in module" `(web server ,impl)))))
143 (else
144 (error "expected a server-impl or a symbol" impl))))
145
146 ;; -> server
147 (define (open-server impl open-params)
148 "Open a server for the given implementation. Return one value, the
149 new server object. The implementation's ‘open’ procedure is
150 applied to OPEN-PARAMS, which should be a list."
151 (apply (server-impl-open impl) open-params))
152
153 ;; -> (client request body | #f #f #f)
154 (define (read-client impl server)
155 "Read a new client from SERVER, by applying the implementation's
156 ‘read’ procedure to the server. If successful, return three
157 values: an object corresponding to the client, a request object, and the
158 request body. If any exception occurs, return ‘#f’ for all three
159 values."
160 (call-with-error-handling
161 (lambda ()
162 ((server-impl-read impl) server))
163 #:pass-keys '(quit interrupt)
164 #:on-error (if (batch-mode?) 'backtrace 'debug)
165 #:post-error (lambda _ (values #f #f #f))))
166
167 (define (extend-response r k v . additional)
168 (let ((r (set-field r (response-headers)
169 (assoc-set! (copy-tree (response-headers r))
170 k v))))
171 (if (null? additional)
172 r
173 (apply extend-response r additional))))
174
175 ;; -> response body
176 (define (sanitize-response request response body)
177 "\"Sanitize\" the given response and body, making them appropriate for
178 the given request.
179
180 As a convenience to web handler authors, RESPONSE may be given as
181 an alist of headers, in which case it is used to construct a default
182 response. Ensures that the response version corresponds to the request
183 version. If BODY is a string, encodes the string to a bytevector,
184 in an encoding appropriate for RESPONSE. Adds a
185 ‘content-length’ and ‘content-type’ header, as necessary.
186
187 If BODY is a procedure, it is called with a port as an argument,
188 and the output collected as a bytevector. In the future we might try to
189 instead use a compressing, chunk-encoded port, and call this procedure
190 later, in the write-client procedure. Authors are advised not to rely
191 on the procedure being called at any particular time."
192 (cond
193 ((list? response)
194 (sanitize-response request
195 (build-response #:version (request-version request)
196 #:headers response)
197 body))
198 ((not (equal? (request-version request) (response-version response)))
199 (sanitize-response request
200 (adapt-response-version response
201 (request-version request))
202 body))
203 ((not body)
204 (values response #vu8()))
205 ((string? body)
206 (let* ((type (response-content-type response
207 '(text/plain)))
208 (declared-charset (assq-ref (cdr type) 'charset))
209 (charset (or declared-charset "utf-8")))
210 (sanitize-response
211 request
212 (if declared-charset
213 response
214 (extend-response response 'content-type
215 `(,@type (charset . ,charset))))
216 (string->bytevector body charset))))
217 ((procedure? body)
218 (let* ((type (response-content-type response
219 '(text/plain)))
220 (declared-charset (assq-ref (cdr type) 'charset))
221 (charset (or declared-charset "utf-8")))
222 (sanitize-response
223 request
224 (if declared-charset
225 response
226 (extend-response response 'content-type
227 `(,@type (charset . ,charset))))
228 (call-with-encoded-output-string charset body))))
229 ((not (bytevector? body))
230 (error "unexpected body type"))
231 ((and (response-must-not-include-body? response)
232 body
233 ;; FIXME make this stricter: even an empty body should be prohibited.
234 (not (zero? (bytevector-length body))))
235 (error "response with this status code must not include body" response))
236 (else
237 ;; check length; assert type; add other required fields?
238 (values (let ((rlen (response-content-length response))
239 (blen (bytevector-length body)))
240 (cond
241 (rlen (if (= rlen blen)
242 response
243 (error "bad content-length" rlen blen)))
244 (else (extend-response response 'content-length blen))))
245 (if (eq? (request-method request) 'HEAD)
246 ;; Responses to HEAD requests must not include bodies.
247 ;; We could raise an error here, but it seems more
248 ;; appropriate to just do something sensible.
249 #f
250 body)))))
251
252 ;; -> response body state
253 (define (handle-request handler request body state)
254 "Handle a given request, returning the response and body.
255
256 The response and response body are produced by calling the given
257 HANDLER with REQUEST and BODY as arguments.
258
259 The elements of STATE are also passed to HANDLER as
260 arguments, and may be returned as additional values. The new
261 STATE, collected from the HANDLER's return values, is then
262 returned as a list. The idea is that a server loop receives a handler
263 from the user, along with whatever state values the user is interested
264 in, allowing the user's handler to explicitly manage its state."
265 (call-with-error-handling
266 (lambda ()
267 (call-with-values (lambda ()
268 (with-stack-and-prompt
269 (lambda ()
270 (apply handler request body state))))
271 (lambda (response body . state)
272 (call-with-values (lambda ()
273 (debug-elapsed 'handler)
274 (sanitize-response request response body))
275 (lambda (response body)
276 (debug-elapsed 'sanitize)
277 (values response body state))))))
278 #:pass-keys '(quit interrupt)
279 #:on-error (if (batch-mode?) 'backtrace 'debug)
280 #:post-error (lambda _
281 (values (build-response #:code 500) #f state))))
282
283 ;; -> unspecified values
284 (define (write-client impl server client response body)
285 "Write an HTTP response and body to CLIENT. If the server and
286 client support persistent connections, it is the implementation's
287 responsibility to keep track of the client thereafter, presumably by
288 attaching it to the SERVER argument somehow."
289 (call-with-error-handling
290 (lambda ()
291 ((server-impl-write impl) server client response body))
292 #:pass-keys '(quit interrupt)
293 #:on-error (if (batch-mode?) 'backtrace 'debug)
294 #:post-error (lambda _ (values))))
295
296 ;; -> unspecified values
297 (define (close-server impl server)
298 "Release resources allocated by a previous invocation of
299 ‘open-server’."
300 ((server-impl-close impl) server))
301
302 (define call-with-sigint
303 (if (not (provided? 'posix))
304 (lambda (thunk handler-thunk) (thunk))
305 (lambda (thunk handler-thunk)
306 (let ((handler #f))
307 (catch 'interrupt
308 (lambda ()
309 (dynamic-wind
310 (lambda ()
311 (set! handler
312 (sigaction SIGINT (lambda (sig) (throw 'interrupt)))))
313 thunk
314 (lambda ()
315 (if handler
316 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
317 (sigaction SIGINT (car handler) (cdr handler))
318 ;; restore original C handler.
319 (sigaction SIGINT #f)))))
320 (lambda (k . _) (handler-thunk)))))))
321
322 (define (with-stack-and-prompt thunk)
323 (call-with-prompt (default-prompt-tag)
324 (lambda () (start-stack #t (thunk)))
325 (lambda (k proc)
326 (with-stack-and-prompt (lambda () (proc k))))))
327
328 ;; -> new-state
329 (define (serve-one-client handler impl server state)
330 "Read one request from SERVER, call HANDLER on the request
331 and body, and write the response to the client. Return the new state
332 produced by the handler procedure."
333 (debug-elapsed 'serve-again)
334 (call-with-values
335 (lambda ()
336 (read-client impl server))
337 (lambda (client request body)
338 (debug-elapsed 'read-client)
339 (if client
340 (call-with-values
341 (lambda ()
342 (handle-request handler request body state))
343 (lambda (response body state)
344 (debug-elapsed 'handle-request)
345 (write-client impl server client response body)
346 (debug-elapsed 'write-client)
347 state))
348 state))))
349
350 (define* (run-server handler #:optional (impl 'http) (open-params '())
351 . state)
352 "Run Guile's built-in web server.
353
354 HANDLER should be a procedure that takes two or more arguments,
355 the HTTP request and request body, and returns two or more values, the
356 response and response body.
357
358 For example, here is a simple \"Hello, World!\" server:
359
360 @example
361 (define (handler request body)
362 (values '((content-type . (text/plain)))
363 \"Hello, World!\"))
364 (run-server handler)
365 @end example
366
367 The response and body will be run through ‘sanitize-response’
368 before sending back to the client.
369
370 Additional arguments to HANDLER are taken from
371 STATE. Additional return values are accumulated into a new
372 STATE, which will be used for subsequent requests. In this way a
373 handler can explicitly manage its state.
374
375 The default server implementation is ‘http’, which accepts
376 OPEN-PARAMS like ‘(#:port 8081)’, among others. See \"Web
377 Server\" in the manual, for more information."
378 (let* ((impl (lookup-server-impl impl))
379 (server (open-server impl open-params)))
380 (call-with-sigint
381 (lambda ()
382 (let lp ((state state))
383 (lp (serve-one-client handler impl server state))))
384 (lambda ()
385 (close-server impl server)
386 (values)))))