changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > werc / bin/contrib/rc-httpd/rc-httpd

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