-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpservedir.sh
executable file
·54 lines (51 loc) · 1.39 KB
/
httpservedir.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
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
notfound="You are asking the wrong questions..."
notimpl="I'm Sorry Dave, I'm afraid I can't do that"
true
while [ $? -eq 0 ]; do
nc -v -v -l -p 80 -c <<EOD
read str;
case $str in
GET\ *)
filename=$(echo $str |\
sed 's/^[^ ]\+ \(.\+\) HTTP\/1.[01]/\1/; s/%/\\0/' )
if [ ! -e $dir/"$filename" || "$filename" = *..* ]; then
response="404 Not Found"
type="application/xhtml+xml"
content="<pre>$(cowsay "$notfound")</pre>"
else if [ -d $dir/"$filename" ]; then
response="200 OK"
content="$(ls |\
awk '{print \"<a href=\\\"\" \$0 \"\\\">\" \$0 \"</a>\"}')"
type="application/xhtml+xml"
fi
;;
*)
response="501 Not Implemented"
type="application/xhtml+xml"
content="<pre>$(cowsay "$notimpl")</pre>"
esac
if [ -z "$content" ]; then
response="200 OK"
type=$(file -bi "$filename")
content=$(cat "$filename")
else
content=<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><title>$title</title></head>
<body>$content</body></html>
EOF
fi
while read str; do
if [ \${#str} -le 1 ]; then
break;
fi;
done
echo -e \
"HTTP/1.1 $response\r
Date: \$(date)\r
Server: netcat $(nc -h 2>&1 | head -1)\r
Content-Type: $type\r
\r
$content"
EOD