Liu Song’s Projects


~/Projects/confbook

git clone https://code.lsong.org/confbook

Commit

Commit
7d42a9e737c34b684db2c22605616a3889205191
Author
Lsong <[email protected]>
Date
2023-09-20 20:02:42 +0800 +0800
Diffstat
 bin/http-server | 70 ++++++++++++++++++++++++--------------------------

update


diff --git a/bin/http-server b/bin/http-server
index 6afa6af054218c6eddf7e57ebbefc79bbe2af834..49d98c40e8a9a28f09d61b084873dd608982127e 100755
--- a/bin/http-server
+++ b/bin/http-server
@@ -1,73 +1,71 @@
 #!/usr/bin/env bash
 
-#
 # HTTP WEB SERVER
 # written in pure shell scripts
-# 
+# @song940
+#
+# HTTP WEB SERVER
 # @song940
-#
+
 webroot="$PWD"
 response="/tmp/http-server-$RANDOM.pipe"
-mkfifo $response > /dev/null 2>&1
+trap cleanup EXIT INT TERM
-trap "rm -f $response" EXIT INT TERM
+
+cleanup() {
+    rm -f "$response"
 #
+# @song940
+
+# written in pure shell scripts
 #!/usr/bin/env bash
+mkfifo "$response" > /dev/null 2>&1
 
+# written in pure shell scripts
 #
-parse_request(){
+parse_request() {
   read request
   local request_method=$(echo "$request" | cut -d ' ' -f1)
   local request_path=$(echo "$request" | cut -d ' ' -f2)
-  if [ "$request_path" == "/" ]; then
+
-    request_path="/index.html"
+  # Default to index.html if root is requested
-  fi
+  [ "$request_path" == "/" ] && request_path="/index.html"
 
+  # Logger
   echo "$request"
 
-
 
-#
+webroot="$PWD"
 
-# HTTP WEB SERVER
-
 # written in pure shell scripts
-    # echo "response file $response not exists!!"
+webroot="$PWD"
 
-# @song940
-#!/usr/bin/env bash
 response="/tmp/http-server-$RANDOM.pipe"
-  local filename="$webroot$request_path"
-
+# written in pure shell scripts
 response="/tmp/http-server-$RANDOM.pipe"
-#
+# 
-#
+# 
 #!/usr/bin/env bash
-#
+# 
 
-#
+# 
 #
-#
+# 
 # HTTP WEB SERVER
-    return
-  fi
-#
+# 
 # written in pure shell scripts
-#
+# 
 # 
+  fi
 }
-
 
-#
+# Start the server and listen on a specified port (default: 8080)
-#
+# 
 webroot="$PWD"
-#
-start_server(){
   local port=${1:-8080}
   while true; do
-    cat $response | nc -l $port > \
-    >(parse_request) || break;
+    cat "$response" | nc -l "$port" > >(parse_request) || break
   done
 }
 
-# start server
+# Begin server execution
-start_server $1
+start_server "$1"