/[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 64 - (show annotations)
Thu Jul 30 16:26:54 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 4057 byte(s)
show server ip and netmask

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

  ViewVC Help
Powered by ViewVC 1.1.26