You are looking at historical revision 10857 of this page. It may differ significantly from its current revision.
A unification framework for web server communications systems like CGI, SCGI or FastCGI and Spiffy.
NOTE: This documentation is a quick write-up. It needs to be improved. The egg is alpha code too.
It always needs http
Extra requirements vary depending on how you want to run it; if you wish to use Spiffy, you'll need the spiffy egg, if you wish to use FastCGI you'll need the fastcgi egg, if you wish to use SCGI you'll need the scgi egg. If you only need CGI, there are no extra requirements.
Web-unity is an attempt to remove the differences between the various ways you can write web applications with Chicken. The goal is to ease professional webdevelopment by making it possible to deploy on different server architectures than you develop on, and making it easier to migrate to a different system when the need arises. This allows you to move from CGI to FastCGI if your webserver can't handle the requests any longer, or develop on Spiffy and deploy on your Lighttpd production server using SCGI.
This is done by abstracting away a couple of things, so it may mean slightly degraded performance or power.
(wu:set-header! name value)
Sets a HTTP header in the current response by changing the value of the current-response-headers parameter. If the header was already set it will be overwritten except in the case of Set-Cookie. In this case, it will just output an additional Set-Cookie header.
This invokes the main loop that handles requests. The dispatcher is a thunk (lambda with no args) that will be invoked whenever a request comes in on the server. This needs to be called last from whatever bootstrap code you run to set up a web application. The run-handler takes care of setting up the environment correctly so that the given dispatcher can have the information it needs to work with independently of the type of web server.
Write the current headers from the wu:response-headers parameter. This must be done before emitting any output and it must be done exactly once.
(wu:get-header name [default])
Get the current value of the given header. If the optional default is given, this is returned if the header is not found, otherwise #f is returned.
Most request information is available through SRFI-39 parameters.
- wu:request-method (symbol)
- The current request's HTTP method as a lower-cased symbol (get, post).
- wu:params (alist)
- The current HTTP method's parameters with their values (all strings). With GET this is equal to the query-params, with POST it contains the POST request attributes
- wu:query-params (alist)
- The current query parameters (URL commandline parameters), even if the method is post, for example.
- wu:script-name (string)
- The absolute path to the script in the URL, up to, but not including, the pathinfo.
- wu:script-filename (string)
- The absolute path to the script in the filesystem.
- wu:path-info (string)
- The pathinfo component of the URL (this is the pathname part after the actual script's filename)
- wu:response-headers (alist)
- The headers that will be sent when you call (wu:write-headers). Default: (("Content-Type" . "text/html))
- wu:response-code (integer-string pair)
- The response code that will be sent when you call (wu:write-headers). Default: (200 . "OK")
- wu:headers-sent (boolean)
- Whether headers have already been sent by wu:write-headers, during this request
- wu:exception-handler (procedure)
- Exception handler for exceptions thrown in web-unity code. Override this to get pretty error output and/or hide stack traces.
TODO: Invent a better example (one that doesn't statically load one file)
Here is a really small example file that is written in web-unity:
;; Example.scm (wu:set-header! "content-type" "text/html") (wu:write-headers) (printf "params: ~A<br />\nquery args: ~A\n<br />method: ~A\n" (wu:params) (wu:query-params) (wu:request-method)) (printf "parameter (http_)accept = ~A\n<br />" (wu:get-header "accept")) (printf "script-name = ~A\n<br />" (wu:script-name)) (printf "script-filename = ~A\n<br />" (wu:script-filename)) (printf "<form action=\"\" method=\"post\"><input name=\"foo\" /><input name=\"bar\" /><input type=\"submit\" /></form>")
You can setup lighttpd using CGI so it loads this script as follows:
cgi.assign = ( ".scm" => "/usr/libexec/cgi-dispatcher.scm")
where the cgi-dispatcher file contains the following:
#!/usr/pkg/bin/csi -s (use web-unity-cgi) (wu:run-handler (lambda () (load "example.scm")))
Lighttpd with FCGI works as follows:
fastcgi.server = ( ".scm" => ( "localhost" => ( "socket" => "/tmp/fastcgi-socket", "min-procs" => 1, "max-procs" => 1, "bin-path" => "/usr/libexec/fcgi-dispatcher.scm" ) ) )
where the cgi-dispatcher file contains the following:
#!/usr/pkg/bin/csi -s (use web-unity-fcgi) (wu:run-handler (lambda () (load "example.scm")))
Spiffy works a little differently (the web-unity-handler comes with the web-unity egg):
(use web-unity-handler) (spiffy-file-ext-handlers `(("scm" . ,(web-unity-handler "/usr/libexec/spiffy-dispatcher.scm"))))
where the spiffy-dispatcher file contains the following:
(use web-unity-spiffy) (define lp ##sys#current-load-path) (wu:run-handler (lambda () (load (string-append lp "example.scm"))))
- trunk Add support for HTTP/1.1 content chunking and HTTP/1.0 force-close because web-unity does not know the content-length in advance, add exception handling, added missing response code handling in spiffy handler. Add cookie functionality (from spiffy-utils egg. Ported by David Krentzlin)
- 0.21 Fix 'author' in meta file
- 0.1 Initial release
Copyright (c) 2007, Peter Bex All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.