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";
44 my ($client,$path) = @_;
46 my $full = "$server::base_dir/tftp/$path";
50 if ( my $pid = fork ) {
53 print "http static child $pid\n";
54 $static_pids->{$pid} = $path;
58 my $type = 'application/octet-stream';
59 $type = 'text/html' if $path =~ m{\.htm};
60 $type = 'application/javascript' if $path =~ m{\.js};
61 $type = 'text/plain' if $path =~ m{\.txt};
63 my $size = -s $full || return;
65 print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
69 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
73 warn "static $path $type $size block: $block\n";
77 while( my $len = read $fh, $buff, $block ) {
81 progress_bar::tick( $path, $pos, $size );
88 warn "exit static child";
98 $SIG{CHLD} = 'IGNORE';
102 my $pid = $pids->{$daemon};
104 warn "start_stop $daemon $pid\n";
106 if ( $pid =~ m{^\d+$} ) {
107 my $pstree = `pstree -p $pid`;
108 my @pids = $pstree =~ m{\((\d+)\)}g;
109 warn "pstree $pstree pids ",dump( @pids );
110 kill 1, $_ foreach reverse @pids;
111 $pids->{$daemon} = 'stopped';
112 return qq|$daemon pid $pid stopped|;
116 $pids->{$daemon} = $pid;
117 warn "forked $daemon $pid\n";
118 return qq|$daemon pid $pid started|;
119 } elsif ( defined $pid ) {
121 my $invoke = 'start';
122 $invoke = $1 if $daemon =~ s{/(.+)}{};
123 my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
126 warn "can't start $daemon: $@" if $@;
134 my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
135 my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
138 my ( $client, $path, $param ) = @_;
140 warn "get_request $path ", $param ? dump( $param ) : '', "\n";
142 if ( my $found = static( $client,$path ) ) {
143 warn "static $found" if $debug;
144 } elsif ( $path eq '/' ) {
146 my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start';
147 my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| :
148 $pids->{screen} ? qq|start| : qq|start screen first|;
151 'ip', html::tt( $server::ip ),
152 'netmask', html::tt( $server::netmask ),
154 'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
159 warn 'pids: ', dump( $pids ) if $debug;
160 foreach my $name ( sort keys %$pids ) {
161 my $pid = $pids->{$name} || next;
163 my $html = qq|<a href=/start_stop/$name>$pid</a>|;
165 my $proc = "/proc/$pid/status";
169 $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
172 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
178 if ( $name->can('start_fork') ) {
179 $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
182 if ( $name->can('actions') ) {
183 $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
187 push @rows, ( $name => $html );
190 my $below_table = '';
192 warn 'static_pids: ', dump( $static_pids ) if $debug;
193 foreach my $pid ( keys %$static_pids ) {
194 my $path = $static_pids->{$pid};
195 if ( -d "/proc/$pid" ) {
196 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
197 } elsif ( $param->{clean_completed_downloads} ) {
198 delete $static_pids->{$pid}
200 push @rows, ( $path => "$pid competed" );
201 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
206 , html::table( 2, @rows )
208 , html::tabs( log::mac_changes )
212 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
213 eval 'our $' . $1 . ' = ' . $2;
215 print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
216 server::debug( $debug ) if $1 eq 'debug';
217 } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
218 print $client $redirect, start_stop($1);
219 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
221 print $client $redirect;
222 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
223 print $client $redirect;
224 kill 1, $1 || kill 9, $2 && warn "killed $1";
225 } elsif ( $path eq '/exit' ) {
228 } elsif ( $path =~ m{/boot} ) {
233 chain http://$server::ip:$httpd::port/
237 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
247 my $server = IO::Socket::INET->new(
249 LocalPort => $httpd::port,
252 ) || die "can't start server on $url: $!";
256 start_stop 'browser', $url;
260 while (my $client = $server->accept()) {
261 $client->autoflush(1);
262 my $request = <$client>;
264 warn "request $request\n" if $debug;
266 Module::Refresh->refresh;
268 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
271 if ( $path =~ s{\?(.+)}{} ) {
272 foreach my $p ( split(/[&;]/, $1) ) {
273 my ($n,$v) = split(/=/, $p, 2);
276 warn "param: ",dump( $param ) if $debug;
278 get_request $client, $path, $param;
280 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
285 <div style="font-size: 80%; color: #888">
286 <a href="">reload</a>
288 <a href=/exit>exit</a>
290 } if $client->connected;