--- lib/PXElator/httpd.pm 2009/07/29 17:42:48 42 +++ lib/PXElator/httpd.pm 2009/07/30 12:10:53 53 @@ -17,13 +17,21 @@ use File::Slurp; #use JSON; use IO::Socket::INET; +use Module::Refresh; +use Parallel::ForkManager; our $port = 7777; our $debug = 1; +my $pm = Parallel::ForkManager->new(10); + +our $screen_pid; + use server; our $url = "http://$server::ip:$port/"; +use html; + sub static { my ($client,$path) = @_; @@ -50,6 +58,66 @@ my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n"; +use boolean; +use screen; + +sub get_request { + my ( $client, $path, $param ) = @_; + + warn "get_request $client $path ",dump( $param ); + + if ( my $found = static( $client,$path ) ) { + warn "static $found" if $debug; + } elsif ( $path eq '/' ) { + + my $screen = $screen_pid ? qq|stop $screen_pid| : 'start'; + + print $client $ok, + html::table( 2, + 'pid', $$, + 'debug', qq|$debug|, + 'screen', qq|$screen|, + ); + + } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) { + eval 'our $' . $1 . ' = ' . $2; + warn $@ if $@; + print $client qq|HTTP/1.1 302 Found\r\nLocation: $url\r\nContent-type: text/html\r\n\r\n$1 = $2
Location: $url|; + } elsif ( $path =~ m{^/screen} ) { + + if ( $screen_pid ) { + if ( ! kill $screen_pid ) { + warn "kill -9 $screen_pid"; + if ( kill 9, $screen_pid ) { + print $client $ok, qq|screen pid $screen_pid stopped|; + $screen_pid = 0; + } else { + print $client $ok, qq|can't stop screen $screen_pid: $!|; + } + } + } else { + if ( $screen_pid = $pm->start ) { + print $client $ok, qq|screen started pid $screen_pid|; + } else { + screen::start; + $pm->finish; + } + } + + } elsif ( $path =~ m{/boot} ) { + print $client qq{$ok +#!gpxe +imgfree +login +chain http://$server::ip:$httpd::port/ + + }; + } else { + print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n404 $path"; + warn "404 $path"; + } + +} sub start { @@ -58,9 +126,7 @@ LocalPort => $httpd::port, Listen => SOMAXCONN, Reuse => 1 - ); - - die "can't setup server" unless $server; + ) || die "can't start server on $url: $!"; print "url $url\n"; @@ -72,51 +138,37 @@ warn "request $request\n" if $debug; + Module::Refresh->refresh; + if ($request =~ m{^GET (/.*) HTTP/1.[01]}) { - my $method = $1; + my $path = $1; my $param; - if ( $method =~ s{\?(.+)}{} ) { + if ( $path =~ 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 $$ - 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"; - } + warn "path $path param: ",dump( $param ); + get_request $client, $path, $param; } 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{ - reload - }; +
+ reload + index +
+ } if $client->connected; - close($client); } die "server died"; } +warn "loaded"; + 1;