Monitorix/lib/HTTPServer.pm

137 lines
3.6 KiB
Perl
Raw Normal View History

2013-01-31 17:48:47 +00:00
#
# Monitorix - A lightweight system monitoring tool.
#
# Copyright (C) 2005-2013 by Jordi Sanfeliu <jordi@fibranet.cat>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
package HTTPServer;
use strict;
use warnings;
2013-02-06 13:26:00 +00:00
use POSIX qw(strftime);
2013-01-31 17:48:47 +00:00
use HTTP::Server::Simple::CGI;
use base qw(HTTP::Server::Simple::CGI);
2013-02-06 13:26:00 +00:00
sub logger {
2013-02-07 14:57:42 +00:00
my ($url, $type) = @_;
2013-02-06 13:26:00 +00:00
2013-02-07 15:06:53 +00:00
if(open(OUT, ">> $main::config{httpd_builtin}->{log_file}")) {
2013-02-07 14:57:42 +00:00
if($type eq "OK") {
print OUT localtime() . " - $type - [$ENV{REMOTE_ADDR}] \"$ENV{REQUEST_METHOD} $url - $ENV{HTTP_USER_AGENT}\"\n";
} else {
print OUT localtime() . " - $type - [$ENV{REMOTE_ADDR}] File does not exist: $url\n";
}
close(OUT);
} else {
2013-02-07 15:06:53 +00:00
print STDERR localtime() . " - ERROR: unable to open logfile '$main::config{httpd_builtin}->{log_file}'.\n";
2013-02-07 14:57:42 +00:00
}
}
sub http_header {
my ($code, $mimetype) = @_;
if($code eq "200") {
print "HTTP/1.0 200 OK\r\n";
} else {
print "HTTP/1.0 404 Not found\r\n";
}
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";
if($mimetype =~ m/(html|cgi)/) {
print "Content-Type: text/html; charset=ISO-8859-1\r\n";
} else {
print "Content-Type: image/$mimetype;\r\n";
}
print "\r\n";
2013-02-06 13:26:00 +00:00
}
2013-01-31 17:48:47 +00:00
sub handle_request {
my ($self, $cgi) = @_;
2013-02-07 14:57:42 +00:00
my $base_url = $main::config{base_url};
my $base_cgi = $main::config{base_cgi};
my $port = $main::config{httpd_builtin}->{port};
my $mimetype;
2013-02-06 13:26:00 +00:00
my $target;
my $target_cgi;
2013-02-06 13:26:00 +00:00
my @data;
2013-01-31 17:48:47 +00:00
return if fork(); # parent returns
my $url = $cgi->path_info();
$0 = "monitorix-httpd"; # change process' name
2013-01-31 17:48:47 +00:00
2013-02-06 13:26:00 +00:00
# sanitizes the $target
$target = $url;
2013-01-31 17:48:47 +00:00
while() {
2013-02-06 13:26:00 +00:00
my $cur = length($target);
$target =~ s/\.\.\///;
$target =~ s/^\///;
$target =~ s/\/$//;
2013-02-19 11:57:27 +00:00
last unless $cur ne length($target);
2013-01-31 17:48:47 +00:00
}
$target = $target_cgi = "/$target";
2013-01-31 17:48:47 +00:00
2013-02-07 14:57:42 +00:00
$target =~ s/^$base_url//; # removes the 'base_url' part
$target_cgi =~ s/^$base_cgi//; # removes the 'base_cgi' part
if(!$target || $target eq $base_url) {
$target = "index.html" unless $target;
}
2013-02-07 14:57:42 +00:00
($mimetype) = ($target =~ m/.*\.(html|cgi|png)$/);
$target =~ s/^\///; # removes leading slash
$target_cgi =~ s/^\///; # removes leading slash
if($target_cgi eq "monitorix.cgi") {
2013-02-14 17:52:29 +00:00
chdir("cgi");
open(EXEC, "./$target_cgi |");
2013-02-07 14:57:42 +00:00
@data = <EXEC>;
close(EXEC);
} elsif($target) {
2013-02-06 13:26:00 +00:00
if(open(IN, $target)) {
@data = <IN>;
close(IN);
}
2013-01-31 17:48:47 +00:00
}
2013-02-06 13:26:00 +00:00
if(scalar(@data)) {
2013-02-07 14:57:42 +00:00
http_header("200", $mimetype);
2013-02-06 13:26:00 +00:00
foreach(@data) {
print $_;
}
2013-02-07 14:57:42 +00:00
logger($url, "OK");
2013-01-31 17:48:47 +00:00
} else {
2013-02-07 14:57:42 +00:00
http_header("404", "html");
2013-02-06 13:26:00 +00:00
print "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n";
print "<html><head>\r\n";
print "<title>404 Not Found</title>\r\n";
print "</head><body>\r\n";
print "<h1>Not Found</h1>\r\n";
print "The requested URL $url was not found on this server.<p>\r\n";
print "<hr>\r\n";
2013-02-07 14:57:42 +00:00
print "<address>Monitorix HTTP Server listening on $port</address>\r\n";
2013-02-06 13:26:00 +00:00
print "</body></html>\r\n";
2013-02-07 14:57:42 +00:00
logger($url, "ERROR");
2013-01-31 17:48:47 +00:00
}
2013-02-06 13:26:00 +00:00
exit(0);
2013-01-31 17:48:47 +00:00
}
1;