Last active
June 12, 2020 21:36
-
-
Save drench/f41547ff0ea7d541d162ffcfe6773d92 to your computer and use it in GitHub Desktop.
loser: a socat-based web server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# loser: a socat-based web server | |
log() { | |
>&2 echo $* | |
} | |
print_http_response() { | |
printf "HTTP/1.0 $1\r\n" | |
printf "Content-type: $2; charset=utf-8\r\n\r\n" | |
} | |
home_page_content() { | |
cat <<HTML | |
<!doctype html> | |
<html> | |
<head><title>Greetings</title></head> | |
<body> | |
Hi this is a | |
<a href="https://gist.github.com/drench/f41547ff0ea7d541d162ffcfe6773d92"> | |
shell-based web framework | |
</a>! | |
</body> | |
</html> | |
HTML | |
} | |
if [ "$1" = "request" ]; then | |
read -r verb uri proto | |
log "$(date) ${verb} ${uri} ${proto}" | |
case $verb in | |
GET) | |
case $uri in | |
/) | |
print_http_response "200 OK" "text/html" | |
home_page_content | |
;; | |
/date) | |
print_http_response "200 OK" "text/plain" | |
echo "The date and time is $(date)." | |
;; | |
*) | |
print_http_response "404 Not Found" "text/plain" | |
echo "Sorry." | |
;; | |
esac | |
;; | |
*) | |
print_http_response "405 Method Not Allowed" "text/plain" | |
echo "Sorry." | |
;; | |
esac | |
else | |
port=$1 | |
test -z "$port" && port=8080 | |
log "$0 starting on port $port" | |
exec socat TCP4-LISTEN:$port,fork,reuseaddr EXEC:"$0 request" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment