From e26901bcb6e77168b458de7b9ced2513aed2f3b2 Mon Sep 17 00:00:00 2001 From: Jordi Sanfeliu Date: Thu, 7 Feb 2013 15:57:42 +0100 Subject: [PATCH] finished HTTP built-in server --- lib/HTTPServer.pm | 75 +++++++++++++++++++++++++++++++---------------- lib/Monitorix.pm | 21 ++++++++++--- 2 files changed, 66 insertions(+), 30 deletions(-) diff --git a/lib/HTTPServer.pm b/lib/HTTPServer.pm index e34dbef..1aeff5d 100644 --- a/lib/HTTPServer.pm +++ b/lib/HTTPServer.pm @@ -27,20 +27,54 @@ use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); sub logger { - my $url = shift; + my ($url, $type) = @_; - print STDERR localtime() . " - [$ENV{REMOTE_ADDR}] The requested URL $url was not found on this server.\n"; + if(open(OUT, ">> $main::config{httpd_builtin}->{logfile}")) { + 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 { + print STDERR localtime() . " - ERROR: unable to open logfile '$main::config{httpd_builtin}->{logfile}'.\n"; + } +} + +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"; } sub handle_request { my ($self, $cgi) = @_; + my $base_url = $main::config{base_url}; + my $base_cgi = $main::config{base_cgi}; + my $port = $main::config{httpd_builtin}->{port}; + my $mimetype; my $target; my @data; return if fork(); # parent returns my $url = $cgi->path_info(); - print STDERR "'$url'\n"; # sanitizes the $target $target = $url; @@ -52,14 +86,17 @@ sub handle_request { } $target = "/$target"; - $target =~ s/^\///; # removes the leading slash + $target =~ s/^$base_url//; # removes the 'base_url' part + $target =~ s/^$base_cgi//; # removes the 'base_cgi' part $target = "index.html" unless $target; + ($mimetype) = ($target =~ m/.*\.(html|cgi|png)$/); + if($target eq "monitorix.cgi") { # chdir("cgi"); chdir("/home/jordi/github/Monitorix/"); # XXX - open(P, "./$target |"); - @data =

; - close(P); + open(EXEC, "./$target |"); + @data = ; + close(EXEC); } else { if(open(IN, $target)) { @data = ; @@ -68,22 +105,13 @@ sub handle_request { } if(scalar(@data)) { - print "HTTP/1.0 200 OK\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"; - print "Content-Type: text/html; charset=ISO-8859-1\r\n"; - print "\r\n"; + http_header("200", $mimetype); foreach(@data) { print $_; } + logger($url, "OK"); } 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"; - print "Content-Type: text/html; charset=ISO-8859-1\r\n"; - print "\r\n"; + http_header("404", "html"); print "\r\n"; print "\r\n"; print "404 Not Found\r\n"; @@ -91,16 +119,11 @@ sub handle_request { 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 "
Monitorix HTTP Server listening on $port
\r\n"; print "\r\n"; - logger($url); + logger($url, "ERROR"); } -# use Data::Dumper; -# print "
";
-#	print Dumper(\@_);
-#	print Dumper(\%ENV);
-
 	exit(0);
 }
 
diff --git a/lib/Monitorix.pm b/lib/Monitorix.pm
index d625527..ee7d854 100644
--- a/lib/Monitorix.pm
+++ b/lib/Monitorix.pm
@@ -55,15 +55,28 @@ sub httpd_setup {
 	my ($config, $debug) = @_;
 	my $pid;
 
+	my (undef, undef, $uid) = getpwnam($config->{httpd_builtin}->{user});
+	my (undef, undef, $gid) = getgrnam($config->{httpd_builtin}->{group});
+	my $port = $config->{httpd_builtin}->{port};
+
+	if(!defined($uid)) {
+		logger("ERROR: invalid user defined for the HTTPd built-in server.");
+		return;
+	}
+	if(!defined($gid)) {
+		logger("ERROR: invalid group defined for the HTTPd built-in server.");
+		return;
+	}
+	if(!defined($port)) {
+		logger("ERROR: invalid port defined for the HTTPd built-in server.");
+		return;
+	}
+
 	if($pid = fork()) {
 		$config->{httpd_pid} = $pid;
 		return;	# parent returns
 	}
 
-	my (undef, undef, $uid) = getpwnam($config->{httpd_builtin}->{user});
-	my (undef, undef, $gid) = getgrnam($config->{httpd_builtin}->{group});
-	my $port = $config->{httpd_builtin}->{port};
-
 	setgid($gid);
 	setuid($uid);
 	setsid();