Introduce <slot> objects in GOOPS
[bpt/guile.git] / module / web / server.scm
CommitLineData
79ef79ee
AW
1;;; Web server
2
8cb9a30c 3;; Copyright (C) 2010, 2011, 2012, 2013 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)
76702cdc 77 #:use-module (srfi srfi-9 gnu)
79ef79ee 78 #:use-module (rnrs bytevectors)
6854c324 79 #:use-module (ice-9 binary-ports)
79ef79ee
AW
80 #:use-module (web request)
81 #:use-module (web response)
82 #:use-module (system repl error-handling)
83 #:use-module (ice-9 control)
8cb9a30c 84 #:use-module (ice-9 iconv)
79ef79ee
AW
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
8bf6cfea
AW
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
79ef79ee
AW
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
0c65f52c
AW
123(define-syntax-rule (define-server-impl name open read write close)
124 (define name
125 (make-server-impl 'name open read write close)))
79ef79ee
AW
126
127(define (lookup-server-impl impl)
06883ae0 128 "Look up a server implementation. If IMPL is a server
43d6659a 129implementation already, it is returned directly. If it is a symbol, the
06883ae0 130binding named IMPL in the ‘(web server IMPL)’ module is
43d6659a
AW
131looked up. Otherwise an error is signaled.
132
133Currently a server implementation is a somewhat opaque type, useful only
134for passing to other procedures in this module, like
06883ae0 135‘read-client’."
79ef79ee
AW
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)
06883ae0
DH
148 "Open a server for the given implementation. Return one value, the
149new server object. The implementation's ‘open’ procedure is
150applied to OPEN-PARAMS, which should be a list."
79ef79ee
AW
151 (apply (server-impl-open impl) open-params))
152
462a1a04
AW
153;; -> (client request body | #f #f #f)
154(define (read-client impl server)
06883ae0
DH
155 "Read a new client from SERVER, by applying the implementation's
156‘read’ procedure to the server. If successful, return three
43d6659a 157values: an object corresponding to the client, a request object, and the
06883ae0 158request body. If any exception occurs, return ‘#f’ for all three
43d6659a 159values."
79ef79ee
AW
160 (call-with-error-handling
161 (lambda ()
462a1a04 162 ((server-impl-read impl) server))
79ef79ee 163 #:pass-keys '(quit interrupt)
2263ccb5
AW
164 #:on-error (if (batch-mode?) 'backtrace 'debug)
165 #:post-error (lambda _ (values #f #f #f))))
79ef79ee 166
f944ee8f 167(define (extend-response r k v . additional)
3c12fc35
AW
168 (define (extend-alist alist k v)
169 (let ((pair (assq k alist)))
170 (acons k v (if pair (delq pair alist) alist))))
76702cdc 171 (let ((r (set-field r (response-headers)
c04bf433 172 (extend-alist (response-headers r) k v))))
f944ee8f
AW
173 (if (null? additional)
174 r
175 (apply extend-response r additional))))
176
79ef79ee
AW
177;; -> response body
178(define (sanitize-response request response body)
43d6659a
AW
179 "\"Sanitize\" the given response and body, making them appropriate for
180the given request.
181
06883ae0 182As a convenience to web handler authors, RESPONSE may be given as
43d6659a
AW
183an alist of headers, in which case it is used to construct a default
184response. Ensures that the response version corresponds to the request
06883ae0
DH
185version. If BODY is a string, encodes the string to a bytevector,
186in an encoding appropriate for RESPONSE. Adds a
187‘content-length’ and ‘content-type’ header, as necessary.
43d6659a 188
06883ae0 189If BODY is a procedure, it is called with a port as an argument,
43d6659a
AW
190and the output collected as a bytevector. In the future we might try to
191instead use a compressing, chunk-encoded port, and call this procedure
192later, in the write-client procedure. Authors are advised not to rely
193on the procedure being called at any particular time."
d9f00c3d
AW
194 (cond
195 ((list? response)
c6371902
AW
196 (sanitize-response request
197 (build-response #:version (request-version request)
198 #:headers response)
199 body))
200 ((not (equal? (request-version request) (response-version response)))
201 (sanitize-response request
202 (adapt-response-version response
203 (request-version request))
204 body))
a4342ba8
AW
205 ((not body)
206 (values response #vu8()))
d9f00c3d
AW
207 ((string? body)
208 (let* ((type (response-content-type response
0acc595b
AW
209 '(text/plain)))
210 (declared-charset (assq-ref (cdr type) 'charset))
af0da6eb 211 (charset (or declared-charset "utf-8")))
d9f00c3d
AW
212 (sanitize-response
213 request
214 (if declared-charset
215 response
216 (extend-response response 'content-type
0acc595b 217 `(,@type (charset . ,charset))))
8cb9a30c 218 (string->bytevector body charset))))
d9f00c3d 219 ((procedure? body)
af0da6eb 220 (let* ((type (response-content-type response
0acc595b
AW
221 '(text/plain)))
222 (declared-charset (assq-ref (cdr type) 'charset))
af0da6eb
AW
223 (charset (or declared-charset "utf-8")))
224 (sanitize-response
225 request
226 (if declared-charset
227 response
228 (extend-response response 'content-type
0acc595b 229 `(,@type (charset . ,charset))))
af0da6eb 230 (call-with-encoded-output-string charset body))))
164a78b3
AW
231 ((not (bytevector? body))
232 (error "unexpected body type"))
eec3a508
AW
233 ((and (response-must-not-include-body? response)
234 body
3b2226ec 235 ;; FIXME make this stricter: even an empty body should be prohibited.
eec3a508 236 (not (zero? (bytevector-length body))))
164a78b3
AW
237 (error "response with this status code must not include body" response))
238 (else
d9f00c3d 239 ;; check length; assert type; add other required fields?
a4342ba8
AW
240 (values (let ((rlen (response-content-length response))
241 (blen (bytevector-length body)))
242 (cond
612aa5be
AW
243 (rlen (if (= rlen blen)
244 response
245 (error "bad content-length" rlen blen)))
a4342ba8 246 (else (extend-response response 'content-length blen))))
164a78b3
AW
247 (if (eq? (request-method request) 'HEAD)
248 ;; Responses to HEAD requests must not include bodies.
249 ;; We could raise an error here, but it seems more
250 ;; appropriate to just do something sensible.
251 #f
252 body)))))
79ef79ee 253
c6371902
AW
254;; -> response body state
255(define (handle-request handler request body state)
43d6659a
AW
256 "Handle a given request, returning the response and body.
257
258The response and response body are produced by calling the given
06883ae0 259HANDLER with REQUEST and BODY as arguments.
43d6659a 260
06883ae0 261The elements of STATE are also passed to HANDLER as
43d6659a 262arguments, and may be returned as additional values. The new
06883ae0 263STATE, collected from the HANDLER's return values, is then
43d6659a
AW
264returned as a list. The idea is that a server loop receives a handler
265from the user, along with whatever state values the user is interested
266in, allowing the user's handler to explicitly manage its state."
c6371902
AW
267 (call-with-error-handling
268 (lambda ()
269 (call-with-values (lambda ()
270 (with-stack-and-prompt
271 (lambda ()
272 (apply handler request body state))))
273 (lambda (response body . state)
274 (call-with-values (lambda ()
8bf6cfea 275 (debug-elapsed 'handler)
c6371902
AW
276 (sanitize-response request response body))
277 (lambda (response body)
8bf6cfea 278 (debug-elapsed 'sanitize)
c6371902
AW
279 (values response body state))))))
280 #:pass-keys '(quit interrupt)
2263ccb5
AW
281 #:on-error (if (batch-mode?) 'backtrace 'debug)
282 #:post-error (lambda _
283 (values (build-response #:code 500) #f state))))
c6371902 284
462a1a04 285;; -> unspecified values
79ef79ee 286(define (write-client impl server client response body)
06883ae0 287 "Write an HTTP response and body to CLIENT. If the server and
43d6659a
AW
288client support persistent connections, it is the implementation's
289responsibility to keep track of the client thereafter, presumably by
06883ae0 290attaching it to the SERVER argument somehow."
79ef79ee
AW
291 (call-with-error-handling
292 (lambda ()
293 ((server-impl-write impl) server client response body))
294 #:pass-keys '(quit interrupt)
2263ccb5
AW
295 #:on-error (if (batch-mode?) 'backtrace 'debug)
296 #:post-error (lambda _ (values))))
79ef79ee
AW
297
298;; -> unspecified values
299(define (close-server impl server)
43d6659a 300 "Release resources allocated by a previous invocation of
06883ae0 301‘open-server’."
79ef79ee
AW
302 ((server-impl-close impl) server))
303
304(define call-with-sigint
305 (if (not (provided? 'posix))
306 (lambda (thunk handler-thunk) (thunk))
307 (lambda (thunk handler-thunk)
308 (let ((handler #f))
309 (catch 'interrupt
310 (lambda ()
311 (dynamic-wind
312 (lambda ()
313 (set! handler
314 (sigaction SIGINT (lambda (sig) (throw 'interrupt)))))
315 thunk
316 (lambda ()
317 (if handler
318 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
319 (sigaction SIGINT (car handler) (cdr handler))
320 ;; restore original C handler.
321 (sigaction SIGINT #f)))))
322 (lambda (k . _) (handler-thunk)))))))
323
324(define (with-stack-and-prompt thunk)
325 (call-with-prompt (default-prompt-tag)
326 (lambda () (start-stack #t (thunk)))
327 (lambda (k proc)
328 (with-stack-and-prompt (lambda () (proc k))))))
329
462a1a04
AW
330;; -> new-state
331(define (serve-one-client handler impl server state)
06883ae0
DH
332 "Read one request from SERVER, call HANDLER on the request
333and body, and write the response to the client. Return the new state
43d6659a 334produced by the handler procedure."
8bf6cfea 335 (debug-elapsed 'serve-again)
79ef79ee
AW
336 (call-with-values
337 (lambda ()
462a1a04
AW
338 (read-client impl server))
339 (lambda (client request body)
8bf6cfea 340 (debug-elapsed 'read-client)
79ef79ee
AW
341 (if client
342 (call-with-values
343 (lambda ()
c6371902
AW
344 (handle-request handler request body state))
345 (lambda (response body state)
8bf6cfea 346 (debug-elapsed 'handle-request)
462a1a04
AW
347 (write-client impl server client response body)
348 (debug-elapsed 'write-client)
349 state))
350 state))))
79ef79ee
AW
351
352(define* (run-server handler #:optional (impl 'http) (open-params '())
353 . state)
43d6659a
AW
354 "Run Guile's built-in web server.
355
06883ae0 356HANDLER should be a procedure that takes two or more arguments,
43d6659a
AW
357the HTTP request and request body, and returns two or more values, the
358response and response body.
359
360For example, here is a simple \"Hello, World!\" server:
361
362@example
363 (define (handler request body)
0acc595b 364 (values '((content-type . (text/plain)))
43d6659a
AW
365 \"Hello, World!\"))
366 (run-server handler)
367@end example
368
06883ae0 369The response and body will be run through ‘sanitize-response’
43d6659a
AW
370before sending back to the client.
371
06883ae0
DH
372Additional arguments to HANDLER are taken from
373STATE. Additional return values are accumulated into a new
374STATE, which will be used for subsequent requests. In this way a
43d6659a
AW
375handler can explicitly manage its state.
376
06883ae0
DH
377The default server implementation is ‘http’, which accepts
378OPEN-PARAMS like ‘(#:port 8081)’, among others. See \"Web
43d6659a 379Server\" in the manual, for more information."
79ef79ee
AW
380 (let* ((impl (lookup-server-impl impl))
381 (server (open-server impl open-params)))
382 (call-with-sigint
383 (lambda ()
462a1a04
AW
384 (let lp ((state state))
385 (lp (serve-one-client handler impl server state))))
79ef79ee
AW
386 (lambda ()
387 (close-server impl server)
388 (values)))))