From: Tobias Gerdin <tgerdin@gmail.com>
To: Guile Users <guile-user@gnu.org>
Subject: The Web, Continuations, and All That
Date: Sun, 22 Jan 2012 19:46:15 +0100 [thread overview]
Message-ID: <CAJiKzQN69Ngr6TyubAEtXi+c6QfEekWpgft4cFB+YO7j8bExnQ@mail.gmail.com> (raw)
Hello,
To get better acquainted with continuations I have been playing with
them in the context of web programming a bit. Much has been written
on the topic (in particular I find the Racket tutorials very
instructive), and here I would just like to share the small
experiments I did using delimited continuations and the Guile (web
server). Kudos to Andy for providing a very nice foundation.
The ingredients:
(use-modules (web server)
(web request)
(web response)
(web uri)
(sxml simple)
(ice-9 control))
To begin with, a simple request handler making use of a table to map
between request URI paths and other request handlers (also functions
as a continuation table):
(define dispatch-table (make-hash-table))
(define (handler request body)
(let* ((path (split-and-decode-uri-path (uri-path (request-uri request))))
(h (hash-ref dispatch-table (string->symbol (car path)))))
(if h
(% (h request body))
(values '((content-type . (text/plain)))
(string-append "Unknown page: " (car path))))))
So a prompt is installed just before the matching request handler is
dispatched to.
Next there is Christian Queinnec's `show' operator (see Christian
Queinnec's seminal "The Influence of Browsers on Evaluators or,
Continuations to Program Web Servers"), although here I've call it
`send-suspend', which is what it is called in Racket (actually, they
call it `send/suspend'):
(define (send-suspend make-response)
(abort (lambda (k)
(let ((k-uri-id (gensym "k"))) ; Use something more
sophisticated here!
(hash-set! dispatch-table k-uri-id k)
(make-response (format #f "/~a" k-uri-id))))))
So it's a something that when called will save the state of the
computation (the continuation `k') in the table and associate it with
a generated uri, and then apply that uri to a procedure that returns a
response object. The comment is there to draw attention to the fact
that security hinges on the generated uri being difficult to predict,
or else the user's session is open to hijacking. Additional security
measures could be put in place as well.
The point of the above is to able to write request handlers spanning
several requests in this style:
(define (hello-world r b)
(send-suspend
(lambda (k-url)
(values '((content-type . (text/html)))
(string-append "<a href=\"" k-url "\">hello</a>"))))
(values '((content-type . (text/html)))
"world!"))
To try it the handler needs to be mounted and the web server fired up,
pointed at the dispatching handler:
(hash-set! dispatch-table 'hello hello-world)
(run-server handler)
Visiting http://localhost:8080/hello should give a "hello" link
yielding a second page saying "world!". Since constructing the
response objects in the above way is tedious and verbose I made use of
the `respond' helper from the "Web Examples" section in the Guile
reference manual:
(define (hello-world2 r b)
(send-suspend
(lambda (k-uri)
(respond `((a (@ (href ,k-uri)) "hello")))))
(respond '("world!")))
(hash-set! dispatch-table 'hello2 hello-world)
Lexical scope is preserved over requests:
(define (count req body)
(let loop ((n 0))
(send-suspend
(lambda (k-uri)
(respond `((a (@ (href ,k-uri)) ,(number->string n))))))
(loop (1+ n))))
(hash-set! dispatch-table 'count count)
An common use-case is to return values from pages (such as data
provided by the user through forms):
;; Dirty hack to extract a single param from encoded form data
(define (get-name form-data)
(uri-decode (cadr (string-split form-data #\=))))
(define (get-greet req body)
(let ((req (send-suspend
(lambda (k-uri)
(respond `((form (@ (action ,k-uri) (method "GET))
"Your name: "
(input (@ (type text) (name name)))
(input (@ (type submit))))))))))
(respond `("Hi " ,(get-name (uri-query (request-uri req)))))))
(hash-set! dispatch-table 'greet get-greet)
Since the stored continuation is invoked with both the request and the
body you access the latter by making use of some multiple-values
binding form:
(use-modules (srfi srfi-11)) ; let-values
(use-modules (rnrs bytevectors)) ; utf8->string
(define (post-greet req body)
(let-values (((req body) (send-suspend
(lambda (k-uri)
(respond `((form (@ (action ,k-uri)
(method "POST"))
"Your name: "
(input (@ (type text)
(name name)))
(input (@ (type submit))))))))))
(respond `("Hi " ,(get-name (utf8->string body))))))
(hash-set! dispatch-table 'greet2 post-greet)
Happy hacking.
-Tobias
next reply other threads:[~2012-01-22 18:46 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-01-22 18:46 Tobias Gerdin [this message]
2012-01-22 20:46 ` The Web, Continuations, and All That Ian Price
2012-01-23 21:11 ` Andy Wingo
2012-01-30 19:17 ` Tobias Gerdin
2012-01-31 10:00 ` Andy Wingo
2012-02-06 19:49 ` Tobias Gerdin
2012-02-07 8:48 ` Andy Wingo
2012-01-25 8:59 ` Antono Vasiljev
[not found] ` <4F1FC437.8020801@antono.info>
2012-01-25 10:32 ` Ian Price
2012-01-30 19:03 ` Tobias Gerdin
2012-01-31 9:06 ` Ian Price
2012-02-04 9:53 ` Ian Price
[not found] ` <CAJiKzQMETO7g6YyqCbeoV0GD09gena6RK6q6bYhjTOe+wNq+Uw@mail.gmail.com>
2012-02-06 19:40 ` Fwd: " Tobias Gerdin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAJiKzQN69Ngr6TyubAEtXi+c6QfEekWpgft4cFB+YO7j8bExnQ@mail.gmail.com \
--to=tgerdin@gmail.com \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).