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

Contents of /lib/PXElator/httpd.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 67 - (show annotations)
Thu Jul 30 21:31:30 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 4999 byte(s)
implemented correct start/stop logic (which now works!)
and pushed debug state into $server::debug and file 
conf/debug for shared state between servers

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

  ViewVC Help
Powered by ViewVC 1.1.26