fukamachi / clack

Web server abstraction layer for Common Lisp
MIT License
1.05k stars 86 forks source link

Body-parameter streams from chunked multipart data are incomplete #129

Open wnortje opened 8 years ago

wnortje commented 8 years ago

I am trying to upload files as part of multipart/form-data with chunked encoding. The drakma request is:

 (http-request url :method :post :form-data t
               :parameters 
               `((:recipe . ,recipe)
                 (:content-hash . ,content-hash)
                 (:content-file . (,content-pathname 
                                   :filename ,content-name
                                   :content-type "application/octet-stream"))))

No matter what I do, I can not get the complete uploaded file from the stream obtained from (body-parameter). The stream always ends at the same position partway into the file content.

I think the problem is in this code in http-body.multipart

(loop with buffer = (make-array 1024 :element-type '(unsigned-byte 8))
                for read-bytes = (read-sequence buffer stream)
                do (funcall parser (subseq buffer 0 read-bytes))
                while (= read-bytes 1024))))))

I can get the complete file using (getf (clack.request:env *request*) :raw-body-buffer) but it is not a viable solution for big files because it loads the complete file into RAM.

For the same request (hunchentoot:post-parameter :content-file) returns a pathname to the saved file. This is what I need, but that call breaks Clack.

wnortje commented 8 years ago

I made a mistake above. The call (hunchentoot:post-parameter :content-file) can not be used inside a Clack handler because clack.handler.hunchentoot::handle-request reads the stream before (post-parameter) can get to it.

eudoxia0 commented 8 years ago

So, why does the upload stream get cut after the first couple kilobytes?