From 44f31b1f27c2a53cf8a4e053a50c245e379558b0 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Wed, 26 Jun 2024 12:18:32 +0800 Subject: Update --- snippets/bash_server | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ snippets/dot_example | 27 ++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 snippets/bash_server create mode 100644 snippets/dot_example (limited to 'snippets') diff --git a/snippets/bash_server b/snippets/bash_server new file mode 100644 index 0000000..f008d11 --- /dev/null +++ b/snippets/bash_server @@ -0,0 +1,90 @@ +#!/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...' + +## Keep server running forever +while true; do + ## 1. wait for FIFO + ## 2. creates a socket and listens to the port 3000 + ## 3. as soon as a request message arrives to the socket, pipes it to the handleRequest function + ## 4. the handleRequest function processes the request message and routes it to the response handler, which writes to the FIFO + ## 5. as soon as the FIFO receives a message, it's sent to the socket + ## 6. closes the connection (`-N`), closes the socket and repeat the loop + cat response | nc -lN 3000 | handleRequest +done diff --git a/snippets/dot_example b/snippets/dot_example new file mode 100644 index 0000000..fa95abc --- /dev/null +++ b/snippets/dot_example @@ -0,0 +1,27 @@ +digraph G { + + subgraph cluster_0 { + style=filled; + color=lightgrey; + node [style=filled,color=white]; + a0 -> a1 -> a2 -> a3; + label = "process #1"; + } + + subgraph cluster_1 { + node [style=filled]; + b0 -> b1 -> b2 -> b3; + label = "process #2"; + color=blue + } + start -> a0; + start -> b0; + a1 -> b3; + b2 -> a3; + a3 -> a0; + a3 -> end; + b3 -> end; + + start [shape=Mdiamond]; + end [shape=Msquare]; +} -- cgit v1.2.3-70-g09d2