/[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

Annotation of /lib/PXElator/httpd.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 543 - (hide annotations)
Sat Oct 9 12:38:00 2010 UTC (11 years ago) by dpavlin
File size: 15151 byte(s)
add params with config prefix if config exists
1 dpavlin 42 package httpd;
2    
3     use warnings;
4     use strict;
5     use autodie;
6    
7     =head1 httpd
8    
9     Start with:
10    
11     perl -Ilib/PXElator -Mhttpd -e httpd::start
12    
13     =cut
14    
15     use Data::Dump qw/dump/;
16     use Carp qw/confess/;
17     use File::Slurp;
18     #use JSON;
19     use IO::Socket::INET;
20 dpavlin 162 use Regexp::Common qw/net/;
21 dpavlin 493 use POSIX qw(strftime);
22 dpavlin 42
23 dpavlin 379 our $title;
24 dpavlin 149
25 dpavlin 379 sub html_start {
26     qq{
27     <html>
28     <head>
29     <title>$title</title>
30     </head>
31     <body>
32     }}
33    
34     sub html_end {
35     qq{
36     </body>
37     </html>
38     }}
39    
40 dpavlin 42 our $port = 7777;
41    
42     use server;
43 dpavlin 67 our $debug = server::debug;
44 dpavlin 64 our $url = "http://$server::ip:$port";
45 dpavlin 42
46 dpavlin 43 use html;
47 dpavlin 70 our $static_pids;
48 dpavlin 115 use progress_bar;
49 dpavlin 153 use config;
50 dpavlin 159 use client;
51     use log;
52 dpavlin 173 use x11;
53 dpavlin 181 use amt;
54 dpavlin 185 use daemons;
55 dpavlin 43
56 dpavlin 185 use kvm;
57     use browser;
58     use network;
59 dpavlin 224 use ip;
60 dpavlin 225 use wireshark;
61 dpavlin 231 use syslogd;
62 dpavlin 311 use nmap;
63 dpavlin 335 use ping;
64     use wol;
65 dpavlin 185
66 dpavlin 482 use store;
67 dpavlin 185
68 dpavlin 431
69     sub menu {
70 dpavlin 487 my $store_url = $url;
71     $store_url =~ s{:\d+.+}{:28017};
72 dpavlin 431 qq{
73     <div style="font-size: 80%; color: #888">
74 dpavlin 433 <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 dpavlin 493 <a target=store href=$store_url >MongoDB</a>
81     <a target=store href=/store/latest >latest</a>
82 dpavlin 433 |
83     <a target=client href=/nmap >nmap</a>
84     <a target=client href=/client >client</a>
85 dpavlin 431 </div>
86    
87     }}
88    
89    
90 dpavlin 42 sub static {
91     my ($client,$path) = @_;
92    
93 dpavlin 65 my $full = "$server::base_dir/tftp/$path";
94 dpavlin 42
95 dpavlin 65 return if ! -f $full;
96 dpavlin 42
97 dpavlin 402 return if $full =~ m{\.ico$};
98    
99 dpavlin 70 if ( my $pid = fork ) {
100     # parent
101     close($client);
102     $static_pids->{$pid} = $path;
103     return 1;
104     }
105    
106 dpavlin 65 my $type = 'application/octet-stream';
107 dpavlin 42 $type = 'text/html' if $path =~ m{\.htm};
108     $type = 'application/javascript' if $path =~ m{\.js};
109 dpavlin 65 $type = 'text/plain' if $path =~ m{\.txt};
110 dpavlin 42
111 dpavlin 65 my $size = -s $full || return;
112    
113     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);
116    
117 dpavlin 96 my $block = 1400; # try not to fragment packages (pxelinux seems to have problems with it)
118 dpavlin 65 my $buff;
119     my $pos = 0;
120    
121 dpavlin 482 store::audit( 'static', { pid => $$, path => $path, type => $type, size => $size, block => $block, peerhost => $client->peerhost });
122 dpavlin 96
123 dpavlin 115 progress_bar::start;
124 dpavlin 105
125 dpavlin 65 while( my $len = read $fh, $buff, $block ) {
126 dpavlin 107 print $client $buff;
127 dpavlin 105 $client->flush;
128 dpavlin 65 $pos += $len;
129 dpavlin 115 progress_bar::tick( $path, $pos, $size );
130 dpavlin 42 }
131 dpavlin 65 close($fh);
132     close($client);
133 dpavlin 42
134 dpavlin 105 print STDERR "\n";
135 dpavlin 65
136 dpavlin 107 exit(0);
137 dpavlin 42 }
138    
139 dpavlin 199 sub ok {
140 dpavlin 379 qq|HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n| . html_start() . menu()
141 dpavlin 199 }
142 dpavlin 67
143 dpavlin 199 sub redirect {
144     my $to = shift;
145     $to ||= $url;
146     qq|HTTP/1.1 302 Found\r\nContent-type: text/html\r\nLocation: $to\r\n\r\n|
147     }
148    
149 dpavlin 484 sub toggle {
150     my $v = shift;
151     return $v ? 0 : 1;
152     }
153    
154 dpavlin 43 sub get_request {
155     my ( $client, $path, $param ) = @_;
156    
157 dpavlin 128 server->refresh;
158    
159 dpavlin 482 store::audit( 'request', { path => $path, param => $param, peerhost => $client->peerhost } );
160 dpavlin 43
161 dpavlin 379 $title = $path;
162    
163 dpavlin 43 if ( my $found = static( $client,$path ) ) {
164     warn "static $found" if $debug;
165     } elsif ( $path eq '/' ) {
166 dpavlin 53
167 dpavlin 251 my @rows;
168 dpavlin 70
169 dpavlin 96 my $debug_proc = '';
170 dpavlin 71
171 dpavlin 243 warn "XXX pids = ", dump( $daemons::pids );
172    
173 dpavlin 185 foreach my $name ( sort keys %$daemons::pids ) {
174 dpavlin 243 my $pid = $daemons::pids->{$name}; # || next;
175 dpavlin 43
176 dpavlin 173 my $html;
177 dpavlin 66
178 dpavlin 81 my $proc = "/proc/$pid/status";
179 dpavlin 71
180 dpavlin 81 if ( -e $proc ) {
181 dpavlin 173 $html .= qq|<a href=/start_stop/$name>$pid</a>|;
182 dpavlin 81 if ( $debug ) {
183 dpavlin 225 $html .= qq| <a name=$pid href=#proc-$pid>?</a>| if $name->can('start');
184 dpavlin 81
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 dpavlin 390 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 dpavlin 81 }
198 dpavlin 88
199 dpavlin 390 if ( $class->can('actions') ) {
200     $html .= qq| <a href=/action/$name/$_>$_</a>| foreach $class->actions;
201 dpavlin 88 }
202 dpavlin 173 } else {
203 dpavlin 243 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 dpavlin 225 if ( $name->can('fork_actions') ) {
210     $html .= qq| <a href=/start_stop/$name/$_>$_</a>| foreach $name->fork_actions;
211     }
212 dpavlin 71 }
213    
214 dpavlin 243 die "no html generated" unless $html;
215    
216 dpavlin 66 push @rows, ( $name => $html );
217     }
218    
219 dpavlin 91 my $below_table = '';
220    
221 dpavlin 78 warn 'static_pids: ', dump( $static_pids ) if $debug;
222 dpavlin 70 foreach my $pid ( keys %$static_pids ) {
223     my $path = $static_pids->{$pid};
224     if ( -d "/proc/$pid" ) {
225     push @rows, ( $path => qq|<a href=/kill/static/$pid>$pid</a>| );
226 dpavlin 91 } elsif ( $param->{clean_completed_downloads} ) {
227     delete $static_pids->{$pid}
228 dpavlin 70 } else {
229     push @rows, ( $path => "$pid competed" );
230 dpavlin 91 $below_table = qq|<a href="/?clean_completed_downloads=1">clean completed downloads</a>|;
231 dpavlin 70 }
232     }
233    
234 dpavlin 405 my $kvm = kvm::next_nr;
235 dpavlin 407 $kvm = qq|<div><a href=/start_stop/kvm?nr=$kvm>create new kvm $kvm</a></div>|;
236 dpavlin 405
237 dpavlin 199 print $client ok
238 dpavlin 72 , html::table( 2, @rows )
239 dpavlin 407 , $below_table
240 dpavlin 405 , $kvm
241 dpavlin 72 , html::tabs( log::mac_changes )
242     , $debug_proc
243     ;
244 dpavlin 66
245 dpavlin 149 } elsif ( $path =~ m{^/server} ) {
246 dpavlin 400 foreach my $name ( keys %$param ) {
247     eval '$server::' . $name . '= $param->{$name}';
248 dpavlin 391 }
249 dpavlin 400 my @table = (
250 dpavlin 484 'debug' => qq|<a href=/our/debug/| . toggle($debug) . qq|>$debug</a>|,
251 dpavlin 400 , 'new_clients' => qq|<input type=text name=new_clients size=3 value="$server::new_clients">|
252     );
253    
254 dpavlin 427 foreach my $editable ( 'ip', 'bcast', 'netmask', 'ip_from', 'ip_to', 'domain' ) {
255 dpavlin 400 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 dpavlin 199 print $client ok
265 dpavlin 391 , qq|<form method=get>|
266 dpavlin 400 , html::table( 2, @table )
267     , qq|
268     <input type=submit name=action value=change>
269     </form>
270     |
271 dpavlin 149 ;
272 dpavlin 400
273 dpavlin 493 } elsif ( $path =~ m{^/store/latest} ) {
274 dpavlin 487 print $client ok
275 dpavlin 490 , 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 dpavlin 487 , qq|<table>|
292     ;
293 dpavlin 490 my ( $s1,$s2 ) = ( ' class=z', '' );
294 dpavlin 491 my @cols;
295    
296 dpavlin 496 my $from_t = $param->{from_t};
297    
298 dpavlin 522 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 dpavlin 487 my $o = shift;
306     my $p = delete( $o->{package} );
307     delete( $o->{_id} );
308 dpavlin 488
309 dpavlin 491 if ( ! @cols ) {
310 dpavlin 493 #@cols = keys %$p;
311     @cols = qw( time name );
312 dpavlin 491 print $client qq|<tr><th>|
313     , join(qq|</th><th>|, @cols)
314     , qq|</th><th></th></tr>|
315     ;
316     }
317    
318 dpavlin 487 # XXX sigh, dump dies if we don't do this
319 dpavlin 493 # delete $o->{$_} foreach ( grep { ! defined $o->{$_} } keys %$o );
320 dpavlin 488
321 dpavlin 493 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 dpavlin 490 ;
326 dpavlin 489 ( $s1, $s2 ) = ( $s2, $s1 );
327 dpavlin 496 $from_t = $p->{time};
328 dpavlin 487 });
329 dpavlin 496 print $client qq|</table>|,
330     qq|<a href="/store/latest?from_t=$from_t">more</a>|
331     ;
332 dpavlin 487
333 dpavlin 162 } elsif ( $path =~ m!^/client(?:/$RE{net}{IPv4}{-keep})?! ) {
334 dpavlin 320 my $ip = $1;
335 dpavlin 419 $title = $ip if $ip;
336 dpavlin 200
337 dpavlin 326 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 dpavlin 200 }
347    
348 dpavlin 320 if ( ! $ip ) {
349     my $peer_ip = $client->peerhost;
350 dpavlin 319
351 dpavlin 334 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 dpavlin 320
357 dpavlin 326 # show edit for clients in our dhcp range
358 dpavlin 334 if ( $ip_int >= $from_int && $ip_int <= $to_int ) {
359 dpavlin 320 $ip = $peer_ip;
360     }
361     }
362    
363     if ( $ip && $ip ne $server::ip ) {
364 dpavlin 188
365 dpavlin 540 my $conf = client::all_conf( $ip );
366     my $config = delete $conf->{config};
367 dpavlin 346
368 dpavlin 543 my @editable = ( qw/hostname config homepage/ );
369     # add params with config prefix if config exists
370     push @editable, grep { m{^\Q$config\E} } keys %$conf if $config;
371 dpavlin 542 my @update = grep { defined $param->{$_} } @editable;
372     if ( @update ) {
373     client::conf( $ip, $_ => $param->{$_} ) foreach @update;
374     print $client redirect("$url/client/$ip");
375     return;
376     }
377 dpavlin 346
378 dpavlin 343 my $nmap = qq|<a href=/nmap?scan=$ip>nmap</a>|;
379 dpavlin 188 my @table = (
380 dpavlin 376 'ping' => ping::host($ip)
381     ? qq|<span style="color:green">up</span> $nmap|
382     : qq|<span style="color: red">down</span> <a href=/wol/$ip>wol</a> $nmap|
383     ,
384 dpavlin 199 '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>|,
385 dpavlin 346 'mac' => format::mac( delete $conf->{mac}, 'html' ),
386     'hostname' => qq|<input type=text name=hostname value="| . delete($conf->{hostname}) . qq|">|,
387 dpavlin 347 'config' => html::select( 'config', $config, config::available ),
388 dpavlin 346 html::conf( $ip, $conf, 'edit', @editable )
389 dpavlin 188 );
390    
391 dpavlin 199 print $client ok
392 dpavlin 162 , qq|<form method=get>|
393 dpavlin 188 , html::table( 2, @table ),
394 dpavlin 326 , qq|
395     <input type=submit name=action value=change>
396     <input type=submit name=action value=remove style="color: red">
397     </form>|
398 dpavlin 162 ;
399 dpavlin 181
400 dpavlin 347 if ( $config ) {
401     if ( my $for_ip = config::for_ip( $ip ) ) {
402     print $client qq|<h2>config::for_ip</h2>| . html::pre( $for_ip );
403     }
404     }
405    
406 dpavlin 385 if ( $conf->{amt} ) {
407     print $client qq|<h2>amt network</h2>|, html::pre_dump( amt::network( $ip ) );
408     print $client qq|<h2>amt log</h2>|, html::pre_dump( amt::log( $ip ) );
409     }
410 dpavlin 329
411 dpavlin 162 } else {
412 dpavlin 190
413 dpavlin 471 print $client ok qq|<h2>Clients on $server::ip</h2>|;
414    
415 dpavlin 334 my @ping;
416     if ( my $host = $param->{ping_target} ) {
417     @ping = ( $host );
418     } elsif ( $param->{ping} ) {
419     @ping = client::all_ips;
420     }
421    
422     my $ping = ping::fping( @ping ) if @ping;
423 dpavlin 330 my $arp = client::arp_mac_dev;
424 dpavlin 190
425 dpavlin 471 my @clients;
426    
427     foreach my $ip ( client::all_ips ) {
428    
429     my $conf = client::all_conf( $ip );
430     my $mac = delete $conf->{mac} || '';
431 dpavlin 474 my $dev = $arp->{$mac};
432 dpavlin 471
433 dpavlin 514 my $in_dhcp_range = ip::in_dhcp_range($ip);
434 dpavlin 471
435 dpavlin 514 next unless $dev || $param->{all} || $in_dhcp_range;
436    
437 dpavlin 471 my $style
438     = 'style="color:'
439     . ( $ping->{$ip} ? 'green' : 'red' )
440     . '"'
441     if $ping;
442    
443     $style ||= '';
444     my $ip_text = qq|<tt>$ip</tt>|;
445 dpavlin 514 $ip_text = qq|<tt><b>$ip</b></tt>| if $in_dhcp_range;
446 dpavlin 471
447 dpavlin 474 $dev = qq|<tt>$dev</tt>| if $dev;
448    
449 dpavlin 471 push @clients
450     , qq|<a $style name=$ip target=client href=/client/$ip>$ip_text</a>|
451     , format::mac( $mac => 'html' )
452 dpavlin 474 , $dev
453 dpavlin 471 , delete $conf->{hostname}
454     , html::conf( $ip, $conf, 'inline' )
455 dpavlin 162 ;
456 dpavlin 471 }
457    
458     my $all = $param->{all} ? 0 : 1;
459    
460     print $client html::table( -5, 'ip', 'mac', qq|<a href="?all=$all">dev</a>|, 'hostname', 'conf', @clients );
461 dpavlin 330 print $client qq|
462     <form method=get>
463 dpavlin 334 <input type=text name=ping_target size=15>
464 dpavlin 330 <input type=submit name=ping value=ping>
465     </form>
466     |;
467 dpavlin 162 }
468 dpavlin 401
469    
470 dpavlin 229 } elsif ( $path =~ m{^/brctl} ) {
471 dpavlin 401
472     system 'brctl addif virtual ' . $param->{addif} if $param->{addif};
473     system 'brctl delif virtual ' . $param->{delif} if $param->{delif};
474    
475     my $in_virtual;
476    
477     my @table =
478 dpavlin 333 map {
479     my @c = split(/\t+/,$_,4);
480     if ( $#c == 1 ) {
481 dpavlin 401 $in_virtual->{ $c[1] }++;
482     @c = ( '', '', '', $c[1] );
483 dpavlin 333 } else {
484 dpavlin 401 $in_virtual->{ $c[3] }++;
485 dpavlin 333 }
486 dpavlin 401 if ( $c[3] =~ m{\d$} ) {
487     $c[3] = qq|<input type=submit name=delif value=$c[3] style="color:red" title="remove $c[3] from bridge">|;
488     }
489     @c
490 dpavlin 333 } split(/\n/, `brctl show`)
491 dpavlin 401 ;
492    
493     my @add_ifs = grep { ! $in_virtual->{$_} && $_ ne 'virtual' } ip::devices_up;
494    
495     push @table, ( '', '', '', html::select( 'addif', @add_ifs ) . qq|<input type=submit value=add></form>| );
496    
497     print $client ok
498     , qq|<form>|
499     , html::table( -4, @table )
500     , qq|</form>|
501     ;
502    
503    
504 dpavlin 224 } elsif ( $path =~ m{^/ip/?(\w+)?} ) {
505     print $client ok
506     , join("\n", map { qq|<a href=/ip/$_>$_</a>| } ( qw/link addr route neigh ntable tunnel maddr mroute xfrm/ ))
507     , ip::html( $1 )
508     ;
509 dpavlin 311 } elsif ( $path =~ m{^/nmap} ) {
510     if ( my $scan = $param->{scan} ) {
511     nmap::scan( $scan );
512     print $client redirect("$url/client#$scan");
513     } else {
514     print $client ok, qq|
515     <form method=get>
516     <input type=text name=scan>
517     <input type=submit value=scan>
518     </form>
519     |;
520     }
521 dpavlin 335 } elsif ( $path =~ m{^/wol/(\S+)} ) {
522     print $client redirect( "$url/client/$1" ), wol::power_on($1);
523 dpavlin 376 } elsif ( $path =~ m!^/amt/(\w+)/$RE{net}{IPv4}{-keep}! ) {
524     my ( $run, $ip ) = ( $1, $2 );
525     print $client redirect( "$url/client/$ip" ), amt::RemoteControl( $ip, $run );
526 dpavlin 43 } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
527     eval 'our $' . $1 . ' = ' . $2;
528     warn $@ if $@;
529 dpavlin 199 print $client redirect($url), qq|<big>$1 = $2</big><br>Location: <a href="$url">$url</a>|;
530 dpavlin 67 server::debug( $debug ) if $1 eq 'debug';
531 dpavlin 158 } elsif ( $path =~ m{^/start_stop/(\S+)} ) {
532 dpavlin 390 print $client redirect, daemons::start_stop($1,$param);
533 dpavlin 88 } elsif ( $path =~ m{^/action/([^/]+)/(.+)} ) {
534 dpavlin 392 my ( $package, $method ) = ( $1, $2 );
535     $ENV{nr} = $1 if $package =~ s{\.(\d+)$}{};
536     $package->$method();
537 dpavlin 199 print $client redirect;
538 dpavlin 70 } elsif ( $path =~ m{^/kill/static/(\d+)} ) {
539 dpavlin 199 print $client redirect;
540 dpavlin 86 kill 1, $1 || kill 9, $2 && warn "killed $1";
541 dpavlin 43 } else {
542     print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<big>404 $path</big>";
543     warn "404 $path";
544     }
545    
546     }
547 dpavlin 53
548 dpavlin 42 sub start {
549    
550 dpavlin 229 warn 'network ', network::setup();
551 dpavlin 140
552 dpavlin 390 daemons::start_stop 'browser', { url => $url };
553 dpavlin 231 daemons::start_stop $_ foreach ( qw/dhcpd tftpd dnsd syslogd/ );
554 dpavlin 390 # daemons::start_stop 'kvm' unless $ENV{DEV}; # skip kvm statup when running on real device
555 dpavlin 189
556 dpavlin 42 my $server = IO::Socket::INET->new(
557     Proto => 'tcp',
558 dpavlin 418 # LocalAddr => $server::ip,
559 dpavlin 42 LocalPort => $httpd::port,
560     Listen => SOMAXCONN,
561     Reuse => 1
562 dpavlin 43 ) || die "can't start server on $url: $!";
563 dpavlin 42
564     print "url $url\n";
565    
566 dpavlin 363 syslogd::install_local;
567 dpavlin 403 client::rebuild_mac_links;
568 dpavlin 363
569 dpavlin 118 while (1) {
570     my $client = $server->accept() || next; # ALARM trickle us
571 dpavlin 42 my $request = <$client>;
572    
573 dpavlin 421 my $headers;
574 dpavlin 42
575 dpavlin 421 while ( my $header = <$client> ) {
576     chomp $header;
577     last if $header =~ m{^\s*$};
578     my ( $n, $v ) = split(/:\s*/, $header);
579     $headers->{ lc $n } = $v;
580     }
581    
582     if ( my $host = $headers->{host} ) {
583     $url = 'http://' . $host;
584     $url .= ":$port" unless $url =~ m{:\d+$};
585     }
586    
587     warn "## $url ## $request", dump( $headers ) if $debug;
588    
589 dpavlin 42 if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
590 dpavlin 43 my $path = $1;
591 dpavlin 328 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
592 dpavlin 42 my $param;
593 dpavlin 43 if ( $path =~ s{\?(.+)}{} ) {
594 dpavlin 42 foreach my $p ( split(/[&;]/, $1) ) {
595     my ($n,$v) = split(/=/, $p, 2);
596     $param->{$n} = $v;
597     }
598     warn "param: ",dump( $param ) if $debug;
599     }
600 dpavlin 43 get_request $client, $path, $param;
601 dpavlin 42 } else {
602     print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
603     warn "500 $request";
604     }
605    
606 dpavlin 379 print $client menu() . html_end() if $client->connected;
607 dpavlin 42
608     }
609    
610     die "server died";
611     }
612    
613 dpavlin 45 warn "loaded";
614    
615 dpavlin 42 1;

  ViewVC Help
Powered by ViewVC 1.1.26