forked from mateusza/shellscripthttpd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpd.sh
More file actions
executable file
·388 lines (330 loc) · 8.87 KB
/
httpd.sh
File metadata and controls
executable file
·388 lines (330 loc) · 8.87 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/bin/sh
# copyright (C) 2014 Mateusz Adamowski <mateusz at adamowski dot pl>
#
# This code is public domain. You are free to use it anywhere you want.
# Please drop a line once you find it useful.
#
# custom settings
TEMP_DIR="/tmp/ss"
SESSION_COOKIE_NAME="SessionId"
SESSION_LIFETIME="$(( 60 * 60 ))"
# initial settings
SERVER_SOFTWARE="shellscripthttpd"
SERVER_VERSION="0.4.4"
SERVER_PROTOCOL="HTTP/1.0"
CHARSET="UTF-8"
CONTENT_TYPE="text/html; charset=$CHARSET"
RESPONSE_HEADERS_FILE="$TEMP_DIR/http-response-headers.$$.txt"
RESPONSE_FILE="$TEMP_DIR/http-response.$$.txt"
REQUEST_HEADERS_FILE="$TEMP_DIR/http-request-headers.$$.txt"
REQUEST_BODY="$TEMP_DIR/http-request.$$.txt"
ROUTES_FILE="$TEMP_DIR/http-routes.$$.txt"
ERRORS_FILE="$TEMP_DIR/http-errors.$$.txt"
SERVER_NAME="$SERVER_SOFTWARE/$SERVER_VERSION"
# functions
response_name(){
[ "$1" = "200" ] && echo "OK"
[ "$1" = "404" ] && echo "Not found"
[ "$1" = "303" ] && echo "See other"
[ "$1" = "500" ] && echo "Internal Server Error"
}
add_header(){
echo "$1: $2" >> $RESPONSE_HEADERS_FILE
}
add_route(){
echo "$1 $2" >> $ROUTES_FILE
}
require_POST(){
if [ "$REQUEST_METHOD" != "POST" ]
then
CODE="500"
VIEW="ERROR500"
return 1
fi
}
require_XSRF(){
if [ "x$XSRF" != "x$( read_post_var XSRF )" ]
then
CODE="500"
VIEW="ERROR500"
return 1
fi
}
read_post_var(){
grep -o "\&$1=[^\&]\+" < $REQUEST_BODY | sed -e 's/.*=//' | urldecode
}
read_get_var(){
echo "&$QUERY_STRING&" | grep -o "\&$1=[^\&]\+" | sed -e 's/.*=//' | urldecode
}
cleanup(){
rm -f "$RESPONSE_HEADERS_FILE"
rm -f "$REQUEST_HEADERS_FILE"
rm -f "$ROUTES_FILE"
rm -f "$RESPONSE_FILE"
rm -f "$REQUEST_BODY"
rm -f "$ERRORS_FILE"
[ "$( head -c 1 /dev/urandom | hexdump -e '1/1 "%u"' )" -lt "26" ] && session_cleanup_stale_files
}
session_cleanup_stale_files(){
local currdate
local filedate
currdate=$( date +%s )
ls "$TEMP_DIR" | grep 'session-.*-.*\.txt' | while read f
do
filedate=$( date +%s -r "$TEMP_DIR/$f" )
if [ "$(( $currdate - $filedate > $SESSION_LIFETIME ))" -eq "1" ]
then
rm "$TEMP_DIR/$f"
fi
done
}
request(){
local hname
local hvalue
mkdir -p "$TEMP_DIR"
touch "$RESPONSE_HEADERS_FILE"
touch "$REQUEST_HEADERS_FILE"
touch "$ROUTES_FILE"
touch "$RESPONSE_FILE"
touch "$REQUEST_BODY"
read REQUEST_METHOD REQUEST_URI CLIENT_PROTOCOL
SCRIPT_NAME="$( echo "$REQUEST_URI" | grep -o '^[^\?]\+' )"
if echo "$REQUEST_URI" | grep '\?' > /dev/null
then
QUERY_STRING="$( echo "$REQUEST_URI" | sed -e 's/^.*?//' )"
else
QUERY_STRING=""
fi
while read -r line
do
line=$( echo $line | tr -d '\r' )
[ -z "$line" ] && break
echo "$line" >> $REQUEST_HEADERS_FILE
hname="$( echo $line | grep -o '^[-A-Za-z]\+' | tr a-z A-Z | sed -e 's/-/_/g' )"
hvalue="$( echo $line | sed -e 's/^.*: //' )"
eval "HTTP_$hname='$hvalue'"
done
if [ "x$HTTP_CONTENT_LENGTH" != "x" ]
then
echo -n '&' > $REQUEST_BODY
head -c $HTTP_CONTENT_LENGTH >> $REQUEST_BODY
echo -n '&' >> $REQUEST_BODY
fi
if [ "x$REQUEST_METHOD" != "x" ]
then
session_load_cookie
session_check_cookie
xsrf_init
fi
# add_header "X-Test" "$REQUEST_METHOD $REQUEST_URI $CLIENT_PROTOCOL"
}
session_load_cookie(){
SESSION_ID="$( echo "$HTTP_COOKIE" | grep -o "\b$SESSION_COOKIE_NAME=[^;]\+" | sed -e 's/^.\+=//' )"
}
session_check_cookie(){
if echo "$SESSION_ID" | grep "^[0-9a-f]\{32\}$" > /dev/null
then
true
else
session_gen_id
EXPIRES=$( date +"%a, %d %b %Y %H:%M:%S %Z" -d @$(( `date +%s` + $SESSION_LIFETIME )) )
add_header "Set-Cookie" "$SESSION_COOKIE_NAME=$SESSION_ID; Expires=$EXPIRES; HttpOnly; Path=/"
fi
}
session_gen_id(){
SESSION_ID=$( head -c 128 /dev/urandom | md5sum | cut -d " " -f 1 )
}
session_set_value(){
cat > "$TEMP_DIR/session-$SESSION_ID-${1}.txt"
}
session_unset_value(){
[ -e "$TEMP_DIR/session-$SESSION_ID-${1}.txt" ] && rm "$TEMP_DIR/session-$SESSION_ID-${1}.txt"
}
session_get_value(){
[ -e "$TEMP_DIR/session-$SESSION_ID-${1}.txt" ] && cat "$TEMP_DIR/session-$SESSION_ID-${1}.txt"
}
session_regenerate_id(){
local old_id
local newname
old_id="$SESSION_ID"
session_gen_id
for filename in $TEMP_DIR/session-$old_id-*.txt
do
newname=$( echo $filename | sed -e "s/$old_id/$SESSION_ID/" )
mv "$filename" "$newname"
done
add_header "Set-Cookie" "$SESSION_COOKIE_NAME=$SESSION_ID; HttpOnly; Path=/"
}
xsrf_init(){
XSRF="$( session_get_value XSRF )"
if [ "x$XSRF" = "x" ]
then
XSRF="$( head -c 32 /dev/urandom | md5sum | cut -d " " -f 1 )"
echo $XSRF | session_set_value XSRF
fi
}
response(){
[ "x$VIEW" = "x" ] || view_$VIEW > $RESPONSE_FILE
if echo "$CONTENT_TYPE" | grep "^text/" > /dev/null
then
CONTENT_BINARY="n"
else
CONTENT_BINARY="y"
fi
if [ "x$CONTENT_BINARY" = "xn" ]
then
add_header "Content-Length" "$( awk 'sub("$", "\r")' < $RESPONSE_FILE | wc -c )"
else
add_header "Content-Length" "$( cat $RESPONSE_FILE | wc -c )"
fi
echo "$SERVER_PROTOCOL $CODE $( response_name $CODE )"
cat "$RESPONSE_HEADERS_FILE"
echo ""
[ "$REQUEST_METHOD" = "HEAD" ] && return
if [ "x$CONTENT_BINARY" = "xn" ]
then
awk 'sub("$", "\r")' < "$RESPONSE_FILE"
else
cat < "$RESPONSE_FILE"
fi
}
route(){
while read route action
do
if echo "$SCRIPT_NAME" | grep -o "$route" > /dev/null
then
CODE=200
ACTION="$action"
break
fi
if echo "$SCRIPT_NAME/" | grep -o "$route" > /dev/null
then
redirect "$SCRIPT_NAME/"
break
fi
done < $ROUTES_FILE
VIEW=$ACTION
if [ "x$CODE" = "x" ]
then
CODE="404"
ACTION=ERROR404
VIEW=ERROR404
fi
action_$ACTION
add_header "Content-Type" "$CONTENT_TYPE"
}
urldecode(){
/usr/bin/printf "$(sed 's/+/ /g;s/%\(..\)/\\x\1/g;')"
}
prepare(){
DATE=$(date +"%a, %d %b %Y %H:%M:%S %Z")
add_header "Date" "$DATE"
add_header "Connection" "close"
add_header "Server" "$SERVER_VERSION"
}
_e(){
sed -e ' s_&_\&_g; s_<_\<_g; s_>_\>_g; s_"_\"_g; '"s_'_\'_g"
}
run(){
request
prepare
route 2> "$ERRORS_FILE"
handle_errors
response
cleanup
}
handle_errors(){
[ -s "$ERRORS_FILE" ] || return
VIEW="ERROR500"
CODE="500"
}
redirect(){
REDIRECTION_LOCATION="$1"
add_header "Location" "$REDIRECTION_LOCATION"
VIEW="REDIRECT"
CODE=303
}
template_server_signature(){
echo "<hr><p>$SERVER_NAME</p>"
}
view_ERROR500(){
echo "<title>Internal Server Error</title>"
echo "<h1>Internal Server Error</h1>"
echo "<h2>Error:</h2>"
echo "<pre style='padding: 10px; background-color: #eee;'>"
cat "$ERRORS_FILE" | _e
echo "</pre>"
echo "<h2>request headers & content</h2>"
echo "<pre style='padding: 10px; background-color: #eee;'>"
echo -n "$REQUEST_METHOD $REQUEST_URI $CLIENT_PROTOCOL" | _e
cat "$REQUEST_HEADERS_FILE" | _e
echo
cat "$REQUEST_BODY" | _e
echo "</pre>"
template_server_signature
}
view_ERROR404(){
echo "<title>404 Not found</title>"
echo "<h1>404 Not found</h1>"
template_server_signature
}
action_ERROR404(){
true
}
view_REDIRECT(){
echo "<h1>redirecting... <code>$REDIRECTION_LOCATION</code></h1>"
template_server_signature
}
#
# CUSTOM ACTIONS AND VIEWS
#
action_index(){
name="Mateusz"
githublink="https://github.com/mateusza/shellscripthttpd"
os=$( uname -a )
}
view_index(){
cat <<EOF
<!doctype html>
<html>
<head>
<title>Hello from $name</title>
<style>
body { background-color: #242; padding: 50px; font-size: 150%; color: #fff; font-family: monospace; text-shadow: #000 1px 1px 1px; text-align: center; }
pre { text-align: left; }
::selection { background-color: #4f4; color: #000; text-shadow: #242; }
a { color: #f90; }
</style>
<link rel="shortcut icon" href="/favicon.png">
</head>
<body>
<h1>Hello world!</h1>
<h2>This is front page of your <b>$SERVER_SOFTWARE</b> instance.</h2>
<p>You can browse the source code on <a href='$githublink'>GitHub</a>.</p>
<p><small>
Running on: <tt>$os</tt>
</small></p>
$( template_server_signature )
</body>
</html>
EOF
}
action_favicon_png(){
CONTENT_TYPE='image/png'
}
view_favicon_png(){
base64 -d <<EOF
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAgMAAABinRfyAAAADFBMVEXW8tsBBQAgRCH+//yFiTnV
AAAAAXRSTlMAQObYZgAAADBJREFUCNdjYFq1qoFBa9WqFQzav/avYFj9bX8WMvH/PwrxtT4LXYlW
2NIMiAEgowB3CiiQUFVI+AAAAABJRU5ErkJggg==
EOF
}
##
## ROUTES
##
add_route '^/$' 'index'
add_route '^/favicon\.png$' 'favicon_png'
##
## process the request
##
run