--- /dev/null
+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;