mirror of https://github.com/mikaku/Monitorix.git
HTTPServer rewritten and simplyfied
This commit is contained in:
parent
c46515aa1a
commit
9938255306
|
@ -22,125 +22,86 @@ package HTTPServer;
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
use Monitorix;
|
use POSIX qw(strftime);
|
||||||
use POSIX qw(strftime getpid);
|
|
||||||
use HTTP::Server::Simple::CGI;
|
use HTTP::Server::Simple::CGI;
|
||||||
use base qw(HTTP::Server::Simple::CGI);
|
use base qw(HTTP::Server::Simple::CGI);
|
||||||
|
|
||||||
my %dispatch = (
|
sub logger {
|
||||||
'/' => \&do_req,
|
my $url = shift;
|
||||||
'/logo_top.png' => \&do_req,
|
|
||||||
'/logo_bot.png' => \&do_req,
|
print STDERR localtime() . " - [$ENV{REMOTE_ADDR}] The requested URL $url was not found on this server.\n";
|
||||||
'/monitorixico.png' => \&do_req,
|
}
|
||||||
'/monitorix.cgi' => \&do_req,
|
|
||||||
);
|
|
||||||
|
|
||||||
sub handle_request {
|
sub handle_request {
|
||||||
my ($self, $cgi) = @_;
|
my ($self, $cgi) = @_;
|
||||||
my $method = $ENV{REQUEST_METHOD};
|
my $target;
|
||||||
|
my @data;
|
||||||
|
|
||||||
return if fork(); # parent returns
|
return if fork(); # parent returns
|
||||||
|
|
||||||
my $url = $cgi->path_info();
|
my $url = $cgi->path_info();
|
||||||
print STDERR getpid() . " = '$url'\n";
|
print STDERR "'$url'\n";
|
||||||
|
|
||||||
my $handler = $dispatch{$url};
|
# sanitizes the $target
|
||||||
|
$target = $url;
|
||||||
# sanitizes the $url
|
|
||||||
while() {
|
while() {
|
||||||
my $cur = length($url);
|
my $cur = length($target);
|
||||||
$url =~ s/\.\.\///;
|
$target =~ s/\.\.\///;
|
||||||
$url =~ s/^\///;
|
$target =~ s/^\///;
|
||||||
last unless $cur ne length $url;
|
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 = <P>;
|
||||||
|
close(P);
|
||||||
|
} else {
|
||||||
|
if(open(IN, $target)) {
|
||||||
|
@data = <IN>;
|
||||||
|
close(IN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# XXX
|
if(scalar(@data)) {
|
||||||
print "HTTP/1.0 200 OK\r\n";
|
|
||||||
do_req($url, $cgi);
|
|
||||||
exit;
|
|
||||||
|
|
||||||
|
|
||||||
if(ref($handler) eq "CODE") {
|
|
||||||
print "HTTP/1.0 200 OK\r\n";
|
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 {
|
} else {
|
||||||
print "HTTP/1.0 404 Not found\r\n";
|
print "HTTP/1.0 404 Not found\r\n";
|
||||||
print $cgi->header,
|
print "Date: " . strftime("%a, %d %b %Y %H:%M:%S %z", localtime) . "\r\n";
|
||||||
$cgi->start_html('404 Not Found'),
|
print "Server: Monitorix HTTP Server\r\n";
|
||||||
$cgi->h1('Not Found'),
|
print "Connection: close\r\n";
|
||||||
$cgi->end_html;
|
print "Content-Type: text/html; charset=ISO-8859-1\r\n";
|
||||||
print "The requested URL $url was not found on this server.<p>";
|
print "\r\n";
|
||||||
print "<hr>";
|
print "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n";
|
||||||
print "<i>Monitorix HTTP Server listening on 8080</i>";
|
print "<html><head>\r\n";
|
||||||
print "<p>";
|
print "<title>404 Not Found</title>\r\n";
|
||||||
}
|
print "</head><body>\r\n";
|
||||||
|
print "<h1>Not Found</h1>\r\n";
|
||||||
if (lc($method) eq 'post') {
|
print "The requested URL $url was not found on this server.<p>\r\n";
|
||||||
print "Received POST request";
|
print "<hr>\r\n";
|
||||||
} else {
|
print "<address>Monitorix HTTP Server listening on 8080</address>\r\n";
|
||||||
print "Received request of type $method";
|
print "</body></html>\r\n";
|
||||||
|
logger($url);
|
||||||
}
|
}
|
||||||
|
|
||||||
# use Data::Dumper;
|
# use Data::Dumper;
|
||||||
# print "<pre>";
|
# print "<pre>";
|
||||||
# print Dumper(\@_);
|
# print Dumper(\@_);
|
||||||
|
# print Dumper(\%ENV);
|
||||||
|
|
||||||
exit(0);
|
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(<P>) {
|
|
||||||
print $_;
|
|
||||||
}
|
|
||||||
close(P);
|
|
||||||
} else {
|
|
||||||
if(open(IN, $url)) {
|
|
||||||
while(<IN>) {
|
|
||||||
print $_;
|
|
||||||
}
|
|
||||||
close(IN);
|
|
||||||
} else {
|
|
||||||
print "ERROR: '$url' not found!<br>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#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;
|
1;
|
||||||
|
|
Loading…
Reference in New Issue