-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp.sh
More file actions
executable file
·215 lines (188 loc) · 6.65 KB
/
http.sh
File metadata and controls
executable file
·215 lines (188 loc) · 6.65 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
#!/usr/bin/env bash
trap ctrl_c INT
ctrl_c() {
[[ $socket != '' ]] && rm $socket
pkill -P $$
echo -e "Cleaned up, exitting.\nHave an awesome day!!"
}
setup_config() {
[[ ! "$1" ]] && namespace=app || namespace="$1"
mkdir -p config
cp ".resources/primary_config.sh" "config/master.sh"
echo "cfg[namespace]=$namespace # default namespace" >> "config/master.sh"
echo "cfg[init_version]=$HTTPSH_VERSION" >> "config/master.sh"
}
if [[ "${0##*/}" == "http.sh" ]]; then
# make sure that working directory is http.sh root
cd "${0%/*}"
elif [[ ! -f "$PWD/http.sh" ]]; then
echo -e "Could not detect HTTP.sh directory\nPlease run HTTP.sh inside its designated directory"
exit 1
fi
source src/version.sh
if [[ "$1" == "init" ]]; then # will get replaced with proper parameter parsing in 1.0
[[ ! "$2" ]] && namespace=app || namespace="$2"
if [[ ! -f "config/master.sh" ]]; then
setup_config
elif [[ -d "$namespace" ]]; then
echo -e "ERR: HTTP.sh has been initialized before.\nSpecify a new namespace directory, or perish (remove '$namespace'?)"
exit 1
else
echo "WARN: HTTP.sh has been initialized before. Continuing w/o recreating config."
fi
source config/master.sh
mkdir -p "${cfg[namespace]}/${cfg[root]}" "${cfg[namespace]}/workers/example" "${cfg[namespace]}/views" "${cfg[namespace]}/templates" "${cfg[namespace]}/util/"
touch "${cfg[namespace]}/config.sh" "${cfg[namespace]}/workers/example/control"
cp ".resources/config.sh" "${cfg[namespace]}/config.sh"
cp ".resources/routes.sh" "${cfg[namespace]}/routes.sh"
cp .resources/example_worker/* "${cfg[namespace]}/workers/example/"
cp .resources/example_webroot/* "${cfg[namespace]}/${cfg[root]}/index.shs"
echo -e "Success..?\nTry running \`./http.sh\` now"
exit 0
elif [[ ! -f "config/master.sh" ]]; then
if [[ -d "app" ]]; then # if the de-facto default app dir already exists, copy the cfg
setup_config
else
echo "ERR: Initialize HTTP.sh first! run './http.sh init'"
exit 1
fi
fi
source config/master.sh
if [[ "$HTTPSH_VERSION" != "${cfg[init_version]}" ]]; then
echo "WARN: HTTP.sh was updated since this instance was initialized (config v${cfg[init_version]:-(none)}, runtime v$HTTPSH_VERSION). There may be breaking changes.
Check for breaking changes (announced on IRC and in \`git log\`),
then use this to ACK the message:
./http.sh bump"
fi
while read i; do
if ! which $i > /dev/null 2>&1; then
echo "ERROR: can't find $i"
error=true
fi
done < src/dependencies.required
while read i; do
which $i > /dev/null 2>&1
[[ $? != 0 ]] && echo "WARNING: can't find $i"
done < src/dependencies.optional
if ! which ncat > /dev/null 2>&1; then
if [[ ${cfg[socat_only]} != true ]]; then
echo "ERR: can't find ncat, and cfg[socat_only] is not set to true"
error=true
fi
fi
if [[ $error == true ]]; then
echo "Fix above dependencies, and I might just let you pass."
exit 1
fi
if [[ "$1" == 'shell' ]]; then
bash --rcfile <(echo '
shopt -s extglob
x() { declare -p data;} # for notORM
source config/master.sh
source src/account.sh
source src/mail.sh
source src/mime.sh
source src/misc.sh
source src/notORM.sh
source src/template.sh
source "${cfg[namespace]}/config.sh"
PS1="[HTTP.sh] \[\033[01;34m\]\w\[\033[00m\]\$ "')
exit 0
fi
if [[ "$1" == "utils" ]]; then # list all available utils
# would be cool to make this more generic
ls "${cfg[namespace]}/util/" "src/util"
exit 0
fi
for path in "${cfg[namespace]}/util/" "src/util/"; do
HTTPSH_SCRIPTNAME="$(basename "$1")"
if [[ -x "$path$HTTPSH_SCRIPTNAME.sh" ]]; then
shift
shopt -s extglob
source src/account.sh
source src/mail.sh
source src/mime.sh
source src/misc.sh
source src/notORM.sh
source src/template.sh
source "${cfg[namespace]}/config.sh"
source "$path$HTTPSH_SCRIPTNAME.sh"
exit 0
elif [[ -f "$path$HTTPSH_SCRIPTNAME.sh" ]]; then
echo "[WARN] util '$1' found, but not executable. Ignoring..." >&2
fi
done
unset path HTTPSH_SCRIPTNAME
cat <<EOF >&2
_ _ _______ _______ _____ ______ _ _
| | | |_______|_______| _ \/ ___/| | | |
| |__| | | | | | | |_| | |___ | |__| |
| |__| | | | | | | ___/\___ \ | |__| |
| | | | | | | | | | ___\ \| | | |
|_| |_| |_| |_| |_| □ /_____/|_| |_| v$HTTPSH_VERSION
EOF
if [[ "$1" == "debug" ]]; then
cfg[dbg]=true
echo "[DEBUG] Activated debug mode - stderr will be shown"
elif [[ "$1" == "debuggier" ]]; then
cfg[dbg]=true
cfg[debuggier]=true
export PS4=' ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
echo "[DEBUG] Activated debuggier mode - stderr and call trace will be shown"
set -x
fi
source src/worker.sh
if [[ -f "${cfg[namespace]}/config.sh" ]]; then
run_once=true
source "${cfg[namespace]}/config.sh"
unset run_once
fi
if [[ "${cfg[ip]}" == *":"* ]]; then
socat_listen="TCP6-LISTEN"
else
socat_listen="TCP-LISTEN"
fi
if [[ ${cfg[socat_only]} == true ]]; then
echo "[INFO] listening directly via socat, assuming no ncat available"
echo "[HTTP] listening on ${cfg[ip]}:${cfg[port]}"
if [[ ${cfg[dbg]} == true ]]; then
socat $socat_listen:${cfg[port]},bind=${cfg[ip]},fork "exec:bash -c \'src/server.sh ${cfg[debuggier]}\'"
else
socat $socat_listen:${cfg[port]},bind=${cfg[ip]},fork "exec:bash -c src/server.sh" 2>> /dev/null
if [[ $? != 0 ]]; then
echo "[WARN] socat quit with a non-zero status; Maybe the port is in use?"
fi
fi
else
if [[ ${cfg[http]} == true ]]; then
# this is a workaround because ncat kept messing up large (<150KB) files over HTTP - but not over HTTPS!
socket=$(mktemp -u /tmp/socket.XXXXXX)
if [[ ${cfg[dbg]} == true ]]; then
# ncat with the "timeout" (-i) option has a bug which forces it
# to quit after the first time-outed connection, ignoring the
# "broker" (-k) mode. This is a workaround for this.
while true; do
ncat -i 600s -l -U "$socket" -c "src/server.sh ${cfg[debuggier]}" -k
done &
else
while true; do
ncat -i 600s -l -U "$socket" -c src/server.sh -k 2>> /dev/null
done &
fi
socat $socat_listen:${cfg[port]},fork,bind=${cfg[ip]} UNIX-CLIENT:$socket &
echo "[HTTP] listening on ${cfg[ip]}:${cfg[port]} through '$socket'"
fi
if [[ ${cfg[ssl]} == true ]]; then
echo "[SSL] listening on port ${cfg[ip]}:${cfg[ssl_port]}"
if [[ ${cfg[dbg]} == true ]]; then
while true; do
ncat -i 600s -l ${cfg[ip]} ${cfg[ssl_port]} -c src/server.sh -k --ssl $([[ ${cfg[ssl_key]} != '' && ${cfg[ssl_cert]} != '' ]] && echo "--ssl-cert ${cfg[ssl_cert]} --ssl-key ${cfg[ssl_key]}")
done &
else
while true; do
ncat -i 600s -l ${cfg[ip]} ${cfg[ssl_port]} -c src/server.sh -k --ssl $([[ ${cfg[ssl_key]} != '' && ${cfg[ssl_cert]} != '' ]] && echo "--ssl-cert ${cfg[ssl_cert]} --ssl-key ${cfg[ssl_key]}") 2>> /dev/null
done &
fi
fi
fi
wait