From 993825530655502ba883e5e23dab9a29b7c34cd8 Mon Sep 17 00:00:00 2001 From: Jordi Sanfeliu Date: Wed, 6 Feb 2013 14:26:00 +0100 Subject: [PATCH] HTTPServer rewritten and simplyfied --- lib/HTTPServer.pm | 151 +++++++++++++++++----------------------------- 1 file changed, 56 insertions(+), 95 deletions(-) diff --git a/lib/HTTPServer.pm b/lib/HTTPServer.pm index a04ac67..e34dbef 100644 --- a/lib/HTTPServer.pm +++ b/lib/HTTPServer.pm @@ -22,125 +22,86 @@ package HTTPServer; use strict; use warnings; -use Monitorix; -use POSIX qw(strftime getpid); +use POSIX qw(strftime); use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); -my %dispatch = ( - '/' => \&do_req, - '/logo_top.png' => \&do_req, - '/logo_bot.png' => \&do_req, - '/monitorixico.png' => \&do_req, - '/monitorix.cgi' => \&do_req, -); +sub logger { + my $url = shift; + + print STDERR localtime() . " - [$ENV{REMOTE_ADDR}] The requested URL $url was not found on this server.\n"; +} sub handle_request { my ($self, $cgi) = @_; - my $method = $ENV{REQUEST_METHOD}; + my $target; + my @data; return if fork(); # parent returns my $url = $cgi->path_info(); - print STDERR getpid() . " = '$url'\n"; + print STDERR "'$url'\n"; - my $handler = $dispatch{$url}; - - # sanitizes the $url + # sanitizes the $target + $target = $url; while() { - my $cur = length($url); - $url =~ s/\.\.\///; - $url =~ s/^\///; - last unless $cur ne length $url; + my $cur = length($target); + $target =~ s/\.\.\///; + $target =~ s/^\///; + last unless $cur ne length $target; } - $url = "/$url"; + $target = "/$target"; + $target =~ s/^\///; # removes the leading slash + $target = "index.html" unless $target; + if($target eq "monitorix.cgi") { +# chdir("cgi"); + chdir("/home/jordi/github/Monitorix/"); # XXX + open(P, "./$target |"); + @data =

; + close(P); + } else { + if(open(IN, $target)) { + @data = ; + close(IN); + } + } - # XXX - print "HTTP/1.0 200 OK\r\n"; - do_req($url, $cgi); - exit; - - - if(ref($handler) eq "CODE") { + if(scalar(@data)) { print "HTTP/1.0 200 OK\r\n"; - $handler->($url, $cgi); + print "Date: " . strftime("%a, %d %b %Y %H:%M:%S %z", localtime) . "\r\n"; + print "Server: Monitorix HTTP Server\r\n"; + print "Connection: close\r\n"; + print "Content-Type: text/html; charset=ISO-8859-1\r\n"; + print "\r\n"; + foreach(@data) { + print $_; + } } else { print "HTTP/1.0 404 Not found\r\n"; - print $cgi->header, - $cgi->start_html('404 Not Found'), - $cgi->h1('Not Found'), - $cgi->end_html; - print "The requested URL $url was not found on this server.

"; - print "


"; - print "Monitorix HTTP Server listening on 8080"; - print "

"; - } - - if (lc($method) eq 'post') { - print "Received POST request"; - } else { - print "Received request of type $method"; + print "Date: " . strftime("%a, %d %b %Y %H:%M:%S %z", localtime) . "\r\n"; + print "Server: Monitorix HTTP Server\r\n"; + print "Connection: close\r\n"; + print "Content-Type: text/html; charset=ISO-8859-1\r\n"; + print "\r\n"; + print "\r\n"; + print "\r\n"; + print "404 Not Found\r\n"; + print "\r\n"; + print "

Not Found

\r\n"; + print "The requested URL $url was not found on this server.

\r\n"; + print "


\r\n"; + print "
Monitorix HTTP Server listening on 8080
\r\n"; + print "\r\n"; + logger($url); } # use Data::Dumper; # print "
";
 #	print Dumper(\@_);
+#	print Dumper(\%ENV);
+
 	exit(0);
 }
 
-sub do_req {
-	my ($url, $cgi) = @_;
-	return if !ref $cgi;
-
-	print STDERR "\t$url\n";
-
-#	my $who = $cgi->param('name');
-#	print $cgi->header,
-#		$cgi->start_html("Hello"),
-#		$cgi->h1("Hello $who!"),
-#		$cgi->end_html;
-
-	print "Date: " . strftime("%a, %d %b %Y %H:%M:%S %z", localtime) . "\r\n";
-	print "Server: Monitorix HTTP Server\r\n";
-	print "Connection: close\r\n";
-	print $cgi->header;
-
-	$url =~ s/^\///;	# removes the leading slash
-	$url = "index.html" unless $url;
-	if($url eq "monitorix.cgi") {
-#		chdir("cgi");
-		chdir("/home/jordi/github/Monitorix/");		# XXX
-		open(P, "./$url |");
-		foreach(

) { - print $_; - } - close(P); - } else { - if(open(IN, $url)) { - while() { - print $_; - } - close(IN); - } else { - print "ERROR: '$url' not found!
"; - } - } -} - - -#sub handle_request { -# my ($self, $cgi) = @_; -# my $method = $ENV{REQUEST_METHOD}; -# if (lc($method) == 'post') { -# my $file = $cgi->param('POSTDATA'); -# open OUT, '>file.xml' or warn $!; -# print OUT $file; -# close OUT; -# print 'Received data.'; -# } else { -# print 'Please POST a file.'; -# } -#} - 1;