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

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

  ViewVC Help
Powered by ViewVC 1.1.26