/[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 96 by dpavlin, Fri Jul 31 20:45:09 2009 UTC revision 153 by dpavlin, Wed Aug 5 23:22:17 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    <a href=/client>client</a>
27    </div>
28    
29    }}
30    
31  our $pids;  our $pids;
32  $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 47  our $url = "http://$server::ip:$port";
47    
48  use html;  use html;
49  our $static_pids;  our $static_pids;
50    use progress_bar;
51    use config;
52    
53  sub static {  sub static {
54          my ($client,$path) = @_;          my ($client,$path) = @_;
# Line 61  sub static { Line 72  sub static {
72    
73          my $size = -s $full || return;          my $size = -s $full || return;
74    
         $client->autoflush(1);  
   
75          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";
76    
77          open(my $fh, $full);          open(my $fh, $full);
# Line 71  sub static { Line 80  sub static {
80          my $buff;          my $buff;
81          my $pos = 0;          my $pos = 0;
82    
83          print "static $path $type $size block: $block\n";          warn "static $path $type $size block: $block\n";
84    
85            progress_bar::start;
86    
87          while( my $len = read $fh, $buff, $block ) {          while( my $len = read $fh, $buff, $block ) {
88                  print $client $buff;                  print $client $buff;
89                    $client->flush;
90                  $pos += $len;                  $pos += $len;
91                  printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;                  progress_bar::tick( $path, $pos, $size );
92          }          }
93          close($fh);          close($fh);
94          close($client);          close($client);
95    
96          print "\n";          print STDERR "\n";
97    
98            warn "exit static child";
99    
100          exit;          exit(0);
101  }  }
102    
103  use boolean;  use boolean;
# Line 95  $SIG{CHLD} = 'IGNORE'; Line 109  $SIG{CHLD} = 'IGNORE';
109    
110  sub start_stop {  sub start_stop {
111          my $daemon = shift;          my $daemon = shift;
112          my $pid = $pids->{$daemon};          my $pid = $pids->{$daemon} || 'not started';
113    
114          warn "start_stop $daemon $pid\n";          warn "start_stop $daemon $pid\n";
115    
# Line 133  my $redirect = qq|HTTP/1.1 302 Found\r\n Line 147  my $redirect = qq|HTTP/1.1 302 Found\r\n
147  sub get_request {  sub get_request {
148          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
149    
150            server->refresh;
151    
152          warn "get_request $path ", $param ? dump( $param ) : '', "\n";          warn "get_request $path ", $param ? dump( $param ) : '', "\n";
153    
154          if ( my $found = static( $client,$path ) ) {          if ( my $found = static( $client,$path ) ) {
# Line 144  sub get_request { Line 160  sub get_request {
160                                           $pids->{screen} ? qq|start|                                                    : qq|start screen first|;                                           $pids->{screen} ? qq|start|                                                    : qq|start screen first|;
161    
162                  my @rows = (                  my @rows = (
                         'ip',           html::tt( $server::ip ),  
                         'netmask',      html::tt( $server::netmask ),  
   
163                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
164                  );                  );
165    
# Line 199  sub get_request { Line 212  sub get_request {
212                  }                  }
213    
214                  print $client $ok                  print $client $ok
215                            , menu()
216                          , html::table( 2, @rows )                          , html::table( 2, @rows )
217                          , $below_table                          , $below_table
218                          , html::tabs( log::mac_changes )                          , html::tabs( log::mac_changes )
219                          , $debug_proc                          , $debug_proc
220                          ;                          ;
221    
222            } elsif ( $path =~ m{^/server} ) {
223                    print $client $ok
224                            , menu()
225                            , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) )
226                            ;
227            } elsif ( $path =~ m{^/client} ) {
228                    my $ip = $client->peerhost;
229                    my $hostname = server::shared( "hostname/$ip", $param->{hostname} );
230                    my $deploy   = server::shared( "deploy/$ip",   $param->{deploy}   );
231                    print $client $ok
232                            , menu()
233                            , qq|<form method=get>|
234                            , html::table( 2,
235                                    'ip' => $ip,
236                                    'hostname' => qq|<input type=text name=hostname value=$hostname>|,
237                                    'deploy' => html::select( 'deploy', $deploy, config::available ),
238                            )
239                            , qq|<input type=submit value=change></form><pre>|
240                            , config::for_ip( $ip )
241                            , qq|</pre>|
242                            ;
243          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
244                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
245                  warn $@ if $@;                  warn $@ if $@;
# Line 237  chain http://$server::ip:$httpd::port/ Line 272  chain http://$server::ip:$httpd::port/
272  }  }
273    
274  use browser;  use browser;
275    use network;
276    
277  sub start {  sub start {
278    
279            warn 'tap ', network::tap();
280    
281          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
282                          Proto     => 'tcp',                          Proto     => 'tcp',
283                          LocalPort => $httpd::port,                          LocalPort => $httpd::port,
# Line 253  sub start { Line 291  sub start {
291          start_stop 'screen';          start_stop 'screen';
292          start_stop 'kvm';          start_stop 'kvm';
293    
294          while (my $client = $server->accept()) {          while (1) {
295                  $client->autoflush(1);                  my $client = $server->accept() || next; # ALARM trickle us
296                  my $request = <$client>;                  my $request = <$client>;
297    
298                  warn "request $request\n" if $debug;                  warn "request $request\n" if $debug;
299    
                 Module::Refresh->refresh;  
   
300                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
301                          my $path = $1;                          my $path = $1;
302                          my $param;                          my $param;
# Line 277  sub start { Line 313  sub start {
313                          warn "500 $request";                          warn "500 $request";
314                  }                  }
315    
316                  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;  
317    
318          }          }
319    

Legend:
Removed from v.96  
changed lines
  Added in v.153

  ViewVC Help
Powered by ViewVC 1.1.26