xeqi / peridot

a basic api for interacting with ring apps
145 stars 20 forks source link

content-length header value should always be be a string #25

Closed borkdude closed 8 years ago

borkdude commented 8 years ago

According to the Ring spec, the content-length header value should be a string: https://github.com/mmcgrana/ring/blob/master/SPEC#L83. This is not the case when using a file as one of the param values, to create a multi-part request:

See the snippet below. r1 is a request with a long content-length (not desired). r2 is a request with a string content-length header value (desired).

(def r1 (pr/build (str "/users/"
               (:id @atm_user)
               "/profile/picture")
          {:request-method :put
          :params {"picture" (io/file (io/resource "nikita.jpg"))}}
          nil nil nil))
;; r1:          
{:remote-addr "localhost",
 :headers {"content-type" "multipart/form-data; boundary=_3sqmDS6uGzN5iAB_-7_899kHFhdR7vwemO",
           "content-length" 218158,
           "host" "localhost"},
 :server-port 80,
 :content-length 218158,
 :content-type "multipart/form-data; boundary=_3sqmDS6uGzN5iAB_-7_899kHFhdR7vwemO",
 :uri "/users/owp5-6nz7-79yz-pb7t-76jn/profile/picture",
 :server-name "localhost",
 :query-string nil,
 :body #object[java.io.BufferedInputStream 0x545a5423 "java.io.BufferedInputStream@545a5423"],
 :scheme :http,
 :request-method :put}          

 (def mp (peridot.multipart/build {"picture" (io/file (io/resource "nikita.jpg"))}))
 (def r2 (pr/build (str "/users/"
               (:id @atm_user)
               "/profile/picture")
          (merge {:request-method :put
          } mp)
          nil nil nil))

;; r2:

{:remote-addr "localhost",
 :headers {"host" "localhost",
           "content-type" "multipart/form-data; boundary=ekGP_QoxWMYMMDtNS5xfCxrJK8e1If",
           "content-length" "218148"},
 :server-port 80,
 :content-length 218148,
 :content-type "multipart/form-data; boundary=ekGP_QoxWMYMMDtNS5xfCxrJK8e1If",
 :uri "/users/owp5-6nz7-79yz-pb7t-76jn/profile/picture",
 :server-name "localhost",
 :query-string nil,
 :body #object[java.io.BufferedInputStream 0x4513ceb8 "java.io.BufferedInputStream@4513ceb8"],
 :scheme :http,
 :request-method :put}
glenjamin commented 8 years ago

Good catch! I'm fairly stacked at the moment but will happily review / merge a PR