Advertisement
ananas

Bash http server for (x)inetd

May 3rd, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.43 KB | None | 0 0
  1. #! /bin/bash
  2. #
  3. # Simple http server with CGI support (launched within inetd/xinetd)
  4. # Command line: httpd.sh [-c] [-d <path>]
  5. #         -c        - show content of current directory if index.html is not found
  6. #         -d <path> - set base directory (default - /home/http)
  7. #         -v        - be verbose
  8. #
  9.  
  10. # simple logger
  11. function msg
  12. {
  13.     [[ $VERBOSE -eq 1 ]] && logger -t httpd.sh -p daemon.info "$1"
  14. }
  15.  
  16. # cleanup on exit
  17. trap "rm -rf /tmp/httpd" EXIT
  18.  
  19. # parse command line
  20. SHOW_DIR=0
  21. base=/home/http
  22.  
  23. OPTIND=1
  24. while getopts cd:v opt ; do
  25.     case "$opt" in
  26.         c) SHOW_DIR=1 ;;
  27.         d) base="$OPTARG" ;;
  28.         v) VERBOSE=1 ;;
  29.     esac
  30. done
  31.  
  32. # get request
  33. read request
  34.  
  35. # get header
  36. mkdir -p /tmp/httpd
  37. while true; do
  38.     read header
  39.     echo $header >> /tmp/httpd/header-$$
  40.     [[ "$header" == $'\r' ]] && break;
  41. done
  42.  
  43. # parse request
  44. url="${request#GET }"
  45. url="${url% HTTP/*}"
  46. query="${url#*\?}"
  47. url="${url%%\?*}"
  48. encoding="utf-8"
  49.  
  50. # set default start page
  51. [[ $url != "/" ]] && fn="$base$url" || fn="$base/index.html"
  52. if [[ "$url" =~ "cgi-bin" ]]; then
  53.     is_cgi="yes"
  54.     [[ "$query" == "$url" ]] && query=
  55. fi
  56.  
  57. # decode filename
  58. filename=$(printf '%b' ${fn//%/\\x})
  59. dirname=${filename%index.html}
  60.  
  61. if [[ $is_cgi == "yes" && -x "$filename" ]]; then
  62.     # run CGI script
  63.     msg "Execute CGI request: $filename"
  64.     export QUERY_STRING="$query"
  65.     echo -e "HTTP/1.1 200 OK\r"
  66.     eval ${filename@Q} $(echo ${query@Q} | sed 's/+/ /g')
  67.     echo -e "\r"
  68. elif [[ -f "$filename" ]]; then
  69.     # show requested file
  70.     msg "Show file: $filename"
  71.     mime=$(file -bi "$filename")
  72.     size=$(wc -c "$filename" | cut -f1 -d' ')
  73.     echo -e "HTTP/1.1 200 OK\r"
  74.     echo -e "Content-Type: $mime\r"
  75.     echo -e "Content-Length: $size\r"
  76.     echo -e "\r"
  77.     cat "$filename"
  78.     echo -e "\r"
  79. elif [[ $SHOW_DIR -eq 1 && -d "$dirname" ]]; then
  80.     # show directory
  81.     msg "Show directory context: $dirname"
  82.     echo -e "HTTP/1.1 200 OK\r"
  83.     echo -e "Content-Type: text/plain; charset=$encoding\r"
  84.     echo -e "\r"
  85.     ls -1gGhB --quoting-style=literal "$dirname"
  86.     echo -e "\r"
  87. else
  88.     # file not found
  89.     msg "File not found: $filename"
  90.     echo -e "HTTP/1.1 404 Not Found\r"
  91.     echo -e "Content-Type: text/html; charset=$encoding\r"
  92.     echo -e "\r"
  93.     echo -e "404 Not Found<br />\r"
  94.     echo -e "The requested resource was not found on the server<br />\r"
  95.     echo -e "\r"
  96. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement