forked from avleen/bashttpd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashttpd.sh
More file actions
executable file
·69 lines (59 loc) · 1.54 KB
/
bashttpd.sh
File metadata and controls
executable file
·69 lines (59 loc) · 1.54 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
# A simple HTTP server written in bash.
# Avleen Vig, 2012-09-13
if [ "$(id -u)" = "0" ]; then
echo "Hold on, tiger! Don't run this as root, k?" 1>&2
exit 1
fi
DOCROOT=${DOCROOT:-/var/www/html}
REPLY_HEADERS=(
"Date: $(date --rfc-2822)"
"Expires: $(date --rfc-2822 --date='+5 hours')"
"Server: bashttpd"
)
function send_header {
echo "HTTP/1.0 $*"$'\r'
for x in "${REPLY_HEADERS[@]}"; do
echo "$x"$'\r'
done
echo $'\r'
}
function bad_request {
send_header 400 "Bad Request"
exit
}
IFS=' ' read HTTP_METHOD URL_PATH HTTP_VER || bad_request
[[ "x$HTTP_METHOD" == xGET && "x$URL_PATH" == x/* ]] || bad_request
URL_PATH="$DOCROOT$URL_PATH"
# If URL_PATH contains
# leading "." in a component,
# "%",
# or isn't set, return 400.
if [[ "$URL_PATH" == */.* || "$URL_PATH" == *%* || -z "${URL_PATH}" ]]; then
bad_request
fi
if [ -f ${URL_PATH} ]; then
# regular file
if [ ! -r ${URL_PATH} ]; then
# unreadable
send_header 403 Forbidden
else
# readable: send contents
REPLY_HEADERS+=("Content-type: $(file --brief --mime-type "$URL_PATH")")
REPLY_HEADERS+=("Content-length: $(stat -c%s "$URL_PATH")")
send_header 200 OK
cat "$URL_PATH"
fi
elif [ -d "${URL_PATH}" ]; then
# directory
if [ ! -x "${URL_PATH}" ]; then
# non-listable
send_header 403 Forbidden
else
REPLY_HEADERS+=("Content-type: text/plain")
send_header 200 OK
ls -l "$URL_PATH"
fi
else
send_header 404 "Not Found"
fi