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";
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";
67 print "static $path $type $size\n";
73 while( my $len = read $fh, $buff, $block ) {
76 printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;
81 print "$path $pos == $size OK\n";
91 $SIG{CHLD} = 'IGNORE';
95 my $pid = $pids->{$daemon};
97 warn "start_stop $daemon $pid pids: ",dump( $pids );
99 if ( $pid =~ m{^\d+$} ) {
100 my $pstree = `pstree -p $pid`;
101 my @pids = $pstree =~ m{\((\d+)\)}g;
102 warn "pstree $pstree pids ",dump( @pids );
103 kill 1, $_ foreach reverse @pids;
104 $pids->{$daemon} = 'stopped';
105 return qq|$daemon pid $pid stopped|;
109 $pids->{$daemon} = $pid;
110 warn "forked $daemon $pid";
111 return qq|$daemon pid $pid started|;
112 } elsif ( defined $pid ) {
114 my $eval = $daemon . '::start(' . ( @_ ? dump(@_) : '' ) . ')';
117 warn "can't start $daemon: $@" if $@;
125 my $ok = qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
126 my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
129 my ( $client, $path, $param ) = @_;
131 warn "get_request $client $path ",dump( $param );
133 if ( my $found = static( $client,$path ) ) {
134 warn "static $found" if $debug;
135 } elsif ( $path eq '/' ) {
137 my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>| : 'start';
138 my $kvm = $pids->{kvm} ? qq|stop <tt>$pids->{kvm}</tt>| :
139 $pids->{screen} ? qq|start| : qq|start screen first|;
142 'ip', html::tt( $server::ip ),
143 'netmask', html::tt( $server::netmask ),
145 'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
150 warn 'pids: ', dump( $pids );
151 foreach my $name ( keys %$pids ) {
152 my $pid = $pids->{$name} || next;
154 my $html = qq|<a href=/$name>$pid</a>|;
157 $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
159 my $proc = "/proc/$pid/status";
161 .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
167 push @rows, ( $name => $html );
170 warn 'static_pids: ', dump( $static_pids );
171 foreach my $pid ( keys %$static_pids ) {
172 my $path = $static_pids->{$pid};
173 if ( -d "/proc/$pid" ) {
174 push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
176 push @rows, ( $path => "$pid competed" );
181 , html::table( 2, @rows )
182 , html::tabs( log::mac_changes )
186 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
187 eval 'our $' . $1 . ' = ' . $2;
189 print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
190 server::debug( $debug ) if $1 eq 'debug';
191 } elsif ( $path =~ m{^/(screen|kvm)} ) {
192 print $client $redirect, start_stop($1);
193 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
194 print $client $redirect;
195 kill 9, $1 && warn "killed $1";
196 } elsif ( $path eq '/exit' ) {
199 } elsif ( $path =~ m{/boot} ) {
204 chain http://$server::ip:$httpd::port/
208 print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
218 my $server = IO::Socket::INET->new(
220 LocalPort => $httpd::port,
223 ) || die "can't start server on $url: $!";
227 start_stop 'browser', $url;
231 while (my $client = $server->accept()) {
232 $client->autoflush(1);
233 my $request = <$client>;
235 warn "request $request\n" if $debug;
237 Module::Refresh->refresh;
239 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
242 if ( $path =~ s{\?(.+)}{} ) {
243 foreach my $p ( split(/[&;]/, $1) ) {
244 my ($n,$v) = split(/=/, $p, 2);
247 warn "param: ",dump( $param ) if $debug;
249 warn "path $path param: ",dump( $param );
250 get_request $client, $path, $param;
252 print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
257 <div style="font-size: 80%; color: #888">
258 <a href="">reload</a>
260 <a href=/exit>exit</a>
262 } if $client->connected;