httpd_post_upload.txt 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. # Beware: bashism $'\r' is used to handle ^M
  23. file=/tmp/$$-$RANDOM
  24. # CGI output must start with at least empty line (or headers)
  25. printf '\r\n'
  26. IFS=$'\r'
  27. read -r delim_line
  28. IFS=''
  29. delim_line="${delim_line}--"$'\r'
  30. while read -r line; do
  31. test "$line" = '' && break
  32. test "$line" = $'\r' && break
  33. done
  34. # This will not work well for binary files: bash 3.2 is upset
  35. # by reading NUL bytes and loses chunks of data.
  36. # If you are not bothered by having junk appended to the uploaded file,
  37. # consider using simple "cat >file" instead of the entire
  38. # fragment below.
  39. while read -r line; do
  40. while test "$line" = $'\r'; do
  41. read -r line
  42. test "$line" = "$delim_line" && {
  43. # Aha! Empty line + delimiter! All done
  44. cat <<EOF
  45. <html>
  46. <body>
  47. File upload has been accepted
  48. EOF
  49. exit 0
  50. }
  51. # Empty line + NOT delimiter. Save empty line,
  52. # and go check next line
  53. printf "%s\n" $'\r' -vC >&3
  54. done
  55. # Not empty line - just save
  56. printf "%s\n" "$line" -vC >&3
  57. done 3>"$file"
  58. cat <<EOF
  59. <html>
  60. <body>
  61. File upload was not terminated with '$delim_line' - ??!
  62. EOF