shithub: riscv

ref: f2716ab22742ecccafea28948e03ddd71f419829
dir: /rc/bin/rc-httpd/handlers/cgi/

View raw version
#!/bin/rc
fn filter_headers{
	response='HTTP/1.1 200 OK'^$cr
	lines=''
	done=false
	while(~ $done false){
		line=`{getline}
		head=`{echo $line | awk '{print tolower($1)}'}
		if(~ $head status:*){
			tmp=`{echo $line | awk '{$1="" ; print}'}
			response='HTTP/1.1 '^$"tmp^$cr
		}
		if not if(~ $line '')
			done=true
		if not
			lines=$"lines^$"line^$cr^'
'
	}
	echo $response
	echo -n $"lines
}

fn run_cgi {
	path=$cgi_path exec $"cgi_bin $params
}

cgi_bin=$1
if(! ~ $cgi_bin /*){
	pwd=`{pwd}
	cgi_bin=$"pwd ^ / ^ $cgi_bin
}

cgi_dir=$*($#*)
if(! test -d $cgi_dir){
	cgi_dir=`{basename -d $cgi_dir}
	cgi_dir=$"cgi_dir
}

if(! test -d $"cgi_dir){
	error 500
	exit
}
if(! test -f  $cgi_bin -x $cgi_bin){
	error 500
	exit
}
do_log 200
builtin cd $"cgi_dir
run_cgi | {
	filter_headers
	emit_extra_headers
	echo $cr
	exec cat
}