11 perl -Ilib/PXElator -Mhttpd -e httpd::start
15 use Data::Dump qw/dump/;
22 $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
25 warn "pids ",dump( $pids );
26 foreach ( values %$pids ) {
28 kill 1,$_ || kill 9, $_;
35 our $debug = server::debug;
36 our $url = "http://$server::ip:$port";
43 my ($client,$path) = @_;
45 my $full = "$server::base_dir/tftp/$path";
49 if ( my $pid = fork ) {
52 print "http static child $pid\n";
53 $static_pids->{$pid} = $path;
57 my $type = 'application/octet-stream';
58 $type = 'text/html' if $path =~ m{\.htm};
59 $type = 'application/javascript' if $path =~ m{\.js};
60 $type = 'text/plain' if $path =~ m{\.txt};
62 my $size = -s $full || return;
64 print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
68 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
72 warn "static $path $type $size block: $block\n";
76 while( my $len = read $fh, $buff, $block ) {
80 progress_bar::tick( $path, $pos, $size );
87 warn "exit static child";
97 $SIG{CHLD} = 'IGNORE';
101 my $pid = $pids->{$daemon} || 'not started';
103 warn "start_stop $daemon $pid\n";
105 if ( $pid =~ m{^\d+$} ) {
106 my $pstree = `pstree -p $pid`;
107 my @pids = $pstree =~ m{\((\d+)\)}g;
108 warn "pstree $pstree pids ",dump( @pids );
109 kill 1, $_ foreach reverse @pids;
110 $pids->{$daemon} = 'stopped';
111 return qq|$daemon pid $pid stopped|;
115 $pids->{$daemon} = $pid;
116 warn "forked $daemon $pid\n";
117 return qq|$daemon pid $pid started|;
118 } elsif ( defined $pid ) {
120 my $invoke = 'start';
121 $invoke = $1 if $daemon =~ s{/(.+)}{};
122 my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
125 warn "can't start $daemon: $@" if $@;
133 my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
134 my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
137 my ( $client, $path, $param ) = @_;
141 warn "get_request $path ", $param ? dump( $param ) : '', "\n";
143 if ( my $found = static( $client,$path ) ) {
144 warn "static $found" if $debug;
145 } elsif ( $path eq '/' ) {
147 my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start';
148 my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| :
149 $pids->{screen} ? qq|start| : qq|start screen first|;
152 'ip', html::tt( $server::ip ),
153 'netmask', html::tt( $server::netmask ),
155 'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
160 warn 'pids: ', dump( $pids ) if $debug;
161 foreach my $name ( sort keys %$pids ) {
162 my $pid = $pids->{$name} || next;
164 my $html = qq|<a href=/start_stop/$name>$pid</a>|;
166 my $proc = "/proc/$pid/status";
170 $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
173 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
179 if ( $name->can('start_fork') ) {
180 $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
183 if ( $name->can('actions') ) {
184 $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
188 push @rows, ( $name => $html );
191 my $below_table = '';
193 warn 'static_pids: ', dump( $static_pids ) if $debug;
194 foreach my $pid ( keys %$static_pids ) {
195 my $path = $static_pids->{$pid};
196 if ( -d "/proc/$pid" ) {
197 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
198 } elsif ( $param->{clean_completed_downloads} ) {
199 delete $static_pids->{$pid}
201 push @rows, ( $path => "$pid competed" );
202 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
207 , html::table( 2, @rows )
209 , html::tabs( log::mac_changes )
213 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
214 eval 'our $' . $1 . ' = ' . $2;
216 print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
217 server::debug( $debug ) if $1 eq 'debug';
218 } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
219 print $client $redirect, start_stop($1);
220 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
222 print $client $redirect;
223 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
224 print $client $redirect;
225 kill 1, $1 || kill 9, $2 && warn "killed $1";
226 } elsif ( $path eq '/exit' ) {
229 } elsif ( $path =~ m{/boot} ) {
234 chain http://$server::ip:$httpd::port/
238 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
249 warn 'tap ', network::tap();
251 my $server = IO::Socket::INET->new(
253 LocalPort => $httpd::port,
256 ) || die "can't start server on $url: $!";
260 start_stop 'browser', $url;
265 my $client = $server->accept() || next; # ALARM trickle us
266 my $request = <$client>;
268 warn "request $request\n" if $debug;
270 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
273 if ( $path =~ s{\?(.+)}{} ) {
274 foreach my $p ( split(/[&;]/, $1) ) {
275 my ($n,$v) = split(/=/, $p, 2);
278 warn "param: ",dump( $param ) if $debug;
280 get_request $client, $path, $param;
282 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
287 <div style="font-size: 80%; color: #888">
288 <a href="">reload</a>
290 <a href=/exit>exit</a>
292 } if $client->connected;