1 Introduction
1.1 Apache mod_ scgi
2 Interface
cgi
cgi-content-length
make-cgi-variable-proc
cgi-content-type
cgi-document-root
cgi-http-cookie
cgi-http-host
cgi-http-referer
cgi-http-user-agent
cgi-https
cgi-path-info
cgi-path-translated
cgi-query-string
cgi-remote-addr
cgi-remote-host
cgi-remote-user
cgi-request-method
cgi-request-uri
cgi-script-name
cgi-server-name
cgi-server-port
scgi-variables
cgi-type
cgi-request-id
stop-cgi-service-immediately
3 Troubleshooting
4 History
5 Legal
Version: 0.4

scgi: Web Server HTTP SCGI and CGI in Racket

Neil Van Dyke

License: LGPL 3   Web: http://www.neilvandyke.org/racket-scgi/

 (require (planet neil/scgi:1:=3))

1 Introduction

The scgi library implements fast Web CGI using the SCGI protocol. This library is used in conjunction with an HTTP server supporting SCGI, such as Apache Server with the mod_scgi module.

The scgi library also supports running as normal Web CGI without any change to the source code of the app, such as during development of an application intended to be deployed using SCGI. This also gives flexibility in deployment, allowing a system administrator to switch between either mode just by editing the HTTP server configuration.

The SCGI protocol was specified by Neil Schemenauer in “SCGI: A Simple Common Gateway Interface alternative,” dated 2008-06-23 (http://python.ca/scgi/protocol.txt).

An example usage of this library:

(require (planet neil/scgi))
 
(cgi (lambda ()
       (and (eq? (cgi-type) 'scgi)
            (my-do-some-init-only-for-scgi)))
 
     (lambda ()
       (display "Content-type: text/html\r\n\r\n")
       (display "<p>Hello, world</p>"))
 
     (lambda ()
       (my-shutdown-stuff)))

1.1 Apache mod_scgi

Note that your Apache Server installation might not have mod_scgi module installed or enabled by default. If you happen to be running Debian Lenny (stable) or similar, this module can be installed via the Debian package libapache2-mod-scgi.

Once you’ve installed mod_scgi, you need some standard SCGI directives to end up in your Apache config files, whether you accomplish that by editing config files manually, making symbolic links in a mods-enabled directory, or clicking in a GUI.

For example, the following loads mod_scgi, maps URL paths under /mypath to the SCGI server on the local machine at the standard SCGI TCP port, and sets a 60-second timeout for the SCGI server to respond to a request before Apache drops the connection:

LoadModule scgi_module /usr/lib/apache2/modules/mod_scgi.so

SCGIMount /mypath 127.0.0.1:4000

SCGIServerTimeout 60

There are additional mod_scgi Apache config directives, including SCGIHandler and SCGIServer.

2 Interface

(cgi startup-proc    
  request-proc    
  shutdown-proc)  any/c
  startup-proc : any/c
  request-proc : any/c
  shutdown-proc : any/c

Implement CGI. Normal CGI is used if the REQUEST_URI environment variable is defined (which suggests that the code is being called in a CGI context); otherwise, SCGI is used.

startup-proc is a thunk that is evaluated once (before listener starts). request-proc is evaluated once for each request (which, in normal CGI, is once). shutdown-proc is evaluated once, as processing of all CGI requests has finished.

For evaluation of request-proc, the default input and output ports are as with normal CGI, regardless of whether normal CGI or SCGI is in use.

This procedure also accepts a few optional keyword arguments, all of which apply to SCGI mode only:

#:scgi-hostname       (scgi-hostname       "127.0.0.1")
#:scgi-portnum        (scgi-portnum        4000)
#:scgi-max-allow-wait (scgi-max-allow-wait 4)
#:reuse-scgi-port?    (reuse-scgi-port?    #t)

scgi-hostname is the hostname or IP address (as a string) for the interface on which to listen. scgi-portnum is the TCP port number on that interface. scgi-max-allow-wait is the maximum number of unaccepted connections to permit waiting. reuse-scgi-port? is whether or not to reuse the TCP port number, such as if a previous server exited and the port is in a TIME_WAIT state.

(cgi-content-length)  any/c

In a CGI request context, returns the CGI content length – the number of bytes that can be read from the default input port – as integer.

(make-cgi-variable-proc sym name-bytes)  any/c
  sym : any/c
  name-bytes : any/c

Produces a procedure for getting a CGI environment variable value as a string. Works whether in normal CGI or SCGI. This is useful for accessing non-standard variables, such as might be provided by an unusual Apache module. Argument sym is a symbol for the name of the procedure, which is used in error reporting. Argument name-bytes is the name of the environment variable, as a byte string.

For example, the cgi-remote-user procedure could be defined as:

(define cgi-remote-user
  (%make-cgi-variable-proc 'cgi-remote-user #"REMOTE_USER"))

(cgi-content-type)  any/c

(cgi-document-root)  any/c

(cgi-http-cookie)  any/c

(cgi-http-host)  any/c

(cgi-http-referer)  any/c

(cgi-http-user-agent)  any/c

(cgi-https)  any/c

(cgi-path-info)  any/c

(cgi-path-translated)  any/c

(cgi-query-string)  any/c

(cgi-remote-addr)  any/c

(cgi-remote-host)  any/c

(cgi-remote-user)  any/c

(cgi-request-method)  any/c

(cgi-request-uri)  any/c

(cgi-script-name)  any/c

(cgi-server-name)  any/c

(cgi-server-port)  any/c

In a CGI request context, returns the corresponding CGI value as a string. Note that cgi-content-length is not in this list, and it returns a number rather than a string.

(scgi-variables)  any/c

When called in SCGI mode, this procedure yields an alist of SCGI variables with both the key and value of each pair being byte strings. Calling this procedure in normal CGI mode is an error.

Note that normally you will not need to use this procedure, and will instead use procedures like cgi-request-uri, which work in both SCGI and normal CGI modes.

(cgi-type)  any/c

Returns a symbol indicating the CGI type: normal or scgi. Behavior outside of the cgi form is undefined.

(cgi-request-id)  any/c

In CGI request context, yields a printable identifying object for the current request that is unique at least for the current requests being handled. This identifying object is intended to be used in debugging messages.

(stop-cgi-service-immediately)  any/c

Stops processing all CGI requests. This works only within the request-proc of the cgi form.

3 Troubleshooting

This section has some troubleshooting tips. Currently, these come from use with mod_scgi atop Apache 2.2.9 atop Debian GNU/Linux.

4 History

5 Legal

Copyright (c) 2010–2011 Neil Van Dyke. This program is Free Software; Software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License (LGPL 3), or (at your option) any later version. This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See http://www.gnu.org/licenses/ for details. For other licenses and consulting, please contact the author.

Standard Documentation Format Note: The API signatures in this documentation are likely incorrect in some regards, such as indicating type any/c for things that are not, and not indicating when arguments are optional. This is due to a transitioning from the Texinfo documentation format to Scribble, which the author intends to finish someday.