guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

The Web, Continuations, and All That


From: Tobias Gerdin
Subject: The Web, Continuations, and All That
Date: Sun, 22 Jan 2012 19:46:15 +0100

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]