simple httpd server
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 29 Jul 2009 17:42:48 +0000 (17:42 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 29 Jul 2009 17:42:48 +0000 (17:42 +0000)
lib/PXElator/httpd.pm [new file with mode: 0644]
lib/PXElator/server.pm [new file with mode: 0644]

diff --git a/lib/PXElator/httpd.pm b/lib/PXElator/httpd.pm
new file mode 100644 (file)
index 0000000..b869c33
--- /dev/null
@@ -0,0 +1,122 @@
+package httpd;
+
+use warnings;
+use strict;
+use autodie;
+
+=head1 httpd
+
+Start with:
+
+  perl -Ilib/PXElator -Mhttpd -e httpd::start
+
+=cut
+
+use Data::Dump qw/dump/;
+use Carp qw/confess/;
+use File::Slurp;
+#use JSON;
+use IO::Socket::INET;
+
+our $port = 7777;
+our $debug = 1;
+
+use server;
+our $url = "http://$server::ip:$port/";
+
+sub static {
+       my ($client,$path) = @_;
+
+       $path = "tftp/$path";
+
+       if ( ! -e $path ||  -d $path ) {
+               print $client "HTTP/1.0 404 $path not found\r\n";
+               return;
+       }
+
+       my $type = 'text/plain';
+       $type = 'text/html' if $path =~ m{\.htm};
+       $type = 'application/javascript' if $path =~ m{\.js};
+
+       print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: ", -s $path,"\r\n\r\n";
+       open(my $html, $path);
+       while(<$html>) {
+               print $client $_;
+       }
+       close($html);
+
+       return $path;
+}
+
+my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";
+
+
+sub start {
+
+       my $server = IO::Socket::INET->new(
+                       Proto     => 'tcp',
+                       LocalPort => $httpd::port,
+                       Listen    => SOMAXCONN,
+                       Reuse     => 1
+       );
+                                                                         
+       die "can't setup server" unless $server;
+
+       print "url $url\n";
+
+       system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &";
+
+       while (my $client = $server->accept()) {
+               $client->autoflush(1);
+               my $request = <$client>;
+
+               warn "request $request\n" if $debug;
+
+               if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
+                       my $method = $1;
+                       my $param;
+                       if ( $method =~ s{\?(.+)}{} ) {
+                               foreach my $p ( split(/[&;]/, $1) ) {
+                                       my ($n,$v) = split(/=/, $p, 2);
+                                       $param->{$n} = $v;
+                               }
+                               warn "param: ",dump( $param ) if $debug;
+                       }
+                       warn "method $method";
+
+                       if ( my $path = static( $client,$1 ) ) {
+                               warn "static $path" if $debug;
+                       } elsif ( $method eq '/' ) {
+                               print $client qq{$ok
+                                       pid <tt>$$</tt>
+                                       debug $debug
+                               };
+                               
+                       } elsif ( $method =~ m{/boot} ) {
+                               print $client qq{$ok
+#!gpxe
+imgfree
+login
+chain http://$server::ip:$httpd::port/
+
+                                       };
+                       } else {
+                               print $client "HTTP/1.0 404 Unkown method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n404 $request";
+                               warn "404 $request";
+                       }
+               } else {
+                       print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
+                       warn "500 $request";
+               }
+
+               print $client qq{
+               <a href="">reload</a>   
+               };
+
+               close($client);
+       }
+
+       die "server died";
+}
+
+1;
diff --git a/lib/PXElator/server.pm b/lib/PXElator/server.pm
new file mode 100644 (file)
index 0000000..4dad1a8
--- /dev/null
@@ -0,0 +1,4 @@
+package server;
+
+our $ip = '172.16.10.1';
+