renatoathaydes / rawhttp

HTTP library to make it easy to deal with raw HTTP.
Apache License 2.0
201 stars 29 forks source link

Server response body doesn't match Content-Length, RawHTTP crashes #65

Closed kwrooijen closed 1 year ago

kwrooijen commented 1 year ago

Currently when a response returns less data than specified in Content-Length, RawHTTP will crash

https://github.com/renatoathaydes/rawhttp/blob/d51e4cedc22c1478ea186538b70e22ef2fdb1e53/rawhttp-core/src/main/java/rawhttp/core/body/BodyConsumer.java#L158-L159

In my case I'm not the owner of the server, I can't fix this flaw. So even though the server is technically incorrect, I still want to be able to handle it.

A possible solution would be to add a flag to indicate that I want to either consume Content-Length amount or less. If it is less than Content-Length then I want to pull as much as possible without crashing.

renatoathaydes commented 1 year ago

When the server does that, it's completely impossible to tell whether the server just gave the wrong value for the header, or if it crashed and failed to send the full response.

I can add an option to treat an incomplete body as "expected" but that feels very unreliable.

renatoathaydes commented 1 year ago

I added an option on RawHttpOptions to allow for this, see example in the test: https://github.com/renatoathaydes/rawhttp/commit/97d94ef8451e313478f9024acf240f823a8a1044#diff-406613a18b54c44acab140482b0332c4d4b0cf646f2465c6d4c818dd0bbd5f33R212