Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Status Code When Request is Piped #25

Open
nicosefer opened this issue Jun 9, 2023 · 0 comments
Open

Incorrect Status Code When Request is Piped #25

nicosefer opened this issue Jun 9, 2023 · 0 comments

Comments

@nicosefer
Copy link

nicosefer commented Jun 9, 2023

Issue Description

Current Behavior

The statusCode is not correctly set when the request is piped. The writeHead method sets the statusCode internally, but when the request is piped, the statusCode is not updated in the response.

Expected Behavior

The statusCode should be correctly set in the response when the request is piped, reflecting the value set using the writeHead method.

Steps to Reproduce

  1. Create a server using low-http-server.
  2. Set the statusCode using the writeHead method.
  3. Pipe a request to the server.
  4. Inspect the statusCode in the response.

Code Example

import cero from "0http";
import low from "low-http-server";
import http from "http";

const { router, server } = cero({
  server: low(),
  cacheSize: 0,
});

server.listen(3000, (socket) => {
  console.log(`server started`);
});

function handleRequest(
  req,
  res,
) {
  const { headers, method, originalUrl, body } = req;
  const host = headers.host;
  const forwardReq = http
    .request(
      {
        host: host,
        path: originalUrl,
        headers: headers,
        method: method,
      },
      (newRes) => {
        const headers = newRes.headers;
        res.writeHead(newRes.statusCode, headers);
        newRes.pipe(res);
      }
    )
  req.pipe(forwardReq);
}

router.delete("/*", handleRequest);

Solution Proposal

I have identified a potential solution to address the issue. In the HttpResponse class of the low-http-server library, within the writeAllHeaders method, I suggest making the following modification:

this.on('pipe', (_) => {
  if (this.finished) return
  this.res.writeStatus(`${this.statusCode.toString()} ${this.statusMessage}`)
  this.writeAllHeaders()
})

Additional Information

  • Library version: [4.1.0]
  • Node.js version: [16.20.0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant