-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttparse
More file actions
executable file
·32 lines (21 loc) · 877 Bytes
/
httparse
File metadata and controls
executable file
·32 lines (21 loc) · 877 Bytes
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
#!/usr/bin/env bash
# Description: http request parser. Output is json.
# Posix incompatibility: Using bash-specific features because it gives better "read" capabilities.
# Remarks:
# jq is used for pretty output only. If plain json is ok, then simply echo to stdout instead of tmp.
# TODO: parse query strings from path
read -rd $'\r\n' method path version && read
tmp="$(mktemp XXXXX)"
trap "rm -f ${tmp}" exit
echo "{" > "${tmp}"
echo -n "\"method\": \"${method//\"/\\\"}\"" >> "${tmp}"
echo -ne ",\n\"path\": \"${path//\"/\\\"}\"" >> "${tmp}"
echo -ne ",\n\"version\": \"${version//\"/\\\"}\"" >> "${tmp}"
while IFS=: read -rd $'\r\n' header value && read && [[ -n "${header}" ]];do
value="${value//\"/\\\"}"
echo -ne ",\n\"${header}\": \"${value#' '}\"" >> "${tmp}"
done
echo -e "\n}" >> "${tmp}"
jq '.' "${tmp}"
exit 0
### Next read should be from payload