CGIservlet

A HTTPd "connector" for running CGI scripts on unix systems as WWW accessible Web sites. The servlet starts a true HTTP daemon that channels HTTP requests to forked daughter processes. CGIservlet.pl is NOT a full fledged server. Moreover, this servlet is definitely NOT intended as a replacement for a real server (e.g., Apache). It's design goal was SIMPLICITY, and not mileage.

Note that a HTTP server can be accessed on your local machine WITHOUT internet access (but WITH a DNS?): use "http://localhost[:port]/[path]" or "http://127.0.0.1[:port]/[path]" as the URL. It is also easy to restrict access to the servlet to localhost users (i.e., the computer running the servlet).

Suggested uses:

When the servlet is started with the -r option, only requests from "localhost" or "127.0.0.1" are accepted (default) or from addresses indicated after the -r switch.

Running demo's and more information can be found at http://www.fon.hum.uva.nl/rob/OSS/OSS.html

Inner workings

Whenever an HTTP request is received, the specified CGI script is started inside a child process as if it was inside a real server (e.g., Apache). The evironment variables are set more or less as in Apache. Note that CGIservlet only uses a SINGLE script for ALL requests. No attemps for security are made, it is the script's responsibility to check access rights and the validity of the request.
When no scripts are given, CGIservlet runs as a bare bone WWW server configurable to execute scripts (the default setting is as a STATIC server).

Use:    CGIservlet.pl -<switch> <argument> 2>pid.log &     (sh)
        CGIservlet.pl -<switch> <argument> >&pid.log &     (csh)
        

The servlet prints out pid and port number on STDERR. It is adviced to store these in a separate file (this will become the error log).
NOTE: When running CGIservlet from a Memmory Image (i.e. RAM), do NOT redirect the error output to a file, but use something like MAILTO!

Stop:   sh pid.log                      (kills the server process)

The first line in the file that receives STDERR output is a command to stop CGIservlet.

examples:

CGIservlet.pl -p 2345 -d /cgi-bin/CGIscriptor.pl -t /WWW 2>pid.log &
CGIservlet.pl -p 8080 -b 'require "CGIscriptor.pl";' -t $PWD -e 'Handle_Request();' 2>pid.log &

The following example settings implement a static WWW server using 'cat' (and prohibiting Queries):

-p 8080
-t `pwd`
-b ''
-e 'exit if $ENV{QUERY_STRING};$ENV{PATH_INFO}=~/\.([\w]+)$/; "Content-type: ".$mimeType{uc($1)}."\n\n";'
-d 'cat -u -s'
-w '/index.html'
-c 32
-l 512
This is identical to the (static) behaviour of CGIservlet when -e '' -d '' -x '' is used.
The CGIservlet command should be run from the intended server-root directory.

Another setting will use a package 'CGIscriptor.pl' with a function 'HandleRequest()' to implement an interactive WWW server with inline Perl scripting:

-p 8080
-t `pwd`
-b 'require "CGIscriptor.pl";'
-e 'HandleRequest();'
-d ''
-w '/index.html'
-c 32
-l 32767

Look in the source code or in the CGIservletSETUP.pl file for the current default settings.

Command-line switches

There are many switches to tailor the workings of CGIservlet.pl. Some are fairly esoteric and you should only look for them if you need something special urgently. When building a Web site, the specific options you need will "suggest" themselves (e.g., port number, script, or server-root directory). Most default settings should work fine.

You can add your own configuration in a file called 'CGIservletSETUP.pl'. This file will be executed ("eval"-ed) after the default setup, but before the command line options take effect. CGIservlet looks for the SETUP file in the startup directory and in the CGIscriptor subdirectory.
(Note that the $beginarg variable is evaluated AFTER the setup file).

In any case, it is best to change the default settings instead of using the option switches. All defaults are put in a single block.

switches and arguments

Realy important
Script related
WWW-tree related
Security related
The following arguments supply some rudimentary security. It is the responsibility of the script to ensure that the requests are indeed "legal".
Speedup

Configuration with the CGIservletSETUP.pl file

You can add your own configuration in a file called 'CGIservletSETUP.pl'. This file will be executed ("eval"-ed) after the default setup, but before the command line options take effect. CGIservlet looks for the SETUP file in the startup directory and in the CGIservlet and CGIscriptor subdirectories. (Note that the $beginarg variable is evaluated even later).

Changing POST to GET requests

CGIservlet normally only handles requests with the GET method. Processing the input from POST requests is left to the reading application. POST requests add some extra complexity to processing requests. Sometimes, the reading application doesn't handle POST requests. CGIservlet already has to manage the HTTP request. Therefore, it can easily handle the POST request. If the variable $POSTtoGET is set to any non-false value, the content of whole POST request is added to the QUERY_STRING environment variable (preceeded by a '&' if necessary). The content-length is set to 0. If $POSTtoGET equals 'GET', the method will also be changed to 'GET'.

remarks:

All of the arguments of -d, -x, and -e are processed sequentially in this order. This might not be what you want so you should be carefull when using multiple executable arguments. If none of the executable arguments is DEFINED (i.e., they are entered as -d '' -e '' -x ''), each request is treated as a simple text-retrieval. THIS CAN BE A SECURITY RISK!

The wiring of an interactive web-server, which also calls shell scripts with the extension '.cgi', is in place. You can "activate" it by changing the "$ExecuteOSshell = 0;" line to "$ExecuteOSshell = 1;".
If you have trouble doing this, it might be a good idea to reconsider using a dynamic web server. Executing shell scripts inside a web server is a rather dangerous practise.

CGIservlet can run its "standard" web server from memory. At startup, all files are read into a hash table. Upon request, the contents of the file are placed in the environment variable: CGI_FILE_CONTENTS.
No further disk access is necessary. This means that:

  1. CGIservlet can run a WWW site from a removable disk, e.g., a floppy
  2. The web servlet can run without any read or write privilege.
  3. The integrity of the Web-site contents can be secured at the level you want
To compres the memory (RAM) immage, you should hook the compression function to
$CompressRAMimage = sub { return shift;};
and the decompression function to
$DecompressRAMimage = sub { return shift;};

license

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, 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 the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

Author: Rob van Son 
        email:
        R.J.J.H.vanSon@gmail.com 
        Institute of Phonetic Sciences/ACLC
        University of Amsterdam

        copying freely from the mhttpd server by Jerry LeVan (levan@eagle.eku.edu)
Date:   15 Jan 2002
Ver:    1.3
Env:    Perl 5.002 and later

Note:   CGIservlet.pl was directly inspired by Jerry LeVan's 
        (levan@eagle.eku.edu) simple mhttpd server which again was 
        inspired by work of others. CGIservlet is used as a bare bones 
        socket server for a single CGI script at a time.