Description
Hey folks!
I've encountered a weird bug. Every now and then it happens that the underlying connection between the client and the server breaks (not related to node, but to the infrastructure) while collecting the body. The easiest way to detect this is by checking the content-length
header, as (as far as I know) there is no event we can listen to for detecting the broken connection while reading the body stream.
While testing the behavior of the client with the content-length
header but with a body with an incorrect length, I've noticed that there is a big difference in how this is handled based on the node version. Following you can find the snippet for reproducing the issue and the behavior I've encountered based on the node version, the presence of the http agent, and the content-length
header.
- Version: latest 10/12/14/15
- Platform: Mac OS
- Subsystem: http
What steps will reproduce the bug?
The code for reproducing the bug can be found in the following gist:
https://gist.github.com/delvedor/0d15a34259fd2d2a21055f6e82a67380#file-http-client-bug-js
How often does it reproduce? Is there a required condition?
Consistently based on the versions of Node.
What is the expected behavior?
I'm not really sure what should be the right behavior, but I would expect:
- If the
content-length
header is not respected, the client should throw an error. - The end event should be emitted in any case
What do you see instead?
The behavior changes between node versions.
You can find the order of the events in the same gist:
https://gist.github.com/delvedor/0d15a34259fd2d2a21055f6e82a67380#file-events-md