--- loncom/lonhttpd 2002/10/30 14:25:33 1.3 +++ loncom/lonhttpd 2008/05/01 14:36:03 1.16 @@ -1,7 +1,7 @@ #!/usr/bin/perl -# $Id: lonhttpd,v 1.3 2002/10/30 14:25:33 www Exp $ +# $Id: lonhttpd,v 1.16 2008/05/01 14:36:03 raeburn Exp $ -$VERSION = "1.3.2 (Demonic/Linux/LON-CAPA Derivative)"; +$VERSION = "1.3.2 (Demonic/Linux/LON-CAPA Derivative $Revison$)"; # HTTPi Hypertext Tiny Truncated Process Implementation # Copyright 1999-2001 Cameron Kaiser # All rights reserved @@ -10,6 +10,17 @@ $VERSION = "1.3.2 (Demonic/Linux/LON-CAP # LON-CAPA: find httpi license and readme at CVS loncom/license # +use lib '/home/httpd/lib/perl/'; +use LONCAPA::Configuration(); +%loncapavar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')}; +$port_to_use=$loncapavar{'lonhttpdPort'}; +if (!defined($port_to_use)) { + $port_to_use='8080'; +} + +# The main server is running on 80, so exit in this case +if ($port_to_use eq '80') { die('Apache is already on Port 80'); } + %system_content_types = ("html" => "text/html", "htm" => "text/html", @@ -40,7 +51,8 @@ $VERSION = "1.3.2 (Demonic/Linux/LON-CAP "mpeg" => "video/mpeg", "mpg" => "video/mpeg", "jpeg" => "image/jpeg", - "jpg" => "image/jpeg"); + "jpg" => "image/jpeg", + "png" => "image/png"); $logfile = "/home/httpd/perl/logs/lonhttpd.log"; @@ -50,7 +62,7 @@ $pidfile="/home/httpd/perl/logs/lonhttpd if (-e $pidfile) { open(LFH,"$pidfile"); - my $pide=<$LFH>; + my $pide=; chomp($pide); close(LFH); if (kill 0 => $pide) { die "already running"; } @@ -183,13 +195,14 @@ open (PIDSAVE,">$pidfile"); print PIDSAVE "$$\n"; close(PIDSAVE); -$0 = "dhttpi: binding port ..."; -$bindthis = pack($sockaddr, 2, 8080, pack('l', chr(0).chr(0).chr(0).chr(0))); +$0 = "lonhttpd: (dhttpi) binding port ..."; +$bindthis = pack($sockaddr, 2, $port_to_use, + pack('l', chr(0).chr(0).chr(0).chr(0))); socket(S, 2, 1, 6); setsockopt(S, 1, 2, 1); -bind(S, $bindthis) || die("$0: while binding port 8080:\n\"$!\"\n"); +bind(S, $bindthis) || die("$0: while binding port $port_to_use:\n\"$!\"\n"); listen(S, 128); -$0 = "dhttpi: connected and waiting ANY:8080"; +$0 = "lonhttpd: (dhttpi) connected and waiting ANY:$port_to_use"; $statiosuptime = time(); @@ -248,7 +261,9 @@ EOF } -sub bye { unlink($pidfile); exit; } +sub bye { exit; } + +sub goodbye { unlink($pidfile); exit; } sub dead { &htsponse(500, "Server Error"); @@ -264,10 +279,11 @@ EOF } $SIG{'__DIE__'} = \&dead; -$SIG{'ALRM'} = $SIG{'TERM'} = $SIG{'INT'} = \&bye; +$SIG{'ALRM'} = \&bye; +$SIG{'TERM'} = $SIG{'INT'} = \&goodbye; sub master { - $0 = "dhttpi: handling request"; + $0 = "lonhttpd: (dhttpi) handling request"; # $sock = getpeername(STDIN); $rfcdate = scalar gmtime; ($dow, $mon, $dt, $tm, $yr) = ($rfcdate =~ @@ -284,7 +300,7 @@ $date = "$dt/$mon/$yr:$tm +0000"; select(STDOUT); $|=1; $address = 0; alarm 1; while () { - if(/^([A-Z]+)\s+([^\s]+)\s+([^\s\r\l\n]*)/) { + if(/^([A-Z]+)\s+(\S+)\s+(\S*)/) { $method = $1; $address = $2; $httpver = $3; @@ -333,11 +349,16 @@ while () { 1 while $address =~ s#/\.(/|$)#\1#; 1 while $address =~ s#/[^/]*/\.\.(/|$)#\1#; 1 while $address =~ s#^/\.\.(/|$)#\1#; - $fail = 0; + $fail = 1; # # Heavily customized for LON-CAPA # - unless ($address=~/^\/(status|adm\/|res\/adm\/)/) { $fail=1; } + $address=~s/\/+/\//g; + if ($address=~/^\/(status|adm\/|res\/adm\/)/) { + $fail = 0; + } elsif ($address =~ /^\/res\/([\w\.\-]+)\/\1\-domainconfig\/(logo|domlogo|img|login)\/[^\/]+$/) { + $fail = 0; + } # # because existing restriction matrix would not do precedence across rules # @@ -361,8 +382,8 @@ while () { if ($fail) { &htsponse(403, "Forbidden"); if ($fail == 1) { - &hterror("Forbidden (Client Disallowed)", <<"EOF"); -Your network address ($ip) is not allowed to access this resource. + &hterror("Wrong URL", <<"EOF"); +You might want to remove the ":$port_to_use" from the web page address (URL). EOF &log; exit; } else { @@ -425,12 +446,12 @@ EOF -HTTPi Status +LonHTTPD (HTTPi) Status -

HTTPi Server Status ($VERSION)

-

lonhttpd on port 8080

+

LonHTTPD (HTTPi) Server Status ($VERSION)

+

lonhttpd on port $port_to_use

Started at: $suptime
Uptime: $d days, $h:$m:$s
Last request time: $statiosltr

@@ -477,8 +498,8 @@ EOF $ENV{'SERVER_NAME'} = "localhost"; $ENV{'SERVER_PROTOCOL'} = "HTTP/$httpver"; $ENV{'SERVER_SOFTWARE'} = "HTTPi/$VERSION"; - $ENV{'SERVER_PORT'} = "8080"; - $ENV{'SERVER_URL'} = "http://localhost:8080/"; + $ENV{'SERVER_PORT'} = "$port_to_use"; + $ENV{'SERVER_URL'} = "http://localhost:$port_to_use/"; $ENV{'SCRIPT_FILENAME'} = $raddress; $ENV{'SCRIPT_NAME'} = $address; $ENV{'REMOTE_HOST'} = $hostname; @@ -570,12 +591,13 @@ for (;;) { $statiosltr = scalar localtime; $statiosreq++; if ($pid = fork()) { - $0 = "dhttpi: waiting for child process"; + $0 = "lonhttpd: (dhttpi) waiting for child process"; waitpid($pid, 0); - $0 = "dhttpi: on ANY:8080, last request " . + $0 = "lonhttpd: (dhttpi) on ANY:$port_to_use, last request " . scalar localtime; + close(NS); } else { - $0 = "dhttpi: child switching to socket"; + $0 = "lonhttpd: (dhttpi) child switching to socket"; open(STDIN, "<&NS"); open(STDOUT, ">&NS"); &master;