]> git.lizzy.rs Git - plan9front.git/blob - rc/bin/rc-httpd/handlers/serve-static
8c2ada581a864daea306594bc259e9b0266cbf18
[plan9front.git] / rc / bin / rc-httpd / handlers / serve-static
1 #!/bin/rc
2 full_path=`{echo $"FS_ROOT^$"PATH_INFO | urlencode -d}
3 full_path=$"full_path
4 if(~ $full_path */)
5         error 503
6 if(test -d $full_path){
7         redirect perm $"location^'/' \
8                 'URL not quite right, and browser did not accept redirect.'
9         exit
10 }
11 if(! test -e $full_path){
12         error 404
13         exit
14 }
15 if(! test -r $full_path){
16         error 503
17         exit
18 }
19 do_log 200
20 switch($full_path){
21 case *.html *.htm
22         type=text/html
23 case *.css
24         type=text/css
25 case *.txt
26         type='text/plain; charset=utf-8'
27 case *.jpg *.jpeg
28         type=image/jpeg
29 case *.gif
30         type=image/gif
31 case *.png
32         type=image/png
33 case *
34         type=`{file -m $full_path}
35 }
36 max_age=3600    # 1 hour
37 echo 'HTTP/1.1 200 OK'^$cr
38 emit_extra_headers
39 echo 'Content-type: '^$type^'; charset=utf-8'^$cr
40 echo 'Content-length: '^`{ls -l $full_path | awk '{print $6}'}
41 echo 'Cache-control: max-age='^$max_age^$cr
42 echo $cr
43 exec cat $full_path