-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebzer.sh
44 lines (39 loc) · 1.18 KB
/
webzer.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
httpReply() {
if [ $1 -ge 400 ] ; then echo "HTTP/1.1 $1 Bad Request"
else echo "HTTP/1.1 $1 OK"
fi
shift; body="$@"
cat <<EOF
Server: Apache/2.4.29 (Ubuntu)
Connection: close
Content-Type: text/html; charset=iso-8859-1
Content-Length: ${#body}
$body
EOF
}
httpDispatcher() {
if [[ $@ =~ ^GET\ /[\ \/\#?] ]]; then
httpReply 200 "<h2>Root page</h2><br><a href='/page1'>Page 1</a><br><a href='/page2'>Page 2</a>"
elif [[ $@ =~ ^GET\ /page1[\ \/\#?] ]]; then
httpReply 200 "<h2>Page 1</h2><br><a href='/'>Root</a><br><a href='/page2'>Page 2</a>"
elif [[ $@ =~ ^GET\ /page2[\ \/\#?] ]]; then
httpReply 200 "<h2>Page 2</h2><br><a href='/'>Root</a><br><a href='/page1'>Page 1</a>"
else
httpReply 404 "<h2>PAGE NOT FOUND<h2>"
fi
}
httpServer() {
requestHead=`while read line && [ " " "<" "$line" ] ; do echo "$line" ; done`
httpRequest="`echo \"$requestHead\" | head -n 1`"
echo "[`date '+%Y-%m-%d %H:%M:%S'`] $httpRequest"
httpDispatcher $httpRequest > $1
}
if [ ${1:-0} -gt 0 ]; then
mkfifo pipe 2>/dev/null
while true ; do cat pipe | nc -l -p $1 | httpServer pipe; done
rm -f pipe
echo Terminated
else
echo Usage webzer.sh port
fi