aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMinteck <nekostarfan@gmail.com>2021-11-14 19:09:34 +0000
committerMinteck <nekostarfan@gmail.com>2021-11-14 19:09:34 +0000
commit4fa28842044d48626b51c94a67ccf623d9a9383f (patch)
tree052f1d0622ec06a3c8ef14f738fe23b39cafa41c
parentd13afb20bac0b682f7e42c9655bc2af65aa9784a (diff)
downloadbashweb-4fa28842044d48626b51c94a67ccf623d9a9383f.tar.gz
bashweb-4fa28842044d48626b51c94a67ccf623d9a9383f.tar.bz2
bashweb-4fa28842044d48626b51c94a67ccf623d9a9383f.zip
Commit
-rw-r--r--.gitignore3
-rwxr-xr-xbashweb.sh28
-rw-r--r--errors/404.bhtml12
-rw-r--r--in.txt (renamed from log)0
-rw-r--r--index.bhtml3
-rwxr-xr-xpreprocessor/main.sh55
-rwxr-xr-xpreprocessor/url.sh12
7 files changed, 70 insertions, 43 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5e18419
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+public/*
+log
+req
diff --git a/bashweb.sh b/bashweb.sh
index aa2d1d0..2d572c6 100755
--- a/bashweb.sh
+++ b/bashweb.sh
@@ -6,32 +6,16 @@
## ##
##########################################################
-data=$(cat index.bhtml | envsubst)
+export bashweb_version="0.1.1"
main () {
-# netcat -q 0 -l 1234 <<EOF
-#HTTP/1.1 200 OK
-#Server: Bashweb
-#Date: $(date)
-#Content-Type: text/html; charset=utf-8
-#Content-Length: ${#data}
-#Connection: keep-alive
-#X-Frame-Options: SAMEORIGIN
-#X-XSS-Protection: 1; mode=block
-#Cache-Control: private, no-cache, no-store, must-revalidate
-#X-Content-Type-Options: nosniff
-#Pragma: no-cache
-#Expires: Sat, 01 Jan 2000 00:00:00 GMT
-#Referrer-Policy: no-referrer-when-downgrade
-#
-#$data
-#$(./preprocessor/url.sh)
-#EOF
- netcat -q 0 -l 1234 < /tmp/backpipe | ./preprocessor/main.sh 1> /tmp/backpipe
+ nc -l -p 1234 < /tmp/backpipe | ./preprocessor/main.sh 1> /tmp/backpipe
}
+rm -f /tmp/backpipe
+mkfifo /tmp/backpipe
+
while true
do
- main &>log
+ main &>>log
done
-
diff --git a/errors/404.bhtml b/errors/404.bhtml
new file mode 100644
index 0000000..f2b4a32
--- /dev/null
+++ b/errors/404.bhtml
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html>
+
+<head>
+ <title>404 Not Found</title>
+</head>
+
+<body>
+ <h1>Not Found</h1>
+</body>
+
+</html>
diff --git a/log b/in.txt
index e69de29..e69de29 100644
--- a/log
+++ b/in.txt
diff --git a/index.bhtml b/index.bhtml
deleted file mode 100644
index 4d4989c..0000000
--- a/index.bhtml
+++ /dev/null
@@ -1,3 +0,0 @@
-<b>Hello!</b>,this is a page from Bashweb!<br>
-It even supports bash variables:<br>
-<pre>$USER</pre>
diff --git a/preprocessor/main.sh b/preprocessor/main.sh
index 5aff989..081e5a1 100755
--- a/preprocessor/main.sh
+++ b/preprocessor/main.sh
@@ -1,17 +1,40 @@
#!/bin/bash
-data=$(cat index.bhtml | envsubst)
-echo "HTTP/1.1 200 OK"
-echo "Server: Bashweb"
-echo "Date: $(date)"
-echo "Content-Type: text/html; charset=utf-8"
-echo "Content-Length: ${#data}"
-echo "Connection: keep-alive"
-echo "X-Frame-Options: SAMEORIGIN"
-echo "X-XSS-Protection: 1; mode=block"
-echo "Cache-Control: private, no-cache, no-store, must-revalidate"
-echo "X-Content-Type-Options: nosniff"
-echo "Pragma: no-cache"
-echo "Expires: Sat, 01 Jan 2000 00:00:00 GMT"
-echo "Referrer-Policy: no-referrer-when-downgrade"
-echo ""
-echo $data
+read -r REQUEST_LINE
+export REQUEST_LINE="$REQUEST_LINE"
+export REQUEST_METHOD=$(echo "$REQUEST_LINE" | cut -d' ' -f1)
+export REQUEST_DOCUMENT=$(echo "$REQUEST_LINE" | cut -d' ' -f2)
+export REQUEST_PROTOCOL=$(echo "$REQUEST_LINE" | rev | cut -d' ' -f1 | rev)
+
+export e404=$(cat errors/404.bhtml | envsubst)
+
+echo "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)" >> log
+if [ -f "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)" ]; then
+ if [[ $(file --mime-type -b "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)") == text/html ]]; then
+ export data=$(cat "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)" | envsubst)
+ fi
+
+ echo "HTTP/1.1 200 OK"
+ echo "Server: Bashweb/"$bashweb_version"; bash/"$(bash --version | head -1 | cut -d' ' -f4)"; "$(uname -s)"/"$(uname -r)
+ echo "Connection: close"
+ if [[ $(file --mime-type -b "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)") == text/html ]]; then
+ echo "Content-Length: "$(echo "$data" | wc -c)
+ fi
+ echo "Content-Type: "$(file --mime-type -b "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)")
+ echo $(file --mime-type -b "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)") >> log
+ echo ""
+ #echo "$data"
+ #echo "garbage"
+ if [[ $(file --mime-type -b "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)") == text/html ]]; then
+ echo "$data"
+ else
+ cat "./public/$(./preprocessor/url.sh $REQUEST_DOCUMENT)"
+ fi
+else
+ echo "HTTP/1.1 404 Not Found"
+ echo "Server: Bashweb/"$bashweb_version"; bash/"$(bash --version | head -1 | cut -d' ' -f4)"; "$(uname -s)"/"$(uname -r)
+ echo "Connection: close"
+ echo "Content-Length: "$(echo "$e404" | wc -c)
+ echo "Content-Type: text/html"
+ echo ""
+ echo "$e404"
+fi
diff --git a/preprocessor/url.sh b/preprocessor/url.sh
index bc4e221..1c04b90 100755
--- a/preprocessor/url.sh
+++ b/preprocessor/url.sh
@@ -1,4 +1,12 @@
#!/bin/bash
-read_from_pipe() { read "$@" <&0; }
+url=$1
-echo "$(read_from_pipe | head -1)"
+if [[ "$url" =~ .*"..".* ]]; then
+ url="/"
+fi
+
+if [ -f "./public/$url/index.bhtml" ]; then
+ url="/$url/index.bhtml"
+fi
+
+echo $url