blob: e4ffd2bb5578901df39abde7efb3fc7b896abf87 [file] [log] [blame]
Denis Vlasenko250aa5b2008-04-17 12:35:09 +00001#!/bin/sh
2
Denys Vlasenko5d27cb32016-12-22 15:33:11 +01003# post_upload.htm example:
4# <html>
5# <body>
6# <form action=/cgi-bin/httpd_post_upload.cgi method=post enctype=multipart/form-data>
7# File to upload: <input type=file name=file1> <input type=submit>
8# </form>
9
Denis Vlasenko250aa5b2008-04-17 12:35:09 +000010# POST upload format:
11# -----------------------------29995809218093749221856446032^M
12# Content-Disposition: form-data; name="file1"; filename="..."^M
13# Content-Type: application/octet-stream^M
14# ^M <--------- headers end with empty line
15# file contents
16# file contents
17# file contents
18# ^M <--------- extra empty line
19# -----------------------------29995809218093749221856446032--^M
20
Denis Vlasenko250aa5b2008-04-17 12:35:09 +000021file=/tmp/$$-$RANDOM
22
Denys Vlasenkoe3600a02009-11-13 09:37:50 +010023CR=`printf '\r'`
24
Denis Vlasenko250aa5b2008-04-17 12:35:09 +000025# CGI output must start with at least empty line (or headers)
26printf '\r\n'
27
Denys Vlasenkoe3600a02009-11-13 09:37:50 +010028IFS="$CR"
Denis Vlasenko250aa5b2008-04-17 12:35:09 +000029read -r delim_line
Denys Vlasenkoe3600a02009-11-13 09:37:50 +010030IFS=""
Denis Vlasenko250aa5b2008-04-17 12:35:09 +000031
32while read -r line; do
Denys Vlasenkoe3600a02009-11-13 09:37:50 +010033 test x"$line" = x"" && break
34 test x"$line" = x"$CR" && break
Denis Vlasenko250aa5b2008-04-17 12:35:09 +000035done
36
Denys Vlasenkoe3600a02009-11-13 09:37:50 +010037cat >"$file"
Denis Vlasenko250aa5b2008-04-17 12:35:09 +000038
Denys Vlasenkoe3600a02009-11-13 09:37:50 +010039# We need to delete the tail of "\r\ndelim_line--\r\n"
40tail_len=$((${#delim_line} + 6))
Denis Vlasenko250aa5b2008-04-17 12:35:09 +000041
Denys Vlasenkoe3600a02009-11-13 09:37:50 +010042# Get and check file size
43filesize=`stat -c"%s" "$file"`
44test "$filesize" -lt "$tail_len" && exit 1
Denis Vlasenko250aa5b2008-04-17 12:35:09 +000045
Denys Vlasenkoe3600a02009-11-13 09:37:50 +010046# Check that tail is correct
47dd if="$file" skip=$((filesize - tail_len)) bs=1 count=1000 >"$file.tail" 2>/dev/null
48printf "\r\n%s--\r\n" "$delim_line" >"$file.tail.expected"
49if ! diff -q "$file.tail" "$file.tail.expected" >/dev/null; then
50 printf "<html>\n<body>\nMalformed file upload"
51 exit 1
52fi
53rm "$file.tail"
54rm "$file.tail.expected"
55
56# Truncate the file
57dd of="$file" seek=$((filesize - tail_len)) bs=1 count=0 >/dev/null 2>/dev/null
58
59printf "<html>\n<body>\nFile upload has been accepted"