From 2afe9b76c613f3edf74ebd2156e1ae78ac971960 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Thu, 14 Apr 2022 16:22:26 +0200 Subject: [PATCH] filename function --- multipart.sh | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/multipart.sh b/multipart.sh index 09b1765..70270f6 100644 --- a/multipart.sh +++ b/multipart.sh @@ -34,7 +34,7 @@ multipart_cache() { } multipart(){ - local name="$1" + local name="$1" count="${2:-1}" local formdata state=begin while read -r formdata; do case "$formdata" in @@ -47,7 +47,8 @@ multipart(){ || state=header ;; "Content-Disposition: form-data; name=\"${name}\""*"${CR}") - [ $state = header ] && state=dheader + [ $state = header -a $count -eq 1 ] && state=dheader + [ $state = header -a $count -gt 1 ] && count=$((count - 1)) [ $state = data ] && printf "%s\n" "$formdata" ;; "${CR}") @@ -59,3 +60,32 @@ multipart(){ ;; esac; done <"${multipart_cachefile}" } + +multipart_filename(){ + local name="$1" count="${2:-1}" + local formdata state=begin + + while read -r formdata; do case "$formdata" in + "--${multipart_boundary}--${CR}") + return 1 + ;; + "--${multipart_boundary}${CR}") + state=header + ;; + "Content-Disposition: form-data; name=\"${name}\"; filename=\""*"\""*"${CR}") + [ $state = header -a $count -eq 1 ] && break + [ $state = header -a $count -gt 1 ] && count=$((count - 1)) + ;; + "${CR}") + [ $state = header ] && state=junk + ;; + *): + ;; + esac; done <"${multipart_cachefile}" + + filename="${formdata#*; filename=\"}" + filename="${filename%%\"${CR}}" + filename="${filename%%\";*}" + + HEX_DECODE % "$filename" +} -- 2.39.2