add stupid mini httpd - randomcrap - random crap programs of varying quality
(HTM) git clone git://git.codemadness.org/randomcrap
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit e6ef7e822e8e88d5ee345bb02fe9bf06e743911c
(DIR) parent 07527edd14eaf6f5a1f2356d80da9c7e1e7b5bda
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Wed, 13 Aug 2025 19:00:55 +0200
add stupid mini httpd
Diffstat:
A minihttpd/htdocs/hello.txt | 1 +
A minihttpd/htdocs/index.txt | 1 +
A minihttpd/htdocs/test.sh | 7 +++++++
A minihttpd/httpd.sh | 79 +++++++++++++++++++++++++++++++
4 files changed, 88 insertions(+), 0 deletions(-)
---
(DIR) diff --git a/minihttpd/htdocs/hello.txt b/minihttpd/htdocs/hello.txt
@@ -0,0 +1 @@
+Hello World
(DIR) diff --git a/minihttpd/htdocs/index.txt b/minihttpd/htdocs/index.txt
@@ -0,0 +1 @@
+mini HTTPd up and running
(DIR) diff --git a/minihttpd/htdocs/test.sh b/minihttpd/htdocs/test.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+printf 'HTTP/1.0 200 OK\r\n'
+printf 'Date: %s\r\n' "$(TZ=UTC date -R)"
+printf 'Connection: close\r\n'
+printf 'Content-Type: %s\r\n' "text/plain"
+printf '\r\n'
+echo "The date time is: $(TZ=UTC date -R)"
(DIR) diff --git a/minihttpd/httpd.sh b/minihttpd/httpd.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+# insecure mini httpd intended for local testing, do not expose to the interwebs.
+# Dependencies: socat, file, UNIX tools, etc.
+
+# httpstatus(msg)
+httpstatus() {
+ printf 'HTTP/1.0 %s \r\n' "$1"
+ printf 'Date: %s\r\n' "$(TZ=UTC date -R)"
+ printf 'Connection: close\r\n'
+ printf 'Content-Type: text/plain\r\n'
+ printf '\r\n'
+ printf '%s\n' "$1"
+}
+
+# notfound()
+notfound() {
+ httpstatus "404 Not Found"
+}
+
+# internalerror()
+internalerror() {
+ httpstatus "500 Internal Server Error"
+}
+
+# servedata(file)
+servedata() {
+ file="$1"
+ contentlen=$(wc -c < "$file")
+
+ printf 'HTTP/1.0 200 OK\r\n'
+ printf 'Date: %s\r\n' "$(TZ=UTC date -R)"
+ printf 'Connection: close\r\n'
+ printf 'Content-Length: %d\r\n' "$contentlen"
+ printf 'Content-Type: %s\r\n' "$(file -bi "$file")"
+ printf '\r\n'
+ cat "$file"
+}
+
+# servefile(file)
+servefile() {
+ servedata "$1"
+}
+
+# servescript(file)
+servescript() {
+ t="$(mktemp)"
+ if "$1" > "$t"; then
+ cat "$t"
+ else
+ internalerror
+ fi
+ rm -f "$t"
+}
+
+if test "$1" = ""; then
+ script="$(readlink -f "$0")"
+ socat TCP4-LISTEN:8080,reuseaddr,fork "SYSTEM:'$script httpd'"
+elif test "$1" = "httpd"; then
+ IFS=" " read -r method request proto
+ while IFS=": " read -r key value; do
+ test "$value" = "" && break
+ done
+
+ if test "$request" != "/"; then
+ file="htdocs${request}"
+ else
+ file="htdocs/index.txt"
+ fi
+
+ if test -f "$file"; then
+ if test -x "$file"; then
+ servescript "$file"
+ else
+ servefile "$file"
+ fi
+ else
+ notfound
+ fi
+fi