--- lib/PXElator/httpd.pm 2009/07/29 19:57:07 43
+++ lib/PXElator/httpd.pm 2009/07/30 17:07:48 65
@@ -23,30 +23,42 @@
our $debug = 1;
use server;
-our $url = "http://$server::ip:$port/";
+our $url = "http://$server::ip:$port";
use html;
sub static {
my ($client,$path) = @_;
- $path = "tftp/$path";
+ my $full = "$server::base_dir/tftp/$path";
- if ( ! -e $path || -d $path ) {
- print $client "HTTP/1.0 404 $path not found\r\n";
- return;
- }
+ return if ! -f $full;
- my $type = 'text/plain';
+ my $type = 'application/octet-stream';
$type = 'text/html' if $path =~ m{\.htm};
$type = 'application/javascript' if $path =~ m{\.js};
+ $type = 'text/plain' if $path =~ m{\.txt};
+
+ my $size = -s $full || return;
+
+ print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
+
+ open(my $fh, $full);
+ print "static $path $type $size\n";
- 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 $_;
+ my $block = 8192;
+ my $buff;
+ my $pos = 0;
+
+ while( my $len = read $fh, $buff, $block ) {
+ print $client $buff;
+ $pos += $len;
+ printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;
}
- close($html);
+ close($fh);
+ close($client);
+
+ print "$path $pos == $size OK\n";
return $path;
}
@@ -55,24 +67,71 @@
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 $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|,
+ 'pid', html::tt( $$ ),
+ 'ip', html::tt( $server::ip ),
+ 'netmask', html::tt( $server::netmask ),
+ '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
@@ -87,6 +146,7 @@
}
}
+
sub start {
my $server = IO::Socket::INET->new(
@@ -137,4 +197,6 @@
die "server died";
}
+warn "loaded";
+
1;