Denis Vlasenko | 250aa5b | 2008-04-17 12:35:09 +0000 | [diff] [blame] | 1 | POST upload example: |
| 2 | |
| 3 | post_upload.htm |
| 4 | =============== |
| 5 | <html> |
| 6 | <body> |
| 7 | <form action=/cgi-bin/post_upload.cgi method=post enctype=multipart/form-data> |
| 8 | File to upload: <input type=file name=file1> <input type=submit> |
| 9 | </form> |
| 10 | |
| 11 | |
| 12 | post_upload.cgi |
| 13 | =============== |
| 14 | #!/bin/sh |
| 15 | |
| 16 | # POST upload format: |
| 17 | # -----------------------------29995809218093749221856446032^M |
| 18 | # Content-Disposition: form-data; name="file1"; filename="..."^M |
| 19 | # Content-Type: application/octet-stream^M |
| 20 | # ^M <--------- headers end with empty line |
| 21 | # file contents |
| 22 | # file contents |
| 23 | # file contents |
| 24 | # ^M <--------- extra empty line |
| 25 | # -----------------------------29995809218093749221856446032--^M |
| 26 | |
Denis Vlasenko | 250aa5b | 2008-04-17 12:35:09 +0000 | [diff] [blame] | 27 | file=/tmp/$$-$RANDOM |
| 28 | |
Denys Vlasenko | e3600a0 | 2009-11-13 09:37:50 +0100 | [diff] [blame] | 29 | CR=`printf '\r'` |
| 30 | |
Denis Vlasenko | 250aa5b | 2008-04-17 12:35:09 +0000 | [diff] [blame] | 31 | # CGI output must start with at least empty line (or headers) |
| 32 | printf '\r\n' |
| 33 | |
Denys Vlasenko | e3600a0 | 2009-11-13 09:37:50 +0100 | [diff] [blame] | 34 | IFS="$CR" |
Denis Vlasenko | 250aa5b | 2008-04-17 12:35:09 +0000 | [diff] [blame] | 35 | read -r delim_line |
Denys Vlasenko | e3600a0 | 2009-11-13 09:37:50 +0100 | [diff] [blame] | 36 | IFS="" |
Denis Vlasenko | 250aa5b | 2008-04-17 12:35:09 +0000 | [diff] [blame] | 37 | |
| 38 | while read -r line; do |
Denys Vlasenko | e3600a0 | 2009-11-13 09:37:50 +0100 | [diff] [blame] | 39 | test x"$line" = x"" && break |
| 40 | test x"$line" = x"$CR" && break |
Denis Vlasenko | 250aa5b | 2008-04-17 12:35:09 +0000 | [diff] [blame] | 41 | done |
| 42 | |
Denys Vlasenko | e3600a0 | 2009-11-13 09:37:50 +0100 | [diff] [blame] | 43 | cat >"$file" |
Denis Vlasenko | 250aa5b | 2008-04-17 12:35:09 +0000 | [diff] [blame] | 44 | |
Denys Vlasenko | e3600a0 | 2009-11-13 09:37:50 +0100 | [diff] [blame] | 45 | # We need to delete the tail of "\r\ndelim_line--\r\n" |
| 46 | tail_len=$((${#delim_line} + 6)) |
Denis Vlasenko | 250aa5b | 2008-04-17 12:35:09 +0000 | [diff] [blame] | 47 | |
Denys Vlasenko | e3600a0 | 2009-11-13 09:37:50 +0100 | [diff] [blame] | 48 | # Get and check file size |
| 49 | filesize=`stat -c"%s" "$file"` |
| 50 | test "$filesize" -lt "$tail_len" && exit 1 |
Denis Vlasenko | 250aa5b | 2008-04-17 12:35:09 +0000 | [diff] [blame] | 51 | |
Denys Vlasenko | e3600a0 | 2009-11-13 09:37:50 +0100 | [diff] [blame] | 52 | # Check that tail is correct |
| 53 | dd if="$file" skip=$((filesize - tail_len)) bs=1 count=1000 >"$file.tail" 2>/dev/null |
| 54 | printf "\r\n%s--\r\n" "$delim_line" >"$file.tail.expected" |
| 55 | if ! diff -q "$file.tail" "$file.tail.expected" >/dev/null; then |
| 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" |