#!/usr/local/bin/perl
# desc{  a nice 404-handler program for a web server  }
# Time-stamp: "2005-08-19 00:38:13 ADT"   sburke@cpan.org
#
# This is a nice 404-handler program for a web server.
#  This is free software.  Share and enjoy.

use strict;
no utf8;   # we want \xFF to mean literal character FF, not UTF8 of char u+00FF.
 # If your Perl doesn't nkow what "no utf8" means, then just comment
 # out that line.

my $req = $ENV{'REDIRECT_URL'} || '??';

# Consider the media type of what we were asked for:
print "Status: 404 Not Found\n";

if($req =~ m<\.(?:gif|jpe?g|png)$>si) {
  # Really no point in returning HTML if what they asked for was an image.
  print "Content-type: image/gif\n\n";
  binmode(STDOUT);
  print
   # Image data of the text "NOT FOUND!" in hard-to-miss colors,
   #  in a very small GIF:
   "\x47\x49\x46\x38\x39\x61\x47\x00\x10\x00\x91\x00\x00\xFF\xFF\xFF",
   # The text color, then the background color:
     "\xFF\x00\xFF",  # screaming magenta
     "\x00\xFF\xFF",  # screaming cyan
   "\x00\x00\x00\x2C\x00\x00\x00\x00\x47\x00",
   "\x10\x00\x00\x02\x89\x8C\x8F\xA9\xCB\xED\xAF\x84\x9C\xB4\xDA\x8B",
   "\xB3\xDE\xD3\xF0\x0F\x86\xA1\xD7\x05\xA5\x84\x08\x51\x79\xA8\xAD",
   "\x8B\x9A\x24\x9C\x9C\x76\x6C\xD2\x39\x39\xDF\xBC\xBC\x03\x06\x61",
   "\xC4\x9E\xB1\xE5\xF9\xC5\x2C\x33\xA5\x13\x09\x5C\x12\xA5\xB8\x94",
   "\x72\xEA\x1B\x3E\x6B\xD7\x23\x25\x09\x95\xF6\xAA\xC5\xA8\xCE\x05",
   "\x16\xE7\xA8\x65\xF3\xF5\xBB\x7E\x2E\x7F\xEE\xE1\x6D\x9A\x2E\x33",
   "\xE3\xDA\xFE\xE9\x5D\xE1\x35\x67\x37\x86\xE7\x77\x36\xA8\x06\xB7",
   "\xA8\xD6\xB4\x76\x97\x42\x66\x88\xE6\xC8\x28\x72\x89\x29\x52\x98",
   "\xC9\xD9\x79\xE7\x09\xCA\x09\x31\x4A\x5A\x6A\x6A\x50\x00\x00\x3B",
  ;
} else {
  # Emit a SMALL SIMPLE error page.

  $req =~   # amquote things severely
    s<([^\x20\x21\x23\x27-\x3b\x3d\x3F-\x5B\x5D-\x7E])>
     <'&#'.(ord($1)).';'>seg
  ;
  
  print "Content-type: text/html\n\n", <<"EOHTML";
<html lang="en-US">
<head><title>404: Not Found</title></head>
<body><h1>404: Not Found</h1>
<p><b>$req</b> could not be found.
<br>You can try looking <a ref="./">in its containing directory</a> or
<a ref="../">one directory higher</a>, or at
<a ref="/">the server's home page</a>.
</p></body></html>
EOHTML

}
exit;

__END__
