-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.zsh
executable file
·46 lines (41 loc) · 1.02 KB
/
http.zsh
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
#!/usr/bin/zsh
set -eu
source json.zsh
zmodload -i zsh/net/tcp
until ztcp -vl 8080; do
sleep 1
done
listener=$REPLY
source http_router.zsh
http_parser(){
client=$1
typeset -A headers
body=""
read -r method url http <&$client || ( ztcp -c $client && return )
headers[METHOD]=method
headers[HTTP]=http
url="${url//\/\///}"
while read -r input <&$client; do
input="${input//$'\r'/}"
if [[ ${#input} == 0 ]]; then
# if there's a content-length
# read exactly that much into body
if [[ "$method" == "POST" ]]; then
if [[ -n "${headers[Content-Length]}" ]]; then
read -rk "${headers[Content-Length]}" -u $client -t 2 body
if [[ ${#body} != ${headers[Content-Length]} ]]; then
echo "Tried to read in ${headers[Content-Length]} bytes but only read ${#body} bytes"
fi
fi
fi
http_router "$client" "$url" "$body" ${${(@qkv)headers}[*]}
break
fi
headers[${input%: *}]="${input#*: }"
done
ztcp -c $client
}
trap "{ztcp -c}" EXIT
while ztcp -a $listener; do
http_parser $REPLY
done