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 = ";
- 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 " ) {
- print $_;
- }
- close(P);
- } else {
- if(open(IN, $url)) {
- while(
";
- print "Monitorix HTTP Server listening on 8080";
- print "Not Found
\r\n";
+ print "The requested URL $url was not found on this server.
\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(
";
- }
- }
-}
-
-
-#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;