/[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 42 by dpavlin, Wed Jul 29 17:42:48 2009 UTC revision 104 by dpavlin, Fri Jul 31 22:52:22 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    
22    our $pids;
23    $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh
24    
25    sub DESTROY {
26            warn "pids ",dump( $pids );
27            foreach ( values %$pids ) {
28                    warn "kill $_";
29                    kill 1,$_ || kill 9, $_;
30            }
31    }
32    
33  our $port = 7777;  our $port = 7777;
 our $debug = 1;  
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;
40    our $static_pids;
41    
42    use Time::HiRes qw/time/;
43    
44  sub static {  sub static {
45          my ($client,$path) = @_;          my ($client,$path) = @_;
46    
47          $path = "tftp/$path";          my $full = "$server::base_dir/tftp/$path";
48    
49            return if ! -f $full;
50    
51          if ( ! -e $path ||  -d $path ) {          my $start_t = time();
52                  print $client "HTTP/1.0 404 $path not found\r\n";  
53                  return;          if ( my $pid = fork ) {
54                    # parent
55                    close($client);
56                    print "http static child $pid\n";
57                    $static_pids->{$pid} = $path;
58                    return 1;
59          }          }
60    
61          my $type = 'text/plain';          my $type = 'application/octet-stream';
62          $type = 'text/html' if $path =~ m{\.htm};          $type = 'text/html' if $path =~ m{\.htm};
63          $type = 'application/javascript' if $path =~ m{\.js};          $type = 'application/javascript' if $path =~ m{\.js};
64            $type = 'text/plain' if $path =~ m{\.txt};
65    
66          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: ", -s $path,"\r\n\r\n";          my $size = -s $full || return;
67          open(my $html, $path);  
68          while(<$html>) {          $client->autoflush(1);
69                  print $client $_;  
70            print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
71    
72            open(my $fh, $full);
73    
74            my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
75            my $buff;
76            my $pos = 0;
77    
78            print "static $path $type $size block: $block\n";
79    
80            while( my $len = read $fh, $buff, $block ) {
81                    print $client $buff;
82                    $pos += $len;
83                    printf "%s %d/%d %.2f%% %.2f K/s\r"
84                            , $path, $pos
85                            , $size, $pos * 100 / $size
86                            , ( $pos / 1024 ) / ( time() - $start_t )
87                            ;
88          }          }
89          close($html);          close($fh);
90            close($client);
91    
92            print "\n";
93    
94          return $path;          exit;
95  }  }
96    
97  my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";  use boolean;
98    
99    use screen;
100    use kvm;
101    
102    $SIG{CHLD} = 'IGNORE';
103    
104    sub start_stop {
105            my $daemon = shift;
106            my $pid = $pids->{$daemon};
107    
108            warn "start_stop $daemon $pid\n";
109    
110            if ( $pid =~ m{^\d+$} ) {
111                    my $pstree = `pstree -p $pid`;
112                    my @pids = $pstree =~ m{\((\d+)\)}g;
113                    warn "pstree $pstree pids ",dump( @pids );
114                    kill 1, $_ foreach reverse @pids;
115                    $pids->{$daemon} = 'stopped';
116                    return qq|$daemon pid $pid stopped|;
117            } else {
118                    if ( $pid = fork ) {
119                            # parent
120                            $pids->{$daemon} = $pid;
121                            warn "forked $daemon $pid\n";
122                            return qq|$daemon pid $pid started|;
123                    } elsif ( defined $pid ) {
124                            # child
125                            my $invoke = 'start';
126                            $invoke = $1 if $daemon =~ s{/(.+)}{};
127                            my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';
128                            warn "eval $eval";
129                            eval $eval;
130                            warn "can't start $daemon: $@" if $@;
131                            exit;
132                    } else {
133                            die "fork error $!";
134                    }
135            }
136    }
137    
138    my $ok =       qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n|;
139    my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;
140    
141    sub get_request {
142            my ( $client, $path, $param ) = @_;
143    
144            warn "get_request $path ", $param ? dump( $param ) : '', "\n";
145    
146            if ( my $found = static( $client,$path ) ) {
147                    warn "static $found" if $debug;
148            } elsif ( $path eq '/' ) {
149    
150                    my $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>|        : 'start';
151                    my $kvm    = $pids->{kvm}    ? qq|stop <tt>$pids->{kvm}</tt>|           :
152                                             $pids->{screen} ? qq|start|                                                    : qq|start screen first|;
153    
154                    my @rows = (
155                            'ip',           html::tt( $server::ip ),
156                            'netmask',      html::tt( $server::netmask ),
157    
158                            'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,
159                    );
160    
161                    my $debug_proc = '';
162    
163                    warn 'pids: ', dump( $pids ) if $debug;
164                    foreach my $name ( sort keys %$pids ) {
165                            my $pid = $pids->{$name} || next;
166    
167                            my $html = qq|<a href=/start_stop/$name>$pid</a>|;
168    
169                            my $proc = "/proc/$pid/status";
170    
171                            if ( -e $proc ) {
172                                    if ( $debug ) {
173                                            $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;
174    
175                                            $debug_proc
176                                                    .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
177                                                    .  read_file($proc)
178                                                    .  qq|</pre>|
179                                                    ;
180                                    }
181    
182                                    if ( $name->can('start_fork') ) {
183                                            $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;
184                                    }
185    
186                                    if ( $name->can('actions') ) {
187                                            $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;
188                                    }
189                            }
190    
191                            push @rows, ( $name => $html );
192                    }
193    
194                    my $below_table = '';
195    
196                    warn 'static_pids: ', dump( $static_pids ) if $debug;
197                    foreach my $pid ( keys %$static_pids ) {
198                            my $path = $static_pids->{$pid};
199                            if ( -d "/proc/$pid" ) {
200                                    push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
201                            } elsif ( $param->{clean_completed_downloads} ) {
202                                    delete $static_pids->{$pid}
203                            } else {
204                                    push @rows, ( $path => "$pid competed" );
205                                    $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
206                            }
207                    }
208    
209                    print $client $ok
210                            , html::table( 2, @rows )
211                            , $below_table
212                            , html::tabs( log::mac_changes )
213                            , $debug_proc
214                            ;
215    
216            } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
217                    eval 'our $' . $1 . ' = ' . $2;
218                    warn $@ if $@;
219                    print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
220                    server::debug( $debug ) if $1 eq 'debug';
221            } elsif ( $path =~ m{^/start_stop/((?:screen|kvm).*)} ) { # XXX we don't want to stop all classes
222                    print $client $redirect, start_stop($1);
223            } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
224                    $1->$2();
225                    print $client $redirect;
226            } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
227                    print $client $redirect;
228                    kill 1, $1 || kill 9, $2 && warn "killed $1";
229            } elsif ( $path eq '/exit' ) {
230    #               DESTROY;
231                    exit 0;
232            } elsif ( $path =~ m{/boot} ) {
233                    print $client qq{$ok
234    #!gpxe
235    imgfree
236    login
237    chain http://$server::ip:$httpd::port/
238    
239                            };
240            } else {
241                    print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
242                    warn "404 $path";
243            }
244    
245    }
246    
247    use browser;
248    
249  sub start {  sub start {
250    
# Line 58  sub start { Line 253  sub start {
253                          LocalPort => $httpd::port,                          LocalPort => $httpd::port,
254                          Listen    => SOMAXCONN,                          Listen    => SOMAXCONN,
255                          Reuse     => 1                          Reuse     => 1
256          );          ) || die "can't start server on $url: $!";
                                                                             
         die "can't setup server" unless $server;  
257    
258          print "url $url\n";          print "url $url\n";
259    
260          system "/mnt/llin/rest/cvs/uzbl/uzbl -u $url &";          start_stop 'browser', $url;
261            start_stop 'screen';
262            start_stop 'kvm';
263    
264          while (my $client = $server->accept()) {          while (my $client = $server->accept()) {
265                  $client->autoflush(1);                  $client->autoflush(1);
# Line 72  sub start { Line 267  sub start {
267    
268                  warn "request $request\n" if $debug;                  warn "request $request\n" if $debug;
269    
270                    Module::Refresh->refresh;
271    
272                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
273                          my $method = $1;                          my $path = $1;
274                          my $param;                          my $param;
275                          if ( $method =~ s{\?(.+)}{} ) {                          if ( $path =~ s{\?(.+)}{} ) {
276                                  foreach my $p ( split(/[&;]/, $1) ) {                                  foreach my $p ( split(/[&;]/, $1) ) {
277                                          my ($n,$v) = split(/=/, $p, 2);                                          my ($n,$v) = split(/=/, $p, 2);
278                                          $param->{$n} = $v;                                          $param->{$n} = $v;
279                                  }                                  }
280                                  warn "param: ",dump( $param ) if $debug;                                  warn "param: ",dump( $param ) if $debug;
281                          }                          }
282                          warn "method $method";                          get_request $client, $path, $param;
   
                         if ( my $path = static( $client,$1 ) ) {  
                                 warn "static $path" if $debug;  
                         } elsif ( $method eq '/' ) {  
                                 print $client qq{$ok  
                                         pid <tt>$$</tt>  
                                         debug $debug  
                                 };  
                                   
                         } elsif ( $method =~ m{/boot} ) {  
                                 print $client qq{$ok  
 #!gpxe  
 imgfree  
 login  
 chain http://$server::ip:$httpd::port/  
   
                                         };  
                         } else {  
                                 print $client "HTTP/1.0 404 Unkown method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n404 $request";  
                                 warn "404 $request";  
                         }  
283                  } else {                  } else {
284                          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";
285                          warn "500 $request";                          warn "500 $request";
286                  }                  }
287    
288                  print $client qq{                  print $client qq{
289                  <a href="">reload</a>                    <div style="font-size: 80%; color: #888">
290                  };                  <a href="">reload</a>
291                    <a href=/>index</a>
292                    <a href=/exit>exit</a>
293                    </div>
294                    } if $client->connected;
295    
                 close($client);  
296          }          }
297    
298          die "server died";          die "server died";
299  }  }
300    
301    warn "loaded";
302    
303  1;  1;

Legend:
Removed from v.42  
changed lines
  Added in v.104

  ViewVC Help
Powered by ViewVC 1.1.26