summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2017-03-15 12:42:42 +0100
committertv <tv@krebsco.de>2017-03-15 12:42:42 +0100
commitc5165ad7774af1ead36f0b28446882c09bb0ec33 (patch)
treea3f6e2e356dba0b429de5955b9bfa7d67c63e829
parent658a4ecfdf660274dbfa9eaaa80f2a97ee7917ab (diff)
parse HTTP headers with readv1.01.0
With sed it's not possible to continue reading the message body.
-rwxr-xr-xhtgen43
1 files changed, 23 insertions, 20 deletions
diff --git a/htgen b/htgen
index 659576f..2a45b10 100755
--- a/htgen
+++ b/htgen
@@ -66,26 +66,29 @@ cat>&2<<EOF
EOF
## }}}
## Parse HTTP-Headers. {{{
-HTTP_Headers="`sed -rn 's/ $//;/^$/q;p'`"
-
-## debug
-echo "$HTTP_Headers" | sed -r 's/.*/^&$/' >&2
-
-## Parse HTTP_Headers into Variables.
-eval "$(
-echo "$HTTP_Headers" | sed -r '
- ## TODO concatenate lines
- /^[^:]+-[^:]+:/s/^([a-zA-Z-]+):[[:space:]]*([^'\'']*)*$/\
- x=`echo \1 | tr - _`; \
- echo $x=\\'\'''\''\2'\''\\'\''\\;;\
- echo HTTP_Header_Variables=\\\${HTTP_Header_Variables+\\\$HTTP_Header_Variables:}$x\\;;\
- /;t
- s/^([a-zA-Z]+):[[:space:]]*([^'\'']*)*$/\
- echo \1=\\'\'''\''\2'\''\\'\''\\;;\
- echo HTTP_Header_Variables=\\\${HTTP_Header_Variables+\\\$HTTP_Header_Variables:}\1\\;;\
- /;t
- s/^.*/# &/
- '| sh)" >&2
+# TODO support https://tools.ietf.org/html/rfc7230#section-3.2.4, obs-fold?
+# TODO allow headers to appear multiple times
+k=
+v=
+while read -r line; do
+ line=${line% }
+ case $line in
+ '')
+ break
+ ;;
+ [A-Za-z][0-9A-Za-z-]*:*)
+ k=${line%%:*}
+ k=${k//-/_}
+ k=${k,,}
+ k=req_$k
+ v=${line#*: }
+ v=${v## }
+ printf '%s=%s\n' "$k" "$v" >&2
+ export "$k=$v"
+ ;;
+ esac
+done
+unset k v
echo >&2
## }}}