Skip to content

Commit

Permalink
Handle additional header fields
Browse files Browse the repository at this point in the history
  • Loading branch information
fumyuun committed Mar 10, 2022
1 parent b408fd7 commit 99fcf7a
Showing 1 changed file with 51 additions and 21 deletions.
72 changes: 51 additions & 21 deletions handle_connection.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
#!/bin/bash

while read -r line; do
echo "[$(date +%T)] $line" >> connection_raw.log
line=$(echo "$line" | tr -d '\r\n')
request=""
path=""
path_sane=""

# extract path
path=$(echo "$line" | cut -d ' ' -f2)
request_keys=()
request_values=()

# remove slashes and dots in the beginning
path_sane=$(echo $path | sed 's/^[./]*//')
while read -r line; do
line=$(echo "$line" | tr -d '\r\n')

if [[ "$line" =~ ^GET ]]; then
echo "[$(date +%T)] GET line: $line; path: $path; path_sane: $path_sane" >> connection.log
echo "[$(date +%T)] '$line'" >> connection_raw.log

if [ -n "$STATIC_DIR" -a -n "$path_sane" -a -r "$STATIC_DIR/$path_sane" ]; then
# End of request, time to handle it
if [ -z "$line" ]; then
# GET, static content directory defined and matches a file
if [ "$request" == "GET" -a -n "$STATIC_DIR" -a -n "$path_sane" -a -r "$STATIC_DIR/$path_sane" ]; then
filetype=$(echo "$path_sane" | sed 's/^.*\.//')
content="text/html"
content="text/plain"

case "$filetype" in
css)
Expand All @@ -24,26 +26,54 @@ while read -r line; do
js)
content="application/javascript"
;;
html)
content="text/html"
;;
esac

echo "[$(date +%T)] GET static, path: $path; path_sane: $path_sane" >> connection.log

cat <<EOF
$($BASHSERV_DIR/header.sh -t $content -l $(wc -c $STATIC_DIR/$path_sane | cut -d ' ' -f1))
$(cat $STATIC_DIR/$path_sane)
EOF
break
exit 0
fi

if [ -n "$GET_HANDLER" ]; then
$GET_HANDLER "$path_sane" "$path"
break
# GET, get handler registered
if [ "$request" == "GET" -a -n "$GET_HANDLER" ]; then
echo "[$(date +%T)] GET dynamic, path: $path; path_sane: $path_sane" >> connection.log
request_fields=""
for i in $(seq 0 $((${#request_keys[@]} - 1))); do
key="${request_keys[$i]}"
value="${request_values[$i]}"
length=$(echo "$value" | wc -w)
request_fields+="$length $key $value "
done

export REQUEST_PATH="$path"
export REQUEST_PATH_SANE="$path_sane"

$GET_HANDLER $request_fields
ret=$?
[ $ret -eq 0 ] && exit 0
fi
fi
if [[ "$line" =~ ^BREW && "$path" =~ ^coffee ]]; then
body="short and stout\n"
cat <<EOF
$($BASHSERV_DIR/header.sh -t "text/plain" -l $(echo -ne "$body" | wc -c) "418 I'm a teapot")
$(echo -ne $body)
EOF

# GET request - GET <path> HTTP/x.x
if [[ "$line" =~ ^GET ]]; then
request="GET"
path=$(echo "$line" | cut -d ' ' -f2)
path_sane=$(echo "$path" | sed 's/^[./]*//')
continue
fi

# Parse request fields of form Key: Value
key=$(echo "$line" | cut -d ':' -f1 | sed 's/^\s*//')
val=$(echo "$line" | cut -d ':' -f2-)
if [ -n "$key" -a -n "$val" ]; then
request_keys+=("$key")
request_values+=("$val")
fi
done

0 comments on commit 99fcf7a

Please sign in to comment.