low-http-server
low-http-server copied to clipboard
Incorrect Status Code When Request is Piped
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
- Create a server using
low-http-server. - Set the
statusCodeusing thewriteHeadmethod. - Pipe a request to the server.
- Inspect the
statusCodein 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]