httpd_post_upload.txt 1.6 KB

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