You are looking at historical revision 15377 of this page. It may differ significantly from its current revision.


uri-dispatch is a simple mechanism to dispatch uris to procedures. This is useful for webprogramming where you might want to map uris of a certain kind to procedures that implement the logic for those uris.


David Krentzlin


Requires the uri-common and environments extensions.


[procedure] (dispatch-uri URI)

The main interface to dispatch-uri. This procedure invokes the dispatch-mechanism with the supplied uri. The dispatch-algorithm is implemented as follows:

(Note Although not explicitly stated here the algorithm performs a whitelist-check on any procedure before it is invoked. See enable-checks and whitelist!)

[procedure] (whitelist! DEFINITION)

Though nice for development it is not advisable to allow anybody out there to invoke abritary procedures of your application. So you might want to enable-checks in production and whitelist only those modules and/or procedures you really want to be callable from the outside. Whitelisting can be done with this procedure. It expects a list that defines the procedure/modules you want to whitelist. You have basically three possibilities to perform whitelisting all of which can be combined.

1) whitelist procedures outside modules <pre>(whitelist! '(procedure1 procedure2 procedure3))</pre>

2) whitelist all procedures inside a module <pre>(whitelist! '((module my-module my-module2))</pre>

3) whitelist specific procedures inside a module <pre>(whitelist! '((module-name procedure1 procedure2)))</pre>

As noted above you can mix those possibilities

<pre>(whitelist! '(proc1 proc2 proc3 (module foo-mod bar-mod) (baz-module proc4 proc5)))</pre>

It is planned to implement some keywords that allow easier use of this procedure.

[parameter] dispatch-error

If no handler for a uri can be found, the dispatcher will invoke the procedure that dispatch-error is currently parameterized with. NOTE dispatch-error is also invoked if the requested handler exists but is not whitelisted.

[parameter] enable-checks
[parameter] default-dispatch-target
[parameter] dispatch-environment



(use spiffy intarweb uri-common uri-dispatch)

(define (my-dispatch-error . path)

 (send-status 404 (sprintf "My Dispatch-error ~A" path)))

(module example

 (echo webiota)
 (import scheme chicken srfi-1 extras)
 (require-library spiffy)
 (import (only spiffy send-status))
 (define (webiota #!optional (start "0") (steps "10") #!rest rest)
   (send-status 200 (sprintf "webiota called: ~A" (iota (string->number steps) (string->number start)))))
 (define (echo . args)
   (send-status 200 (sprintf "echo: ~A" args))))

(define (outsidemodule . args) (send-status 200 "Outside module"))

(vhost-map `(("localhost" . ,(lambda (continue)

                             (parameterize ((handle-not-found  (lambda (path) (dispatch-uri (request-uri (current-request)))))
                                            (dispatch-error (lambda path (send-status 404 (sprintf "Path not found: ~A" path)))))



Now start the server and visit the following pages: