Advertisement
Spirit_of_Stallman

Untitled

Feb 20th, 2013
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.29 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Configurable variables
  4. PATH=/bin:/usr/bin
  5. DOC_ROOT=/home/lord/tmp
  6. DEF_HTML=index.html
  7. DEF_DIR=www
  8. # End of configurables
  9.  
  10. HTTP_VERSION="HTTP/1.0"
  11. SERVER="bash-httpd/0.03"
  12.  
  13. CR=`printf "\015"`
  14. program="${0##/*/}"
  15.  
  16. error(){
  17.     num="$1"
  18.     case "$num" in
  19.         200) err="OK"                        ;;
  20.         403) err="Forbidden"                 ;;
  21.         404) err="Not Found"                 ;;
  22.         500) err="Internal Server Error"     ;;
  23.         501) err="Not Implemented"           ;;
  24.         *  ) err="Internal Server Error"
  25.              num=500                         ;;
  26.     esac
  27.     if [ "$2" ]; then problem="$2"; else problem="$err"; fi
  28.     echo -e "Content-Type: text/html\n\n<html><head><title>$problem</title><body><h1>$num $problem</h1>$problem</body></html>"
  29.     exit 1
  30. }
  31.  
  32. function processor(){
  33.     case "$version" in
  34.         ""|http/0.9|HTTP/0.9)
  35.             null=1;;
  36.         http/1.0|HTTP/1.0|http/1.1|HTTP/1.1)
  37.             #echo "$HTTP_VERSION $num $err"
  38.             read foo; while [ "$foo" != "$CR" -a "$foo" != "" ]; do read foo; done ;;
  39.             #null='1';;
  40.         *)
  41.             error 501 "Unknown version";;
  42.     esac
  43.  
  44.     case "$method" in
  45.         GET|get  )   do_method=get                   ;;
  46.         HEAD|head)   do_method=head                  ;;
  47.         *        )   error 501 "Unimplemented method";;
  48.     esac
  49.  
  50.     case "$url" in
  51.         */../*|*[^/A-Za-z0-9~.-]*)
  52.             error 403 "Illegal attempt to access resource"
  53.             ;;
  54.         /~*)
  55.             user="${url#/~}";
  56.             user="${user%%/*}";
  57.             user=`eval echo ~$user`; # we had *better* have cleaned up $url
  58.             rest="${url#/~*/}"; rest="${rest##/~*}";
  59.             type=file; file="$user/$DEF_DIR/$rest"
  60.             ;;
  61.         ""|/*)
  62.             type=file; file="$DOC_ROOT/$url"
  63.             ;;
  64.         *)
  65.             error 501 "Unknown request type"
  66.             ;;
  67.     esac
  68.  
  69. }
  70.  
  71. function GET(){
  72.     action_url="$1"
  73.     action="$2"
  74.     [[ "$method" == "GET" && "$url" == "$action_url" ]] && {
  75.         eval "$action"
  76.         trig="1"
  77.     }
  78. }
  79.  
  80. function post_processor(){
  81.     [ ! "$trig" ] && {
  82.     case $type in
  83.         file)
  84.             [   -d "$file" ] && file="$file/$DEF_HTML"
  85.             [ ! -e "$file" ] && error 404
  86.             [ ! -r "$file" ] && error 403
  87.             #response_code 200
  88.             echo "$HTTP_VERSION 200 OK"
  89.             if [ "$do_method" = "head" ]; then echo; exit 0; fi
  90.             case "$file" in
  91.                 *.html | *.htm) mime=text/html           ;;
  92.                 *.css         ) mime=text/css            ;;
  93.                 *.jpg|*.jpeg  ) mime=image/jpeg          ;;
  94.                 *.gif         ) mime=image/gif           ;;
  95.                 *.gz|*.tgz    ) mime=application/binary  ;;
  96.                 *.txt|*.text  ) mime=text/plain          ;;
  97.                 *             ) mime=application/binary  ;;
  98.             esac
  99.             echo Content-Type: $mime; echo; cat $file
  100.             ;;
  101.         *) error 501 "Messed up internal type";;
  102.     esac
  103.     }
  104. }
  105.  
  106. read method url version
  107.  
  108. method="${method%$CR}";
  109. url="${url%$CR}";
  110. version="${version%$CR}";
  111.  
  112. processor
  113.  
  114. GET '/hello' 'echo -e "<html><head><title>Hello</title><body><h1>$(date +%T)</h1></body></html>"'
  115.  
  116. post_processor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement