Annotation of loncom/lonhttpd, revision 1.6

1.1       www         1: #!/usr/bin/perl
1.6     ! albertel    2: # $Id: lonhttpd,v 1.5 2002/11/06 22:38:42 albertel Exp $
1.1       www         3: 
1.6     ! albertel    4: $VERSION = "1.3.2 (Demonic/Linux/LON-CAPA Derivative $Revison$)";
1.1       www         5: 
1.3       www         6: # HTTPi Hypertext Tiny Truncated Process Implementation
                      7: # Copyright 1999-2001 Cameron Kaiser # All rights reserved
                      8: # Please read LICENSE # Do not strip this copyright message.
                      9: #
                     10: # LON-CAPA: find httpi license and readme at CVS loncom/license
                     11: #
                     12: 
1.6     ! albertel   13: use lib '/home/httpd/lib/perl/';
        !            14: use LONCAPA::Configuration();
        !            15: %loncapavar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')};
        !            16: $port_to_use=$loncapavar{'lonhttpdPort'};
        !            17: if (!defined($port_to_use)) {
        !            18:     $port_to_use='8080';
        !            19: }
        !            20: 
        !            21: # The main server is running on 80, so exit in this case
        !            22: if ($port_to_use eq '80') { die('Apache is already on Port 80'); }
        !            23: 
1.3       www        24: %system_content_types =
                     25: 	("html" => "text/html",
                     26: 	 "htm" => "text/html",
                     27: 	 "wml" => "text/vnd.wap.wml",
                     28: 	 "wbmp" => "image/vnd.wap.wbmp",
                     29: 	 "wbm" => "image/vnd.wap.wbmp",
                     30: 	 "xbm" => "image/x-xbitmap",
                     31: 	 "pdf" => "application/pdf",
                     32: 	 "fdf" => "application/vnd.fdf",
                     33: 	 "bin" => "application/octet-stream",
                     34: 	 "class" => "application/octet-stream",
                     35: 	 "jar" => "application/octet-stream",
                     36: 	 "js" => "application/x-javascript",
                     37: 	 "lnk" => "application/x-hyperlink",
                     38: 	 "wav" => "audio/x-wav",
                     39: 	 "mp3" => "audio/x-mpeg",
                     40: 	 "tif" => "image/tiff",
                     41: 	 "tiff" => "image/tiff",
                     42: 	 "mid" => "audio/x-midi",
                     43: 	 "txt" => "text/plain",
                     44: 	 "gif" => "image/gif",
                     45: 	 "sit" => "application/x-stuffit",
                     46: 	 "zip" => "application/x-zip-compressed",
                     47: 	 "lzh" => "application/octet-stream",
                     48: 	 "lha" => "application/octet-stream",
                     49: 	 "gz"  => "application/x-gzip",
                     50: 	 "mov" => "movie/quicktime",
                     51: 	 "mpeg" => "video/mpeg",
                     52: 	 "mpg" => "video/mpeg",
                     53: 	 "jpeg" => "image/jpeg",
                     54: 	 "jpg" => "image/jpeg");
                     55: 
                     56: $logfile = "/home/httpd/perl/logs/lonhttpd.log";
                     57: 
                     58: # Write out PID
                     59: 
                     60: $pidfile="/home/httpd/perl/logs/lonhttpd.pid";
                     61: 
                     62: if (-e $pidfile) {
                     63:    open(LFH,"$pidfile");
                     64:    my $pide=<$LFH>;
                     65:    chomp($pide);
                     66:    close(LFH);
                     67:    if (kill 0 => $pide) { die "already running"; }
                     68: }
                     69: 
                     70: $path = "/home/httpd/html";
                     71: $sockaddr = 'S n a4 x8';
1.1       www        72: 
                     73: 
1.3       www        74: %content_types =
                     75: 	("html" => "text/html",
                     76: 	 "htm" => "text/html");
                     77: %restrictions =
                     78: 	("/"        => "#.##",  # deny everything
                     79:          "/res/adm" => ".###",  # allow /res/adm
                     80:          "/adm"     => ".###",  # allow /adm
                     81: 	 "/status"  => ".####lonadm:oeRooOvb3HtpI");
                     82: 		# See documentation for interpreting this string.
                     83: 
                     84: $headers = <<"EOF";
                     85: Server: HTTPi/$VERSION
                     86: MIME-Version: 1.0
                     87: EOF
                     88: 
                     89: %virtual_files =
                     90: 	(
                     91: "/adm/lonLCDfont/0.gif" => [ "image/gif", "FILE",
                     92: 		"/home/httpd/html/adm/lonLCDfont/0.gif" ] ,
                     93: "/adm/lonLCDfont/1.gif" => [ "image/gif", "FILE",
                     94: 		"/home/httpd/html/adm/lonLCDfont/1.gif" ] ,
                     95: "/adm/lonLCDfont/2.gif" => [ "image/gif", "FILE",
                     96: 		"/home/httpd/html/adm/lonLCDfont/2.gif" ] ,
                     97: "/adm/lonLCDfont/3.gif" => [ "image/gif", "FILE",
                     98: 		"/home/httpd/html/adm/lonLCDfont/3.gif" ] ,
                     99: "/adm/lonLCDfont/4.gif" => [ "image/gif", "FILE",
                    100: 		"/home/httpd/html/adm/lonLCDfont/4.gif" ] ,
                    101: "/adm/lonLCDfont/5.gif" => [ "image/gif", "FILE",
                    102: 		"/home/httpd/html/adm/lonLCDfont/5.gif" ] ,
                    103: "/adm/lonLCDfont/6.gif" => [ "image/gif", "FILE",
                    104: 		"/home/httpd/html/adm/lonLCDfont/6.gif" ] ,
                    105: "/adm/lonLCDfont/7.gif" => [ "image/gif", "FILE",
                    106: 		"/home/httpd/html/adm/lonLCDfont/7.gif" ] ,
                    107: "/adm/lonLCDfont/8.gif" => [ "image/gif", "FILE",
                    108: 		"/home/httpd/html/adm/lonLCDfont/8.gif" ] ,
                    109: "/adm/lonLCDfont/9.gif" => [ "image/gif", "FILE",
                    110: 		"/home/httpd/html/adm/lonLCDfont/9.gif" ] ,
                    111: "/adm/lonLCDfont/a.gif" => [ "image/gif", "FILE",
                    112: 		"/home/httpd/html/adm/lonLCDfont/a.gif" ] ,
                    113: "/adm/lonLCDfont/b.gif" => [ "image/gif", "FILE",
                    114: 		"/home/httpd/html/adm/lonLCDfont/b.gif" ] ,
                    115: "/adm/lonLCDfont/c.gif" => [ "image/gif", "FILE",
                    116: 		"/home/httpd/html/adm/lonLCDfont/c.gif" ] ,
                    117: "/adm/lonLCDfont/d.gif" => [ "image/gif", "FILE",
                    118: 		"/home/httpd/html/adm/lonLCDfont/d.gif" ] ,
                    119: "/adm/lonLCDfont/e.gif" => [ "image/gif", "FILE",
                    120: 		"/home/httpd/html/adm/lonLCDfont/e.gif" ] ,
                    121: "/adm/lonLCDfont/f.gif" => [ "image/gif", "FILE",
                    122: 		"/home/httpd/html/adm/lonLCDfont/f.gif" ] ,
                    123: "/adm/lonLCDfont/g.gif" => [ "image/gif", "FILE",
                    124: 		"/home/httpd/html/adm/lonLCDfont/g.gif" ] ,
                    125: "/adm/lonLCDfont/h.gif" => [ "image/gif", "FILE",
                    126: 		"/home/httpd/html/adm/lonLCDfont/h.gif" ] ,
                    127: "/adm/lonLCDfont/i.gif" => [ "image/gif", "FILE",
                    128: 		"/home/httpd/html/adm/lonLCDfont/i.gif" ] ,
                    129: "/adm/lonLCDfont/j.gif" => [ "image/gif", "FILE",
                    130: 		"/home/httpd/html/adm/lonLCDfont/j.gif" ] ,
                    131: "/adm/lonLCDfont/k.gif" => [ "image/gif", "FILE",
                    132: 		"/home/httpd/html/adm/lonLCDfont/k.gif" ] ,
                    133: "/adm/lonLCDfont/l.gif" => [ "image/gif", "FILE",
                    134: 		"/home/httpd/html/adm/lonLCDfont/l.gif" ] ,
                    135: "/adm/lonLCDfont/m.gif" => [ "image/gif", "FILE",
                    136: 		"/home/httpd/html/adm/lonLCDfont/m.gif" ] ,
                    137: "/adm/lonLCDfont/n.gif" => [ "image/gif", "FILE",
                    138: 		"/home/httpd/html/adm/lonLCDfont/n.gif" ] ,
                    139: "/adm/lonLCDfont/o.gif" => [ "image/gif", "FILE",
                    140: 		"/home/httpd/html/adm/lonLCDfont/o.gif" ] ,
                    141: "/adm/lonLCDfont/p.gif" => [ "image/gif", "FILE",
                    142: 		"/home/httpd/html/adm/lonLCDfont/p.gif" ] ,
                    143: "/adm/lonLCDfont/q.gif" => [ "image/gif", "FILE",
                    144: 		"/home/httpd/html/adm/lonLCDfont/q.gif" ] ,
                    145: "/adm/lonLCDfont/r.gif" => [ "image/gif", "FILE",
                    146: 		"/home/httpd/html/adm/lonLCDfont/r.gif" ] ,
                    147: "/adm/lonLCDfont/s.gif" => [ "image/gif", "FILE",
                    148: 		"/home/httpd/html/adm/lonLCDfont/s.gif" ] ,
                    149: "/adm/lonLCDfont/t.gif" => [ "image/gif", "FILE",
                    150: 		"/home/httpd/html/adm/lonLCDfont/t.gif" ] ,
                    151: "/adm/lonLCDfont/u.gif" => [ "image/gif", "FILE",
                    152: 		"/home/httpd/html/adm/lonLCDfont/u.gif" ] ,
                    153: "/adm/lonLCDfont/v.gif" => [ "image/gif", "FILE",
                    154: 		"/home/httpd/html/adm/lonLCDfont/v.gif" ] ,
                    155: "/adm/lonLCDfont/w.gif" => [ "image/gif", "FILE",
                    156: 		"/home/httpd/html/adm/lonLCDfont/w.gif" ] ,
                    157: "/adm/lonLCDfont/x.gif" => [ "image/gif", "FILE",
                    158: 		"/home/httpd/html/adm/lonLCDfont/x.gif" ] ,
                    159: "/adm/lonLCDfont/y.gif" => [ "image/gif", "FILE",
                    160: 		"/home/httpd/html/adm/lonLCDfont/y.gif" ] ,
                    161: "/adm/lonLCDfont/z.gif" => [ "image/gif", "FILE",
                    162: 		"/home/httpd/html/adm/lonLCDfont/z.gif" ] ,
                    163: "/adm/lonLCDfont/colon.gif" => [ "image/gif", "FILE",
                    164: 		"/home/httpd/html/adm/lonLCDfont/colon.gif" ] ,
                    165: "/adm/lonLCDfont/slash.gif" => [ "image/gif", "FILE",
                    166: 		"/home/httpd/html/adm/lonLCDfont/slash.gif" ] ,
                    167: "/adm/lonLCDfont/hyphen.gif" => [ "image/gif", "FILE",
                    168: 		"/home/httpd/html/adm/lonLCDfont/hyphen.gif" ] ,
                    169: "/adm/lonLCDfont/space.gif" => [ "image/gif", "FILE",
                    170: 		"/home/httpd/html/adm/lonLCDfont/space.gif" ] ,
                    171: 	);
                    172: 
                    173: %content_types = (%system_content_types, %content_types);
                    174: undef %system_content_types;
                    175: 
                    176: while (($file, $arrayref) = each(%virtual_files)) {
                    177: 	my ($mime, $type, $block) = (@{ $arrayref });
                    178: 	next if ($type ne 'FILE');
                    179: 	if(open(S, "$block")) {
                    180: 		$j = $/; undef $/; $virtual_files{$file}->[2] = scalar(<S>);
                    181: 		$/ = $j; close(S);
                    182: 	} else {
                    183: 		warn "while getting virtual file $file: $!\n";
                    184: 		map_delete(%virtual_files, $file);
                    185: 	}
                    186: }
                    187: if ($pid = fork()) { exit; }
                    188: 
                    189: #
                    190: # Store parent PID
                    191: #
1.1       www       192: 
1.3       www       193: open (PIDSAVE,">$pidfile");
1.1       www       194: print PIDSAVE "$$\n";
                    195: close(PIDSAVE);
                    196: 
1.5       albertel  197: $0 = "lonhttpd: (dhttpi) binding port ...";
1.6     ! albertel  198: $bindthis = pack($sockaddr, 2, $port_to_use,
        !           199: 		 pack('l', chr(0).chr(0).chr(0).chr(0)));
1.3       www       200: socket(S, 2, 1, 6);
                    201: setsockopt(S, 1, 2, 1);
1.6     ! albertel  202: bind(S, $bindthis) || die("$0: while binding port $port_to_use:\n\"$!\"\n");
1.3       www       203: listen(S, 128);
1.6     ! albertel  204: $0 = "lonhttpd: (dhttpi) connected and waiting ANY:$port_to_use";
1.3       www       205: 
                    206: $statiosuptime = time();
                    207: 
                    208: ###############################################################
                    209: # WHITE HATS ONLY BELOW THIS POINT -- SEE DOCUMENTATION FIRST #
                    210: ###############################################################
                    211: 
                    212: sub sock_to_host {
                    213: 	local($sock) = getpeername(STDIN);
                    214: 
                    215: 	return (undef, undef, undef) if (!$sock);
                    216: 	local($AFC, $port, $thataddr, $zero) = unpack($sockaddr, $sock);
                    217: 	local($ip) = join('.', unpack("C4", $thataddr));
                    218: 	return ($ip, $port, $ip);
                    219: }
                    220: 
                    221: sub htsponse {
                    222: 	($currentcode, $currentstring) = (@_);
                    223: 	return if (0+$httpver < 1);
                    224: 	local($what) = <<"EOF";
                    225: HTTP/$httpver $currentcode $currentstring
                    226: ${headers}Date: $rfcdate
                    227: EOF
                    228: 	$what =~ s/\n/\r\n/g;
                    229: 	print stdout $what;
                    230: 	&hthead("Connection: close") if (0+$httpver > 1);
                    231: }
                    232: 
                    233: sub hthead {
                    234: 	local($header, $term) = (@_);
                    235: 	return if (0+$httpver < 1);
                    236: 	print stdout "$header\r\n" , ($term) ? "\r\n" : "";
                    237: }
                    238: 
                    239: sub htcontent {
                    240: 	local($what, $ctype, $mode) = (@_);
                    241: 	($contentlength) = $mode || length($what);
                    242: 	&hthead("Content-Length: $contentlength");
                    243: 	&hthead("Content-Type: $ctype", 1);
                    244: 	return if ($method eq 'HEAD' || $mode);
                    245: 	print stdout $what;
                    246: }
                    247: 
                    248: sub log {
                    249:  	if (open(J, ">>$logfile")) {
                    250: 		local $q = $address . (($variables) ? "?$variables" : "");
                    251: 		$contentlength += 0;
                    252: 		$contentlength = 0 if ($method eq 'HEAD');
                    253: 		local ($hostname, $port, $ip) = &sock_to_host();
                    254: 		$hostname = $hostname || "-";
                    255: 		$httpuser = $httpuser || "-";
                    256: 		print J <<"EOF";
                    257: $hostname - $httpuser [$date] "$method $q HTTP/$httpver" $currentcode $contentlength "$httpref" "$httpua"
                    258: EOF
                    259: 		close(J); }
                    260: 	}
                    261: 
                    262: 
                    263: sub bye { unlink($pidfile); exit; }
                    264: 
                    265: sub dead {
                    266: 	&htsponse(500, "Server Error");
                    267: 	&hterror("Server Error", <<"EOF");
                    268: While handling a request for resource $address, the server crashed. Please
                    269: attempt to notify the administrators.
                    270: <p>Useful(?) debugging information:
                    271: <pre>
                    272: @_
                    273: </pre>
                    274: EOF
                    275: 	&log; unlink($pidfile); exit;
                    276: }
                    277: 
                    278: $SIG{'__DIE__'} = \&dead;
                    279: $SIG{'ALRM'} = $SIG{'TERM'} = $SIG{'INT'} = \&bye;
                    280: 
                    281: sub master {
1.5       albertel  282: 	$0 = "lonhttpd: (dhttpi) handling request";
1.3       www       283: # $sock = getpeername(STDIN);
                    284: $rfcdate = scalar gmtime;
                    285: ($dow, $mon, $dt, $tm, $yr) = ($rfcdate =~
                    286: 	m/(...) (...) (..) (..:..:..) (....)/);
                    287: $dt += 0; $yr += 0;
                    288: $rfcdate = "$dow, $dt $mon $yr $tm GMT";
                    289: $date = scalar localtime;
                    290: ($dow, $mon, $dt, $tm, $yr) = ($date =~
                    291: 	m/(...) (...) (..) (..:..:..) (....)/);
                    292: $dt += 0;
                    293: $dt = substr("0$dt", length("0$dt") - 2, 2);
                    294: $date = "$dt/$mon/$yr:$tm +0000"; 
                    295: 
                    296: select(STDOUT); $|=1; $address = 0; 
                    297: alarm 1;
                    298: while (<STDIN>) {
                    299: 	if(/^([A-Z]+)\s+([^\s]+)\s+([^\s\r\l\n]*)/) {
                    300: 		$method = $1;
                    301: 		$address = $2; 
                    302: 		$httpver = $3;
                    303: 		$httpref = '';
                    304: 		$httpua = '';
                    305: 		$httpver = ($httpver =~ m#HTTP/([0-9]\.[0-9]+)#) ?
                    306: 			($1) : (0.9);
                    307: 		$address =~ s#^http://[^/]+/#/#;
                    308: 		next unless ($httpver < 1);
                    309: 	} else {
                    310: 		s/[\r\l\n\s]+$//;
                    311: 		(/^Host: (.+)/i) && ($httphost = $1) && ($httphost =~
                    312: 			s/:\d+$//);
                    313: 		(/^Referer: (.+)/i) && ($httpref = $1);
                    314: 		(/^User-agent: (.+)/i) && ($httpua = $1);
                    315: 		(/^Content-length: (\d+)/i) && ($ENV{'CONTENT_LENGTH'} =
                    316: 			$httpcl = $1);
                    317: 		(/^Content-type: (.+)/i) && ($ENV{'CONTENT_TYPE'} =
                    318: 			$httpct = $1);
                    319: 		(/^Expect: /) && ($expect = 1);
                    320: 		(/^Authorization: Basic (.+)/i) && ($httprawu = $1);
                    321: 		(/^Range: (.+)/i) && ($ENV{'CONTENT_RANGE'} = $1);
                    322: 		next unless (/^$/);
                    323: 	}
                    324: 	if ($expect) {
                    325: 		&htsponse(417, "Expectation Failed");
                    326: 		&hterror("Expectation Failed",
                    327: 			"The server does not support this method.");
                    328: 		&log; exit;
                    329: 	}
                    330: 	if (!$address || (0+$httpver > 1 && !$httphost)) {
                    331: 		&htsponse(400, "Bad Request");
                    332: 		&hterror("Bad Request",
                    333: 			"The server cannot understand your request.");
                    334: 		&log; exit;
                    335: 	}
                    336: 	if ($method !~ /^(GET|HEAD|POST)$/) {
                    337: 		&htsponse(501, "Illegal Method");
                    338: 		&hterror("Illegal Method",
                    339: 			"Only GET, HEAD and POST are supported.");
                    340: 		&log; exit;
                    341: 	}
                    342: 	($address, $variables) = split(/\?/, $address);
                    343: 	$address =~ s/%([0-9a-fA-F]{2})/pack("H2", $1)/eg;
                    344: 	$address=~ s#^/?#/#;
                    345: 	1 while $address =~ s#/\.(/|$)#\1#;
                    346:         1 while $address =~ s#/[^/]*/\.\.(/|$)#\1#;
                    347: 	1 while $address =~ s#^/\.\.(/|$)#\1#;
                    348: 	$fail = 0;
                    349: #
                    350: # Heavily customized for LON-CAPA
                    351: #
1.4       www       352: 	$address=~s/\/+/\//g;
1.3       www       353: 	unless ($address=~/^\/(status|adm\/|res\/adm\/)/) { $fail=1; }
                    354: #
                    355: # because existing restriction matrix would not do precedence across rules
                    356: #
                    357: #	J: foreach(sort { length $a <=> length $b }
                    358: #			keys %restrictions) {
                    359: #		next if ($address !~ /^$_/);
                    360: #		($allowip, $denyip, $allowua, $denyua, $auser) =
                    361: #			split(/#/, $restrictions{$_});
                    362: #		if ($allowip || $denyip) {
                    363: #			($hostname, $port, $ip) = &sock_to_host();
                    364: #			($allowip && $ip !~ /$allowip/) && ($fail = 1,
                    365: #				last J);
                    366: #			($denyip && $ip =~ /$denyip/) && ($fail = 1,
                    367: #				last J);
                    368: #		}
                    369: #		($allowua && $httpua !~ /$allowua/) &&
                    370: #			($fail = 2, last J);
                    371: #		($denyua && $httpua =~ /$denyua/) &&
                    372: #			($fail = 2, last J);
                    373: #	}
                    374: 	if ($fail) {
                    375: 		&htsponse(403, "Forbidden");
                    376: 		if ($fail == 1) {
                    377: 			&hterror("Forbidden (Client Disallowed)", <<"EOF");
                    378: Your network address (<i>$ip</i>) is not allowed to access this resource.
                    379: EOF
                    380: 			&log; exit;
                    381: 		} else {
                    382: 			&hterror("Forbidden (Browser Disallowed)", <<"EOF");
                    383: The browser you are using (<i>$httpua</i>) is not capable of or
                    384: is not allowed access to this resource.
                    385: EOF
                    386: 			&log; exit;
                    387: 		}
                    388: 	}
                    389: 	if ($auser) {
                    390: 		$httprawu =~ tr#A-Za-z0-9+/##cd;
                    391: 		$httprawu =~ tr#A-Za-z0-9+/# -_#;
                    392: 		$httprawu = unpack("u", pack("c", 32+0.75*length($httprawu))
                    393: 			. $httprawu);
                    394: 		($httpuser, $httppw) = split(/:/, $httprawu);
                    395: 		$fail = 1;
                    396: 		foreach $user (split(/,/, $auser)) {
                    397: 			($user, $pw) = split(/:/, $user);
                    398: 			($fail = 0, last) if ($user eq $httpuser &&
                    399: 				crypt($httppw, substr($pw, 0, 2)) eq $pw);
                    400: 		}
                    401: 		if ($fail) {
                    402: 			$httpuser = '';
                    403: 			&htsponse(401, "Authorization Required");
                    404: 			&hthead("WWW-Authenticate: Basic realm=\"$address\"");
                    405: 			&hterror("Authorization Required", <<"EOF");
                    406: You must provide a username and password to use this resource. Either you
                    407: entered this information incorrectly, or your browser does not know how to
                    408: present the credentials required.
                    409: EOF
                    410: 			&log; exit;
                    411: 		}
                    412: 	}
1.1       www       413: 
1.3       www       414: 	alarm 0;
                    415: 
                    416: 	if ($address eq '/status') {
                    417: 		&htsponse(200, "OK");
                    418: 		$contentlength = 0; # kludge
                    419: 		&log;
                    420: 		if(open(S, $logfile)) {
                    421: 			seek(S, -5000, 2);
                    422: 			undef $/;
                    423: 			$logsnap = <S>;
                    424: 			$logsnap =~ s/^[^\n]+\n//s if
                    425: 				(length($logsnap) > 4999);
                    426: 			close(S);
                    427: 		}
                    428: 		$p = (time() - $statiosuptime);
                    429: 		$rps = $p/$statiosreq;
                    430: 		$d = int($p / 86400); $p -= $d * 86400;
                    431: 		$h = int($p / 3600); $p -= $h * 3600;
                    432: 		$m = int($p / 60); $s = $p - ($m * 60);
                    433: 		("0$s" =~ /(\d{2})$/) && ($s = $1);
                    434: 		("0$m" =~ /(\d{2})$/) && ($m = $1);
                    435: 		$h +=0; $d += 0;
                    436: 		$suptime = scalar localtime $statiosuptime;
                    437: 		&htcontent(<<"EOF", "text/html");
                    438: <html>
                    439: <head>
                    440: <title>
1.5       albertel  441: LonHTTPD (HTTPi) Status
1.3       www       442: </title>
                    443: </head>
                    444: <body bgcolor = "#ffffff" text = "#000000" vlink = "#0000ff" link = "#0000ff">
1.5       albertel  445: <h1>LonHTTPD (HTTPi) Server Status (<code>$VERSION</code>)</h1>
1.6     ! albertel  446: <h3>lonhttpd on port $port_to_use</h3>
1.3       www       447: <b>Started at:</b> $suptime<br>
                    448: <b>Uptime:</b> $d days, $h:$m:$s<br>
                    449: <b>Last request time:</b> $statiosltr<p>
                    450: <b>Requests received:</b> $statiosreq<br>
                    451: <b>Average time between requests:</b> ${rps}s
                    452: <p>
                    453: <b>Most recent requests:</b>
                    454: <form action = "/status" method = "post">
                    455: <textarea name = "bletch" rows = "8" cols = "70">
                    456: $logsnap
                    457: </textarea>
                    458: </form>
                    459: <hr>
                    460: <address>maintained by <a href =
                    461: "http://httpi.floodgap.com/">httpi/$VERSION</a></address>
                    462: </body>
                    463: </html>
                    464: EOF
                    465: 		exit;
                    466: 	}
                    467: 	if (defined $virtual_files{$address}) {
                    468: 		$virt_buffer = 1;
                    469: 		$mtime = $statiosuptime; # thus always needed
                    470: 		goto SERVEIT;		# yes, it's bad but it's fast
                    471: 	}
                    472: 	$raddress = "$path$address"
                    473: 	;
                    474: 	&hterror301("$address/")
                    475: 		if ($address !~ m#/$# && -d $raddress);
                    476: 	$raddress = "${raddress}index.html" if (-d $raddress);
                    477: 	if(!sysopen(S, $raddress, 0)) { &hterror404; } else {
                    478: 		if (-x $raddress) {
                    479: 			$currentcode = 100;
                    480: 			&log;
                    481: 			if (!$<) {
                    482: 				($x,$x,$x,$x,$uid,$gid) = stat(S);
                    483: 				(!$uid || !$gid) &&
                    484: 					die "executable is root-owned";
                    485: 				$> = $uid || die "can't set effuid";
                    486: 				$) = $gid || die "can't set effgid";
                    487: 			}
                    488: 			($hostname, $port, $ip) = &sock_to_host() if (!$port);
                    489: 			$ENV{'REQUEST_METHOD'} = $method;
                    490: 			$ENV{'SERVER_NAME'} = "localhost";
                    491: 			$ENV{'SERVER_PROTOCOL'} = "HTTP/$httpver";
                    492: 			$ENV{'SERVER_SOFTWARE'} = "HTTPi/$VERSION";
1.6     ! albertel  493: 			$ENV{'SERVER_PORT'} = "$port_to_use";
        !           494: 			$ENV{'SERVER_URL'} = "http://localhost:$port_to_use/";
1.3       www       495: 			$ENV{'SCRIPT_FILENAME'} = $raddress;
                    496: 			$ENV{'SCRIPT_NAME'} = $address;
                    497: 			$ENV{'REMOTE_HOST'} = $hostname;
                    498: 			$ENV{'REMOTE_ADDR'} = $ip;
                    499: 			$ENV{'REMOTE_PORT'} = $port;
                    500: 			$ENV{'QUERY_STRING'} = $variables;
                    501: 			$ENV{'HTTP_USER_AGENT'} = $httpua;
                    502: 			$ENV{'HTTP_REFERER'} = $httpref;
                    503: 			if ($pid = fork()) { exit; } else {
                    504: 				if ($method eq 'POST') { # needs stdin
                    505: 					open(W, "|$raddress") || die
                    506: 						"can't POST to $raddress";
                    507: 					read(STDIN, $buf, $httpcl);
                    508: 					print W $buf;
                    509: 					exit;
                    510: 				}
                    511: 				exec "$raddress", "$variables";
                    512: 				die "exec() returned -1";
                    513: 			}
                    514: 		}
                    515: 		($x,$x,$x,$x,$x,$x,$x,$length,$x,$mtime) = stat(S);
                    516: 		$ctype = 0;
                    517: 		foreach(keys %content_types) {
                    518: 			if ($raddress =~ /\.$_$/i) {
                    519: 				$ctype = $content_types{$_};
                    520: 			}
                    521: 		}
                    522: SERVEIT:	$ctype ||= 'text/plain';
                    523: 		&htsponse(200, "OK");
                    524: 		$mtime = scalar gmtime $mtime;
                    525: 		($dow, $mon, $dt, $tm, $yr) =
                    526: 			($mtime =~ m/(...) (...) (..) (..:..:..) (....)/);
                    527: 		$dt += 0; $yr += 0;
                    528: 		&hthead("Last-Modified: $dow, $dt $mon $yr $tm GMT");
                    529: 		if ($pid = fork()) { exit; }
                    530: 		if ($virt_buffer) {
                    531: 			&htcontent($virtual_files{$address}->[2],
                    532: 				$virtual_files{$address}->[0], 0);
                    533: 		} else {
                    534: 			&htcontent("", $ctype, $length);
                    535: 			unless ($method eq 'HEAD') {
                    536: 				while(!eof(S)) {
                    537: 					read(S, $q, 16384);
                    538: 					print stdout $q;
                    539: 				}
                    540: 			}
                    541: 		}
                    542: 		alarm 0;
                    543: 	}
                    544: 	&log;
                    545: 	exit;
                    546: }
1.1       www       547: 
1.3       www       548: exit;
                    549: }
                    550: 
                    551: 
                    552: sub hterror {
                    553: 	local($errstr, $expl) = (@_);
                    554: 	&htcontent(<<"EOF", "text/html");
                    555: <html>
                    556: <body>
                    557: <h1>$errstr</h1>
                    558: $expl
                    559: <hr>
                    560: <address><a href = "http://httpi.floodgap.com/">httpi/$VERSION</a>
                    561: by Cameron Kaiser</address>
                    562: </body>
                    563: </html>
                    564: EOF
                    565: 	}
                    566: 
                    567: sub hterror404 {
                    568: 	&htsponse(404, "File Not Found");
                    569: 	&hterror("File Not Found",
                    570: 		"The resource $address was not found on this system.");
                    571: }
                    572: 
                    573: sub hterror301 {
                    574: 	&htsponse(301, "Moved Permanently");
                    575: 	&hthead("Location: @_");
                    576: 	&hterror("Resource Moved Permanently",
                    577: 		"This resource has moved <a href = \"@_\">here</a>.");
                    578: 	$keep = 0; &log; exit;
                    579: }
1.1       www       580: 
                    581: for (;;) {
1.3       www       582: 	$addr=accept(NS,S);
                    583: 	$statiosltr = scalar localtime;
                    584: 	$statiosreq++;
                    585: 	if ($pid = fork()) {
1.5       albertel  586: 		$0 = "lonhttpd: (dhttpi) waiting for child process";
1.3       www       587: 		waitpid($pid, 0);
1.6     ! albertel  588: 		$0 = "lonhttpd: (dhttpi) on ANY:$port_to_use, last request " .
1.3       www       589: 			scalar localtime;
                    590: 	} else {
1.5       albertel  591: 		$0 = "lonhttpd: (dhttpi) child switching to socket";
1.3       www       592: 		open(STDIN, "<&NS");
                    593: 		open(STDOUT, ">&NS");
                    594: 		&master;
                    595: 		exit;
                    596: 	}
1.1       www       597: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>