/[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 70 by dpavlin, Thu Jul 30 22:44:55 2009 UTC revision 540 by dpavlin, Sat Oct 9 12:25:41 2010 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;  use Regexp::Common qw/net/;
21    use POSIX qw(strftime);
22    
23  our $pids;  our $title;
 $pids = { httpd => $$ } unless defined $pids; # keep pids on refresh  
24    
25  sub DESTROY {  sub html_start {
26          warn "pids ",dump( $pids );  qq{
27          foreach ( values %$pids ) {  <html>
28                  warn "kill $_";  <head>
29                  kill 1,$_ || kill 9, $_;  <title>$title</title>
30          }  </head>
31  }  <body>
32    }}
33    
34    sub html_end {
35    qq{
36    </body>
37    </html>
38    }}
39    
40  our $port = 7777;  our $port = 7777;
41    
# Line 38  our $url = "http://$server::ip:$port"; Line 45  our $url = "http://$server::ip:$port";
45    
46  use html;  use html;
47  our $static_pids;  our $static_pids;
48    use progress_bar;
49    use config;
50    use client;
51    use log;
52    use x11;
53    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 46  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 64  sub static { Line 113  sub static {
113          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";          print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
114    
115          open(my $fh, $full);          open(my $fh, $full);
         print "static $path $type $size\n";  
116    
117          my $block = 8192;          my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
118          my $buff;          my $buff;
119          my $pos = 0;          my $pos = 0;
120    
121            store::audit( 'static', { pid => $$, path => $path, type => $type, size => $size, block => $block, peerhost => $client->peerhost });
122    
123            progress_bar::start;
124    
125          while( my $len = read $fh, $buff, $block ) {          while( my $len = read $fh, $buff, $block ) {
126                  print $client $buff;                  print $client $buff;
127                    $client->flush;
128                  $pos += $len;                  $pos += $len;
129                  printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;                  progress_bar::tick( $path, $pos, $size );
130          }          }
131          close($fh);          close($fh);
132          close($client);          close($client);
133    
134          print "$path $pos == $size OK\n";          print STDERR "\n";
135    
136          exit;          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 screen;  }
 use kvm;  
   
 $SIG{CHLD} = 'IGNORE';  
142    
143  sub start_stop {  sub redirect {
144          my $daemon = shift;          my $to = shift;
145          my $pid = $pids->{$daemon};          $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 pids: ",dump( $pids );  
   
         if ( $pid =~ m{^\d+$} ) {  
                 warn "kill 9 $pid";  
                 kill 9, $pid;  
                 $pids->{$daemon} = 'stopped';  
                 return qq|$daemon pid $pid stopped|;  
         } else {  
                 if ( $pid = fork ) {  
                         # parent  
                         $pids->{$daemon} = $pid;  
                         warn "forked $daemon $pid";  
                         return qq|$daemon pid $pid started|;  
                 } elsif ( defined $pid ) {  
                         # child  
                         my $eval = $daemon . '::start(' . ( @_ ? 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|;  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          warn "get_request $client $path ",dump( $param );          server->refresh;
158    
159            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 $screen = $pids->{screen} ? qq|stop <tt>$pids->{screen}</tt>|        : 'start';                  my @rows;
                 my $kvm    = $pids->{kvm}    ? qq|stop <tt>$pids->{kvm}</tt>|           :  
                                          $pids->{screen} ? qq|start|                                                    : qq|start screen first|;  
   
                 my @rows = (  
                         'ip',           html::tt( $server::ip ),  
                         'netmask',      html::tt( $server::netmask ),  
168    
169                          'debug',        qq|<a href=/our/debug/| . boolean::toggle($debug) . qq|>$debug</a>|,                  my $debug_proc = '';
170                  );  
171    warn "XXX pids = ", dump( $daemons::pids );
172    
173                    foreach my $name ( sort keys %$daemons::pids ) {
174                            my $pid = $daemons::pids->{$name}; # || next;
175    
176                            my $html;
177    
                 warn 'pids: ', dump( $pids );  
                 foreach my $name ( keys %$pids ) {  
                         my $pid = $pids->{$name} || next;  
178                          my $proc = "/proc/$pid/status";                          my $proc = "/proc/$pid/status";
179    
180                          my $html = qq|<a href=/$name>$pid</a>|;                          if ( -e $proc ) {
181                          $html   .= qq|<pre style="font-size: 10%">|                                  $html .= qq|<a href=/start_stop/$name>$pid</a>|;
182                                          .  ( $debug && -e $proc ? read_file($proc) : '' )                                  if ( $debug ) {
183                                          .  qq|</pre>| if $debug;                                          $html .= qq| <a name=$pid href=#proc-$pid>?</a>| if $name->can('start');
184    
185                                            $debug_proc
186                                                    .= qq|<a name=proc-$pid href=#$pid>$proc</a><pre style="font-size: 10%">|
187                                                    .  read_file($proc)
188                                                    .  qq|</pre>|
189                                                    ;
190                                    }
191    
192                                    my $class = $name;
193                                    $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 ( $class->can('actions') ) {
200                                            $html .= qq| <a href=/action/$name/$_>$_</a>| foreach $class->actions;
201                                    }
202                            } else {
203                                    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    
219                  warn 'static_pids: ', dump( $static_pids );                  my $below_table = '';
220    
221                    warn 'static_pids: ', dump( $static_pids ) if $debug;
222                  foreach my $pid ( keys %$static_pids ) {                  foreach my $pid ( keys %$static_pids ) {
223                          my $path = $static_pids->{$pid};                          my $path = $static_pids->{$pid};
224                          if ( -d "/proc/$pid" ) {                          if ( -d "/proc/$pid" ) {
225                                  push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );                                  push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
226                            } elsif ( $param->{clean_completed_downloads} ) {
227                                    delete $static_pids->{$pid}
228                          } else {                          } else {
229                                  push @rows, ( $path => "$pid competed" );                                  push @rows, ( $path => "$pid competed" );
230                                    $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
231                          }                          }
232                  }                  }
233    
234                  print $client $ok, html::table( 2, @rows );                  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 )
239                            , $below_table
240                            , $kvm
241                            , html::tabs( log::mac_changes )
242                            , $debug_proc
243                            ;
244    
245            } elsif ( $path =~ m{^/server} ) {
246                    foreach my $name ( keys %$param ) {
247                            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})?! ) {
334                    my $ip = $1;
335                    $title = $ip if $ip;
336    
337                    if ( $param->{action} eq 'remove' ) {
338                            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 $conf = client::all_conf( $ip );
366                            my $config = delete $conf->{config};
367    
368                            my @editable = ( qw/hostname config homepage/, grep { m{^\Q$config\E} } keys %$conf );
369                            client::conf( $ip, $_ => $param->{$_} ) foreach @editable;
370    
371                            my $nmap = qq|<a href=/nmap?scan=$ip>nmap</a>|;
372                            my @table = (
373                                    'ping' => ping::host($ip)
374                                            ? qq|<span style="color:green">up</span> $nmap|
375                                            : qq|<span style="color: red">down</span> <a href=/wol/$ip>wol</a> $nmap|
376                                            ,
377                                    '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>|,
378                                    'mac' => format::mac( delete $conf->{mac}, 'html' ),
379                                    'hostname' => qq|<input type=text name=hostname value="| . delete($conf->{hostname}) . qq|">|,
380                                    'config' => html::select( 'config', $config, config::available ),
381                                    html::conf( $ip, $conf, 'edit', @editable )
382                            );
383    
384                            print $client ok
385                                    , qq|<form method=get>|
386                                    , html::table( 2, @table ),
387                                    , qq|
388                                            <input type=submit name=action value=change>
389                                            <input type=submit name=action value=remove style="color: red">
390                                            </form>|
391                                    ;
392    
393                            if ( $config ) {
394                                    if ( my $for_ip = config::for_ip( $ip ) ) {
395                                            print $client qq|<h2>config::for_ip</h2>| . html::pre( $for_ip );
396                                    }
397                            }
398    
399                            if ( $conf->{amt} ) {
400                                    print $client qq|<h2>amt network</h2>|, html::pre_dump( amt::network( $ip ) );
401                                    print $client qq|<h2>amt log</h2>|, html::pre_dump( amt::log( $ip ) );
402                            }
403    
404                    } else {
405    
406                            print $client ok qq|<h2>Clients on $server::ip</h2>|;
407    
408                            my @ping;
409                            if ( my $host = $param->{ping_target} ) {
410                                    @ping = ( $host );
411                            } elsif ( $param->{ping} ) {
412                                    @ping = client::all_ips;
413                            }
414    
415                            my $ping = ping::fping( @ping ) if @ping;
416                            my $arp = client::arp_mac_dev;
417    
418                            my @clients;
419    
420                            foreach my $ip ( client::all_ips ) {
421                                    
422                                    my $conf = client::all_conf( $ip );
423                                    my $mac = delete $conf->{mac} || '';
424                                    my $dev = $arp->{$mac};
425    
426                                    my $in_dhcp_range = ip::in_dhcp_range($ip);
427    
428                                    next unless $dev || $param->{all} || $in_dhcp_range;
429    
430                                    my $style
431                                            = 'style="color:'
432                                            . ( $ping->{$ip} ? 'green' : 'red' )
433                                            . '"'
434                                            if $ping;
435    
436                                    $style ||= '';
437                                    my $ip_text = qq|<tt>$ip</tt>|;
438                                    $ip_text = qq|<tt><b>$ip</b></tt>| if $in_dhcp_range;
439    
440                                    $dev = qq|<tt>$dev</tt>| if $dev;
441    
442                                    push @clients
443                                            , qq|<a $style name=$ip target=client href=/client/$ip>$ip_text</a>|
444                                            , format::mac( $mac => 'html' )
445                                            , $dev
446                                            , delete $conf->{hostname}
447                                            , html::conf( $ip, $conf, 'inline' )
448                                    ;
449                            }
450    
451                            my $all = $param->{all} ? 0 : 1;
452    
453                            print $client html::table( -5, 'ip', 'mac', qq|<a href="?all=$all">dev</a>|, 'hostname', 'conf', @clients );
454                            print $client qq|
455                                    <form method=get>
456                                    <input type=text   name=ping_target   size=15>
457                                    <input type=submit name=ping value=ping>
458                                    </form>
459                            |;
460                    }
461    
462    
463            } elsif ( $path =~ m{^/brctl} ) {
464    
465                    system 'brctl addif virtual ' . $param->{addif} if $param->{addif};
466                    system 'brctl delif virtual ' . $param->{delif} if $param->{delif};
467    
468                    my $in_virtual;
469    
470                    my @table =
471                            map {
472                                    my @c = split(/\t+/,$_,4);
473                                    if ( $#c == 1 ) {
474                                            $in_virtual->{ $c[1] }++;
475                                            @c = ( '', '', '', $c[1] );
476                                    } else {
477                                            $in_virtual->{ $c[3] }++;
478                                    }
479                                    if ( $c[3] =~ m{\d$} ) {
480                                            $c[3] = qq|<input type=submit name=delif value=$c[3] style="color:red" title="remove $c[3] from bridge">|;
481                                    }
482                                    @c
483                            } split(/\n/, `brctl show`)
484                    ;
485    
486                    my @add_ifs = grep { ! $in_virtual->{$_} && $_ ne 'virtual' } ip::devices_up;
487    
488                    push @table, ( '', '', '', html::select( 'addif', @add_ifs ) . qq|<input type=submit value=add></form>| );
489    
490                    print $client ok
491                            , qq|<form>|
492                            , html::table( -4, @table )
493                            , qq|</form>|
494                            ;
495    
496    
497            } elsif ( $path =~ m{^/ip/?(\w+)?} ) {
498                    print $client ok
499                            , join("\n", map { qq|<a href=/ip/$_>$_</a>| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ ))
500                            , ip::html( $1 )
501                            ;
502            } elsif ( $path =~ m{^/nmap} ) {
503                    if ( my $scan = $param->{scan} ) {
504                            nmap::scan( $scan );
505                            print $client redirect("$url/client#$scan");
506                    } else {
507                            print $client ok, qq|
508                                    <form method=get>
509                                    <input type=text name=scan>
510                                    <input type=submit value=scan>
511                                    </form>
512                            |;
513                    }
514            } elsif ( $path =~ m{^/wol/(\S+)} ) {
515                    print $client redirect( "$url/client/$1" ), wol::power_on($1);
516            } elsif ( $path =~ m!^/amt/(\w+)/$RE{net}{IPv4}{-keep}! ) {
517                    my ( $run, $ip ) = ( $1, $2 );
518                    print $client redirect( "$url/client/$ip" ), amt::RemoteControl( $ip, $run );
519          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {          } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
520                  eval 'our $' . $1 . ' = ' . $2;                  eval 'our $' . $1 . ' = ' . $2;
521                  warn $@ if $@;                  warn $@ if $@;
522                  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>|;
523                  server::debug( $debug ) if $1 eq 'debug';                  server::debug( $debug ) if $1 eq 'debug';
524          } elsif ( $path =~ m{^/(screen|kvm)} ) {          } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
525                  print $client $redirect, start_stop($1);                  print $client redirect, daemons::start_stop($1,$param);
526            } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
527                    my ( $package, $method ) = ( $1, $2 );
528                    $ENV{nr} = $1 if $package =~ s{\.(\d+)$}{};
529                    $package->$method();
530                    print $client redirect;
531          } elsif ( $path =~ m{^/kill/static/(\d+)} ) {          } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
532                  print $client $redirect;                  print $client redirect;
533                  kill 9, $1 && warn "killed $1";                  kill 1, $1 || kill 9, $2 && warn "killed $1";
         } elsif ( $path eq '/exit' ) {  
 #               DESTROY;  
                 exit 0;  
         } elsif ( $path =~ m{/boot} ) {  
                 print $client qq{$ok  
 #!gpxe  
 imgfree  
 login  
 chain http://$server::ip:$httpd::port/  
   
                         };  
534          } else {          } else {
535                  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>";
536                  warn "404 $path";                  warn "404 $path";
# Line 196  chain http://$server::ip:$httpd::port/ Line 538  chain http://$server::ip:$httpd::port/
538    
539  }  }
540    
 use browser;  
   
541  sub start {  sub start {
542    
543            warn 'network ', network::setup();
544    
545            daemons::start_stop 'browser', { url => $url };
546            daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd syslogd/ );
547    #       daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device
548    
549          my $server = IO::Socket::INET->new(          my $server = IO::Socket::INET->new(
550                          Proto     => 'tcp',                          Proto     => 'tcp',
551    #                       LocalAddr => $server::ip,
552                          LocalPort => $httpd::port,                          LocalPort => $httpd::port,
553                          Listen    => SOMAXCONN,                          Listen    => SOMAXCONN,
554                          Reuse     => 1                          Reuse     => 1
# Line 209  sub start { Line 556  sub start {
556    
557          print "url $url\n";          print "url $url\n";
558    
559          start_stop 'browser', $url;          syslogd::install_local;
560          start_stop 'screen';          client::rebuild_mac_links;
         start_stop 'kvm';  
561    
562          while (my $client = $server->accept()) {          while (1) {
563                  $client->autoflush(1);                  my $client = $server->accept() || next; # ALARM trickle us
564                  my $request = <$client>;                  my $request = <$client>;
565    
566                  warn "request $request\n" if $debug;                  my $headers;
567    
568                    while ( my $header = <$client> ) {
569                            chomp $header;
570                            last if $header =~ m{^\s*$};
571                            my ( $n, $v ) = split(/:\s*/, $header);
572                            $headers->{ lc $n } = $v;
573                    }
574    
575                    if ( my $host = $headers->{host} ) {
576                            $url = 'http://' . $host;
577                            $url .= ":$port" unless $url =~ m{:\d+$};
578                    }
579    
580                  Module::Refresh->refresh;                  warn "## $url ## $request", dump( $headers ) if $debug;
581    
582                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
583                          my $path = $1;                          my $path = $1;
584                            $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
585                          my $param;                          my $param;
586                          if ( $path =~ s{\?(.+)}{} ) {                          if ( $path =~ s{\?(.+)}{} ) {
587                                  foreach my $p ( split(/[&;]/, $1) ) {                                  foreach my $p ( split(/[&;]/, $1) ) {
# Line 231  sub start { Line 590  sub start {
590                                  }                                  }
591                                  warn "param: ",dump( $param ) if $debug;                                  warn "param: ",dump( $param ) if $debug;
592                          }                          }
                         warn "path $path param: ",dump( $param );  
593                          get_request $client, $path, $param;                          get_request $client, $path, $param;
594                  } else {                  } else {
595                          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";
596                          warn "500 $request";                          warn "500 $request";
597                  }                  }
598    
599                  print $client qq{                  print $client menu() . html_end() if $client->connected;
                 <div style="font-size: 80%; color: #888">  
                 <a href="">reload</a>  
                 <a href=/>index</a>  
                 <a href=/exit>exit</a>  
                 </div>  
                 } if $client->connected;  
600    
601          }          }
602    

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

  ViewVC Help
Powered by ViewVC 1.1.26