The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

p5httpd - tiny perl http server

SYNOPSIS

path/to/p5httpd.pl (or click on the icon)

DESCRIPTION

p5httpd is a simple HTTP 1.0 server written as a single perl file. Written for use on a hand-held machine, it should be useful on any machine as a quick and dirty, non-secure webserver for occasional use.

Understands PUT, GET, and HEAD, can do basic authentication and directory listings. CGI scripts can be executed or, if they are perl scripts, eval'ed.

INSTALLATION AND CONFIGURATION

p5httpd.pl is a single file, containing a small configuration section at the beginning, and this POD documentation at the end. This single file, unedited, is already functional, but it will be more useful if you unzip the whole distribution and edit the first few lines of the server program to adapt it to your installation

FORKING POLICY

Unix servers typically use fork() in order to be ready for the next request as soon as possible, delegating the hard work to a child process. This may result in better performance (e.g. when requesting a page with a lot of images), but perl CGI scripts will have to load all their modules every time they're run.

A non-forking server will run all scripts in the same interpreter process, an thus will have to load the modules ony once. For heavyweight modules like CGI.pm this may make a big difference.

p5httpd can be configured (with the config variable $when_to_fork) to fork always, never, or always except the first time a particular script is run. This last policy combines the advantages of the always-forking and never-forking policies, as the server (and hence its children) will have the script's required modules loaded after its first (non-forking) run. In this case, expensive re-initialisations can also be avoided.

EVAL OR EXECUTE?

cgi scripts can be executed as a separate process, or they can be run (eval'ed) in the same interpreter as the server, if they are written in perl. You can configure the sever (with the config variable $eval_or_execute) to always execute, or always eval cgi scripts. It can also look at the script and try to find out whether it is perl (it then looks for the slashbang pattern #!/.../perl).

SECURITY AND AUTHENTICATION

p5httpd should not be used when security is critical. It can only use the "Basic" authentication scheme, where usernames and passwords are sent unencrypted over the network. It uses the same htpasswd files as apache (use the htpasswd (1) program, or http://www.euronet.nl/~arnow/htpasswd/ to generate them).

A list of public directories and another list of private directories (in the config variables @public_directories and @private_directories) determines when authentication is requested: for any file access, p5httpd climbs up the directory tree until it finds a directory in either list (the public list is tried first)

PATH INFO

As a workaround for a bug in EPOC Opera (which will not reliably POST to an URL of the form /path/to.cgi?args) any requests to /path/to.cgi/args are redirected to /path/to.cgi?args. This is not path info as per HTTP/1.0, and PATH_INFO will not be set.

CGI SCRIPT PITFALLS WITH p5httpd

Depending on the forking policy,and whether cgi's are eval'ed or executed, you may have to take some care when writing your scripts. When all cgi's are executed, and/or when the server forks for evey request, your scripts execute with "a clean slate" every time. This is the setting to use whenever you use cgi's that normally run on e.g. apache, at least initially, before you try the more dangerous (but possibly faster) settings.

On the other extreme, when you evaluate your scripts and never fork(), (which is the only setting that works on EPOC/psion), there are a couple of things to watch out for:

scope issues

All CGI scripts run in a separate namespace, derived from the script name. Just as with e.g. mod_perl, package globals remain defined across invocations. This may be very useful in some situations (e.g. for preserving an expensive initialisation), but you should be aware of it. Unless you know what you're doing, take the following advice from the mod_perl FAQ:

Properly scope your variables. Stop and read that sentence again. Conventional CGI scripts can be as sloppy with their namespace as they want, since they are restarted anew for each request. Your mod_perl script has a much longer lifetime (potentially as long as your [...] server is running), and requires much more care. Scope everything except long-lived variables with my() and use strict; so Perl will demand that you recognize your global variables.

Localize global variables. If you change any of Perl's global variables (e.g. $/ to change the input record separator), or even your own global variables remember to reset them or better still, always localize global variables before using them, e.g. local($/) = undef. If you can, reduce your dependency on global variables

die() and exit()

If you call exit() in your script, the whole server will quit (die() will just print an error message). CGI scripts may hang, or even crash, the server. Filehandles remain open across invocations.

CGI.pm

If you use the CGI.pm package, you have to use the (undocumented) subroutine CGI::initialize_globals() to get it to re-read the script parameters. If you don't, the script will only read them the first time it runs.

The server does a chdir to a script's directory before running it, and sets the environment variable SERVER_ROOT to the absolute pathname of the server root directory.

REQUIRES

p5httpd needs perl 5.6.0 or newer It works on machines that cannot fork() (like Psion handhelds) but it can use fork() if available. Needs the IPC::Open2 module whenever a cgi should be executed (not eval'ed). This module may only be present on Unix-like systems.

CREDITS

Originally based on phttpd (pure perl httpd, (C) Paul Tchistopolskii 1998, 99 The Wiki packaged with this server is based on QuickiWiki (C) Copyright 1999-2000 Ward Cunningham.

AUTHOR

Hans Lub. Bug reports to hlub@knoware.nl

COPYRIGHT

Copyright (c) 2002-2004 Hans Lub. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself