From: Paul Hänsch Date: Thu, 14 Apr 2022 14:22:26 +0000 (+0200) Subject: filename function X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=2afe9b76c613f3edf74ebd2156e1ae78ac971960;p=shellwiki filename function --- 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" +}