From c49b33b3594f832edbde07601cf3cc73a1783963 Mon Sep 17 00:00:00 2001 From: Vitaliy Turov Date: Thu, 28 Nov 2024 21:12:50 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A2=D1=80=D0=B5=D0=BD=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=BA=D0=B0=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20echo=20=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B0=20bash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bash/rest/404.html | 4 ++ bash/rest/home.html | 48 ++++++++++++++++++++++ bash/rest/login.html | 7 ++++ bash/rest/post-login.http | 3 ++ bash/rest/post-logout.http | 3 ++ bash/rest/server.bash | 83 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 148 insertions(+) create mode 100644 bash/rest/404.html create mode 100644 bash/rest/home.html create mode 100644 bash/rest/login.html create mode 100644 bash/rest/post-login.http create mode 100644 bash/rest/post-logout.http create mode 100755 bash/rest/server.bash diff --git a/bash/rest/404.html b/bash/rest/404.html new file mode 100644 index 0000000..f650098 --- /dev/null +++ b/bash/rest/404.html @@ -0,0 +1,4 @@ +HTTP/1.1 404 NotFound +Content-Type: text/html + +

Sorry, not found

\ No newline at end of file diff --git a/bash/rest/home.html b/bash/rest/home.html new file mode 100644 index 0000000..28d593e --- /dev/null +++ b/bash/rest/home.html @@ -0,0 +1,48 @@ +HTTP/1.1 200 OK +Content-Type: text/html + + + + + + + + +
+

Hello, {{name}}

+ +
+ +
+ + Blue theme +
+ + + + diff --git a/bash/rest/login.html b/bash/rest/login.html new file mode 100644 index 0000000..c96455e --- /dev/null +++ b/bash/rest/login.html @@ -0,0 +1,7 @@ +HTTP/1.1 200 OK +Content-Type: text/html + +
+ + +
\ No newline at end of file diff --git a/bash/rest/post-login.http b/bash/rest/post-login.http new file mode 100644 index 0000000..37b5ac5 --- /dev/null +++ b/bash/rest/post-login.http @@ -0,0 +1,3 @@ +HTTP/1.1 301 +Location: http://localhost:3000/ +Set-Cookie: {{cookie_name}}={{cookie_value}}; path=/; HttpOnly \ No newline at end of file diff --git a/bash/rest/post-logout.http b/bash/rest/post-logout.http new file mode 100644 index 0000000..de0a59b --- /dev/null +++ b/bash/rest/post-logout.http @@ -0,0 +1,3 @@ +HTTP/1.1 301 +Location: http://localhost:3000/login +Set-Cookie: {{cookie_name}}={{cookie_value}}; path=/; HttpOnly; Expires=Thu, 01 Jan 1970 00:00:00 GMT \ No newline at end of file diff --git a/bash/rest/server.bash b/bash/rest/server.bash new file mode 100755 index 0000000..8da2af2 --- /dev/null +++ b/bash/rest/server.bash @@ -0,0 +1,83 @@ +#!/bin/bash + +### Create the response FIFO +rm -f response +mkfifo response + +function handle_GET_home() { + RESPONSE=$(cat home.html | + sed "s/{{$COOKIE_NAME}}/$COOKIE_VALUE/") +} + +function handle_GET_login() { + RESPONSE=$(cat login.html) +} + +function handle_POST_login() { + RESPONSE=$(cat post-login.http | + sed "s/{{cookie_name}}/$INPUT_NAME/" | + sed "s/{{cookie_value}}/$INPUT_VALUE/") +} + +function handle_POST_logout() { + RESPONSE=$(cat post-logout.http | + sed "s/{{cookie_name}}/$COOKIE_NAME/" | + sed "s/{{cookie_value}}/$COOKIE_VALUE/") +} + +function handle_not_found() { + RESPONSE=$(cat 404.html) +} + +function handleRequest() { + ## Read request + while read line; do + echo $line + trline=$(echo $line | tr -d '[\r\n]') + + [ -z "$trline" ] && break + + HEADLINE_REGEX='(.*?)\s(.*?)\sHTTP.*?' + [[ "$trline" =~ $HEADLINE_REGEX ]] && + REQUEST=$(echo $trline | sed -E "s/$HEADLINE_REGEX/\1 \2/") + + CONTENT_LENGTH_REGEX='Content-Length:\s(.*?)' + [[ "$trline" =~ $CONTENT_LENGTH_REGEX ]] && + CONTENT_LENGTH=$(echo $trline | sed -E "s/$CONTENT_LENGTH_REGEX/\1/") + + COOKIE_REGEX='Cookie:\s(.*?)\=(.*?).*?' + [[ "$trline" =~ $COOKIE_REGEX ]] && + read COOKIE_NAME COOKIE_VALUE <<<$(echo $trline | sed -E "s/$COOKIE_REGEX/\1 \2/") + done + + ## Read body + if [ ! -z "$CONTENT_LENGTH" ]; then + BODY_REGEX='(.*?)=(.*?)' + + while read -n$CONTENT_LENGTH -t1 line; do + echo $line + trline=$(echo $line | tr -d '[\r\n]') + + [ -z "$trline" ] && break + + read INPUT_NAME INPUT_VALUE <<<$(echo $trline | sed -E "s/$BODY_REGEX/\1 \2/") + done + fi + + ## Route to the response handlers + case "$REQUEST" in + "GET /login") handle_GET_login ;; + "GET /") handle_GET_home ;; + "POST /login") handle_POST_login ;; + "POST /logout") handle_POST_logout ;; + *) handle_not_found ;; + esac + + echo -e "$RESPONSE" >response +} + +echo 'Listening on 3000...' + +while true; do + cat response | nc -lN 3000 | handleRequest +done