shithub: riscv

ref: 1ae5fc01a6e9c6fb1a1befd4c60653a6af10a9e0
dir: /rc/bin/rc-httpd/handlers/serve-static/

View raw version
#!/bin/rc
full_path=`{echo $"FS_ROOT^$"PATH_INFO | urlencode -d}
full_path=$"full_path
if(! ~ `{cleanname -d$FS_ROOT $full_path} `{cleanname $FS_ROOT}^*){
       error 503
       exit
}
if(~ $full_path */)
	error 503
if(test -d $full_path){
	redirect perm $"location^'/' \
		'URL not quite right, and browser did not accept redirect.'
	exit
}
if(! test -e $full_path){
	error 404
	exit
}
if(! test -r $full_path){
	error 503
	exit
}
do_log 200
switch($full_path){
case *.html *.htm
        type=text/html
case *.css
        type=text/css
case *.txt *.md
        type=text/plain
case *.jpg *.jpeg
        type=image/jpeg
case *.gif
        type=image/gif
case *.png
        type=image/png
case *
        type=`{file -m $full_path}
}
if(~ $type text/*)
	type=$type^'; charset=utf-8'
max_age=3600	# 1 hour
echo 'HTTP/1.1 200 OK'^$cr
emit_extra_headers
echo 'Content-type: '^$type^$cr
echo 'Content-length: '^`{ls -l $full_path | awk '{print $6}'}^$cr
echo 'Cache-control: max-age='^$max_age^$cr
echo $cr
exec cat $full_path