demo.cgi 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/bash
  2. _WWW="/www"
  3. _SRC="${_WWW}/htdocs/demo.superglue.it/default-demo.html"
  4. _HTDOCS="${_WWW}/htdocs-demo"
  5. if ( [[ $REQUEST_URI != '/' ]] &&
  6. [[ -e $_HTDOCS/$REQUEST_URI ]] ); then
  7. printf '%b' 'HTTP/1.1 200 OK\nAccess-Control-Allow-Origin: *\n\n'
  8. cat $_HTDOCS/$REQUEST_URI
  9. exit 0
  10. fi
  11. ## http response
  12. headerPrint() {
  13. case ${1} in
  14. 200) printf '%b' 'HTTP/1.1 200 OK\nAccess-Control-Allow-Origin: *\n';;
  15. 405) printf '%b' 'HTTP/1.1 405 Method Not Allowed\n';;
  16. 406) printf '%b' 'HTTP/1.1 406 Not Acceptable\n';;
  17. esac
  18. return 0
  19. }
  20. setCookie() {
  21. printf '%b' 'Set-Cookie: ' "$1\n"
  22. }
  23. #_HASH=$(cat /dev/urandom | tr -dc 0-9 | head -c10)
  24. ## is this slow?
  25. _HASH=($_HTDOCS/*) ## get all files
  26. _HASH=${#_HASH[@]} ## count files
  27. _PAGE="tryout-page-$_HASH"
  28. ## set cookie of we have none
  29. if [[ -z $_PAGE ]]; then
  30. setCookie $_HASH
  31. fi
  32. ## see if we have a file matching the cookie
  33. if [[ -e $_HTDOCS/$_PAGE ]]; then
  34. printf '%b' "HTTP/1.1 301 Moved Permanently\nLocation: http://demo.superglue.it/$_PAGE\n\n"
  35. exit 0
  36. fi
  37. ## if neither URL or COOKIE is given then make a new demo page and direct user to it
  38. cat $_SRC/default-demo.html > $_HTDOCS/$_PAGE
  39. printf '%b' "HTTP/1.1 301 Moved Permanently\nLocation: http://demo.superglue.it/$_PAGE\n\n"
  40. exit 0