The objective was to write a simple HTTP server that supported resuming of downloads via the Range heeaders. The server should also be fairly secure, as it runs unattended on my firewall box, and thus, it should also not require any fancy setup that could be misconfigured.

The server must be run with the taint option of Perl for even more security. I toyed with the idea of locking the server in a chroot() jail, but getting Perl/HTTP::Daemon to work in such a jail is no easy feat, so I ditched the idea (see objective above).

The server works under both, Win32 and Unix, but under Win32 it dosen't support fork, as the fork emulation is not always working for me. In the spirit of simplicity, it also dosen't support any CGI capabilities, since CGI capabilities present a security hole waiting to be exploited. Also, "hidden" files starting with a dot (".") are not available for download as well. On the upside, the server fully supports persistent connections.

Of course, the total number of lines is arbitrarily set at 99 lines, as there is much whitespace to be compressed, but I wanted to keep a sensible mix of obfuscation/compression and readability, as I think that maybe other people than me would want to use this code.

Command line options

perl -wT miniserver.pl [document_root]

Disclaimer

Even though I tried to be as paranoid as possible about my own code, I can't guarantee that the code is secure in any way other than taking up valuable CPU time and/or disk space. If you find any interesting exploits, please add them in a reply here, so others can learn from my mistakes.

2001-10-05 Update: Fixed missing use strict; at the top of the script. Thanks to ybiC for mentioning it to me. My Pascal upbringing seems to have protected me against violating strict, as no code rework was necessary...

2004-07-11 Update: Fixed _stamp routine so it actually displays a useful timestamp. Thanks to BrowserUK for finding it in that code.

Used this 12 years later as the base for a simple web service on a server for which I cannot modify the Perl distribution. Simple, performant, working smootlhy, multi-forking like a pro. Thank you Corion.

Interesting! Here's what I got..
- had trouble running it with -wT in line 1; was able to run it after cutting that out as you suggest, perl -wT perlsvr.pl perlsvrhome
- could not get -h to work (I think this and above mean my getopts or something is corrupt)

- does not close connection right away though kills child.
- sending ctrl-c instead of something like GET / causes child to quit with
"Use of uninitialized value at /usr/lib/perl5/site_perl/5.005/HTTP/Daemon.pm line 498."
which is a funny reason to quit..
- was able to download a file set to chmod 700 and ending in .pl

So I'm not so sure about the security side, although presumably
perl's proof against buffer attacks, we are told. How about
keeping a list of children and killing them after a certain
amount of time just in case? I wanted to do a kill -HUP on it
when I had made a lot of children processes, but it just died.

Trouble with running the server without -T on the command line - I get this error every time
I try to use taint mode : Too late for -T. I don't know about any way around it except
specifying -T on the command line.

-h dosen't work. - There is no -h option, so it won't work.

Connections are not always closed when a child exits. I'm not sure when this happens - my experience showed that IE kept a child alive until either some timeout happened or I closed that IE window (or visited another URL with that window)

Sending ^C to the server gives a funny error message - This is a "problem" with HTTP::Daemon which seems to want to call a ^C (instead of GET) method. I don't know how to fix this without patching HTTP::Daemon.

.pl files will be downloaded, even though they are executable and only readable by the user running the webserver. - This works as designed. Everything below the webserver root is exported, and as long as the user under which the webserver is run can read the file it will transfer that file. Note that especially no execution of files is possible.

I'm not keen to add any more features to that server as it would transcend both the line count limit and the complexity limit - also, this server is used to stream 200MB files over my meager 128kbit line, so each child lives a very long time.