File:  [LON-CAPA] / loncom / lonhttpd
Revision 1.15: download - view: text, annotated - select for diffs
Wed Jul 11 18:48:20 2007 UTC (16 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: version_2_6_X, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_99_0, HEAD
- clean up the socket when done with it

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

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