Author: Adam <git@apiote.xyz>
fix responding with proper content type
server2.sh | 22 +++++++++++++---------
diff --git a/server2.sh b/server2.sh index 2f6578f125c8bfbd8d52c0f07e04305fdbb98917..8b8a9c2ac86018b3c3f516148dde112fe3162192 100755 --- a/server2.sh +++ b/server2.sh @@ -72,10 +72,12 @@ } respond200() { page="$1" + contentType="$2" + [ -z "$contentType" ] && contentType='text/html' # { cat "$fifo2" > /dev/null; printf ""; } > "$fifo1" & - printf "HTTP/1.1 200 OK\r\nContent-Length: %s\r\nContent-Type: text/html\r\n\r\n%s\r\n" "${#page}" "$page" > "$fifo1" + printf "HTTP/1.1 200 OK\r\nContent-Length: %s\r\nContent-Type: %s\r\n\r\n%s\r\n" "${#page}" "$contentType" "$page" > "$fifo1" # printf "%s\r\n" "$page" >> "$fifo1" @@ -84,8 +86,8 @@ printf "200 %s" "${#page}" } respond200file() { - contentType="$1" - filePath="$2" + filePath="$1" + contentType="$2" { cat "$fifo2" > /dev/null; printf ""; } > "$fifo1" & @@ -101,7 +103,9 @@ respond202() { toy=$1 item=$2 message=$3 - printf "HTTP/1.1 202 Accepted\r\nContent-Length: %s\r\nLocation: /toys/%s/%s\r\n\r\n%s\r\n" "${#message}" "$name" "$item" "$message" > "$fifo1" + contentType="$4" + [ -z "$contentType" ] && contentType='text/html' + printf "HTTP/1.1 202 Accepted\r\nContent-Length: %s\r\nLocation: /toys/%s/%s\r\n\r\n%s\r\n" "${#message}" "$toy" "$item" "$message" > "$fifo1" printf "202 -" } @@ -194,7 +198,7 @@ status=$(echo "$r" | cut -d ';' -f 1) if [ "$status" = 'n' ] then respond404 "$path" - elif page=$(templateMessage "$(list_toys)" 'toys' 'text/html') + elif page=$(templateMessage "$r" 'items' 'text/html') then respond200 "$page" else @@ -210,13 +214,13 @@ status=$(echo "$r" | cut -d ';' -f 1) if [ "$status" = 'n' ] then respond404 "$path" - elif page=$(templateMessage "$(list_toys)" 'toys' 'text/html') + elif page=$(templateMessage "$r" 'item' "$accept") then if [ "$status" = 'r' ] then - respond202 "$page" + respond202 "$toy" "$item" "$page" "$accept" else - respond200 "$page" + respond200 "$page" "$accept" fi else respond406 "$accept" @@ -234,7 +238,7 @@ respond404 "$path" else contentType=$(echo "$r" | cut -d ';' -f 2) filePath=$(echo "$r" | cut -d ';' -f 3) - respond200file "$contentType" "$filePath" + respond200file "$filePath" "$contentType" fi else respond404 "$path"