-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_file_server.sh
executable file
·87 lines (71 loc) · 2 KB
/
http_file_server.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
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
#!/bin/bash -e
#############################################################
# A lightweight HTTP server written in bash and based on ncat
# Author : David Gayerie
#
# This server distributes available files in the current
# directory. You can optionnally specify as parameter
# the ABSOLUTE path of the root server directory
# (without trailing slash).
# This serveur only supports the HTTP GET method
#
# This server is provided as-is without any warranty.
# DO NOT USE this script for production environment.
#############################################################
# Server port
HTTP_PORT=8080
function send_http_error ()
{
local STATUS_CODE=$1
local MESSAGE="$2"
echo "HTTP/1.1 $STATUS_CODE $MESSAGE"
echo "Content-type: text/plain"
echo "Content-length: ${#MESSAGE}"
echo
echo -n "$MESSAGE"
exit 1
}
function process_request ()
{
DOCUMENT_DIR=$1
# Parsing incoming request
read STATUS_LINE
>&2 echo "$(date -uR) - $STATUS_LINE"
read -a REQUEST <<< "$STATUS_LINE"
RESOURCE=$(readlink -f "$DOCUMENT_DIR${REQUEST[1]%%\?*}")
# check errors
if [ ${REQUEST[0]} != "GET" ]
then
send_http_error 405 "Method ${REQUEST[0]} not allowed"
fi
if [[ "$RESOURCE" != $DOCUMENT_DIR\/* ]]
then
send_http_error 403 "File access forbidden"
fi
if [ ! -f "$RESOURCE" ]
then
send_http_error 404 "File ${REQUEST[1]} not found!"
fi
# Generating response
# FIX : the 'file' command does not return the expected MIME-type for css file
if [[ "$RESOURCE" == *.css ]]
then
MIME_TYPE="text/css"
else
MIME_TYPE="$(file -i $RESOURCE | cut -d : -f 2)"
fi
echo "HTTP/1.1 200 OK"
echo "Content-type:$MIME_TYPE"
echo "Content-length: $(stat -c%s $RESOURCE)"
echo "Date: $(date -uR)"
echo
cat $RESOURCE
}
if [ "$1" == "-daemon" ]
then
process_request "$2"
else
echo "Starting server on port $HTTP_PORT"
echo "Please open your browser at this URL : http://localhost:$HTTP_PORT/index.html"
ncat -k -l $HTTP_PORT --sh-exec "$0 -daemon ${1:-$(pwd)/web}"
fi