diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-11-13 09:37:50 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-11-13 09:37:50 +0100 |
commit | e3600a042e80cf279cfcb7f4c5e5f236b02bba7a (patch) | |
tree | f98fca60ca13a2b045893c4261225d93bc53927e | |
parent | 5799248976c1227663c4c9b7758e64dee3d5185b (diff) | |
download | busybox-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.txt | 63 |
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 | |||
29 | file=/tmp/$$-$RANDOM | 27 | file=/tmp/$$-$RANDOM |
30 | 28 | ||
29 | CR=`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) |
32 | printf '\r\n' | 32 | printf '\r\n' |
33 | 33 | ||
34 | IFS=$'\r' | 34 | IFS="$CR" |
35 | read -r delim_line | 35 | read -r delim_line |
36 | 36 | IFS="" | |
37 | IFS='' | ||
38 | delim_line="${delim_line}--"$'\r' | ||
39 | 37 | ||
40 | while read -r line; do | 38 | while 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 |
43 | done | 41 | done |
44 | 42 | ||
45 | # This will not work well for binary files: bash 3.2 is upset | 43 | cat >"$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 | ||
51 | while read -r line; do | 45 | # We need to delete the tail of "\r\ndelim_line--\r\n" |
46 | tail_len=$((${#delim_line} + 6)) | ||
52 | 47 | ||
53 | while test "$line" = $'\r'; do | 48 | # Get and check file size |
54 | read -r line | 49 | filesize=`stat -c"%s" "$file"` |
55 | test "$line" = "$delim_line" && { | 50 | test "$filesize" -lt "$tail_len" && exit 1 |
56 | # Aha! Empty line + delimiter! All done | ||
57 | cat <<EOF | ||
58 | <html> | ||
59 | <body> | ||
60 | File upload has been accepted | ||
61 | EOF | ||
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 | ||
70 | done 3>"$file" | ||
71 | 51 | ||
72 | cat <<EOF | 52 | # Check that tail is correct |
73 | <html> | 53 | dd if="$file" skip=$((filesize - tail_len)) bs=1 count=1000 >"$file.tail" 2>/dev/null |
74 | <body> | 54 | printf "\r\n%s--\r\n" "$delim_line" >"$file.tail.expected" |
75 | File upload was not terminated with '$delim_line' - ??! | 55 | if ! diff -q "$file.tail" "$file.tail.expected" >/dev/null; then |
76 | EOF | 56 | printf "<html>\n<body>\nMalformed file upload" |
57 | exit 1 | ||
58 | fi | ||
59 | rm "$file.tail" | ||
60 | rm "$file.tail.expected" | ||
61 | |||
62 | # Truncate the file | ||
63 | dd of="$file" seek=$((filesize - tail_len)) bs=1 count=0 >/dev/null 2>/dev/null | ||
64 | |||
65 | printf "<html>\n<body>\nFile upload has been accepted" | ||