finished HTTP built-in server

This commit is contained in:
Jordi Sanfeliu 2013-02-07 15:57:42 +01:00
parent dcf78cc9ad
commit e26901bcb6
2 changed files with 66 additions and 30 deletions

View File

@ -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 = <P>;
close(P);
open(EXEC, "./$target |");
@data = <EXEC>;
close(EXEC);
} else {
if(open(IN, $target)) {
@data = <IN>;
@ -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 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n";
print "<html><head>\r\n";
print "<title>404 Not Found</title>\r\n";
@ -91,16 +119,11 @@ sub handle_request {
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";
print "<address>Monitorix HTTP Server listening on 8080</address>\r\n";
print "<address>Monitorix HTTP Server listening on $port</address>\r\n";
print "</body></html>\r\n";
logger($url);
logger($url, "ERROR");
}
# use Data::Dumper;
# print "<pre>";
# print Dumper(\@_);
# print Dumper(\%ENV);
exit(0);
}

View File

@ -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();