#! /usr/bin/perl -w # # Collect a web request, save to the file on the cmdline # use IO::Socket::INET; use IO::File; use Getopt::Long; $debug = 1; $port = 80; $directory = "."; GetOptions ("debug=i" => \$debug, "port=i" => \$port, "directory=s" => \$directory, ) or die "Argument errors"; chdir $directory or die "Unable to chdir to $directory $!\n"; $sock = new IO::Socket::INET(LocalPort => $port, Proto => 'tcp', Listen => 1, ReuseAddr => 1, ); $sock or die "Creating socket failed: $!\n"; print "Socket created\n" if $debug; # not multi-threaded or otherwise able to handle more than one # connection. while (1) { $in = $sock->accept() or die "accept failed: $!\n"; $out = open_next(); while (<$in>) { last if /^\r?\n?$/; s/\r//g; /\n$/ or s/$/\n/; print $out $_; print "Received '$_'\n" if $debug; } send_reply($in); undef $in; undef $out; } sub open_next { my $n = 0; while ( -f $n ) { $n++; } my $out = new IO::File("> $n"); defined($out) or die "Unable to open $n: $!\n"; return $out; } sub send_reply { my $peer = shift; print $peer "\r\n"; }