/[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 72 - (hide annotations)
Thu Jul 30 23:57:19 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 5820 byte(s)
record and display log changes by mac addresses

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

  ViewVC Help
Powered by ViewVC 1.1.26