more define-syntax-rule usage
[bpt/guile.git] / module / web / server.scm
CommitLineData
79ef79ee
AW
1;;; Web server
2
0acc595b 3;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
79ef79ee
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;;; 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.
462a1a04
AW
46;;; The `read' hook takes one arguments, the server socket. It
47;;; should return three values: an opaque client socket, the
79ef79ee
AW
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;;;
79ef79ee
AW
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
462a1a04
AW
64;;; socket, the response, and the body. The `write' hook returns no
65;;; values.
79ef79ee
AW
66;;;
67;;; * At this point the request handling is complete. For a loop, we
462a1a04 68;;; loop back and try to read a new request.
79ef79ee
AW
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)
6854c324 78 #:use-module (ice-9 binary-ports)
79ef79ee
AW
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
8bf6cfea
AW
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
79ef79ee
AW
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
0c65f52c
AW
121(define-syntax-rule (define-server-impl name open read write close)
122 (define name
123 (make-server-impl 'name open read write close)))
79ef79ee
AW
124
125(define (lookup-server-impl impl)
43d6659a
AW
126 "Look up a server implementation. If @var{impl} is a server
127implementation already, it is returned directly. If it is a symbol, the
128binding named @var{impl} in the @code{(web server @var{impl})} module is
129looked up. Otherwise an error is signaled.
130
131Currently a server implementation is a somewhat opaque type, useful only
132for passing to other procedures in this module, like
133@code{read-client}."
79ef79ee
AW
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)
43d6659a
AW
146 "Open a server for the given implementation. Returns one value, the
147new server object. The implementation's @code{open} procedure is
148applied to @var{open-params}, which should be a list."
79ef79ee
AW
149 (apply (server-impl-open impl) open-params))
150
462a1a04
AW
151;; -> (client request body | #f #f #f)
152(define (read-client impl server)
43d6659a
AW
153 "Read a new client from @var{server}, by applying the implementation's
154@code{read} procedure to the server. If successful, returns three
155values: an object corresponding to the client, a request object, and the
156request body. If any exception occurs, returns @code{#f} for all three
157values."
79ef79ee
AW
158 (call-with-error-handling
159 (lambda ()
462a1a04 160 ((server-impl-read impl) server))
79ef79ee
AW
161 #:pass-keys '(quit interrupt)
162 #:on-error (if (batch-mode?) 'pass 'debug)
163 #:post-error
164 (lambda (k . args)
165 (warn "Error while accepting client" k args)
462a1a04 166 (values #f #f #f))))
79ef79ee 167
a964aa62
AW
168;; like call-with-output-string, but actually closes the port (doh)
169(define (call-with-output-string* proc)
170 (let ((port (open-output-string)))
171 (proc port)
172 (let ((str (get-output-string port)))
173 (close-port port)
174 str)))
175
176(define (call-with-output-bytevector* proc)
177 (call-with-values
178 (lambda ()
179 (open-bytevector-output-port))
180 (lambda (port get-bytevector)
181 (proc port)
182 (let ((bv (get-bytevector)))
183 (close-port port)
184 bv))))
185
af0da6eb 186(define (call-with-encoded-output-string charset proc)
998191fd 187 (if (string-ci=? charset "utf-8")
af0da6eb 188 ;; I don't know why, but this appears to be faster; at least for
998191fd 189 ;; examples/debug-sxml.scm (1464 reqs/s versus 850 reqs/s).
a964aa62
AW
190 (string->utf8 (call-with-output-string* proc))
191 (call-with-output-bytevector*
192 (lambda (port)
193 (set-port-encoding! port charset)
194 (proc port)))))
af0da6eb 195
d9f00c3d 196(define (encode-string str charset)
af0da6eb
AW
197 (if (string-ci=? charset "utf-8")
198 (string->utf8 str)
199 (call-with-encoded-output-string charset
200 (lambda (port)
201 (display str port)))))
d9f00c3d 202
f944ee8f
AW
203(define (extend-response r k v . additional)
204 (let ((r (build-response #:version (response-version r)
205 #:code (response-code r)
206 #:headers
207 (assoc-set! (copy-tree (response-headers r))
208 k v)
209 #:port (response-port r))))
210 (if (null? additional)
211 r
212 (apply extend-response r additional))))
213
79ef79ee
AW
214;; -> response body
215(define (sanitize-response request response body)
43d6659a
AW
216 "\"Sanitize\" the given response and body, making them appropriate for
217the given request.
218
219As a convenience to web handler authors, @var{response} may be given as
220an alist of headers, in which case it is used to construct a default
221response. Ensures that the response version corresponds to the request
222version. If @var{body} is a string, encodes the string to a bytevector,
223in an encoding appropriate for @var{response}. Adds a
224@code{content-length} and @code{content-type} header, as necessary.
225
226If @var{body} is a procedure, it is called with a port as an argument,
227and the output collected as a bytevector. In the future we might try to
228instead use a compressing, chunk-encoded port, and call this procedure
229later, in the write-client procedure. Authors are advised not to rely
230on the procedure being called at any particular time."
d9f00c3d
AW
231 (cond
232 ((list? response)
c6371902
AW
233 (sanitize-response request
234 (build-response #:version (request-version request)
235 #:headers response)
236 body))
237 ((not (equal? (request-version request) (response-version response)))
238 (sanitize-response request
239 (adapt-response-version response
240 (request-version request))
241 body))
a4342ba8
AW
242 ((not body)
243 (values response #vu8()))
d9f00c3d
AW
244 ((string? body)
245 (let* ((type (response-content-type response
0acc595b
AW
246 '(text/plain)))
247 (declared-charset (assq-ref (cdr type) 'charset))
af0da6eb 248 (charset (or declared-charset "utf-8")))
d9f00c3d
AW
249 (sanitize-response
250 request
251 (if declared-charset
252 response
253 (extend-response response 'content-type
0acc595b 254 `(,@type (charset . ,charset))))
d9f00c3d
AW
255 (encode-string body charset))))
256 ((procedure? body)
af0da6eb 257 (let* ((type (response-content-type response
0acc595b
AW
258 '(text/plain)))
259 (declared-charset (assq-ref (cdr type) 'charset))
af0da6eb
AW
260 (charset (or declared-charset "utf-8")))
261 (sanitize-response
262 request
263 (if declared-charset
264 response
265 (extend-response response 'content-type
0acc595b 266 `(,@type (charset . ,charset))))
af0da6eb 267 (call-with-encoded-output-string charset body))))
d9f00c3d
AW
268 ((bytevector? body)
269 ;; check length; assert type; add other required fields?
a4342ba8
AW
270 (values (let ((rlen (response-content-length response))
271 (blen (bytevector-length body)))
272 (cond
612aa5be
AW
273 (rlen (if (= rlen blen)
274 response
275 (error "bad content-length" rlen blen)))
a4342ba8
AW
276 ((zero? blen) response)
277 (else (extend-response response 'content-length blen))))
d9f00c3d
AW
278 body))
279 (else
280 (error "unexpected body type"))))
79ef79ee 281
c6371902
AW
282;; -> response body state
283(define (handle-request handler request body state)
43d6659a
AW
284 "Handle a given request, returning the response and body.
285
286The response and response body are produced by calling the given
287@var{handler} with @var{request} and @var{body} as arguments.
288
289The elements of @var{state} are also passed to @var{handler} as
290arguments, and may be returned as additional values. The new
291@var{state}, collected from the @var{handler}'s return values, is then
292returned as a list. The idea is that a server loop receives a handler
293from the user, along with whatever state values the user is interested
294in, allowing the user's handler to explicitly manage its state."
c6371902
AW
295 (call-with-error-handling
296 (lambda ()
297 (call-with-values (lambda ()
298 (with-stack-and-prompt
299 (lambda ()
300 (apply handler request body state))))
301 (lambda (response body . state)
302 (call-with-values (lambda ()
8bf6cfea 303 (debug-elapsed 'handler)
c6371902
AW
304 (sanitize-response request response body))
305 (lambda (response body)
8bf6cfea 306 (debug-elapsed 'sanitize)
c6371902
AW
307 (values response body state))))))
308 #:pass-keys '(quit interrupt)
309 #:on-error (if (batch-mode?) 'pass 'debug)
310 #:post-error
311 (lambda (k . args)
312 (warn "Error handling request" k args)
313 (values (build-response #:code 500) #f state))))
314
462a1a04 315;; -> unspecified values
79ef79ee 316(define (write-client impl server client response body)
43d6659a
AW
317 "Write an HTTP response and body to @var{client}. If the server and
318client support persistent connections, it is the implementation's
319responsibility to keep track of the client thereafter, presumably by
320attaching it to the @var{server} argument somehow."
79ef79ee
AW
321 (call-with-error-handling
322 (lambda ()
323 ((server-impl-write impl) server client response body))
324 #:pass-keys '(quit interrupt)
325 #:on-error (if (batch-mode?) 'pass 'debug)
326 #:post-error
327 (lambda (k . args)
328 (warn "Error while writing response" k args)
462a1a04 329 (values))))
79ef79ee
AW
330
331;; -> unspecified values
332(define (close-server impl server)
43d6659a
AW
333 "Release resources allocated by a previous invocation of
334@code{open-server}."
79ef79ee
AW
335 ((server-impl-close impl) server))
336
337(define call-with-sigint
338 (if (not (provided? 'posix))
339 (lambda (thunk handler-thunk) (thunk))
340 (lambda (thunk handler-thunk)
341 (let ((handler #f))
342 (catch 'interrupt
343 (lambda ()
344 (dynamic-wind
345 (lambda ()
346 (set! handler
347 (sigaction SIGINT (lambda (sig) (throw 'interrupt)))))
348 thunk
349 (lambda ()
350 (if handler
351 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
352 (sigaction SIGINT (car handler) (cdr handler))
353 ;; restore original C handler.
354 (sigaction SIGINT #f)))))
355 (lambda (k . _) (handler-thunk)))))))
356
357(define (with-stack-and-prompt thunk)
358 (call-with-prompt (default-prompt-tag)
359 (lambda () (start-stack #t (thunk)))
360 (lambda (k proc)
361 (with-stack-and-prompt (lambda () (proc k))))))
362
462a1a04
AW
363;; -> new-state
364(define (serve-one-client handler impl server state)
43d6659a
AW
365 "Read one request from @var{server}, call @var{handler} on the request
366and body, and write the response to the client. Returns the new state
367produced by the handler procedure."
8bf6cfea 368 (debug-elapsed 'serve-again)
79ef79ee
AW
369 (call-with-values
370 (lambda ()
462a1a04
AW
371 (read-client impl server))
372 (lambda (client request body)
8bf6cfea 373 (debug-elapsed 'read-client)
79ef79ee
AW
374 (if client
375 (call-with-values
376 (lambda ()
c6371902
AW
377 (handle-request handler request body state))
378 (lambda (response body state)
8bf6cfea 379 (debug-elapsed 'handle-request)
462a1a04
AW
380 (write-client impl server client response body)
381 (debug-elapsed 'write-client)
382 state))
383 state))))
79ef79ee
AW
384
385(define* (run-server handler #:optional (impl 'http) (open-params '())
386 . state)
43d6659a
AW
387 "Run Guile's built-in web server.
388
389@var{handler} should be a procedure that takes two or more arguments,
390the HTTP request and request body, and returns two or more values, the
391response and response body.
392
393For example, here is a simple \"Hello, World!\" server:
394
395@example
396 (define (handler request body)
0acc595b 397 (values '((content-type . (text/plain)))
43d6659a
AW
398 \"Hello, World!\"))
399 (run-server handler)
400@end example
401
402The response and body will be run through @code{sanitize-response}
403before sending back to the client.
404
405Additional arguments to @var{handler} are taken from
406@var{state}. Additional return values are accumulated into a new
407@var{state}, which will be used for subsequent requests. In this way a
408handler can explicitly manage its state.
409
410The default server implementation is @code{http}, which accepts
411@var{open-params} like @code{(#:port 8081)}, among others. See \"Web
412Server\" in the manual, for more information."
79ef79ee
AW
413 (let* ((impl (lookup-server-impl impl))
414 (server (open-server impl open-params)))
415 (call-with-sigint
416 (lambda ()
462a1a04
AW
417 (let lp ((state state))
418 (lp (serve-one-client handler impl server state))))
79ef79ee
AW
419 (lambda ()
420 (close-server impl server)
421 (values)))))