From c5165ad7774af1ead36f0b28446882c09bb0ec33 Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 15 Mar 2017 12:42:42 +0100 Subject: parse HTTP headers with read With sed it's not possible to continue reading the message body. --- htgen | 43 +++++++++++++++++++++++-------------------- 1 file 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<&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 ## }}} -- cgit v1.2.3