Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Configurable variables
- PATH=/bin:/usr/bin
- DOC_ROOT=/home/lord/tmp
- DEF_HTML=index.html
- DEF_DIR=www
- # End of configurables
- HTTP_VERSION="HTTP/1.0"
- SERVER="bash-httpd/0.03"
- CR=`printf "\015"`
- program="${0##/*/}"
- error(){
- num="$1"
- case "$num" in
- 200) err="OK" ;;
- 403) err="Forbidden" ;;
- 404) err="Not Found" ;;
- 500) err="Internal Server Error" ;;
- 501) err="Not Implemented" ;;
- * ) err="Internal Server Error"
- num=500 ;;
- esac
- if [ "$2" ]; then problem="$2"; else problem="$err"; fi
- echo -e "Content-Type: text/html\n\n<html><head><title>$problem</title><body><h1>$num $problem</h1>$problem</body></html>"
- exit 1
- }
- function processor(){
- case "$version" in
- ""|http/0.9|HTTP/0.9)
- null=1;;
- http/1.0|HTTP/1.0|http/1.1|HTTP/1.1)
- #echo "$HTTP_VERSION $num $err"
- read foo; while [ "$foo" != "$CR" -a "$foo" != "" ]; do read foo; done ;;
- #null='1';;
- *)
- error 501 "Unknown version";;
- esac
- case "$method" in
- GET|get ) do_method=get ;;
- HEAD|head) do_method=head ;;
- * ) error 501 "Unimplemented method";;
- esac
- case "$url" in
- */../*|*[^/A-Za-z0-9~.-]*)
- error 403 "Illegal attempt to access resource"
- ;;
- /~*)
- user="${url#/~}";
- user="${user%%/*}";
- user=`eval echo ~$user`; # we had *better* have cleaned up $url
- rest="${url#/~*/}"; rest="${rest##/~*}";
- type=file; file="$user/$DEF_DIR/$rest"
- ;;
- ""|/*)
- type=file; file="$DOC_ROOT/$url"
- ;;
- *)
- error 501 "Unknown request type"
- ;;
- esac
- }
- function GET(){
- action_url="$1"
- action="$2"
- [[ "$method" == "GET" && "$url" == "$action_url" ]] && {
- eval "$action"
- trig="1"
- }
- }
- function post_processor(){
- [ ! "$trig" ] && {
- case $type in
- file)
- [ -d "$file" ] && file="$file/$DEF_HTML"
- [ ! -e "$file" ] && error 404
- [ ! -r "$file" ] && error 403
- #response_code 200
- echo "$HTTP_VERSION 200 OK"
- if [ "$do_method" = "head" ]; then echo; exit 0; fi
- case "$file" in
- *.html | *.htm) mime=text/html ;;
- *.css ) mime=text/css ;;
- *.jpg|*.jpeg ) mime=image/jpeg ;;
- *.gif ) mime=image/gif ;;
- *.gz|*.tgz ) mime=application/binary ;;
- *.txt|*.text ) mime=text/plain ;;
- * ) mime=application/binary ;;
- esac
- echo Content-Type: $mime; echo; cat $file
- ;;
- *) error 501 "Messed up internal type";;
- esac
- }
- }
- read method url version
- method="${method%$CR}";
- url="${url%$CR}";
- version="${version%$CR}";
- processor
- GET '/hello' 'echo -e "<html><head><title>Hello</title><body><h1>$(date +%T)</h1></body></html>"'
- post_processor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement