Multipart response
cristovao-trevisan opened this issue · 4 comments
Hello,
I'm receiving a multipart-data response with an invalid Content-Lenght header, and thus using this package.
The problem I have is that the body_bytes is being set to NaN, which prevents the response from being read.
My current solution is to change this line:
var length = Math.min(this.end - this.offset, this.body_bytes);
To this:
var length = Math.min(this.end - this.offset, this.body_bytes || Infinity);
Don't know if that's the correct answer though, but tests are still passing.
I have a fork for this, and the time to create a PR :)
I don't think http-parser does anything specific in regards to multipart, I'm guessing that's parsed at a higher level. When you say "invalid Content-Length header", do you mean it's missing, the wrong number, or non-numeric?
A PR would be welcome, it would be great if you could provide a test, ideally just a raw response and expected results like one of these.
Will do so as soon as I can, also think on a better way to fix the issue
PS.: I have a non-numeric header, like: "1978,1978" (a bug on the server, which I have no control over)
Okay, given the code, that makes sense that body_bytes would end up with NaN. I think maybe change it at parse time, from:
currentContentLengthValue = +headers[i + 1];
to:
currentContentLengthValue = +headers[i + 1];
if (isNaN(currentContentLengthValue)) {
// does not parse as a valid number, ignore
continue;
}
Note that values with a comma means multiple headers, so:
Content-Length: 1978,1978
Is functionally identical to:
Content-Length: 1978
Content-Length: 1978
HTTP has some advice on how to handle this:
https://httpwg.org/specs/rfc7230.html#header.content-length
If a message is received that has multiple Content-Length header fields with field-values consisting of the same decimal value, or a single Content-Length header field with a field value containing a list of identical decimal values (e.g., "Content-Length: 42, 42"), indicating that duplicate Content-Length header fields have been generated or combined by an upstream message processor, then the recipient MUST either reject the message as invalid or replace the duplicated field-values with a single valid Content-Length field containing that decimal value prior to determining the message body length or forwarding the message.