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

  ViewVC Help
Powered by ViewVC 1.1.26