3
0

httpd_post_upload.cgi 1.5 KB

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