/[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 66 by dpavlin, Thu Jul 30 20:15:39 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 = { httpd => $$ };
 our $debug = 1;  
23    
24  my $pm = Parallel::ForkManager->new(10);  sub DESTROY {
25            warn "pids ",dump( $pids );
26            foreach ( values %$pids ) {
27                    warn "kill $_";
28                    kill 1,$_ || kill 9, $_;
29            }
30    }
31    
32  our $screen_pid;  our $port = 7777;
33    our $debug = 0;
34    
35  use server;  use server;
36  our $url = "http://$server::ip:$port/";  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          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: ", -s $path,"\r\n\r\n";          my $block = 8192;
60          open(my $html, $path);          my $buff;
61          while(<$html>) {          my $pos = 0;
62                  print $client $_;  
63            while( my $len = read $fh, $buff, $block ) {
64                    print $client $buff;
65                    $pos += $len;
66                    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  }  }
# Line 59  sub static { Line 76  sub static {
76  my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";  my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";
77    
78  use boolean;  use boolean;
79    
80  use screen;  use screen;
81    use kvm;
82    
83    $SIG{CHLD} = 'IGNORE';
84    
85    sub start_stop {
86            my $daemon = shift;
87            my $pid = $pids->{$daemon};
88    
89            warn "start_stop $daemon pids: ",dump( $pids );
90    
91            if ( $pid ) {
92                    warn "kill 9 $pid";
93                    kill 9, $pid;
94                    $pids->{$daemon} = 'stopped';
95                    return qq|$daemon pid $pid stopped|;
96            } else {
97                    if ( $pid = fork ) {
98                            # parent
99                            $pids->{$daemon} = $pid;
100                            warn "forked $daemon $pid";
101                            return qq|$daemon pid $pid started|;
102                    } elsif ( defined $pid ) {
103                            # child
104                            my $eval = $daemon . '::start(' . dump(@_) . ')';
105                            warn "eval $eval";
106                            eval $eval;
107                            warn "can't start $daemon: $@" if $@;
108                            exit;
109                    } else {
110                            die "fork error $!";
111                    }
112            }
113    }
114    
115  sub get_request {  sub get_request {
116          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
# Line 70  sub get_request { Line 121  sub get_request {
121                  warn "static $found" if $debug;                  warn "static $found" if $debug;
122          } elsif ( $path eq '/' ) {          } elsif ( $path eq '/' ) {
123    
124                  my $screen = $screen_pid ? qq|stop <tt>$screen_pid</tt>| : 'start';                  my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>|        : 'start';
125                    my $kvm    = $pids->{kvm}    ? qq|stop <tt>$pids->{kvm}</tt>|           :
126                                             $pids->{screen} ? qq|start|                                                    : qq|start screen first|;
127    
128                    my @rows = (
129                            'ip',           html::tt( $server::ip ),
130                            'netmask',      html::tt( $server::netmask ),
131    
132                  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>|,  
133                  );                  );
134                    foreach my $name ( %$pids ) {
135                            my $pid = $pids->{$name} || next;
136    
137                            my $html = qq|<a href=/$name>$pid</a>|;
138                            $html   .= qq|<pre style="font-size: 10%">|
139                                            .  ( $debug ? read_file("/proc/$pid/status") : '' )
140                                            .  qq|</pre>| if $debug;
141    
142                            push @rows, ( $name => $html );
143                    }
144    
145                    print $client $ok, html::table( 2, @rows );
146    
147          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
148                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
149                  warn $@ if $@;                  warn $@ if $@;
150                  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 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>|;
151          } elsif ( $path =~ m{^/screen} ) {          } elsif ( $path =~ m{^/(screen|kvm)} ) {
152                    print $client $ok, start_stop($1);
153                  if ( $screen_pid ) {          } elsif ( $path eq '/exit' ) {
154                          if ( ! kill $screen_pid ) {  #               DESTROY;
155                                  warn "kill -9 $screen_pid";                  exit 0;
                                 if ( kill 9, $screen_pid ) {  
                                         print $client $ok, qq|screen pid $screen_pid stopped|;  
                                         $screen_pid = 0;  
                                 } else {  
                                         print $client $ok, qq|can't stop screen $screen_pid: $!|;  
                                 }  
                         }  
                 } else {  
                         if ( $screen_pid = $pm->start ) {  
                                 print $client $ok, qq|screen started pid $screen_pid|;  
                         } else {  
                                 screen::start;  
                                 $pm->finish;  
                         }  
                 }  
   
156          } elsif ( $path =~ m{/boot} ) {          } elsif ( $path =~ m{/boot} ) {
157                  print $client qq{$ok                  print $client qq{$ok
158  #!gpxe  #!gpxe
# Line 119  chain http://$server::ip:$httpd::port/ Line 168  chain http://$server::ip:$httpd::port/
168    
169  }  }
170    
171    use browser;
172    
173  sub start {  sub start {
174    
175          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
# Line 130  sub start { Line 181  sub start {
181    
182          print "url $url\n";          print "url $url\n";
183    
184          system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &";          start_stop 'browser', $url;
185            start_stop 'screen';
186            start_stop 'kvm';
187    
188          while (my $client = $server->accept()) {          while (my $client = $server->accept()) {
189                  $client->autoflush(1);                  $client->autoflush(1);
# Line 160  sub start { Line 213  sub start {
213                  print $client qq{                  print $client qq{
214                  <div style="font-size: 80%; color: #888">                  <div style="font-size: 80%; color: #888">
215                  <a href="">reload</a>                  <a href="">reload</a>
216                  <a href="/">index</a>                  <a href=/>index</a>
217                    <a href=/exit>exit</a>
218                  </div>                  </div>
219                  } if $client->connected;                  } if $client->connected;
220    

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

  ViewVC Help
Powered by ViewVC 1.1.26