summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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
## }}}