File:  [LON-CAPA] / loncom / lonhttpd
Revision 1.13: download - view: text, annotated - select for diffs
Wed Apr 11 21:37:24 2007 UTC (17 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Put reg exp check for domain logo in lonnet  - &is_domainimage()
checkauthen, publiccheck and lonacc all need this to serve images for log-in page if port 8080 is not in use.
Other possibilities - (1) call once in checkauthen and pass along to publiccheck and lonacc in Apache notes, or (2) modify loncapa_apach.conf.
lonhttpd to use a call to the same routine.

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