Permalink
Browse files

BEGIN

  • Loading branch information...
0 parents commit 0418943081500a0cbf05d0fab3fc2d41c318e723 @sayanriju committed Aug 26, 2010
Showing with 251 additions and 0 deletions.
  1. +4 −0 etc/tbs/default.config
  2. +73 −0 usr/bin/htsh
  3. +28 −0 usr/bin/tbs.start
  4. +24 −0 usr/bin/tbs.stop
  5. +122 −0 usr/bin/tinybashserver
@@ -0,0 +1,4 @@
+DOCROOT=/var/www/
+PORT=1983
+DEFAULT_INDEX=index.html
+QUIET=0
@@ -0,0 +1,73 @@
+#!/usr/bin/env bash
+
+## N.B. $1 -> Name of .htsh script
+## $2 -> Arguments sent by server to script
+tmpfile="/tmp/tbs.tmp"
+>"$tmpfile"
+
+flag=0
+lc=0
+txt=''
+while read line;do
+ let lc=lc+1
+ # Start and end tags in same line
+ echo "$line"| grep -e "<?.*?>" -e "<?bash.*?>"
+ if [ $? -eq 0 ];then
+ if [ $flag -eq 0 ];then
+ line=${line//<?bash/<?}
+ pre=`echo -e "$line" | sed -e 's/<?.*//'`
+ comm=`echo -e "$line" | sed -e 's/^.*<? //' | sed -e 's/?>.*//'`
+ post=`echo -e "$line" | sed -e 's/^.*?>//'`
+
+ if [ -n "$pre" ];then
+ mod_pre=`echo -e "$pre" | sed "s/'/\\\'/g" | sed 's/"/\\\"/g'`
+ echo -e "echo -e \"$mod_pre\"" >>"$tmpfile"
+ fi
+ echo -e "$comm" >> "$tmpfile"
+ if [ -n "$post" ];then
+ mod_post=`echo -e "$post" | sed "s/'/\\\'/g" | sed 's/"/\\\"/g'`
+ echo -e "echo -e \"$mod_post\"" >>"$tmpfile"
+ fi
+ continue
+ else
+ echo -e "\n\n<br/><br/> ERROR : Non-terminated &lt;?bash or &lt;? in file $1 (Line $lc) "
+ exit 1
+ fi
+ fi
+ # Start tag
+ echo "$line"|grep -q "^<?bash\|^<?"
+ if [ $? -eq 0 ];then
+ if [ $flag -eq 0 ];then
+ flag=1
+ line=${line/<?bash}
+ echo -e "${line/<?}" >> "$tmpfile"
+ else
+ echo -e "\n\n<br/><br/> ERROR : Non-terminated &lt;?bash or &lt;? in file $1 (Line $lc) "
+ exit 1
+ fi
+ continue;
+ fi
+ # End tag
+ echo "$line"|grep -q "?>$"
+ if [ $? -eq 0 ];then
+ if [ $flag -eq 1 ];then
+ flag=0
+ echo -e"${line/?>}" >> "$tmpfile"
+ else
+ echo -e "\n\n<br/><br/> ERROR : Unmatched ?&gt; in file $1 (Line $lc) "
+ exit 1
+ fi
+ continue;
+ fi
+ # Take action on current line based on flag
+ if [ $flag -eq 0 ];then
+ modline=`echo -e "$line" | sed "s/'/\\\'/g" | sed 's/"/\\\"/g'`
+ echo -e "echo -e \"$modline\"" >>"$tmpfile"
+ else
+ echo -e "${line}" >> "$tmpfile"
+ fi
+done < "$1"
+
+/bin/bash "$tmpfile" "$2"
+rm -f "$tmpfile"
+exit 0
@@ -0,0 +1,28 @@
+#!/usr/bin/env bash
+
+if [ $# -ne 0 -a -f "$1" ];then
+ . "$1"
+else
+ . "/etc/tbs/default.config"
+fi
+lockfile="/tmp/tbs.$PORT.lck"
+if [ -f "$lockfile" ];then
+ #[ $QUIET -eq 0 ] &&
+ echo "Tiny Bash Server is already running on port $PORT with PID" `head -1 "$lockfile"`
+ echo "Aborting."
+ exit 2
+fi
+echo -e "$$\n"Started by `whoami` at `date` > "$lockfile"
+while /bin/true;do
+ if [ -f "$lockfile" ];then
+ if [ $QUIET -eq 0 ];then
+ nc -vlp "$PORT" -c "/usr/bin/tinybashserver $DOCROOT $DEFAULT_INDEX"
+ else
+ nc -lp "$PORT" -c "/usr/bin/tinybashserver $DOCROOT $DEFAULT_INDEX" 2>&1 >/dev/null
+ fi
+ else
+ break;
+ fi
+done
+[ $QUIET -eq 0 ] && echo -e "\nBye Bye!"
+exit 0
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+
+if [ $# -ne 1 ];then
+ echo -e "Stops a running instance of Tiny Bash Server at given port.\nUSAGE: tbs.stop <port_number>"
+ exit 1
+fi
+PORT="$1"
+
+lckfile="/tmp/tbs.$PORT.lck"
+if [ -f "$lckfile" ];then
+ rm -f "$lckfile"
+ if [ $? -eq 0 ];then
+ echo "Stoped Tiny Bash Server running on port $PORT with PID $id"
+ echo "You may need to send another request to the server before seeing the effects"
+ exit 0
+ else
+ echo "ERROR while killing Tiny Bash Server running on port $PORT with PID $id"
+ exit 1
+ fi
+else
+ echo "Tiny Bash Server is NOT running on port $PORT"
+ echo "Nothing killed"
+fi
+exit 0
@@ -0,0 +1,122 @@
+#!/usr/bin/env bash
+
+DOCROOT="$1"
+DEFAULT_INDEX="$2"
+DOCROOT=${DOCROOT:-"/var/www/"}
+DEFAULT_INDEX=${DEFAULT_INDEX:-"index.html"}
+
+function parseHttpRequest(){
+read request
+local line=''
+while /bin/true; do
+ read line
+ [ "$line" == $'\r' ] && break;
+ header="$header\n$line"
+done
+header="${header#??}"
+
+echo "$request" | grep -qi "POST"
+if [ $? -eq 0 ];then # POST type; get message body & set var $type
+ type="POST"
+ while /bin/true; do
+ read line
+ [ "$line" == $'\r' -o "$line" == '' ] && break;
+ body="$body\n$line"
+ done
+ body="${body#??}"
+else # GET type; set var $type
+ type="GET"
+fi
+}
+function parseRequestUrl(){
+ query_str=''
+ if [ "$type" == "POST" ];then # POST
+ url="${request#POST }"
+ query_str="$body"
+ else # GET
+ url="${request#GET }"
+ echo "$url" | grep -q "?"
+ [ $? -eq 0 ] && query_str="${url/*\?/}"
+ fi
+ url="${url% HTTP/*}"
+ url="${url#/}"
+ url="${url/\?*/}"
+ query_str="${query_str% HTTP/*}"
+ #query_str="${query_str//&/&&}"
+}
+function createVariableString(){
+ local v1=`echo -e "$header"|grep "^Host"`
+ local hn="${v1#*: }"
+ local ua=`echo -e "$header"|grep "^User-Agent"`
+ var_str="DOCUMENT_ROOT=\"$DOCROOT\"&&HTTP_HOST=\"$hn\"&&HTTP_USER_AGENT=\"$ua\"&&QUERY_STRING=\"$query_str\"&&REQUEST_METHOD=\"$type\"&&REQUEST_URI=\"$url\""
+}
+
+
+function aboutMe(){
+ local v1=`echo -e "$header"|grep "^Host"`
+ local hn="${v1#*:}"
+ echo -e "<br/><br/><hr/><i>Tiny Bash Server running at" ${hn:-`hostname`} "</i>"
+}
+function Bye(){
+ echo "Bye"
+ exit 0
+}
+
+## Main
+parseHttpRequest
+parseRequestUrl
+createVariableString
+
+filename="$DOCROOT$url"
+if [ -d "$filename" ];then
+ filename="$filename/$DEFAULT_INDEX"
+fi
+# Decide & send repsonse
+if [ ! -f "$filename" ];then
+ echo -e "HTTP/1.0 404 Not Found\r"
+ echo -e "Date:" `date`
+ echo -e "Content-Type: text/html\r"
+ echo -e "\r"
+ echo -e "<h1>404 Not Found</h1>\r"
+ echo -e "The requested resource was not found\r"
+ aboutMe
+ echo -e "\r"
+elif [ -r "$filename" ];then
+ echo -e "HTTP/1.1 200 OK\r"
+ echo -e "Date:" `date`
+ case "${filename##*.}" in
+ "htsh") echo -e "Content-Type: text/html\r"
+ echo -e "\r"
+ /usr/bin/htsh "$filename" "`echo $var_str`"
+ ;;
+ "sh") echo -e "Content-Type: text/html\r"
+ echo -e "\r"
+ /bin/bash "$filename" "`echo $var_str`"
+ ;;
+ "html") echo -e "Content-Type: text/html\r"
+ echo -e "\r"
+ cat "$filename"
+ ;;
+ "php") echo -e "Content-Type: text/html\r"
+ echo -e "\r"
+ /usr/bin/php "$filename" "`echo $var_str`"
+ ;;
+ "py") echo -e "Content-Type: text/html\r"
+ echo -e "\r"
+ /usr/bin/python "$filename"
+ ;;
+ "*") echo -e "Content-Type: `/usr/bin/file -bi \"$filename\"`\r"
+ echo -e "\r"
+ cat "$filename"
+ ;;
+ esac
+else
+ echo -e "HTTP/1.0 403 Forbidden\r"
+ echo -e "Date:" `date`
+ echo -e "Content-Type: text/html\r"
+ echo -e "\r"
+ echo -e "<h1>403 Forbidden</h1>\r"
+ echo -e "You do not have permissions to access the requested resource\r"
+ aboutMe
+ echo -e "\r"
+fi

0 comments on commit 0418943

Please sign in to comment.