Permalink
Browse files

Incorporate suggestions 1, 2, and 4 from http://github.com/tlrobinson…

  • Loading branch information...
Tom Robinson
Tom Robinson committed Apr 3, 2010
1 parent 43998d6 commit 916af50507d179307c8be8cc327267d378301f5c
Showing with 39 additions and 39 deletions.
  1. +5 −5 example.sh
  2. +17 −17 martin.sh
  3. +17 −17 wwwoosh.sh
View
@@ -1,9 +1,9 @@
#!/bin/sh
-source martin.sh
+. martin.sh
get "/" root
-function root () {
+root () {
header "Content-Type" "text/html"
cat <<EOT
<html>
@@ -21,19 +21,19 @@ EOT
}
get "/ps" ps_handler
-function ps_handler () {
+ps_handler () {
header "Content-Type" "text/plain"
ps aux
}
get "/DeanMartin.jpg" dean_handler
-function dean_handler () {
+dean_handler () {
header "Content-Type" "image/jpeg"
cat "DeanMartin.jpg"
}
get "/redirect" redirect_handler
-function redirect_handler () {
+redirect_handler () {
status 302
header "Location" "http://jackjs.org/"
}
View
@@ -1,34 +1,34 @@
-source wwwoosh.sh
+. wwwoosh.sh
martin_response="/tmp/martin_response"
routes_method=()
routes_path=()
routes_action=()
-function route () {
+route () {
routes_method=( ${routes_method[@]} "$1" )
routes_path=( ${routes_path[@]} "$2" )
routes_action=( ${routes_action[@]} "$3" )
}
-function get () {
+get () {
route "GET" $@
}
-function post () {
+post () {
route "POST" $@
}
-function delete () {
+delete () {
route "DELETE" $@
}
-function status () {
+status () {
response_status="$1"
}
-function header () {
+header () {
head="$1: $2"
if [ "$response_headers" ]; then
response_headers="$response_headers\n$head"
@@ -37,7 +37,7 @@ function header () {
fi
}
-function not_found () {
+not_found () {
status "404"
header "Content-type" "text/plain"
if [ $# -gt 0 ]; then
@@ -47,36 +47,36 @@ function not_found () {
fi
}
-function martin_dispatch () {
+martin_dispatch () {
action=""
-
+
for (( i = 0 ; i < ${#routes_method[@]} ; i++ )); do
method=${routes_method[$i]}
path=${routes_path[$i]}
act=${routes_action[$i]}
- if [ "$REQUEST_METHOD" == "$method" ]; then
- if [ "$PATH_INFO" == "$path" ]; then
+ if [ "$REQUEST_METHOD" = "$method" ]; then
+ if [ "$PATH_INFO" = "$path" ]; then
action="$act"
break
fi
fi
done
-
+
[ ! "$action" ] && action="not_found"
-
+
reset_response
-
+
# execute the action, storing output in a temporary file
"$action" > "$martin_response"
-
+
# set status header, echo headers, blank line, then body
header "Status" "$response_status"
echo "$response_headers"
echo ""
cat "$martin_response"
}
-function reset_response () {
+reset_response () {
response_status="200 OK"
response_headers=""
}
View
@@ -10,26 +10,26 @@ CRLF="$CR$LF"
function handle_request () {
app="$1"
-
+
# read the request line
read request_line
-
+
# read the header lines until we reach a blank line
- while read header && [ ! "$header" == $'\r' ]; do
+ while read header && [ ! "$header" = $'\r' ]; do
# FIXME: multiline headers
header_name="HTTP_$(echo $header | cut -d ':' -f 1 | tr 'a-z-' 'A-Z_')"
export $header_name="$(echo $header | cut -d ':' -f 2 | sed 's/^ //')"
done
-
+
# extract HTTP method and HTTP version
export REQUEST_METHOD=$(echo $request_line | cut -d ' ' -f 1)
export HTTP_VERSION=$(echo $request_line | cut -d ' ' -f 3 | tr -d $'\r')
-
+
# extract the request_path, then PATH_INFO and QUERY_STRING components
request_path=$(echo $request_line | cut -d ' ' -f 2)
export PATH_INFO=$(echo $request_path | cut -d '?' -f 1)
export QUERY_STRING=$(echo $request_path | cut -d '?' -f 2)
-
+
export SCRIPT_NAME=""
export SERVER_NAME="localhost"
export SERVER_PORT="$port"
@@ -40,13 +40,13 @@ function handle_request () {
function handle_response () {
response_status="200 OK"
response_headers=""
-
- while read header && [ ! "$header" == "" ]; do
+
+ while read header && [ ! "$header" = "" ]; do
header_name="$(echo $header | cut -d ':' -f 1 | tr 'A-Z' 'a-z')"
- if [ "$header_name" == "status" ]; then
+ if [ "$header_name" = "status" ]; then
response_status="$(echo $header | cut -d ':' -f 2 | sed 's/^ //')"
- #elif [ "$header_name" == "content-type" ]; then
- #elif [ "$header_name" == "location" ]; then
+ #elif [ "$header_name" = "content-type" ]; then
+ #elif [ "$header_name" = "location" ]; then
else
if [ "$response_headers" ]; then
response_headers="$response_headers$CRLF$header"
@@ -55,29 +55,29 @@ function handle_response () {
fi
fi
done
-
+
# echo status line, headers, blank line, body
echo "$http_version $response_status$CRLF$response_headers$CRLF"
cat
}
function wwwoosh_run () {
app="$1"
-
+
# TODO: is there a better way than a named pipe?
rm -f "$wwwoosh_fifo"
mkfifo "$wwwoosh_fifo"
-
+
port="$default_port"
[ $# -gt 1 ] && port="$2"
-
+
debug=""
[ $# -gt 2 ] && debug="$3"
-
+
echo "Starting Wwwoosh on port $port..."
while true; do
-
+
if [ "$debug" ]; then
nc -l $port < "$wwwoosh_fifo" | tee /dev/stderr | handle_request "$app" | handle_response | tee /dev/stderr > "$wwwoosh_fifo"
else

0 comments on commit 916af50

Please sign in to comment.