rc-httpd (2306B)
1 #!/bin/rc 2 rc_httpd_dir=/home/sl/www/werc/bin/contrib/rc-httpd 3 libdir = $rc_httpd_dir/lib 4 path=($PLAN9/bin $rc_httpd_dir/handlers $PATH) 5 cgi_path=$PLAN9/bin 6 SERVER_PORT=80 # default for CGI scripts, may be overridden by the Host header 7 extra_headers='Server: rc-httpd' 8 cr= 9 10 fn do_log{ 11 echo `{date} :: $SERVER_NAME :: $request :: \ 12 $HTTP_USER_AGENT :: $1 :: $HTTP_REFERER >[1=2] 13 } 14 15 fn emit_extra_headers{ 16 for(header in $extra_headers) 17 echo $"header^$cr 18 } 19 20 fn getline{ read | sed 's/'^$"cr^'$//g' } 21 22 fn terminate{ 23 echo `{date} connection terminated >[1=2] 24 exit terminate 25 } 26 27 fn trim_input{ dd -bs 1 -count $CONTENT_LENGTH } 28 29 request=`{getline} 30 if(~ $#request 0) 31 terminate 32 REQUEST_METHOD=$request(1) 33 REQUEST_URI=$request(2) 34 reqlines='' 35 HTTP_COOKIE='' 36 REMOTE_USER='' 37 done=false 38 chunked=no 39 while(~ $"done false){ 40 line=`{getline} 41 if(~ $#line 0) 42 done=true 43 reqlines=$"reqlines$"line' 44 ' 45 h=`{echo $line | awk '{print tolower($1)}'} 46 switch($h){ 47 case '' 48 done=true 49 case host: 50 SERVER_NAME=$line(2) 51 case referer: 52 HTTP_REFERER=$line(2) 53 case user-agent: 54 HTTP_USER_AGENT=`{echo $line | sed 's;[^:]+:[ ]+;;'} 55 case content-length: 56 CONTENT_LENGTH=$line(2) 57 case content-type: 58 CONTENT_TYPE=$line(2) 59 case cookie: 60 cookie=`{echo $line | sed 's;^[^:]+:[ ]*;;'} 61 HTTP_COOKIE=$"HTTP_COOKIE^$"cookie^'; ' 62 case authorization: 63 REMOTE_USER=`{auth/httpauth $line(3)} 64 case transfer-encoding: 65 ~ $line(2) chunked && chunked=yes 66 } 67 } 68 if(~ $REQUEST_URI *://* //*){ 69 SERVER_NAME=`{echo $REQUEST_URI | sed ' 70 s;^[^:]+:;; 71 s;^//([^/]+).*;\1;'} 72 REQUEST_URI=`{echo $REQUEST_URI | sed ' 73 s;^[^:]+:;; 74 s;^//[^/]+/?;/;'} 75 } 76 QUERY_STRING=`{echo $REQUEST_URI | sed 's;[^?]*\??;;'} 77 params=`{echo $QUERY_STRING | sed 's;\+; ;g'} 78 location=`{echo $REQUEST_URI | sed 's;\?.*;;'} 79 location=`{echo $location | sed ' 80 s;[^/]+/\.\./;/;g 81 s;/\./;/;g 82 s;//+;/;g 83 '} 84 SERVER_NAME=`{echo $SERVER_NAME | sed 's;^(\[[^\]]+\]|[^:]+)\:([0-9]+)$;\1 \2;'} 85 if(~ $#SERVER_NAME 2){ 86 SERVER_PORT=$SERVER_NAME(2) 87 SERVER_NAME=$SERVER_NAME(1) 88 } 89 if(~ $REQUEST_METHOD (PUT POST)){ 90 if(! ~ $"CONTENT_LENGTH '') 91 trim_input | exec $rc_httpd_dir/select-handler 92 if not{ 93 if(~ $chunked yes){ 94 echo 'HTTP/1.1 411 Length required'^$cr 95 echo $cr 96 exit 97 } 98 exec $rc_httpd_dir/select-handler 99 } 100 } 101 if not 102 . $rc_httpd_dir/select-handler