aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-11-13 09:37:50 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2009-11-13 09:37:50 +0100
commite3600a042e80cf279cfcb7f4c5e5f236b02bba7a (patch)
treef98fca60ca13a2b045893c4261225d93bc53927e
parent5799248976c1227663c4c9b7758e64dee3d5185b (diff)
downloadbusybox-w32-e3600a042e80cf279cfcb7f4c5e5f236b02bba7a.tar.gz
busybox-w32-e3600a042e80cf279cfcb7f4c5e5f236b02bba7a.tar.bz2
busybox-w32-e3600a042e80cf279cfcb7f4c5e5f236b02bba7a.zip
httpd_post_upload.txt example: handle binary files too
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/httpd_post_upload.txt63
1 files changed, 26 insertions, 37 deletions
diff --git a/networking/httpd_post_upload.txt b/networking/httpd_post_upload.txt
index fd7fc2be4..9d504f484 100644
--- a/networking/httpd_post_upload.txt
+++ b/networking/httpd_post_upload.txt
@@ -24,53 +24,42 @@ post_upload.cgi
24# ^M <--------- extra empty line 24# ^M <--------- extra empty line
25# -----------------------------29995809218093749221856446032--^M 25# -----------------------------29995809218093749221856446032--^M
26 26
27# Beware: bashism $'\r' is used to handle ^M
28
29file=/tmp/$$-$RANDOM 27file=/tmp/$$-$RANDOM
30 28
29CR=`printf '\r'`
30
31# CGI output must start with at least empty line (or headers) 31# CGI output must start with at least empty line (or headers)
32printf '\r\n' 32printf '\r\n'
33 33
34IFS=$'\r' 34IFS="$CR"
35read -r delim_line 35read -r delim_line
36 36IFS=""
37IFS=''
38delim_line="${delim_line}--"$'\r'
39 37
40while read -r line; do 38while read -r line; do
41 test "$line" = '' && break 39 test x"$line" = x"" && break
42 test "$line" = $'\r' && break 40 test x"$line" = x"$CR" && break
43done 41done
44 42
45# This will not work well for binary files: bash 3.2 is upset 43cat >"$file"
46# by reading NUL bytes and loses chunks of data.
47# If you are not bothered by having junk appended to the uploaded file,
48# consider using simple "cat >file" instead of the entire
49# fragment below.
50 44
51while read -r line; do 45# We need to delete the tail of "\r\ndelim_line--\r\n"
46tail_len=$((${#delim_line} + 6))
52 47
53 while test "$line" = $'\r'; do 48# Get and check file size
54 read -r line 49filesize=`stat -c"%s" "$file"`
55 test "$line" = "$delim_line" && { 50test "$filesize" -lt "$tail_len" && exit 1
56 # Aha! Empty line + delimiter! All done
57 cat <<EOF
58<html>
59<body>
60File upload has been accepted
61EOF
62 exit 0
63 }
64 # Empty line + NOT delimiter. Save empty line,
65 # and go check next line
66 printf "%s\n" $'\r' >&3
67 done
68 # Not empty line - just save
69 printf "%s\n" "$line" >&3
70done 3>"$file"
71 51
72cat <<EOF 52# Check that tail is correct
73<html> 53dd if="$file" skip=$((filesize - tail_len)) bs=1 count=1000 >"$file.tail" 2>/dev/null
74<body> 54printf "\r\n%s--\r\n" "$delim_line" >"$file.tail.expected"
75File upload was not terminated with '$delim_line' - ??! 55if ! diff -q "$file.tail" "$file.tail.expected" >/dev/null; then
76EOF 56 printf "<html>\n<body>\nMalformed file upload"
57 exit 1
58fi
59rm "$file.tail"
60rm "$file.tail.expected"
61
62# Truncate the file
63dd of="$file" seek=$((filesize - tail_len)) bs=1 count=0 >/dev/null 2>/dev/null
64
65printf "<html>\n<body>\nFile upload has been accepted"