Tools and working examples of HTML, XML, FORM, HTTP and CGI programming in Scheme
QUERY_STRING
or a POST
messageget-url
and web agentstail-f
: file watching CGI script that sends updates via a server push
This script contains a great deal of generic code for web services and for common form processing chores. Many other CGI scripts of mine are written by cloning of this script and making appropriate changes.
get-url
and web agentsget-url
. This general-purpose function can be used
independently, to retrieve a page or a file from a web server. The
function handles direct and proxied connections.Tcl, Scheme, Web, robot [Re: Why Scheme?] [plain text file]
An article posted on a comp.lang.scheme newsgroup on Sun, 26 Dec 1999 22:50:47 GMT. It contains the source code for the stock quote robot in Scheme and Tcl. The Scheme code includes the source for a get-url
function.
Uni-, Bi-, and TCP pipes as regular files
A detailed explanation of this trick and of its applications.
vext-io.scm [18K]
Code that illustrates and validates extended file names, uni-, bi-directional and tcp://
-like pipes.
This code is an example of submitting an HTTP request and of parsing an HTTP response -- done entirely in Scheme.
Way extended handling of files and "files" [plain text file]
An article posted on a comp.lang.scheme newsgroup on Sun, 21 Feb 1999 22:57:26 GMT
Uni-, Bi-, and TCP pipes as regular files
A detailed explanation of this trick and of its applications.
vext-io.scm [18K]
Code that illustrates and validates extended file names, uni-, bi-directional and tcp://
-like pipes.
CGI:lookup NAME TYPE [DEFAULT-VALUE]The procedure looks up a
NAME
(a symbol) among CGI
variables. The result is converted to TYPE
. If the
NAME
is not CGI-bound and the DEFAULT-VALUE
is
specified, the procedures returns the DEFAULT-VALUE.
Otherwise, the procedure signals an error. A failure of the TYPE
-conversion also triggers an error. The TYPE
is
a symbol, one of the following: token
, tokens
, string
, io
, int
, number
. The detailed source code comments describe how the procedure
handles all four cases of CGI parameters: param=
, param
, param=val
, and param=val1+val2
.
The collection of utilities includes several other useful procedures: to obtain a query string from a GET or a POST request, to parse the query string into an associative list of parameter bindings, to determine the name of a remote user, to handle a signal raised in a CGI script.
cgi-util.scm [11K]
The commented source code
Exceptions handling SRFI-12 is required for the function CGI:exception-handler
OS:getenv
procedure, which on many Scheme systems can be defined as getenv
QUERY_STRING
CGI:url-unquote QUERY-STRING
parm1=val1&parm2=val2+val%253+val4&%7Eparm3=&parm4into an associative list
((parm4) (~parm3 "") (parm2 "val2" "val%3" "val4") (parm1 "val1"))
The parsing is done by a finite state machine,
which takes into account (i) the current state: looking for a parm,
looking for a value; (ii) an action-prefix: =
, +
, or &
; (iii) the
token that follows the action prefix.
This function is intended for parsing of a
QUERY_STRING
, POST
action messages, or similar parameters passed by an HTTP server to a CGI script.
MIME:parse-content-type CTYPE-STR
CTYPE-STR
of a form media-type [; attr=value]*
the procedure returns the list of associations (attr . value)
where attr
is a symbol and value
is a string.
The media-type is returned as an association with a tag
=mime-type
. See Sections 2.2 and 3.6 of RFC2616 (HTTP/1.1) for the syntax of the Content-Type string.
text/html; cid=10;name=" val "the function returns a list
((=mime-type . "text/html") (cid . "10") (name . " val "))
MIME:read-headers PORT
PORT
. The port
will be positioned after the empty line that terminates the headers.
mime.scm [6K]
Commented source code
vmime.scm [3K]
Commented validation code
http-transaction REQ-METHOD REQ-URL REQ-PARMS RESPONSE-HANDLER
Here, REQ-METHOD
is a string, typically "GET"
or "POST"
, although many others may be
allowed. REQ-URL
is an absolute URL of the HTTP server.
REQ-PARMS
is an associative list, a list of (name
. value)
pairs. The list may be regarded as "keyword arguments"
of the http-transaction procedure. All keyword arguments are optional:
if omitted or specified with a value #f
, a suitable
default value will be used. The following "keyword parameters" are
supported: http-proxy
, user-agent
,
http-req
, logger
. See the title comments in the code
for the full description.
RESPONSE-HANDLER
is a procedure
RESP-CODE RESP-HEADERS RESP-PORT => ANY
. Here RESP-CODE
is a number, which is one of the HTTP codes, e.g., 200, 304, 404, or
500. The argument RESP-HEADERS
is a list of pairs (http-header-name . http-header-val)
where http-header-name
is an upper-case symbol. This list carries the
HTTP server response headers. In addition to the standard header names
defined by HTTP 1.1, a special pair (HTTP-RESPONSE . the-whole-response-line)
tells the
response handler the entire HTTP response line. The argument RESP-PORT
is an input port from which to read the body of the
reply, if any.
The validation code gives several examples of invoking http-transaction
to retrieve a web page, to submit a web form, or
to make other HTTP remote procedure call.
http.scm [11K]
Commented source code
vhttp.scm [4K]
Commented validation code
Speaking HTTP: A File-Uploader Tool
USENIX ;login: -- vol. 25, No. 2 -- April 2000, pp. 6-14.
tail-f
: file watching CGI scriptThis CGI code is activated by an HTTP server when serving a URL
like
/cgi-bin/tail-f?period=600&file=/opt/apache/logs/access
This code watches the given file
, sending its
last nlines
lines to the client. This script does not
quit after the first peek into the file, but rather keeps watching the
file, checking up on it every period
seconds. The
watching stops when a user agent (a browser, that is) closes the
connection: e.g., in response to the user's pressing the 'stop'
button. The watching also terminates when the file disappears. All in
all, this is similar to a familiar UNIX tail -f
command,
working across the network.
Opening the tail-f
URL starts off a
server-web-browser finite automaton. The latter quickly goes
through several states, without any user intervention. When the Finite
State Machine reaches its final state, it leaves the result of the
computation in a browser window: a frame set with a HTML form
on the top, and the tail lines of the file
. The form in the top frame presents the parameters of the script -- file name, watch period, nlines -- and their specified or implied values. You can enter or modify them right on
the form, and then click on the watch button. The bottom frame
shows the tail of the file, automatically updated via a server
push.
When scanning a file, we deliberately avoid a direct
positioning of the file. For one thing, fseek()
etc. functions are not standard in Scheme. For another, treating a
file as a purely sequential stream enables us to handle file
s which are not regular files per se: named (FIFO)
pipes, raw devices, serial ports, etc.
An extensively commented source code. [plain text file]
The code implements all CGI functionality, tail-f
functionality, and the server push. It also declares and implements a
circular buffer object, which can be used independently.
The Web IS a computer, HTML its language (and a pushy Scheme CGI setting it off) [plain text file]
A USENET article posted on comp.lang.scheme, comp.programming, comp.infosystems.www.authoring.cgi, and comp.infosystems.www.misc newsgroups on Tue Mar 25 13:17:13 CST 1997.
This site's top page is http://okmij.org/ftp/
Converted from SXML by SXML->HTML