Skip to content

Node.js abort the response if the body length do not match "Content-Length" during 304 #31037

@RomainLanz

Description

@RomainLanz

Hey 👋

  • Version: 13.X.X
  • Platform: Windows & macOS

Since the version 13 of Node.js, the response is aborted if the Content-Length isn't matched in the body when sending back a 304.

According to the HTTP spec, we have the right to provide a Content-Length header.

A server MAY send a Content-Length header field in a 304 (Not Modified) response to a conditional GET request

Reproduction:

"use strict";

const http = require("http");

const server = http.createServer((req, res) => {
  console.log("got request");
  res.setHeader("Content-Length", 11);
  res.statusCode = 304;
  res.end(null);
});

server.listen(3000, () => {
  console.log("listening");
  const request = http.request({
    hostname: "localhost",
    port: 3000,
    method: "GET",
    path: "/"
  });
  request.on("response", response => {
    console.log("response");
    response.on("aborted", () => {
      console.log("aborted");
    });
    response.on("close", () => {
      console.log("close");
    });
    response.on("end", () => {
      console.log("end");
    });
    response.on("data", console.log);
  });
  request.end("");
});

Logs in Node 12:

listening
got request
response
aborted
end
close

Logs in Node 13:

listening
got request
response
aborted
close

c/c @targos

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpIssues or PRs related to the http subsystem.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions