3 [ "$include_multipart" ] && return 0
6 # Copyright 2022 - 2023 Paul Hänsch
8 # Permission to use, copy, modify, and/or distribute this software for any
9 # purpose with or without fee is hereby granted, provided that the above
10 # copyright notice and this permission notice appear in all copies.
12 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
15 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
18 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 if [ "${CONTENT_TYPE}" -a ! "${CONTENT_TYPE##multipart/form-data;*}" ]; then
21 multipart_boundary="${CONTENT_TYPE#*; boundary=}"
22 multipart_boundary="${multipart_boundary%%;*}"
23 multipart_boundary="${multipart_boundary%${CR}}"
25 multipart_cachefile="/tmp/multipart.$$"
28 # read n bytes, like `head -c` but do not consume input
31 for block in 65536 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1; do
32 if [ $size -ge $block ]; then
33 dd status=none bs="$block" count="$((size / block))"
34 size="$((size % block))"
40 multipart_cachefile="${1:-${multipart_cachefile}}" # global
42 if [ "${multipart_boundary}" ]; then
43 # readbytes "$(( CONTENT_LENGTH ))" >"${multipart_cachefile}"
44 head -c "$(( CONTENT_LENGTH ))" >"${multipart_cachefile}"
51 local name="$1" count="${2:-1}"
52 local formdata state=begin
54 while IFS='' read -r formdata; do case "$formdata" in
55 "--${multipart_boundary}--${CR}")
56 [ $state = data ] && return 0 \
59 "--${multipart_boundary}${CR}")
60 [ $state = data ] && return 0 \
63 "Content-Disposition: form-data; name=\"${name}\""*"${CR}")
64 [ $state = header -a $count -eq 1 ] && state=dheader
65 [ $state = header -a $count -gt 1 ] && count=$((count - 1))
66 [ $state = data ] && printf "%s\n" "$formdata"
69 if [ $state = dheader ]; then
70 # Do not use `sed -n` (or busybox sed will "convert" NULL to LF)
71 sed "/--${multipart_boundary}\(--\)\?${CR}/{x;q;}" \
75 [ $state = header ] && state=junk
77 esac; done <"${multipart_cachefile}"
81 local name="$1" count="${2:-1}"
82 local formdata state=begin
84 while read -r formdata; do case "$formdata" in
85 "--${multipart_boundary}--${CR}")
88 "--${multipart_boundary}${CR}")
91 "Content-Disposition: form-data; name=\"${name}\"; filename=\""*"\""*"${CR}")
92 [ $state = header -a $count -eq 1 ] && break
93 [ $state = header -a $count -gt 1 ] && count=$((count - 1))
96 [ $state = header ] && state=junk
98 esac; done <"${multipart_cachefile}"
100 filename="${formdata#*; filename=\"}"
101 filename="${filename%%\"${CR}}"
102 filename="${filename%%\";*}"
104 HEX_DECODE % "$filename"