forked from dzove855/Bash-web-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-server.busion
executable file
·308 lines (248 loc) · 8.87 KB
/
bash-server.busion
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/local/bin/bash
# https://github.com/dylanaraps/pure-bash-bible#decode-a-percent-encoded-string
urldecode() {
: "${1//+/ }"
printf '%b\n' "${_//%/\\x}"
}
parseHttpRequest(){
# Get information about the request
read -r REQUEST_METHOD REQUEST_PATH HTTP_VERSION
HTTP_VERSION="${HTTP_VERSION%%$'\r'}"
}
parseHttpHeaders(){
local line
# Split headers and put it inside HTTP_HEADERS, so it can be reused
while read -r line; do
line="${line%%$'\r'}"
[[ -z "$line" ]] && return
HTTP_HEADERS["${line%%:*}"]="${line#*:}"
done
}
parseGetData(){
local entry
# Split QUERY_STRING into an assoc, so it can be easy reused
IFS='?' read -r REQUEST_PATH get <<<"$REQUEST_PATH"
# Url decode get data
get="$(urldecode "$get")"
# Split html #
IFS='#' read -r REQUEST_PATH _ <<<"$REQUEST_PATH"
QUERY_STRING="$get"
IFS='&' read -ra data <<<"$get"
for entry in "${data[@]}"; do
GET["${entry%%=*}"]="${entry#*:}"
done
}
parsePostData(){
local entry
# Split POst data into an assoc if is a form, if not create a key raw
if [[ "${HTTP_HEADERS["Content-type"]}" == "application/x-www-form-urlencoded" ]]; then
IFS='&' read -rN "${HTTP_HEADERS["Content-Length"]}" -a data
for entry in "${data[@]}"; do
entry="${entry%%$'\r'}"
POST["${entry%%=*}"]="${entry#*:}"
done
else
read -rN "${HTTP_HEADERS["Content-Length"]}" data
POST["raw"]="${data%%$'\r'}"
fi
}
parseCookieData(){
local -a cookie
local entry key value
IFS=';' read -ra cookie <<<"${HTTP_HEADERS["Cookie"]}"
for entry in "${cookie[@]}"; do
IFS='=' read -r key value <<<"$entry"
COOKIE["${key# }"]="${value% }"
done
}
httpSendStatus(){
local -A status_code=(
[200]="200 OK"
[201]="201 Created"
[301]="301 Moved Permanently"
[302]="302 Found"
[400]="400 Bad Request"
[401]="401 Unauthorized"
[403]="403 Forbidden"
[404]="404 Not Found"
[405]="405 Method Not Allowed"
[500]="500 Internal Server Error"
)
HTTP_RESPONSE_HEADERS['status']="${status_code[${1:-200}]}"
}
buildHttpHeaders(){
# We will first send the status header and then all the other headers
printf '%s %s\n' "$HTTP_VERSION" "${HTTP_RESPONSE_HEADERS['status']}"
unset HTTP_RESPONSE_HEADERS['status']
for key in "${!HTTP_RESPONSE_HEADERS[@]}"; do
printf '%s: %s\n' "$key" "${HTTP_RESPONSE_HEADERS[$key]}"
done
}
buildResponse(){
# Every output will first be saved in a file and then printed to the output
# Like this we can build a clean output to the client
# build a default header
httpSendStatus 200
# get mime type
IFS=. read -r _ extension <<<"$REQUEST_PATH"
[[ -z "${MIME_TYPES["${extension:-html}"]}" ]] || HTTP_RESPONSE_HEADERS["Content-Type"]="${MIME_TYPES["${extension:-html}"]}"
"$run" >"$TMPDIR/output"
# get content-legth
PATH="" type -p "finfo" &>/dev/null && HTTP_RESPONSE_HEADERS["Content-Length"]="$(finfo -s $TMPDIR/output)"
# print output to logfile
(( LOGGING )) && logPrint
buildHttpHeaders
# From HTTP RFC 2616 send newline before body
printf "\n"
printf '%s\n' "$(<$TMPDIR/output)"
# remove tmpfile, this should be trapped...
# XXX: No needed anymore, since the clean will do the job for use
# rm "$tmpFile"
}
parseAndPrint(){
# We will alway reset all variables and build them again
local REQUEST_METHOD REQUEST_PATH HTTP_VERSION QUERY_STRING
local -A HTTP_HEADERS
local -A POST
local -A GET
local -A HTTP_RESPONSE_HEADERS
local -A COOKIE
# Now mktemp will write create files inside the temporary directory
local -r TMPDIR="$serverTmpDir"
# Parse Request
parseHttpRequest
# Create headers assoc
parseHttpHeaders
# Parse Get Data
parseGetData
# Parse cookie data
parseCookieData
# Parse post data only if length is > 0 and post is specified
# bash (( will not fail if var is not a number, it will just return 1, no need of int check
if [[ "$REQUEST_METHOD" == "POST" ]] && (( ${HTTP_HEADERS['Content-Length']} > 0 )); then
parsePostData
fi
buildResponse
}
serveHtml(){
if [[ ! -z "$DOCUMENT_ROOT" ]]; then
DOCUMENT_ROOT="${DOCUMENT_ROOT%/}"
# Don't allow going out of DOCUMENT_ROOT
case "$DOCUMENT_ROOT" in
*".."*|*"~"*)
httpSendStatus 404
printf '404 Page Not Found!\n'
return
;;
esac
[[ "$REQUEST_PATH" == "/" ]] && REQUEST_PATH="/index.html"
if [[ -f "$DOCUMENT_ROOT/${REQUEST_PATH#/}" ]]; then
printf '%s\n' "$(<$DOCUMENT_ROOT/${REQUEST_PATH#/})"
else
httpSendStatus 404
printf '404 Page Not Found!\n'
fi
else
httpSendStatus 404
printf '404 Page Not Found!\n'
fi
}
logPrint(){
local -A logformat
local output="${LOGFORMAT}"
logformat["%a"]="$RHOST"
logformat["%A"]="${BIND_ADDRESS}"
logformat["%b"]="${HTTP_RESPONSE_HEADERS["Content-Length"]}"
logformat["%m"]="$REQUEST_METHOD"
logformat["%q"]="$QUERY_STRING"
logformat["%t"]="$TIME_FORMATTED"
logformat["%s"]="${HTTP_RESPONSE_HEADERS['status']%% *}"
logformat["%T"]="$(( $(printf '%(%s)T' -1 ) - $TIME_SECONDS))"
logformat["%U"]="$REQUEST_PATH"
for key in "${!logformat[@]}"; do
output="${output//"$key"/"${logformat[$key]}"}"
done
printf '%s\n' "$output" >> "$LOGFILE"
}
# Busion source url https://raw.githubusercontent.com/dzove855/bash-useful-functions/main/function/_verbose
main(){
local -A MIME_TYPES
: "${BASH_LOADABLE_PATH:=/usr/lib/bash}"
: "${HTTP_PORT:=8080}"
: "${BIND_ADDRESS:=127.0.0.1}"
: "${MIME_TYPES_FILE:=./mime.types}"
: "${TMPDIR:=/tmp}"
: "${LOGFORMAT:="[%t] - %a %m %U %s %b %T"}"
: "${LOGFILE:=access.log}"
: "${LOGGING:=1}"
TMPDIR="${TMPDIR%/}"
! [[ ${BIND_ADDRESS} == "0.0.0.0" ]] && acceptArg="-b ${BIND_ADDRESS}"
if ! [[ -f "${BASH_LOADABLE_PATH%/}/accept" ]]; then
printf '%s\n' "Cannot load accept..."
exit 1
fi
[[ -f "$MIME_TYPES_FILE" ]] && \
while read -r types extension; do
read -a extensions <<<"$extension"
for ext in "${extensions[@]}"; do
MIME_TYPES["$ext"]="$types"
done
done <"$MIME_TYPES_FILE"
# Enable mktemp and rm as a builtin :D
# Don't fail if it doesn't exist
enable -f "${BASH_LOADABLE_PATH%/}/mktemp" mktemp &>/dev/null || true
enable -f "${BASH_LOADABLE_PATH%/}/rm" rm &>/dev/null || true
enable -f "${BASH_LOADABLE_PATH%/}/finfo" finfo &>/dev/null || true
enable -f "${BASH_LOADABLE_PATH%/}/accept" accept || {
printf '%s\n' "Could not load accept..."
exit 1
}
case "$1" in
serveHtml)
run="serveHtml"
;;
*)
# source the configuration file and check if runner is defined
[[ -z "$1" || ! -f "$1" ]] && {
printf '%s\n' "please provide a file to source as the first argument..."
exit 1
}
# source main file
source "$1"
type runner &>/dev/null || {
printf '%s\n' "The source file need a function nammed runner which will be executed on each request..."
exit 1
}
run="runner"
;;
esac
while :; do
# create temporary directory for each request
_verbose 1 "Listening on $BIND_ADDRESS port $HTTP_PORT"
serverTmpDir="$(mktemp -d)"
# Create the file, but do not zrite inside
: > "$serverTmpDir/spawnNewProcess"
(
# XXX: Accept puts the connection in a TIME_WAIT status.. :(
# Verifiy if bind_address is specified default to 127.0.0.1
# You should use the custom accept in order to use bind address and multiple connections
accept $acceptArg "${HTTP_PORT}" || {
printf '%s\n' "Could not listen on ${BIND_ADDRESS}:${HTTP_PORT}"
exit 1
}
printf '1' > "$serverTmpDir/spawnNewProcess"
printf -v TIME_FORMATTED '%(%d/%b/%Y:%H:%M:%S)T' -1
printf -v TIME_SECONDS '%(%s)T' -1
parseAndPrint <&${ACCEPT_FD} >&${ACCEPT_FD}
# XXX: This is needed to close the connection to the client
# XXX: Currently no other way found around it.. :(
exec {ACCEPT_FD}>&-
# remove the temporary directoru
rm -rf "$serverTmpDir"
) &
until [[ -s "$serverTmpDir/spawnNewProcess" || ! -f "$serverTmpDir/spawnNewProcess" ]]; do : ; done
# Since the patch, no need of sleep anymore
#sleep "${TIME_WAIT:-0}"
done
}
main "$@"