From 9466883aed4b1933dbf98117724062f0621f7465 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Fri, 20 Sep 2019 14:27:09 -0400 Subject: [PATCH] initial commit --- README.md | 4 ++++ server.sh | 41 +++++++++++++++++++++++++++++++++++++++++ start.sh | 2 ++ 3 files changed, 47 insertions(+) create mode 100644 README.md create mode 100755 server.sh create mode 100755 start.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..feeaa7a --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +## Bash Web Application Framework + +* Requires https://cr.yp.to/ucspi-tcp.html +* Simply run `./start.sh` to launch it, and then go to [http://127.0.0.1:6001/name/luigi](http://127.0.0.1:6001/name/luigi) to see it in action. diff --git a/server.sh b/server.sh new file mode 100755 index 0000000..00ecbc9 --- /dev/null +++ b/server.sh @@ -0,0 +1,41 @@ +#! /usr/bin/env bash +shopt -s extglob + +url_components=() + +function parse_url() { + IFS='/' read -r -a url_components <<< $1 +} + +function parse_request() { + read -u 0 request + method=$(echo "$request" | cut -d ' ' -f 1) + url=$(echo "$request" | cut -d ' ' -f 2) + echo "$url" +} + +parse_url $(parse_request) + +url_matcher="${url_components[@]}" + +case $url_matcher in + *home*) + data="Hello!" + ;; + *name*) + data="Your name is ${url_components[-1]}" + ;; + *) + data="it's something else, $url_matcher" + ;; +esac + +response="$data" +content_length=$(( $(echo $response | wc -c) - 1)) + +printf "HTTP/1.1 200 OK\r\n" +printf "Server: ucspi\r\n" +printf "Content-Type: text/html; charset=UTF-8\r\n" +printf "Content-Length: $content_length\r\n" +printf "\r\n" +printf "$response" diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..e9820e3 --- /dev/null +++ b/start.sh @@ -0,0 +1,2 @@ +#! /usr/bin/env bash +tcpserver -v 127.0.0.1 6001 ./server.sh