--- lib/PXElator/httpd.pm 2009/07/29 17:42:48 42
+++ lib/PXElator/httpd.pm 2009/07/30 14:16:59 57
@@ -17,6 +17,7 @@
use File::Slurp;
#use JSON;
use IO::Socket::INET;
+use Module::Refresh;
our $port = 7777;
our $debug = 1;
@@ -24,6 +25,8 @@
use server;
our $url = "http://$server::ip:$port/";
+use html;
+
sub static {
my ($client,$path) = @_;
@@ -50,6 +53,85 @@
my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";
+use boolean;
+
+use screen;
+use kvm;
+our $pids;
+
+$SIG{CHLD} = 'IGNORE';
+
+sub start_stop {
+ my $daemon = shift;
+ my $pid = $pids->{$daemon};
+
+ warn "start_stop $daemon pids: ",dump( $pids );
+
+ if ( $pid ) {
+ warn "kill 9 $pid";
+ kill 9, $pid;
+ delete $pids->{$daemon};
+ return qq|$daemon pid $pid stopped|;
+ } else {
+ if ( $pid = fork ) {
+ # parent
+ $pids->{$daemon} = $pid;
+ warn "forked $daemon $pid";
+ return qq|$daemon pid $pid started|;
+ } elsif ( defined $pid ) {
+ # child
+ my $eval = $daemon . '::start';
+ warn "eval $eval";
+ eval $eval;
+ warn "can't start $daemon: $@" if $@;
+ exit;
+ } else {
+ die "fork error $!";
+ }
+ }
+}
+
+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 = $pids->{screen} ? qq|stop $pids->{screen}| : 'start';
+ my $kvm = $pids->{kvm} ? qq|stop $pids->{kvm}| :
+ $pids->{screen} ? qq|start| : qq|start screen first|;
+
+ print $client $ok,
+ html::table( 2,
+ 'pid', $$,
+ 'debug', qq|$debug|,
+ 'screen', qq|$screen|,
+ 'kvm', qq|$kvm|,
+ );
+
+ } 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|kvm)} ) {
+ print $client $ok, start_stop($1);
+ } 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 +140,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 +152,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
- };
+