Fixed port-Bug.
authorHeiko Schlittermann <hs@schlittermann.de>
Tue, 27 Jan 2009 21:28:55 +0100
changeset 3 a00ff9ea5ad6
parent 2 c49b14910394
child 4 47d496816ba7
Fixed port-Bug.
hlog
--- a/hlog	Tue Jan 27 20:50:54 2009 +0100
+++ b/hlog	Tue Jan 27 21:28:55 2009 +0100
@@ -31,14 +31,14 @@
 
 sub handle_request($);
 sub date1123(;$);
-sub http($$);
+sub http($@);
 sub bad_request();
 
 MAIN: {
 
     GetOptions(
-        "addr" => \$opt_addr,
-        "port" => \$opt_port,
+        "addr=s" => \$opt_addr,
+        "port=i" => \$opt_port,
     ) or pod2usage();
 
     open(LOG, ">>$logfile");
@@ -49,7 +49,6 @@
     my $listener = new IO::Socket::INET(
         LocalAddr => $opt_addr,
         LocalPort => $opt_port,
-        Port      => $opt_port,
         Proto     => "tcp",
         Listen    => 1,
         ReuseAddr => 1,
@@ -58,11 +57,13 @@
     warn "listener $opt_addr:$opt_port\n";
     while (my $client = $listener->accept) {
 
-        print LOG localtime()
+        print LOG $_ = localtime()
           . " access from "
           . $client->peerhost . ":"
           . $client->peerport . "\n";
 
+	warn $_;
+
         my $pid = fork();
         die "Can't fork: $!\n" if not defined $pid;
         if ($pid == 0) {
@@ -91,27 +92,30 @@
     }
 
     open(my $file, $FILE);
-    $client->print(http "200 OK" => join "", 
-	"# Proof of concept ;-)\n",
-	"# see https://keller.schlittermann.de/hg/hlog\n",
-	<$file>);
+
+    $client->print(http "200 OK" => join "", <<__EOF, <$file>);
+# Proof of concept ;-)
+# see https://keller.schlittermann.de/hg/hlog
+#
+# FILE: $FILE 
+#
+__EOF
 
 }
 
-sub http($$) {
-    my ($code, $msg) = @_;
+sub http($@) {
+    my $code = shift;
     my $date = date1123();
 
-    my $type = $msg =~ /^<!DOCTYPE HTML/ ? "text/html" : "text/plain";
+    my $type = $_[0] =~ /^<!DOCTYPE HTML/ ? "text/html" : "text/plain";
 
-    return <<EOF;
+    return <<__EOF, @_;
 HTTP/1.1 $code
 Date: $date
 Connection: close
 Content-Type: $type
 
-$msg
-EOF
+__EOF
 }
 
 sub date1123(;$) {
@@ -124,7 +128,7 @@
 }
 
 sub bad_request() {
-    return <<'EOF';
+    return <<'__EOF';
 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
 <html><head>
 <title>400 Bad Request</title>
@@ -134,7 +138,7 @@
 />
 </p>
 </body></html>
-EOF
+__EOF
 }
 
 __END__