/[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 91 by dpavlin, Fri Jul 31 18:48:29 2009 UTC revision 128 by dpavlin, Mon Aug 3 19:59:08 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;
 use Module::Refresh;  
20    
21  our $pids;  our $pids;
22  $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 37  our $url = "http://$server::ip:$port";
37    
38  use html;  use html;
39  our $static_pids;  our $static_pids;
40    use progress_bar;
41    
42  sub static {  sub static {
43          my ($client,$path) = @_;          my ($client,$path) = @_;
# Line 64  sub static { Line 64  sub static {
64          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";
65    
66          open(my $fh, $full);          open(my $fh, $full);
         print "static $path $type $size\n";  
67    
68          my $block = 8192;          my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
69          my $buff;          my $buff;
70          my $pos = 0;          my $pos = 0;
71    
72            warn "static $path $type $size block: $block\n";
73    
74            progress_bar::start;
75    
76          while( my $len = read $fh, $buff, $block ) {          while( my $len = read $fh, $buff, $block ) {
77                  print $client $buff;                  print $client $buff;
78                    $client->flush;
79                  $pos += $len;                  $pos += $len;
80                  printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;                  progress_bar::tick( $path, $pos, $size );
81          }          }
82          close($fh);          close($fh);
83          close($client);          close($client);
84    
85          print "$path $pos == $size OK\n";          print STDERR "\n";
86    
87            warn "exit static child";
88    
89          exit;          exit(0);
90  }  }
91    
92  use boolean;  use boolean;
# Line 94  sub start_stop { Line 100  sub start_stop {
100          my $daemon = shift;          my $daemon = shift;
101          my $pid = $pids->{$daemon};          my $pid = $pids->{$daemon};
102    
103          warn "start_stop $daemon $pid pids: ",dump( $pids );          warn "start_stop $daemon $pid\n";
104    
105          if ( $pid =~ m{^\d+$} ) {          if ( $pid =~ m{^\d+$} ) {
106                  my $pstree = `pstree -p $pid`;                  my $pstree = `pstree -p $pid`;
# Line 107  sub start_stop { Line 113  sub start_stop {
113                  if ( $pid = fork ) {                  if ( $pid = fork ) {
114                          # parent                          # parent
115                          $pids->{$daemon} = $pid;                          $pids->{$daemon} = $pid;
116                          warn "forked $daemon $pid";                          warn "forked $daemon $pid\n";
117                          return qq|$daemon pid $pid started|;                          return qq|$daemon pid $pid started|;
118                  } elsif ( defined $pid ) {                  } elsif ( defined $pid ) {
119                          # child                          # child
# Line 130  my $redirect = qq|HTTP/1.1 302 Found\r\n Line 136  my $redirect = qq|HTTP/1.1 302 Found\r\n
136  sub get_request {  sub get_request {
137          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
138    
139          warn "get_request $client $path ",dump( $param );          server->refresh;
140    
141            warn "get_request $path ", $param ? dump( $param ) : '', "\n";
142    
143          if ( my $found = static( $client,$path ) ) {          if ( my $found = static( $client,$path ) ) {
144                  warn "static $found" if $debug;                  warn "static $found" if $debug;
# Line 147  sub get_request { Line 155  sub get_request {
155                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
156                  );                  );
157    
158                  my $debug_proc;                  my $debug_proc = '';
159    
160                  warn 'pids: ', dump( $pids ) if $debug;                  warn 'pids: ', dump( $pids ) if $debug;
161                  foreach my $name ( sort keys %$pids ) {                  foreach my $name ( sort keys %$pids ) {
# Line 250  sub start { Line 258  sub start {
258          start_stop 'screen';          start_stop 'screen';
259          start_stop 'kvm';          start_stop 'kvm';
260    
261          while (my $client = $server->accept()) {          while (1) {
262                  $client->autoflush(1);                  my $client = $server->accept() || next; # ALARM trickle us
263                  my $request = <$client>;                  my $request = <$client>;
264    
265                  warn "request $request\n" if $debug;                  warn "request $request\n" if $debug;
266    
                 Module::Refresh->refresh;  
   
267                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
268                          my $path = $1;                          my $path = $1;
269                          my $param;                          my $param;
# Line 268  sub start { Line 274  sub start {
274                                  }                                  }
275                                  warn "param: ",dump( $param ) if $debug;                                  warn "param: ",dump( $param ) if $debug;
276                          }                          }
                         warn "path $path param: ",dump( $param );  
277                          get_request $client, $path, $param;                          get_request $client, $path, $param;
278                  } else {                  } else {
279                          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";

Legend:
Removed from v.91  
changed lines
  Added in v.128

  ViewVC Help
Powered by ViewVC 1.1.26