/[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 181 by dpavlin, Sun Aug 9 19:00:52 2009 UTC revision 522 by dpavlin, Thu Aug 26 19:29:18 2010 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 Regexp::Common qw/net/;  use Regexp::Common qw/net/;
21    use POSIX qw(strftime);
22    
23  sub menu {qq{  our $title;
   
 <div style="font-size: 80%; color: #888">  
 <a href=/>home</a>  
 <a href=/server>server</a>  
 <a href=/client>client</a>  
 </div>  
24    
25    sub html_start {
26    qq{
27    <html>
28    <head>
29    <title>$title</title>
30    </head>
31    <body>
32  }}  }}
33    
34  our $pids;  sub html_end {
35  $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh  qq{
36    </body>
37  sub DESTROY {  </html>
38          warn "pids ",dump( $pids );  }}
         foreach ( values %$pids ) {  
                 warn "kill $_";  
                 kill 1,$_ || kill 9, $_;  
         }  
 }  
39    
40  our $port = 7777;  our $port = 7777;
41    
# Line 54  use client; Line 51  use client;
51  use log;  use log;
52  use x11;  use x11;
53  use amt;  use amt;
54    use daemons;
55    
56    use kvm;
57    use browser;
58    use network;
59    use ip;
60    use wireshark;
61    use syslogd;
62    use nmap;
63    use ping;
64    use wol;
65    
66    use store;
67    
68    
69    sub menu {
70            my $store_url = $url;
71            $store_url =~ s{:\d+.+}{:28017};
72    qq{
73    <div style="font-size: 80%; color: #888">
74    <a target=pids href=/ >home</a>
75    |
76    <a target=server href=/server >server</a>
77    <a target=server href=/brctl >brctl</a>
78    <a target=server href=/ip >ip</a>
79    |
80    <a target=store href=$store_url >MongoDB</a>
81    <a target=store href=/store/latest >latest</a>
82    |
83    <a target=client href=/nmap >nmap</a>
84    <a target=client href=/client >client</a>
85    </div>
86    
87    }}
88    
89    
90  sub static {  sub static {
91          my ($client,$path) = @_;          my ($client,$path) = @_;
# Line 62  sub static { Line 94  sub static {
94    
95          return if ! -f $full;          return if ! -f $full;
96    
97            return if $full =~ m{\.ico$};
98    
99          if ( my $pid = fork ) {          if ( my $pid = fork ) {
100                  # parent                  # parent
101                  close($client);                  close($client);
                 print "http static child $pid\n";  
102                  $static_pids->{$pid} = $path;                  $static_pids->{$pid} = $path;
103                  return 1;                  return 1;
104          }          }
# Line 85  sub static { Line 118  sub static {
118          my $buff;          my $buff;
119          my $pos = 0;          my $pos = 0;
120    
121          warn "static $path $type $size block: $block\n";          store::audit( 'static', { pid => $$, path => $path, type => $type, size => $size, block => $block, peerhost => $client->peerhost });
122    
123          progress_bar::start;          progress_bar::start;
124    
# Line 100  sub static { Line 133  sub static {
133    
134          print STDERR "\n";          print STDERR "\n";
135    
         warn "exit static child";  
   
136          exit(0);          exit(0);
137  }  }
138    
139  use boolean;  sub ok {
140            qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . html_start() . menu()
141  use kvm;  }
   
 $SIG{CHLD} = 'IGNORE';  
142    
143  sub start_stop {  sub redirect {
144          my $daemon = shift;          my $to = shift;
145          my $pid = $pids->{$daemon} || 'not started';          $to ||= $url;
146            qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $to\r\n\r\n|
         warn "start_stop $daemon $pid\n";  
   
         if ( $pid =~ m{^\d+$} ) {  
                 my $pstree = `pstree -p $pid`;  
                 my @pids = $pstree =~ m{\((\d+)\)}g;  
                 warn "pstree $pstree pids ",dump( @pids );  
                 kill 1, $_ foreach reverse @pids;  
                 $pids->{$daemon} = 'stopped';  
                 return qq|$daemon pid $pid stopped|;  
         } else {  
                 if ( $pid = fork ) {  
                         # parent  
                         $pids->{$daemon} = $pid;  
                         warn "forked $daemon $pid\n";  
                         return qq|$daemon pid $pid started|;  
                 } elsif ( defined $pid ) {  
                         # child  
                         my $invoke = 'start';  
                         $invoke = $1 if $daemon =~ s{/(.+)}{};  
                         if ( $daemon =~ m{dhcpd|tftpd|dnsd} ) {  
                                 my $exec = "perl -I$server::base_dir/lib -I$server::base_dir/lib/PXElator -M$daemon -e ${daemon}::${invoke}";  
                                 warn "exec $exec";  
                                 x11::xterm( $daemon => $exec );  
                         } else {  
                                 my $eval = $daemon . '::' . $invoke . '(' . ( @_ ? dump(@_) : '' ) . ')';  
                                 warn "eval $eval";  
                                 eval $eval;  
                                 warn "can't start $daemon: $@" if $@;  
                         }  
                         exit;  
                 } else {  
                         die "fork error $!";  
                 }  
         }  
147  }  }
148    
149  my $ok =       qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . menu();  sub toggle {
150  my $redirect = qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $url\r\n\r\n|;          my $v = shift;
151            return $v ? 0 : 1;
152    }
153    
154  sub get_request {  sub get_request {
155          my ( $client, $path, $param ) = @_;          my ( $client, $path, $param ) = @_;
156    
157          server->refresh;          server->refresh;
158    
159          warn "get_request $path ", $param ? dump( $param ) : '', "\n";          store::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } );
160    
161            $title = $path;
162    
163          if ( my $found = static( $client,$path ) ) {          if ( my $found = static( $client,$path ) ) {
164                  warn "static $found" if $debug;                  warn "static $found" if $debug;
165          } elsif ( $path eq '/' ) {          } elsif ( $path eq '/' ) {
166    
167                  my @rows = (                  my @rows;
                         'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,  
                 );  
168    
169                  my $debug_proc = '';                  my $debug_proc = '';
170    
171                  warn 'pids: ', dump( $pids ) if $debug;  warn "XXX pids = ", dump( $daemons::pids );
172                  foreach my $name ( sort keys %$pids ) {  
173                          my $pid = $pids->{$name} || next;                  foreach my $name ( sort keys %$daemons::pids ) {
174                            my $pid = $daemons::pids->{$name}; # || next;
175    
176                          my $html;                          my $html;
177    
# Line 182  sub get_request { Line 180  sub get_request {
180                          if ( -e $proc ) {                          if ( -e $proc ) {
181                                  $html .= qq|<a href=/start_stop/$name>$pid</a>|;                                  $html .= qq|<a href=/start_stop/$name>$pid</a>|;
182                                  if ( $debug ) {                                  if ( $debug ) {
183                                          $html .= qq| <a name=$pid href=#proc-$pid>?</a>|;                                          $html .= qq| <a name=$pid href=#proc-$pid>?</a>| if $name->can('start');
184    
185                                          $debug_proc                                          $debug_proc
186                                                  .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|                                                  .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
# Line 191  sub get_request { Line 189  sub get_request {
189                                                  ;                                                  ;
190                                  }                                  }
191    
192                                  if ( $name->can('start_fork') ) {                                  my $class = $name;
193                                          $html .= qq| <a href=/start_stop/kvm/$_>$_</a>| foreach $name->start_fork;                                  $class =~ s{\.\d+$}{};
194    
195                                    if ( $class->can('fork_if_active') ) {
196                                            $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $class->fork_if_active;
197                                  }                                  }
198    
199                                  if ( $name->can('actions') ) {                                  if ( $class->can('actions') ) {
200                                          $html .= qq| <a href=/action/kvm/$_>$_</a>| foreach $name->actions;                                          $html .= qq| <a href=/action/$name/$_>$_</a>| foreach $class->actions;
201                                  }                                  }
202                          } else {                          } else {
203                                  $html .= qq|<a href=/start_stop/$name>restart</a> $pid exited|;                                  if ( $pid =~ m{^\d+$} ) {
204                                            $html .= qq|$pid exited |
205                                    } else {
206                                            $html .= qq|$pid |;
207                                    }
208                                    $html .= qq|<a href=/start_stop/$name>restart</a>| if $pid || $name->can('start');
209                                    if ( $name->can('fork_actions') ) {
210                                            $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $name->fork_actions;
211                                    }
212                          }                          }
213    
214                            die "no html generated" unless $html;
215    
216                          push @rows, ( $name => $html );                          push @rows, ( $name => $html );
217                  }                  }
218    
# Line 220  sub get_request { Line 231  sub get_request {
231                          }                          }
232                  }                  }
233    
234                  print $client $ok                  my $kvm = kvm::next_nr;
235                    $kvm = qq|<div><a href=/start_stop/kvm?nr=$kvm>create new kvm $kvm</a></div>|;
236    
237                    print $client ok
238                          , html::table( 2, @rows )                          , html::table( 2, @rows )
239                          , $below_table                          , $below_table
240                            , $kvm
241                          , html::tabs( log::mac_changes )                          , html::tabs( log::mac_changes )
242                          , $debug_proc                          , $debug_proc
243                          ;                          ;
244    
245          } elsif ( $path =~ m{^/server} ) {          } elsif ( $path =~ m{^/server} ) {
246                  print $client $ok                  foreach my $name ( keys %$param ) {
247                          , html::table( 2, map { ( $_, eval '$server::'.$_ ) } ( 'ip', 'netmask', 'ip_from', 'ip_to', 'domain_name', 'base_dir' ) )                          eval '$server::' . $name . '= $param->{$name}';
248                    }
249                    my @table = (
250                              'debug' => qq|<a href=/our/debug/| . toggle($debug) . qq|>$debug</a>|,
251                            , 'new_clients' => qq|<input type=text name=new_clients size=3 value="$server::new_clients">|
252                    );
253    
254                    foreach my $editable ( 'ip', 'bcast', 'netmask', 'ip_from', 'ip_to', 'domain' ) {
255                            my $v = eval '$server::' . $editable;
256                            push @table, ( $editable, qq|<input type=text name=$editable value="$v">| );
257                    }
258    
259                    foreach my $readonly ( 'base_dir', 'conf' ) {
260                            my $v = eval '$server::' . $readonly;
261                            push @table, ( $readonly, html::tt $v );
262                    }
263                            
264                    print $client ok
265                            , qq|<form method=get>|
266                            , html::table( 2, @table )
267                            , qq|
268                                    <input type=submit name=action value=change>
269                                    </form>
270                            |
271                          ;                          ;
272    
273            } elsif ( $path =~ m{^/store/latest} ) {
274                    print $client ok
275                            , qq|
276    <style type=text/css>
277            .z {
278                    background: #eee;
279            }
280            td > pre {
281                    margin: 0;
282                    max-height: 3em;
283                    overflow: hidden;
284            }
285            td:hover > pre {
286                    max-height: 100%;
287                    overflow: show;
288            }
289    </style>
290                            |
291                            , qq|<table>|
292                    ;
293                    my ( $s1,$s2 ) = ( ' class=z', '' );
294                    my @cols;
295    
296                    my $from_t = $param->{from_t};
297    
298                    my $q;
299                    $q->{'package.time'} = { '$lt' => $from_t * 1 } if $from_t;
300    #               $q->{'package.name'} = { '$ne' => 'syslogd' };
301                    $q->{'package.name'} = { '$ne' => 'dhcpd' };
302    #               $q->{'package.name'} = { '$ne' => [ 'dhcpd', 'dnsd' ] };
303                    $q->{'tag'} = { '$ne' => 'CRON' };
304                    store::query( $q, sub {
305                            my $o = shift;
306                            my $p = delete( $o->{package} );
307                            delete( $o->{_id} );
308    
309                            if ( ! @cols ) {
310                                    #@cols = keys %$p;
311                                    @cols = qw( time name );
312                                    print $client qq|<tr><th>|
313                                            , join(qq|</th><th>|, @cols)
314                                            , qq|</th><th></th></tr>|
315                                    ;
316                            }
317    
318                            # XXX sigh, dump dies if we don't do this
319    #                       delete $o->{$_} foreach ( grep { ! defined $o->{$_} } keys %$o );
320    
321                            print $client qq|<tr$s1>|
322                                    , strftime( qq|<td title="%Y-%m-%d">%H:%M:%S</td>|, localtime($p->{time}) )
323                                    , map { qq|<td>$_<td>| } ( $p->{name} , html::pre_dump($o) )
324                                    , qq|</tr>\n|
325                            ;
326                            ( $s1, $s2 ) = ( $s2, $s1 );
327                            $from_t = $p->{time};
328                    });
329                    print $client qq|</table>|,
330                            qq|<a href="/store/latest?from_t=$from_t">more</a>|
331                    ;
332    
333          } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {          } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
334                  my $ip = $1 || $client->peerhost;                  my $ip = $1;
335                  if ( $ip ne $server::ip ) {                  $title = $ip if $ip;
336                          my $hostname = client::conf( $ip, 'hostname' => $param->{hostname} );  
337                          my $deploy   = client::conf( $ip, 'deploy'   => $param->{deploy}   );                  if ( $param->{action} eq 'remove' ) {
338                          print $client $ok                          client::remove( $param->{change_ip} );
339                            print $client redirect("$url/client");
340                            return;
341                    } elsif ( $param->{action} eq 'change' ) {
342                            if ( my $new_ip = client::change_ip( $ip, $param->{change_ip} ) ) {
343                                    print $client redirect("$url/client#$new_ip");
344                                    return;
345                            }
346                    }
347    
348                    if ( ! $ip ) {
349                            my $peer_ip = $client->peerhost;
350    
351                            my $netmask  = ip::to_int $server::netmask;
352                            my $network  = ip::to_int($server::ip) & $netmask;
353                            my $from_int = $network | $server::ip_from;
354                            my $to_int   = $network | $server::ip_to;
355                            my $ip_int   = ip::to_int $peer_ip;
356    
357                            # show edit for clients in our dhcp range
358                            if ( $ip_int >= $from_int && $ip_int <= $to_int ) {
359                                    $ip = $peer_ip;
360                            }
361                    }
362    
363                    if ( $ip && $ip ne $server::ip ) {
364    
365                            my @editable = ( qw/hostname config homepage/ );
366    
367                            client::conf( $ip, $_ => $param->{$_} ) foreach @editable;
368    
369                            my $conf = client::all_conf( $ip );
370                            my $config = delete $conf->{config};
371    
372                            my $nmap = qq|<a href=/nmap?scan=$ip>nmap</a>|;
373                            my @table = (
374                                    'ping' => ping::host($ip)
375                                            ? qq|<span style="color:green">up</span> $nmap|
376                                            : qq|<span style="color: red">down</span> <a href=/wol/$ip>wol</a> $nmap|
377                                            ,
378                                    'ip' => qq|<input type=text name=change_ip value="$ip" onChange="document.getElementById('old_ip').style.display = '';"><span id=old_ip style="display: none; color: #888;">old: $ip<span>|,
379                                    'mac' => format::mac( delete $conf->{mac}, 'html' ),
380                                    'hostname' => qq|<input type=text name=hostname value="| . delete($conf->{hostname}) . qq|">|,
381                                    'config' => html::select( 'config', $config, config::available ),
382                                    html::conf( $ip, $conf, 'edit', @editable )
383                            );
384    
385                            print $client ok
386                                  , qq|<form method=get>|                                  , qq|<form method=get>|
387                                  , html::table( 2,                                  , html::table( 2, @table ),
388                                          'ip' => $ip,                                  , qq|
389                                          'mac' => client::mac( $ip ),                                          <input type=submit name=action value=change>
390                                          'hostname' => qq|<input type=text name=hostname value=$hostname>|,                                          <input type=submit name=action value=remove style="color: red">
391                                          'deploy' => html::select( 'deploy', $deploy, config::available ),                                          </form>|
                                 )  
                                 , qq|<input type=submit value=change></form>|  
                                 , qq|<h2>PXElinux $deploy</h2><pre>|  
                                 , config::for_ip( $ip )  
                                 , qq|</pre>|  
392                                  ;                                  ;
393    
394                          if ( my $amt = client::conf( $ip, 'amt' ) ) {                          if ( $config ) {
395                                  print $client amt::info( $amt );                                  if ( my $for_ip = config::for_ip( $ip ) ) {
396                                            print $client qq|<h2>config::for_ip</h2>| . html::pre( $for_ip );
397                                    }
398                          }                          }
399    
400                            if ( $conf->{amt} ) {
401                                    print $client qq|<h2>amt network</h2>|, html::pre_dump( amt::network( $ip ) );
402                                    print $client qq|<h2>amt log</h2>|, html::pre_dump( amt::log( $ip ) );
403                            }
404    
405                  } else {                  } else {
406                          print $client $ok  
407                                  , qq|<h2>Clients on $server::ip</h2>|                          print $client ok qq|<h2>Clients on $server::ip</h2>|;
408                                  , html::table( -4,  
409                                          'ip', 'mac', 'hostname', 'deploy',                          my @ping;
410                                          map {                          if ( my $host = $param->{ping_target} ) {
411                                                  my $ip = $_;                                  @ping = ( $host );
412                                                  $ip =~ s{^.+/ip/}{};                          } elsif ( $param->{ping} ) {
413                                                  ( qq|<a href=/client/$ip>$ip</a>|, client::mac($ip), client::conf( $ip, 'hostname' ), client::conf( $ip, 'deploy' ) );                                  @ping = client::all_ips;
414                                          }                          }
415                                          glob("$server::conf/ip/*")  
416                                  )                          my $ping = ping::fping( @ping ) if @ping;
417                            my $arp = client::arp_mac_dev;
418    
419                            my @clients;
420    
421                            foreach my $ip ( client::all_ips ) {
422                                    
423                                    my $conf = client::all_conf( $ip );
424                                    my $mac = delete $conf->{mac} || '';
425                                    my $dev = $arp->{$mac};
426    
427                                    my $in_dhcp_range = ip::in_dhcp_range($ip);
428    
429                                    next unless $dev || $param->{all} || $in_dhcp_range;
430    
431                                    my $style
432                                            = 'style="color:'
433                                            . ( $ping->{$ip} ? 'green' : 'red' )
434                                            . '"'
435                                            if $ping;
436    
437                                    $style ||= '';
438                                    my $ip_text = qq|<tt>$ip</tt>|;
439                                    $ip_text = qq|<tt><b>$ip</b></tt>| if $in_dhcp_range;
440    
441                                    $dev = qq|<tt>$dev</tt>| if $dev;
442    
443                                    push @clients
444                                            , qq|<a $style name=$ip target=client href=/client/$ip>$ip_text</a>|
445                                            , format::mac( $mac => 'html' )
446                                            , $dev
447                                            , delete $conf->{hostname}
448                                            , html::conf( $ip, $conf, 'inline' )
449                                  ;                                  ;
450                            }
451    
452                            my $all = $param->{all} ? 0 : 1;
453    
454                            print $client html::table( -5, 'ip', 'mac', qq|<a href="?all=$all">dev</a>|, 'hostname', 'conf', @clients );
455                            print $client qq|
456                                    <form method=get>
457                                    <input type=text   name=ping_target   size=15>
458                                    <input type=submit name=ping value=ping>
459                                    </form>
460                            |;
461                  }                  }
462    
463    
464            } elsif ( $path =~ m{^/brctl} ) {
465    
466                    system 'brctl addif virtual ' . $param->{addif} if $param->{addif};
467                    system 'brctl delif virtual ' . $param->{delif} if $param->{delif};
468    
469                    my $in_virtual;
470    
471                    my @table =
472                            map {
473                                    my @c = split(/\t+/,$_,4);
474                                    if ( $#c == 1 ) {
475                                            $in_virtual->{ $c[1] }++;
476                                            @c = ( '', '', '', $c[1] );
477                                    } else {
478                                            $in_virtual->{ $c[3] }++;
479                                    }
480                                    if ( $c[3] =~ m{\d$} ) {
481                                            $c[3] = qq|<input type=submit name=delif value=$c[3] style="color:red" title="remove $c[3] from bridge">|;
482                                    }
483                                    @c
484                            } split(/\n/, `brctl show`)
485                    ;
486    
487                    my @add_ifs = grep { ! $in_virtual->{$_} && $_ ne 'virtual' } ip::devices_up;
488    
489                    push @table, ( '', '', '', html::select( 'addif', @add_ifs ) . qq|<input type=submit value=add></form>| );
490    
491                    print $client ok
492                            , qq|<form>|
493                            , html::table( -4, @table )
494                            , qq|</form>|
495                            ;
496    
497    
498            } elsif ( $path =~ m{^/ip/?(\w+)?} ) {
499                    print $client ok
500                            , join("\n", map { qq|<a href=/ip/$_>$_</a>| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ ))
501                            , ip::html( $1 )
502                            ;
503            } elsif ( $path =~ m{^/nmap} ) {
504                    if ( my $scan = $param->{scan} ) {
505                            nmap::scan( $scan );
506                            print $client redirect("$url/client#$scan");
507                    } else {
508                            print $client ok, qq|
509                                    <form method=get>
510                                    <input type=text name=scan>
511                                    <input type=submit value=scan>
512                                    </form>
513                            |;
514                    }
515            } elsif ( $path =~ m{^/wol/(\S+)} ) {
516                    print $client redirect( "$url/client/$1" ), wol::power_on($1);
517            } elsif ( $path =~ m!^/amt/(\w+)/$RE{net}{IPv4}{-keep}! ) {
518                    my ( $run, $ip ) = ( $1, $2 );
519                    print $client redirect( "$url/client/$ip" ), amt::RemoteControl( $ip, $run );
520          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
521                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
522                  warn $@ if $@;                  warn $@ if $@;
523                  print $client $redirect, qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;                  print $client redirect($url), qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
524                  server::debug( $debug ) if $1 eq 'debug';                  server::debug( $debug ) if $1 eq 'debug';
525          } elsif ( $path =~ m{^/start_stop/(\S+)} ) {          } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
526                  print $client $redirect, start_stop($1);                  print $client redirect, daemons::start_stop($1,$param);
527          } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {          } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
528                  $1->$2();                  my ( $package, $method ) = ( $1, $2 );
529                  print $client $redirect;                  $ENV{nr} = $1 if $package =~ s{\.(\d+)$}{};
530                    $package->$method();
531                    print $client redirect;
532          } elsif ( $path =~ m{^/kill/static/(\d+)} ) {          } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
533                  print $client $redirect;                  print $client redirect;
534                  kill 1, $1 || kill 9, $2 && warn "killed $1";                  kill 1, $1 || kill 9, $2 && warn "killed $1";
535          } else {          } else {
536                  print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";                  print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
# Line 287  sub get_request { Line 539  sub get_request {
539    
540  }  }
541    
 use browser;  
 use network;  
   
542  sub start {  sub start {
543    
544          warn 'tap ', network::tap();          warn 'network ', network::setup();
545    
546            daemons::start_stop 'browser', { url => $url };
547            daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd syslogd/ );
548    #       daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device
549    
550          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
551                          Proto     => 'tcp',                          Proto     => 'tcp',
552    #                       LocalAddr => $server::ip,
553                          LocalPort => $httpd::port,                          LocalPort => $httpd::port,
554                          Listen    => SOMAXCONN,                          Listen    => SOMAXCONN,
555                          Reuse     => 1                          Reuse     => 1
# Line 303  sub start { Line 557  sub start {
557    
558          print "url $url\n";          print "url $url\n";
559    
560          start_stop 'browser', $url;          syslogd::install_local;
561          start_stop 'dhcpd';          client::rebuild_mac_links;
         start_stop 'tftpd';  
         start_stop 'dnsd';  
         start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device  
562    
563          while (1) {          while (1) {
564                  my $client = $server->accept() || next; # ALARM trickle us                  my $client = $server->accept() || next; # ALARM trickle us
565                  my $request = <$client>;                  my $request = <$client>;
566    
567                  warn "request $request\n" if $debug;                  my $headers;
568    
569                    while ( my $header = <$client> ) {
570                            chomp $header;
571                            last if $header =~ m{^\s*$};
572                            my ( $n, $v ) = split(/:\s*/, $header);
573                            $headers->{ lc $n } = $v;
574                    }
575    
576                    if ( my $host = $headers->{host} ) {
577                            $url = 'http://' . $host;
578                            $url .= ":$port" unless $url =~ m{:\d+$};
579                    }
580    
581                    warn "## $url ## $request", dump( $headers ) if $debug;
582    
583                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
584                          my $path = $1;                          my $path = $1;
585                            $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
586                          my $param;                          my $param;
587                          if ( $path =~ s{\?(.+)}{} ) {                          if ( $path =~ s{\?(.+)}{} ) {
588                                  foreach my $p ( split(/[&;]/, $1) ) {                                  foreach my $p ( split(/[&;]/, $1) ) {
# Line 331  sub start { Line 597  sub start {
597                          warn "500 $request";                          warn "500 $request";
598                  }                  }
599    
600                  print $client menu() if $client->connected;                  print $client menu() . html_end() if $client->connected;
601    
602          }          }
603    

Legend:
Removed from v.181  
changed lines
  Added in v.522

  ViewVC Help
Powered by ViewVC 1.1.26