--- lib/PXElator/httpd.pm 2009/07/29 17:42:48 42
+++ lib/PXElator/httpd.pm 2009/07/29 19:57:07 43
@@ -17,6 +17,7 @@
use File::Slurp;
#use JSON;
use IO::Socket::INET;
+use Module::Refresh;
our $port = 7777;
our $debug = 1;
@@ -24,6 +25,8 @@
use server;
our $url = "http://$server::ip:$port/";
+use html;
+
sub static {
my ($client,$path) = @_;
@@ -50,7 +53,40 @@
my $ok = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";
+use boolean;
+
+sub get_request {
+ my ( $client, $path, $param ) = @_;
+
+ warn "get_request $client $path $param";
+
+ if ( my $found = static( $client,$path ) ) {
+ warn "static $found" if $debug;
+ } elsif ( $path eq '/' ) {
+ print $client $ok,
+ html::table( 2,
+ 'pid', $$,
+ 'debug', qq|$debug|,
+ );
+
+ } elsif ( $path =~ m{^/our/(\w+)/(\S+)} ) {
+ eval 'our $' . $1 . ' = ' . $2;
+ warn $@ if $@;
+ print $client qq|HTTP/1.1 302 Found\r\nLocation: $url\r\nContent-type: text/html\r\n\r\n$1 = $2
Location: $url|;
+ } elsif ( $path =~ m{/boot} ) {
+ print $client qq{$ok
+#!gpxe
+imgfree
+login
+chain http://$server::ip:$httpd::port/
+
+ };
+ } else {
+ print $client "HTTP/1.0 404 $path\r\nConnection: close\r\nContent-type: text/html\r\n\r\n404 $path";
+ warn "404 $path";
+ }
+}
sub start {
my $server = IO::Socket::INET->new(
@@ -58,9 +94,7 @@
LocalPort => $httpd::port,
Listen => SOMAXCONN,
Reuse => 1
- );
-
- die "can't setup server" unless $server;
+ ) || die "can't start server on $url: $!";
print "url $url\n";
@@ -72,48 +106,32 @@
warn "request $request\n" if $debug;
+ Module::Refresh->refresh;
+
if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
- my $method = $1;
+ my $path = $1;
my $param;
- if ( $method =~ s{\?(.+)}{} ) {
+ if ( $path =~ s{\?(.+)}{} ) {
foreach my $p ( split(/[&;]/, $1) ) {
my ($n,$v) = split(/=/, $p, 2);
$param->{$n} = $v;
}
warn "param: ",dump( $param ) if $debug;
}
- warn "method $method";
-
- if ( my $path = static( $client,$1 ) ) {
- warn "static $path" if $debug;
- } elsif ( $method eq '/' ) {
- print $client qq{$ok
- pid $$
- debug $debug
- };
-
- } elsif ( $method =~ m{/boot} ) {
- print $client qq{$ok
-#!gpxe
-imgfree
-login
-chain http://$server::ip:$httpd::port/
-
- };
- } else {
- print $client "HTTP/1.0 404 Unkown method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n404 $request";
- warn "404 $request";
- }
+ warn "path $path param: ",dump( $param );
+ get_request $client, $path, $param;
} else {
print $client "HTTP/1.0 500 No method\r\nConnection: close\r\nContent-type: text/plain\r\n\r\n500 $request";
warn "500 $request";
}
print $client qq{
- reload
- };
+