Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / module / web / server.scm
1 ;;; Web server
2
3 ;; Copyright (C) 2010, 2011, 2012 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 (rnrs bytevectors)
78 #:use-module (ice-9 binary-ports)
79 #:use-module (web request)
80 #:use-module (web response)
81 #:use-module (system repl error-handling)
82 #:use-module (ice-9 control)
83 #:export (define-server-impl
84 lookup-server-impl
85 open-server
86 read-client
87 handle-request
88 sanitize-response
89 write-client
90 close-server
91 serve-one-client
92 run-server))
93
94 (define *timer* (gettimeofday))
95 (define (print-elapsed who)
96 (let ((t (gettimeofday)))
97 (pk who (+ (* (- (car t) (car *timer*)) 1000000)
98 (- (cdr t) (cdr *timer*))))
99 (set! *timer* t)))
100
101 (eval-when (expand)
102 (define *time-debug?* #f))
103
104 (define-syntax debug-elapsed
105 (lambda (x)
106 (syntax-case x ()
107 ((_ who)
108 (if *time-debug?*
109 #'(print-elapsed who)
110 #'*unspecified*)))))
111
112 (define-record-type server-impl
113 (make-server-impl name open read write close)
114 server-impl?
115 (name server-impl-name)
116 (open server-impl-open)
117 (read server-impl-read)
118 (write server-impl-write)
119 (close server-impl-close))
120
121 (define-syntax-rule (define-server-impl name open read write close)
122 (define name
123 (make-server-impl 'name open read write close)))
124
125 (define (lookup-server-impl impl)
126 "Look up a server implementation. If IMPL is a server
127 implementation already, it is returned directly. If it is a symbol, the
128 binding named IMPL in the ‘(web server IMPL)’ module is
129 looked up. Otherwise an error is signaled.
130
131 Currently a server implementation is a somewhat opaque type, useful only
132 for passing to other procedures in this module, like
133 ‘read-client’."
134 (cond
135 ((server-impl? impl) impl)
136 ((symbol? impl)
137 (let ((impl (module-ref (resolve-module `(web server ,impl)) impl)))
138 (if (server-impl? impl)
139 impl
140 (error "expected a server impl in module" `(web server ,impl)))))
141 (else
142 (error "expected a server-impl or a symbol" impl))))
143
144 ;; -> server
145 (define (open-server impl open-params)
146 "Open a server for the given implementation. Return one value, the
147 new server object. The implementation's ‘open’ procedure is
148 applied to OPEN-PARAMS, which should be a list."
149 (apply (server-impl-open impl) open-params))
150
151 ;; -> (client request body | #f #f #f)
152 (define (read-client impl server)
153 "Read a new client from SERVER, by applying the implementation's
154 ‘read’ procedure to the server. If successful, return three
155 values: an object corresponding to the client, a request object, and the
156 request body. If any exception occurs, return ‘#f’ for all three
157 values."
158 (call-with-error-handling
159 (lambda ()
160 ((server-impl-read impl) server))
161 #:pass-keys '(quit interrupt)
162 #:on-error (if (batch-mode?) 'backtrace 'debug)
163 #:post-error (lambda _ (values #f #f #f))))
164
165 ;; like call-with-output-string, but actually closes the port (doh)
166 (define (call-with-output-string* proc)
167 (let ((port (open-output-string)))
168 (proc port)
169 (let ((str (get-output-string port)))
170 (close-port port)
171 str)))
172
173 (define (call-with-output-bytevector* proc)
174 (call-with-values
175 (lambda ()
176 (open-bytevector-output-port))
177 (lambda (port get-bytevector)
178 (proc port)
179 (let ((bv (get-bytevector)))
180 (close-port port)
181 bv))))
182
183 (define (call-with-encoded-output-string charset proc)
184 (if (string-ci=? charset "utf-8")
185 ;; I don't know why, but this appears to be faster; at least for
186 ;; examples/debug-sxml.scm (1464 reqs/s versus 850 reqs/s).
187 (string->utf8 (call-with-output-string* proc))
188 (call-with-output-bytevector*
189 (lambda (port)
190 (set-port-encoding! port charset)
191 (proc port)))))
192
193 (define (encode-string str charset)
194 (if (string-ci=? charset "utf-8")
195 (string->utf8 str)
196 (call-with-encoded-output-string charset
197 (lambda (port)
198 (display str port)))))
199
200 (define (extend-response r k v . additional)
201 (define (extend-alist alist k v)
202 (let ((pair (assq k alist)))
203 (acons k v (if pair (delq pair alist) alist))))
204 (let ((r (build-response #:version (response-version r)
205 #:code (response-code r)
206 #:headers
207 (extend-alist (response-headers r) k v)
208 #:port (response-port r))))
209 (if (null? additional)
210 r
211 (apply extend-response r additional))))
212
213 ;; -> response body
214 (define (sanitize-response request response body)
215 "\"Sanitize\" the given response and body, making them appropriate for
216 the given request.
217
218 As a convenience to web handler authors, RESPONSE may be given as
219 an alist of headers, in which case it is used to construct a default
220 response. Ensures that the response version corresponds to the request
221 version. If BODY is a string, encodes the string to a bytevector,
222 in an encoding appropriate for RESPONSE. Adds a
223 ‘content-length’ and ‘content-type’ header, as necessary.
224
225 If BODY is a procedure, it is called with a port as an argument,
226 and the output collected as a bytevector. In the future we might try to
227 instead use a compressing, chunk-encoded port, and call this procedure
228 later, in the write-client procedure. Authors are advised not to rely
229 on the procedure being called at any particular time."
230 (cond
231 ((list? response)
232 (sanitize-response request
233 (build-response #:version (request-version request)
234 #:headers response)
235 body))
236 ((not (equal? (request-version request) (response-version response)))
237 (sanitize-response request
238 (adapt-response-version response
239 (request-version request))
240 body))
241 ((not body)
242 (values response #vu8()))
243 ((string? body)
244 (let* ((type (response-content-type response
245 '(text/plain)))
246 (declared-charset (assq-ref (cdr type) 'charset))
247 (charset (or declared-charset "utf-8")))
248 (sanitize-response
249 request
250 (if declared-charset
251 response
252 (extend-response response 'content-type
253 `(,@type (charset . ,charset))))
254 (encode-string body charset))))
255 ((procedure? body)
256 (let* ((type (response-content-type response
257 '(text/plain)))
258 (declared-charset (assq-ref (cdr type) 'charset))
259 (charset (or declared-charset "utf-8")))
260 (sanitize-response
261 request
262 (if declared-charset
263 response
264 (extend-response response 'content-type
265 `(,@type (charset . ,charset))))
266 (call-with-encoded-output-string charset body))))
267 ((not (bytevector? body))
268 (error "unexpected body type"))
269 ((and (response-must-not-include-body? response)
270 body
271 (not (zero? (bytevector-length body))))
272 (error "response with this status code must not include body" response))
273 (else
274 ;; check length; assert type; add other required fields?
275 (values (let ((rlen (response-content-length response))
276 (blen (bytevector-length body)))
277 (cond
278 (rlen (if (= rlen blen)
279 response
280 (error "bad content-length" rlen blen)))
281 ((zero? blen) response)
282 (else (extend-response response 'content-length blen))))
283 (if (eq? (request-method request) 'HEAD)
284 ;; Responses to HEAD requests must not include bodies.
285 ;; We could raise an error here, but it seems more
286 ;; appropriate to just do something sensible.
287 #f
288 body)))))
289
290 ;; -> response body state
291 (define (handle-request handler request body state)
292 "Handle a given request, returning the response and body.
293
294 The response and response body are produced by calling the given
295 HANDLER with REQUEST and BODY as arguments.
296
297 The elements of STATE are also passed to HANDLER as
298 arguments, and may be returned as additional values. The new
299 STATE, collected from the HANDLER's return values, is then
300 returned as a list. The idea is that a server loop receives a handler
301 from the user, along with whatever state values the user is interested
302 in, allowing the user's handler to explicitly manage its state."
303 (call-with-error-handling
304 (lambda ()
305 (call-with-values (lambda ()
306 (with-stack-and-prompt
307 (lambda ()
308 (apply handler request body state))))
309 (lambda (response body . state)
310 (call-with-values (lambda ()
311 (debug-elapsed 'handler)
312 (sanitize-response request response body))
313 (lambda (response body)
314 (debug-elapsed 'sanitize)
315 (values response body state))))))
316 #:pass-keys '(quit interrupt)
317 #:on-error (if (batch-mode?) 'backtrace 'debug)
318 #:post-error (lambda _
319 (values (build-response #:code 500) #f state))))
320
321 ;; -> unspecified values
322 (define (write-client impl server client response body)
323 "Write an HTTP response and body to CLIENT. If the server and
324 client support persistent connections, it is the implementation's
325 responsibility to keep track of the client thereafter, presumably by
326 attaching it to the SERVER argument somehow."
327 (call-with-error-handling
328 (lambda ()
329 ((server-impl-write impl) server client response body))
330 #:pass-keys '(quit interrupt)
331 #:on-error (if (batch-mode?) 'backtrace 'debug)
332 #:post-error (lambda _ (values))))
333
334 ;; -> unspecified values
335 (define (close-server impl server)
336 "Release resources allocated by a previous invocation of
337 ‘open-server’."
338 ((server-impl-close impl) server))
339
340 (define call-with-sigint
341 (if (not (provided? 'posix))
342 (lambda (thunk handler-thunk) (thunk))
343 (lambda (thunk handler-thunk)
344 (let ((handler #f))
345 (catch 'interrupt
346 (lambda ()
347 (dynamic-wind
348 (lambda ()
349 (set! handler
350 (sigaction SIGINT (lambda (sig) (throw 'interrupt)))))
351 thunk
352 (lambda ()
353 (if handler
354 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
355 (sigaction SIGINT (car handler) (cdr handler))
356 ;; restore original C handler.
357 (sigaction SIGINT #f)))))
358 (lambda (k . _) (handler-thunk)))))))
359
360 (define (with-stack-and-prompt thunk)
361 (call-with-prompt (default-prompt-tag)
362 (lambda () (start-stack #t (thunk)))
363 (lambda (k proc)
364 (with-stack-and-prompt (lambda () (proc k))))))
365
366 ;; -> new-state
367 (define (serve-one-client handler impl server state)
368 "Read one request from SERVER, call HANDLER on the request
369 and body, and write the response to the client. Return the new state
370 produced by the handler procedure."
371 (debug-elapsed 'serve-again)
372 (call-with-values
373 (lambda ()
374 (read-client impl server))
375 (lambda (client request body)
376 (debug-elapsed 'read-client)
377 (if client
378 (call-with-values
379 (lambda ()
380 (handle-request handler request body state))
381 (lambda (response body state)
382 (debug-elapsed 'handle-request)
383 (write-client impl server client response body)
384 (debug-elapsed 'write-client)
385 state))
386 state))))
387
388 (define* (run-server handler #:optional (impl 'http) (open-params '())
389 . state)
390 "Run Guile's built-in web server.
391
392 HANDLER should be a procedure that takes two or more arguments,
393 the HTTP request and request body, and returns two or more values, the
394 response and response body.
395
396 For example, here is a simple \"Hello, World!\" server:
397
398 @example
399 (define (handler request body)
400 (values '((content-type . (text/plain)))
401 \"Hello, World!\"))
402 (run-server handler)
403 @end example
404
405 The response and body will be run through ‘sanitize-response’
406 before sending back to the client.
407
408 Additional arguments to HANDLER are taken from
409 STATE. Additional return values are accumulated into a new
410 STATE, which will be used for subsequent requests. In this way a
411 handler can explicitly manage its state.
412
413 The default server implementation is ‘http’, which accepts
414 OPEN-PARAMS like ‘(#:port 8081)’, among others. See \"Web
415 Server\" in the manual, for more information."
416 (let* ((impl (lookup-server-impl impl))
417 (server (open-server impl open-params)))
418 (call-with-sigint
419 (lambda ()
420 (let lp ((state state))
421 (lp (serve-one-client handler impl server state))))
422 (lambda ()
423 (close-server impl server)
424 (values)))))