ocaml-cohttp icon indicating copy to clipboard operation
ocaml-cohttp copied to clipboard

cohttp-eio: fails to consume body on error

Open talex5 opened this issue 3 years ago • 0 comments

cohttp-eio does not ensure that the body has been fully consumed before trying to parse the next request.

Example test case with the current output (add to the server.md file in #887 to run it):

# run @@ fun () ->
  Eio_mock.Flow.on_read socket [
    `Return "POST /no-such-resource HTTP/1.1\r\n\
             Content-Length: 4\r\n\
             \r\n\
             Hi\r\n";
    `Return "GET / HTTP/1.1\r\n\r\n";
  ];;
+socket: read "POST /no-such-resource HTTP/1.1\r\n"
+             "Content-Length: 4\r\n"
+             "\r\n"
+             "Hi\r\n"
+socket: wrote "HTTP/1.1 404 Not Found\r\n"
+              "\r\n"
+              "HTTP/1.1 400 Bad Request\r\n"
+              "\r\n"
Exception: Failure "Expected ' ' but got '\\r'".

I would expect something like this:

# run @@ fun () ->
  Eio_mock.Flow.on_read socket [
    `Return "POST /no-such-resource HTTP/1.1\r\n\
             Content-Length: 4\r\n\
             \r\n\
             Hi\r\n";
    `Return "GET / HTTP/1.1\r\n\r\n";
  ];;
+socket: read "POST /no-such-resource HTTP/1.1\r\n"
+             "Content-Length: 4\r\n"
+             "\r\n"
+             "Hi\r\n"
+socket: wrote "HTTP/1.1 404 Not Found\r\n"
+              "content-length: 0\r\n"
+              "\r\n"
+socket: read "GET / HTTP/1.1\r\n"
+             "\r\n"
+socket: wrote "HTTP/1.1 200 OK\r\n"
+              "content-length: 4\r\n"
+              "content-type: text/plain; charset=UTF-8\r\n"
+              "\r\n"
+              "root"
- : unit = ()

(this also demonstrates the missing content-length header, as reported in #883)

/cc @bikallem

talex5 avatar Jul 07 '22 09:07 talex5