/[pxelator]/lib/PXElator/httpd.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/PXElator/httpd.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 88 by dpavlin, Fri Jul 31 18:16:14 2009 UTC revision 149 by dpavlin, Wed Aug 5 13:25:55 2009 UTC
# Line 17  use Carp qw/confess/; Line 17  use Carp qw/confess/;
17  use File::Slurp;  use File::Slurp;
18  #use JSON;  #use JSON;
19  use IO::Socket::INET;  use IO::Socket::INET;
20  use Module::Refresh;  
21    sub menu {qq{
22    
23    <div style="font-size: 80%; color: #888">
24    <a href=/>home</a>
25    <a href=/server>server</a>
26    </div>
27    
28    }}
29    
30  our $pids;  our $pids;
31  $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh  $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
# Line 38  our $url = "http://$server::ip:$port"; Line 46  our $url = "http://$server::ip:$port";
46    
47  use html;  use html;
48  our $static_pids;  our $static_pids;
49    use progress_bar;
50    
51  sub static {  sub static {
52          my ($client,$path) = @_;          my ($client,$path) = @_;
# Line 64  sub static { Line 73  sub static {
73          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
74    
75          open(my $fh, $full);          open(my $fh, $full);
         print "static $path $type $size\n";  
76    
77          my $block = 8192;          my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
78          my $buff;          my $buff;
79          my $pos = 0;          my $pos = 0;
80    
81            warn "static $path $type $size block: $block\n";
82    
83            progress_bar::start;
84    
85          while( my $len = read $fh, $buff, $block ) {          while( my $len = read $fh, $buff, $block ) {
86                  print $client $buff;                  print $client $buff;
87                    $client->flush;
88                  $pos += $len;                  $pos += $len;
89                  printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;                  progress_bar::tick( $path, $pos, $size );
90          }          }
91          close($fh);          close($fh);
92          close($client);          close($client);
93    
94          print "$path $pos == $size OK\n";          print STDERR "\n";
95    
96            warn "exit static child";
97    
98          exit;          exit(0);
99  }  }
100    
101  use boolean;  use boolean;
# Line 92  $SIG{CHLD} = 'IGNORE'; Line 107  $SIG{CHLD} = 'IGNORE';
107    
108  sub start_stop {  sub start_stop {
109          my $daemon = shift;          my $daemon = shift;
110          my $pid = $pids->{$daemon};          my $pid = $pids->{$daemon} || 'not started';
111    
112          warn "start_stop $daemon $pid pids: ",dump( $pids );          warn "start_stop $daemon $pid\n";
113    
114          if ( $pid =~ m{^\d+$} ) {          if ( $pid =~ m{^\d+$} ) {
115                  my $pstree = `pstree -p $pid`;                  my $pstree = `pstree -p $pid`;
# Line 107  sub start_stop { Line 122  sub start_stop {
122                  if ( $pid = fork ) {                  if ( $pid = fork ) {
123                          # parent                          # parent
124                          $pids->{$daemon} = $pid;                          $pids->{$daemon} = $pid;
125                          warn "forked $daemon $pid";                          warn "forked $daemon $pid\n";
126                          return qq|$daemon pid $pid started|;                          return qq|$daemon pid $pid started|;
127                  } elsif ( defined $pid ) {                  } elsif ( defined $pid ) {
128                          # child                          # child
# Line 130  my $redirect = qq|HTTP/1.1 302 Found\r\n Line 145  my $redirect = qq|HTTP/1.1 302 Found\r\n
145  sub get_request {  sub get_request {
146          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
147    
148          warn "get_request $client $path ",dump( $param );          server->refresh;
149    
150            warn "get_request $path ", $param ? dump( $param ) : '', "\n";
151    
152          if ( my $found = static( $client,$path ) ) {          if ( my $found = static( $client,$path ) ) {
153                  warn "static $found" if $debug;                  warn "static $found" if $debug;
# Line 141  sub get_request { Line 158  sub get_request {
158                                           $pids->{screen} ? qq|start|                                                    : qq|start screen first|;                                           $pids->{screen} ? qq|start|                                                    : qq|start screen first|;
159    
160                  my @rows = (                  my @rows = (
                         'ip',           html::tt( $server::ip ),  
                         'netmask',      html::tt( $server::netmask ),  
   
161                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
162                  );                  );
163    
164                  my $debug_proc;                  my $debug_proc = '';
165    
166                  warn 'pids: ', dump( $pids ) if $debug;                  warn 'pids: ', dump( $pids ) if $debug;
167                  foreach my $name ( sort keys %$pids ) {                  foreach my $name ( sort keys %$pids ) {
168                          my $pid = $pids->{$name} || next;                          my $pid = $pids->{$name} || next;
169    
170                          my $html = qq|<a href=/$name>$pid</a>|;                          my $html = qq|<a href=/start_stop/$name>$pid</a>|;
171    
172                          my $proc = "/proc/$pid/status";                          my $proc = "/proc/$pid/status";
173    
# Line 180  sub get_request { Line 194  sub get_request {
194                          push @rows, ( $name => $html );                          push @rows, ( $name => $html );
195                  }                  }
196    
197                    my $below_table = '';
198    
199                  warn 'static_pids: ', dump( $static_pids ) if $debug;                  warn 'static_pids: ', dump( $static_pids ) if $debug;
200                  foreach my $pid ( keys %$static_pids ) {                  foreach my $pid ( keys %$static_pids ) {
201                          my $path = $static_pids->{$pid};                          my $path = $static_pids->{$pid};
202                          if ( -d "/proc/$pid" ) {                          if ( -d "/proc/$pid" ) {
203                                  push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );                                  push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
204                            } elsif ( $param->{clean_completed_downloads} ) {
205                                    delete $static_pids->{$pid}
206                          } else {                          } else {
207                                  push @rows, ( $path => "$pid competed" );                                  push @rows, ( $path => "$pid competed" );
208                                    $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
209                          }                          }
210                  }                  }
211    
212                  print $client $ok                  print $client $ok
213                            , menu()
214                          , html::table( 2, @rows )                          , html::table( 2, @rows )
215                            , $below_table
216                          , html::tabs( log::mac_changes )                          , html::tabs( log::mac_changes )
217                          , $debug_proc                          , $debug_proc
218                          ;                          ;
219    
220            } elsif ( $path =~ m{^/server} ) {
221                    print $client $ok
222                            , menu()
223                            , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) )
224                            ;
225          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
226                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
227                  warn $@ if $@;                  warn $@ if $@;
228                  print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;                  print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
229                  server::debug( $debug ) if $1 eq 'debug';                  server::debug( $debug ) if $1 eq 'debug';
230          } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) {          } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
231                  print $client $redirect, start_stop($1);                  print $client $redirect, start_stop($1);
232          } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {          } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
233                  $1->$2();                  $1->$2();
# Line 228  chain http://$server::ip:$httpd::port/ Line 254  chain http://$server::ip:$httpd::port/
254  }  }
255    
256  use browser;  use browser;
257    use network;
258    
259  sub start {  sub start {
260    
261            warn 'tap ', network::tap();
262    
263          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
264                          Proto     => 'tcp',                          Proto     => 'tcp',
265                          LocalPort => $httpd::port,                          LocalPort => $httpd::port,
# Line 244  sub start { Line 273  sub start {
273          start_stop 'screen';          start_stop 'screen';
274          start_stop 'kvm';          start_stop 'kvm';
275    
276          while (my $client = $server->accept()) {          while (1) {
277                  $client->autoflush(1);                  my $client = $server->accept() || next; # ALARM trickle us
278                  my $request = <$client>;                  my $request = <$client>;
279    
280                  warn "request $request\n" if $debug;                  warn "request $request\n" if $debug;
281    
                 Module::Refresh->refresh;  
   
282                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
283                          my $path = $1;                          my $path = $1;
284                          my $param;                          my $param;
# Line 262  sub start { Line 289  sub start {
289                                  }                                  }
290                                  warn "param: ",dump( $param ) if $debug;                                  warn "param: ",dump( $param ) if $debug;
291                          }                          }
                         warn "path $path param: ",dump( $param );  
292                          get_request $client, $path, $param;                          get_request $client, $path, $param;
293                  } else {                  } else {
294                          print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";                          print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
295                          warn "500 $request";                          warn "500 $request";
296                  }                  }
297    
298                  print $client qq{                  print $client menu() if $client->connected;
                 <div style="font-size: 80%; color: #888">  
                 <a href="">reload</a>  
                 <a href=/>index</a>  
                 <a href=/exit>exit</a>  
                 </div>  
                 } if $client->connected;  
299    
300          }          }
301    

Legend:
Removed from v.88  
changed lines
  Added in v.149

  ViewVC Help
Powered by ViewVC 1.1.26