/[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 53 by dpavlin, Thu Jul 30 12:10:53 2009 UTC revision 70 by dpavlin, Thu Jul 30 22:44:55 2009 UTC
# Line 18  use File::Slurp; Line 18  use File::Slurp;
18  #use JSON;  #use JSON;
19  use IO::Socket::INET;  use IO::Socket::INET;
20  use Module::Refresh;  use Module::Refresh;
 use Parallel::ForkManager;  
21    
22  our $port = 7777;  our $pids;
23  our $debug = 1;  $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
24    
25  my $pm = Parallel::ForkManager->new(10);  sub DESTROY {
26            warn "pids ",dump( $pids );
27            foreach ( values %$pids ) {
28                    warn "kill $_";
29                    kill 1,$_ || kill 9, $_;
30            }
31    }
32    
33  our $screen_pid;  our $port = 7777;
34    
35  use server;  use server;
36  our $url = "http://$server::ip:$port/";  our $debug = server::debug;
37    our $url = "http://$server::ip:$port";
38    
39  use html;  use html;
40    our $static_pids;
41    
42  sub static {  sub static {
43          my ($client,$path) = @_;          my ($client,$path) = @_;
44    
45          $path = "tftp/$path";          my $full = "$server::base_dir/tftp/$path";
46    
47            return if ! -f $full;
48    
49          if ( ! -e $path ||  -d $path ) {          if ( my $pid = fork ) {
50                  print $client "HTTP/1.0 404 $path not found\r\n";                  # parent
51                  return;                  close($client);
52                    print "http static child $pid\n";
53                    $static_pids->{$pid} = $path;
54                    return 1;
55          }          }
56    
57          my $type = 'text/plain';          my $type = 'application/octet-stream';
58          $type = 'text/html' if $path =~ m{\.htm};          $type = 'text/html' if $path =~ m{\.htm};
59          $type = 'application/javascript' if $path =~ m{\.js};          $type = 'application/javascript' if $path =~ m{\.js};
60            $type = 'text/plain' if $path =~ m{\.txt};
61    
62            my $size = -s $full || return;
63    
64            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);
67            print "static $path $type $size\n";
68    
69            my $block = 8192;
70            my $buff;
71            my $pos = 0;
72    
73          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 ) {
74          open(my $html, $path);                  print $client $buff;
75          while(<$html>) {                  $pos += $len;
76                  print $client $_;                  printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;
77          }          }
78          close($html);          close($fh);
79            close($client);
80    
81          return $path;          print "$path $pos == $size OK\n";
 }  
82    
83  my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";          exit;
84    }
85    
86  use boolean;  use boolean;
87    
88  use screen;  use screen;
89    use kvm;
90    
91    $SIG{CHLD} = 'IGNORE';
92    
93    sub start_stop {
94            my $daemon = shift;
95            my $pid = $pids->{$daemon};
96    
97            warn "start_stop $daemon $pid pids: ",dump( $pids );
98    
99            if ( $pid =~ m{^\d+$} ) {
100                    warn "kill 9 $pid";
101                    kill 9, $pid;
102                    $pids->{$daemon} = 'stopped';
103                    return qq|$daemon pid $pid stopped|;
104            } else {
105                    if ( $pid = fork ) {
106                            # parent
107                            $pids->{$daemon} = $pid;
108                            warn "forked $daemon $pid";
109                            return qq|$daemon pid $pid started|;
110                    } elsif ( defined $pid ) {
111                            # child
112                            my $eval = $daemon . '::start(' . ( @_ ? dump(@_) : '' ) . ')';
113                            warn "eval $eval";
114                            eval $eval;
115                            warn "can't start $daemon: $@" if $@;
116                            exit;
117                    } else {
118                            die "fork error $!";
119                    }
120            }
121    }
122    
123    my $ok =       qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
124    my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
125    
126  sub get_request {  sub get_request {
127          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
# Line 70  sub get_request { Line 132  sub get_request {
132                  warn "static $found" if $debug;                  warn "static $found" if $debug;
133          } elsif ( $path eq '/' ) {          } elsif ( $path eq '/' ) {
134    
135                  my $screen = $screen_pid ? qq|stop <tt>$screen_pid</tt>| : 'start';                  my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>|        : 'start';
136                    my $kvm    = $pids->{kvm}    ? qq|stop <tt>$pids->{kvm}</tt>|           :
137                                             $pids->{screen} ? qq|start|                                                    : qq|start screen first|;
138    
139                    my @rows = (
140                            'ip',           html::tt( $server::ip ),
141                            'netmask',      html::tt( $server::netmask ),
142    
143                  print $client $ok,                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
                 html::table( 2,  
                         'pid',   $$,  
                         'debug', qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,  
                         'screen', qq|<a href=/screen>$screen</a>|,  
144                  );                  );
145    
146          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {                  warn 'pids: ', dump( $pids );
147                  eval 'our $' . $1 . ' = ' . $2;                  foreach my $name ( keys %$pids ) {
148                  warn $@ if $@;                          my $pid = $pids->{$name} || next;
149                  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>|;                          my $proc = "/proc/$pid/status";
150          } elsif ( $path =~ m{^/screen} ) {  
151                            my $html = qq|<a href=/$name>$pid</a>|;
152                            $html   .= qq|<pre style="font-size: 10%">|
153                                            .  ( $debug && -e $proc ? read_file($proc) : '' )
154                                            .  qq|</pre>| if $debug;
155    
156                  if ( $screen_pid ) {                          push @rows, ( $name => $html );
157                          if ( ! kill $screen_pid ) {                  }
158                                  warn "kill -9 $screen_pid";  
159                                  if ( kill 9, $screen_pid ) {                  warn 'static_pids: ', dump( $static_pids );
160                                          print $client $ok, qq|screen pid $screen_pid stopped|;                  foreach my $pid ( keys %$static_pids ) {
161                                          $screen_pid = 0;                          my $path = $static_pids->{$pid};
162                                  } else {                          if ( -d "/proc/$pid" ) {
163                                          print $client $ok, qq|can't stop screen $screen_pid: $!|;                                  push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
                                 }  
                         }  
                 } else {  
                         if ( $screen_pid = $pm->start ) {  
                                 print $client $ok, qq|screen started pid $screen_pid|;  
164                          } else {                          } else {
165                                  screen::start;                                  push @rows, ( $path => "$pid competed" );
                                 $pm->finish;  
166                          }                          }
167                  }                  }
168    
169                    print $client $ok, html::table( 2, @rows );
170    
171            } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
172                    eval 'our $' . $1 . ' = ' . $2;
173                    warn $@ if $@;
174                    print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
175                    server::debug( $debug ) if $1 eq 'debug';
176            } elsif ( $path =~ m{^/(screen|kvm)} ) {
177                    print $client $redirect, start_stop($1);
178            } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
179                    print $client $redirect;
180                    kill 9, $1 && warn "killed $1";
181            } elsif ( $path eq '/exit' ) {
182    #               DESTROY;
183                    exit 0;
184          } elsif ( $path =~ m{/boot} ) {          } elsif ( $path =~ m{/boot} ) {
185                  print $client qq{$ok                  print $client qq{$ok
186  #!gpxe  #!gpxe
# Line 119  chain http://$server::ip:$httpd::port/ Line 196  chain http://$server::ip:$httpd::port/
196    
197  }  }
198    
199    use browser;
200    
201  sub start {  sub start {
202    
203          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
# Line 130  sub start { Line 209  sub start {
209    
210          print "url $url\n";          print "url $url\n";
211    
212          system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &";          start_stop 'browser', $url;
213            start_stop 'screen';
214            start_stop 'kvm';
215    
216          while (my $client = $server->accept()) {          while (my $client = $server->accept()) {
217                  $client->autoflush(1);                  $client->autoflush(1);
# Line 160  sub start { Line 241  sub start {
241                  print $client qq{                  print $client qq{
242                  <div style="font-size: 80%; color: #888">                  <div style="font-size: 80%; color: #888">
243                  <a href="">reload</a>                  <a href="">reload</a>
244                  <a href="/">index</a>                  <a href=/>index</a>
245                    <a href=/exit>exit</a>
246                  </div>                  </div>
247                  } if $client->connected;                  } if $client->connected;
248    

Legend:
Removed from v.53  
changed lines
  Added in v.70

  ViewVC Help
Powered by ViewVC 1.1.26