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