[plt-scheme] URL rewrite/dispatch in PLT Web server

From: Sridhar Ratna (sridhar.ratna at gmail.com)
Date: Wed Mar 28 01:27:52 EDT 2007

On 3/28/07, Eli Barzilay <eli at barzilay.org> wrote:
> Rewriting responses seems like a bad idea.  My guess is that the OP
> wanted to simply use servlets in a different location, which is
> something that is sorely missing.  The whole thing with artifical
> separation between data and code (like Apache's /cgi-bin) is something
> that belongs in the not-too-dynamic-past.  These days, separating
> scripts and passive content is a bad idea.

I very much agree.  To accomodate restful urls, we need an url
dispatcher like http://lukearno.com/projects/selector/

In Scheme CGI, it goes like...

  (define-syntax run-with-urls
    (syntax-rules ()
      ((_ (url-regex handler) ...)
       (let ((dispatch-map (list (cons url-regex handler) ...))
             (url          (or (getenv "PATH_INFO") "")))
         (receive (content-type content) (let loop ((d-map dispatch-map))
                                           (if (null? d-map)
                                               (values "text/plain" "not found")
                                               (let ((m (regexp-match
(caar d-map) url)))
                                                 (if m
                                                     (apply (cdar
d-map) (cdr m))
                                                     (loop (cdr d-map))))))
                  (display (format "content-type: ~a; charset=utf-8"
                  (display content))))))

  (run-with-urls ("^/$"          (lambda ()
                                   (values "text/plain" "This in index page")))
                 ("^/post/(.)*$" (lambda (x)
                                   (values "text/html" (format "This
is post with id = ~a" x)))))

As you can see how elegantly URLs like "/", "/post/2" are mapped to
functions. There is no "/servlets/foo.ss/" magic here.


Posted on the users mailing list.