plan9fox/rc/bin/rc-httpd/handlers/cgi
cinap_lenrek 1a1863e5dc rc-httpd: cleanup cgi handler
- avoid silly stats and checks for $cgi_dir, just use status
  from builtin cd.

- log proper http status code from the cgi script
2015-06-02 16:56:19 +02:00

46 lines
796 B
Bash
Executable file

#!/bin/rc
fn filter_headers{
response=(200 OK)
lines=''
done=false
while(~ $done false){
line=`{getline}
head=`{echo $line | awk '{print tolower($1)}'}
if(~ $head status:*)
response=`{echo $line | awk '{$1="" ; print}'}
if not if(~ $line '')
done=true
if not
lines=$"lines^$"line^$cr^'
'
}
echo 'HTTP/1.1' $"response^$cr
echo -n $"lines
do_log $response(1)
}
fn run_cgi {
path=$cgi_path exec $"cgi_bin $params || echo 'Status: 500'
}
cgi_bin=$1
cgi_dir=.
if(! ~ $#* 1)
cgi_dir=$*($#*)
if not if(~ $"cgi_bin /*){
cgi_dir=`{basename -d $"cgi_bin}
cgi_dir=$"cgi_dir
}
if(! ~ $"cgi_bin */*)
cgi_bin=./$"cgi_bin
if(! builtin cd $"cgi_dir >[2]/dev/null || ! test -x $"cgi_bin){
error 500
exit
}
run_cgi | {
filter_headers
emit_extra_headers
echo $cr
exec cat
}