1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(node/http): wrong req.url value (#25081)

This PR addresses a regression introduced in
https://github.com/denoland/deno/pull/25021 that would cause the
`req.url` parameter in Node's http server to always be a single
character instead of the expected value. The regression was caused by
effectively calling `.indexOf()` on an empty string and thus passing the
wrong index for slicing.

```js
"".indexOf("/") // -> -1
request.url.slice(-1) // effectively only giving us the last character
```

Fixes https://github.com/denoland/deno/issues/25080
This commit is contained in:
Marvin Hagemeister 2024-08-18 17:37:39 +02:00 committed by GitHub
parent 35a17f38f6
commit 558d2a098b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 1 deletions

View file

@ -1781,7 +1781,8 @@ export class ServerImpl extends EventEmitter {
});
const req = new IncomingMessageForServer(socket);
req.url = request.url?.slice(req.url.indexOf("/", 8));
// Slice off the origin so that we only have pathname + search
req.url = request.url?.slice(request.url.indexOf("/", 8));
req.method = request.method;
req.upgrade =
request.headers.get("connection")?.toLowerCase().includes("upgrade") &&

View file

@ -1539,3 +1539,21 @@ Deno.test("[node/http] ClientRequest PUT subarray", async () => {
await promise;
assertEquals(body, "world");
});
Deno.test("[node/http] req.url equals pathname + search", async () => {
const { promise, resolve } = Promise.withResolvers<void>();
const server = http.createServer((req, res) => res.end(req.url));
server.listen(async () => {
const { port } = server.address() as net.AddressInfo;
const res = await fetch(`http://localhost:${port}/foo/bar?baz=1`);
const text = await res.text();
assertEquals(text, "/foo/bar?baz=1");
server.close(() => {
resolve();
});
});
await promise;
});