11 perl -Ilib/PXElator -Mhttpd -e httpd::start
15 use Data::Dump qw/dump/;
23 $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
26 warn "pids ",dump( $pids );
27 foreach ( values %$pids ) {
29 kill 1,$_ || kill 9, $_;
36 our $debug = server::debug;
37 our $url = "http://$server::ip:$port";
42 use Time::HiRes qw/time/;
45 my ($client,$path) = @_;
47 my $full = "$server::base_dir/tftp/$path";
53 if ( my $pid = fork ) {
56 print "http static child $pid\n";
57 $static_pids->{$pid} = $path;
61 my $type = 'application/octet-stream';
62 $type = 'text/html' if $path =~ m{\.htm};
63 $type = 'application/javascript' if $path =~ m{\.js};
64 $type = 'text/plain' if $path =~ m{\.txt};
66 my $size = -s $full || return;
68 $client->autoflush(1);
70 print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
74 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
78 print "static $path $type $size block: $block\n";
80 while( my $len = read $fh, $buff, $block ) {
83 printf "%s %d/%d %.2f%% %.2f K/s\r"
85 , $size, $pos * 100 / $size
86 , ( $pos / 1024 ) / ( time() - $start_t )
102 $SIG{CHLD} = 'IGNORE';
106 my $pid = $pids->{$daemon};
108 warn "start_stop $daemon $pid\n";
110 if ( $pid =~ m{^\d+$} ) {
111 my $pstree = `pstree -p $pid`;
112 my @pids = $pstree =~ m{\((\d+)\)}g;
113 warn "pstree $pstree pids ",dump( @pids );
114 kill 1, $_ foreach reverse @pids;
115 $pids->{$daemon} = 'stopped';
116 return qq|$daemon pid $pid stopped|;
120 $pids->{$daemon} = $pid;
121 warn "forked $daemon $pid\n";
122 return qq|$daemon pid $pid started|;
123 } elsif ( defined $pid ) {
125 my $invoke = 'start';
126 $invoke = $1 if $daemon =~ s{/(.+)}{};
127 my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
130 warn "can't start $daemon: $@" if $@;
138 my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
139 my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
142 my ( $client, $path, $param ) = @_;
144 warn "get_request $path ", $param ? dump( $param ) : '', "\n";
146 if ( my $found = static( $client,$path ) ) {
147 warn "static $found" if $debug;
148 } elsif ( $path eq '/' ) {
150 my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start';
151 my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| :
152 $pids->{screen} ? qq|start| : qq|start screen first|;
155 'ip', html::tt( $server::ip ),
156 'netmask', html::tt( $server::netmask ),
158 'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
163 warn 'pids: ', dump( $pids ) if $debug;
164 foreach my $name ( sort keys %$pids ) {
165 my $pid = $pids->{$name} || next;
167 my $html = qq|<a href=/start_stop/$name>$pid</a>|;
169 my $proc = "/proc/$pid/status";
173 $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
176 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
182 if ( $name->can('start_fork') ) {
183 $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
186 if ( $name->can('actions') ) {
187 $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
191 push @rows, ( $name => $html );
194 my $below_table = '';
196 warn 'static_pids: ', dump( $static_pids ) if $debug;
197 foreach my $pid ( keys %$static_pids ) {
198 my $path = $static_pids->{$pid};
199 if ( -d "/proc/$pid" ) {
200 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
201 } elsif ( $param->{clean_completed_downloads} ) {
202 delete $static_pids->{$pid}
204 push @rows, ( $path => "$pid competed" );
205 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
210 , html::table( 2, @rows )
212 , html::tabs( log::mac_changes )
216 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
217 eval 'our $' . $1 . ' = ' . $2;
219 print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
220 server::debug( $debug ) if $1 eq 'debug';
221 } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
222 print $client $redirect, start_stop($1);
223 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
225 print $client $redirect;
226 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
227 print $client $redirect;
228 kill 1, $1 || kill 9, $2 && warn "killed $1";
229 } elsif ( $path eq '/exit' ) {
232 } elsif ( $path =~ m{/boot} ) {
237 chain http://$server::ip:$httpd::port/
241 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
251 my $server = IO::Socket::INET->new(
253 LocalPort => $httpd::port,
256 ) || die "can't start server on $url: $!";
260 start_stop 'browser', $url;
264 while (my $client = $server->accept()) {
265 $client->autoflush(1);
266 my $request = <$client>;
268 warn "request $request\n" if $debug;
270 Module::Refresh->refresh;
272 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
275 if ( $path =~ s{\?(.+)}{} ) {
276 foreach my $p ( split(/[&;]/, $1) ) {
277 my ($n,$v) = split(/=/, $p, 2);
280 warn "param: ",dump( $param ) if $debug;
282 get_request $client, $path, $param;
284 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
289 <div style="font-size: 80%; color: #888">
290 <a href="">reload</a>
292 <a href=/exit>exit</a>
294 } if $client->connected;