yawaramin / re-web

Experimental web framework for ReasonML & OCaml
https://yawaramin.github.io/re-web/re-web/index.html
MIT License
261 stars 8 forks source link

multipart_form filter fails for some files #38

Closed eWert-Online closed 3 years ago

eWert-Online commented 3 years ago

I am currently trying to write a file-upload with re-web using the provided multipart_form Filter (https://github.com/yawaramin/re-web/blob/main/ReWeb/Filter.ml#L237).

But some files are failing with the message Invalid_argument("parse_header"). Then only fragments of the complete file get written to disk.

As mentioned on Discord it may be caused by this issue: https://github.com/cryptosense/multipart-form-data/issues/17

eWert-Online commented 3 years ago

I did some more research on this and I don't think the error originates from multipart-form-data. It seems like the requests body isn't parsed correctly.

With the following code, for minified files I get the complete body back. If the files are not minified, I only get some fragments back. The beginning of the requests body (including the headers) is always missing which does explain the error message.

let* body = request |> ReWeb.Request.body |> Piaf.Body.to_string;

switch (body) {
  | Ok(body) => body |> ReWeb.Response.of_text |> Lwt.return;
  | Error(_e) => `Bad_request |> Response.of_status |> Lwt.return;
};

Edit: It has to be something in re-web. I rewrote the exact same code with @reason-native-web/morph and there I am getting the complete body for both files.

Response of a minified file ``` ------WebKitFormBoundaryUOMidP9367vTDcqw Content-Disposition: form-data; name="file"; filename="workspace.svg" Content-Type: image/svg+xml ------WebKitFormBoundaryUOMidP9367vTDcqw-- ```
Response of a non minified file ``` 2.3,1-18.5,1.3c-12.4,0.6-24.8,0.7-37.2-0.3 c-12.4-0.9-24.9-2.6-37.2-5.7c-6.2-1.5-12.3-3.5-18.3-5.9s-11.9-5.3-17.5-8.9c-5.5-3.6-10.8-7.9-15.3-13.2 c-4.5-5.2-8-11.4-10.1-17.9l12.2-4.4c4,9.5,11.9,16.9,21.3,22.2c9.5,5.2,20.3,8.7,31.5,10.8c11.2,2.2,22.7,3.3,34.3,3.6 c11.6,0.3,23.3-0.1,35-1s23.3-2.5,34.9-4.4c11.6-1.9,23.2-4.2,34.6-7c5.7-1.4,11.4-2.8,17.1-4.4c5.7-1.5,11.4-3.1,17-4.8 c11.3-3.4,22.5-7.2,33.6-11.3c11.1-4.1,22.1-8.5,32.9-13.2s21.5-9.7,32-15s20.9-10.9,31-16.9s20-12.4,29.5-19.1 c9.6-6.8,18.8-13.9,27.4-21.7c8.7-7.7,16.9-15.9,24-24.7c7.2-8.8,13.3-18.3,17.3-28.4c3.9-10.1,5.2-20.7,2.2-30.6L487,91.1z"/> ------WebKitFormBoundarySu5J6WEyT9g0ugMd-- 3c-9,8.6-18.6,16.6-28.4,24.1 c-39.6,29.8-84,51.9-130,68.8c-11.5,4.2-23.2,8-35,11.5c-5.9,1.7-11.8,3.4-17.7,4.9c-6,1.5-11.9,2.9-17.9,4.3 c-12,2.6-24.1,4.8-36.3,6.6c-6.1,0.9-12.2,1.6-18.4,2.2c-6.1,0.6-12.3,1-18.5,1.3c-12.4,0.6-24.8,0.7-37.2-0.3 c-12.4-0.9-24.9-2.6-37.2-5.7c-6.2-1.5-12.3-3.5-18.3-5.9s-11.9-5.3-17.5-8.9c-5.5-3.6-10.8-7.9-15.3-13.2 c-4.5-5.2-8-11.4-10.1-17.9l12.2-4.4c4,9.5,11.9,16.9,21.3,22.2c9.5,5.2,20.3,8.7,31.5,10.8c11.2,2.2,22.7,3.3,34.3,3.6 c11.6,0.3,23.3-0.1,35-1s23.3-2.5,34.9-4.4c11.6-1.9,23.2-4.2,34.6-7c5.7-1.4,11.4-2.8,17.1-4.4c5.7-1.5,11.4-3.1,17-4.8 c11.3-3.4,22.5-7.2,33.6-11.3c11.1-4.1,22.1-8.5,32.9-13.2s21.5-9.7,32-15s20.9-10.9,31-16.9s20-12.4,29.5-19.1 c9.6-6.8,18.8-13.9,27.4-21.7c8.7-7.7,16.9-15.9,24-24.7c7.2-8.8,13.3-18.3,17.3-28.4c3.9-10.1,5.2-20.7,2.2-30.6L487,91.1z"/> ------WebKitFormBoundarySu5J6WEyT9g0ugMd-- ```
yawaramin commented 3 years ago

Thank you @eWert-Online , you were indeed correct. Seems this issue was introduced in #36. Should be fixed now if you want to try it out.