/[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 57 by dpavlin, Thu Jul 30 14:16:59 2009 UTC revision 67 by dpavlin, Thu Jul 30 21:31:30 2009 UTC
# Line 19  use File::Slurp; Line 19  use File::Slurp;
19  use IO::Socket::INET;  use IO::Socket::INET;
20  use Module::Refresh;  use Module::Refresh;
21    
22    our $pids = { httpd => $$ };
23    
24    sub DESTROY {
25            warn "pids ",dump( $pids );
26            foreach ( values %$pids ) {
27                    warn "kill $_";
28                    kill 1,$_ || kill 9, $_;
29            }
30    }
31    
32  our $port = 7777;  our $port = 7777;
 our $debug = 1;  
33    
34  use server;  use server;
35  our $url = "http://$server::ip:$port/";  our $debug = server::debug;
36    our $url = "http://$server::ip:$port";
37    
38  use html;  use html;
39    
40  sub static {  sub static {
41          my ($client,$path) = @_;          my ($client,$path) = @_;
42    
43          $path = "tftp/$path";          my $full = "$server::base_dir/tftp/$path";
44    
45          if ( ! -e $path ||  -d $path ) {          return if ! -f $full;
                 print $client "HTTP/1.0 404 $path not found\r\n";  
                 return;  
         }  
46    
47          my $type = 'text/plain';          my $type = 'application/octet-stream';
48          $type = 'text/html' if $path =~ m{\.htm};          $type = 'text/html' if $path =~ m{\.htm};
49          $type = 'application/javascript' if $path =~ m{\.js};          $type = 'application/javascript' if $path =~ m{\.js};
50            $type = 'text/plain' if $path =~ m{\.txt};
51    
52            my $size = -s $full || return;
53    
54            print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
55    
56            open(my $fh, $full);
57            print "static $path $type $size\n";
58    
59            my $block = 8192;
60            my $buff;
61            my $pos = 0;
62    
63          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: ", -s $path,"\r\n\r\n";          while( my $len = read $fh, $buff, $block ) {
64          open(my $html, $path);                  print $client $buff;
65          while(<$html>) {                  $pos += $len;
66                  print $client $_;                  printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;
67          }          }
68          close($html);          close($fh);
69            close($client);
70    
71            print "$path $pos == $size OK\n";
72    
73          return $path;          return $path;
74  }  }
75    
 my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";  
   
76  use boolean;  use boolean;
77    
78  use screen;  use screen;
79  use kvm;  use kvm;
 our $pids;  
80    
81  $SIG{CHLD} = 'IGNORE';  $SIG{CHLD} = 'IGNORE';
82    
# Line 65  sub start_stop { Line 84  sub start_stop {
84          my $daemon = shift;          my $daemon = shift;
85          my $pid = $pids->{$daemon};          my $pid = $pids->{$daemon};
86    
87          warn "start_stop $daemon pids: ",dump( $pids );          warn "start_stop $daemon $pid pids: ",dump( $pids );
88    
89          if ( $pid ) {          if ( $pid =~ m{^\d+$} ) {
90                  warn "kill 9 $pid";                  warn "kill 9 $pid";
91                  kill 9, $pid;                  kill 9, $pid;
92                  delete $pids->{$daemon};                  $pids->{$daemon} = 'stopped';
93                  return qq|$daemon pid $pid stopped|;                  return qq|$daemon pid $pid stopped|;
94          } else {          } else {
95                  if ( $pid = fork ) {                  if ( $pid = fork ) {
# Line 80  sub start_stop { Line 99  sub start_stop {
99                          return qq|$daemon pid $pid started|;                          return qq|$daemon pid $pid started|;
100                  } elsif ( defined $pid ) {                  } elsif ( defined $pid ) {
101                          # child                          # child
102                          my $eval = $daemon . '::start';                          my $eval = $daemon . '::start(' . ( @_ ? dump(@_) : '' ) . ')';
103                          warn "eval $eval";                          warn "eval $eval";
104                          eval $eval;                          eval $eval;
105                          warn "can't start $daemon: $@" if $@;                          warn "can't start $daemon: $@" if $@;
# Line 91  sub start_stop { Line 110  sub start_stop {
110          }          }
111  }  }
112    
113    my $ok =       qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
114    my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
115    
116  sub get_request {  sub get_request {
117          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
118    
# Line 104  sub get_request { Line 126  sub get_request {
126                  my $kvm    = $pids->{kvm}    ? qq|stop <tt>$pids->{kvm}</tt>|           :                  my $kvm    = $pids->{kvm}    ? qq|stop <tt>$pids->{kvm}</tt>|           :
127                                           $pids->{screen} ? qq|start|                                                    : qq|start screen first|;                                           $pids->{screen} ? qq|start|                                                    : qq|start screen first|;
128    
129                  print $client $ok,                  my @rows = (
130                  html::table( 2,                          'ip',           html::tt( $server::ip ),
131                          'pid',    $$,                          'netmask',      html::tt( $server::netmask ),
132                          'debug',  qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,  
133                          'screen', qq|<a href=/screen>$screen</a>|,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
                         'kvm',    qq|<a href=/kvm>$kvm</a>|,  
134                  );                  );
135                    foreach my $name ( %$pids ) {
136                            my $pid = $pids->{$name} || next;
137                            my $proc = "/proc/$pid/status";
138    
139                            my $html = qq|<a href=/$name>$pid</a>|;
140                            $html   .= qq|<pre style="font-size: 10%">|
141                                            .  ( $debug && -e $proc ? read_file($proc) : '' )
142                                            .  qq|</pre>| if $debug;
143    
144                            push @rows, ( $name => $html );
145                    }
146    
147                    print $client $ok, html::table( 2, @rows );
148    
149          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
150                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
151                  warn $@ if $@;                  warn $@ if $@;
152                  print $client qq|HTTP/1.1 302 Found\r\nLocation: $url\r\nContent-type: text/html\r\n\r\n<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>|;
153                    server::debug( $debug ) if $1 eq 'debug';
154          } elsif ( $path =~ m{^/(screen|kvm)} ) {          } elsif ( $path =~ m{^/(screen|kvm)} ) {
155                  print $client $ok, start_stop($1);                  print $client $redirect, start_stop($1);
156            } elsif ( $path eq '/exit' ) {
157    #               DESTROY;
158                    exit 0;
159          } elsif ( $path =~ m{/boot} ) {          } elsif ( $path =~ m{/boot} ) {
160                  print $client qq{$ok                  print $client qq{$ok
161  #!gpxe  #!gpxe
# Line 133  chain http://$server::ip:$httpd::port/ Line 171  chain http://$server::ip:$httpd::port/
171    
172  }  }
173    
174    use browser;
175    
176  sub start {  sub start {
177    
178          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
# Line 144  sub start { Line 184  sub start {
184    
185          print "url $url\n";          print "url $url\n";
186    
187          system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &";          start_stop 'browser', $url;
188            start_stop 'screen';
189            start_stop 'kvm';
190    
191          while (my $client = $server->accept()) {          while (my $client = $server->accept()) {
192                  $client->autoflush(1);                  $client->autoflush(1);
# Line 174  sub start { Line 216  sub start {
216                  print $client qq{                  print $client qq{
217                  <div style="font-size: 80%; color: #888">                  <div style="font-size: 80%; color: #888">
218                  <a href="">reload</a>                  <a href="">reload</a>
219                  <a href="/">index</a>                  <a href=/>index</a>
220                    <a href=/exit>exit</a>
221                  </div>                  </div>
222                  } if $client->connected;                  } if $client->connected;
223    

Legend:
Removed from v.57  
changed lines
  Added in v.67

  ViewVC Help
Powered by ViewVC 1.1.26