Skip to content

http: allow Content-Length header for 304 responses

Rodrigo Muino Tomonari requested to merge github/fork/BlackYoup/issue-31037 into master

According to the HTTP spec, a 304 Not Modified response can have a Content-Length header:

A server MAY send a Content-Length header field in a 304 (Not Modified) response to a conditional GET request (Section 4.1 of [RFC7232]); a server MUST NOT send Content-Length in such a response unless its field-value equals the decimal number of octets that would have been sent in the payload body of a 200 (OK) response to the same request.

On the other hand, the spec doesn't allow a 304 response to have a body: A 304 response cannot contain a message-body; it is always terminated by the first empty line after the header fields.

Currently, the http request module will emit an aborted event if the response has a status code of 304 with a Content-Length header. This pull request fixes this behavior and the request can successfully finish.

This fixes issue https://github.com/nodejs/node/issues/31037

I tried to integrate the fix in the best place I could find, let me know if there are better ways to fix this!

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Merge request reports

Loading