Skip to content

Commit 18e4d0d

Browse files
pi0claude
andcommitted
fix(utils): preserve multiple consecutive slashes in request URL
`new URL("//path", base)` interprets double slashes as protocol-relative, collapsing the path. Use manual string splitting instead. Closes #80 Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent d3d7f39 commit 18e4d0d

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎src/_utils.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,12 @@ export function setupOutgoing(
106106
? (target as URL).pathname || (target as LegacyURL).path || ""
107107
: "";
108108

109-
const parsed = new URL(req.url!, "http://localhost");
110-
let outgoingPath = options.toProxy ? "/" + req.url : parsed.pathname + parsed.search || "";
109+
const reqUrl = req.url || "";
110+
const qIdx = reqUrl.indexOf("?");
111+
const reqPath = qIdx === -1 ? reqUrl : reqUrl.slice(0, qIdx);
112+
const reqSearch = qIdx === -1 ? "" : reqUrl.slice(qIdx);
113+
const normalizedPath = reqPath ? (reqPath[0] === "/" ? reqPath : "/" + reqPath) : "/";
114+
let outgoingPath = options.toProxy ? "/" + reqUrl : normalizedPath + reqSearch;
111115

112116
//
113117
// Remark: ignorePath will just straight up ignore whatever the request's

‎test/_utils.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,26 @@ describe("lib/http-proxy/common.js", () => {
251251
expect(outgoing.path).to.eql("/forward/static/path");
252252
});
253253

254+
it("should preserve multiple consecutive slashes in path (#80)", () => {
255+
const outgoing = {} as any;
256+
common.setupOutgoing(
257+
outgoing,
258+
{ target: "http://localhost:3004" },
259+
{ url: "//test" } as any,
260+
);
261+
expect(outgoing.path).to.eql("//test");
262+
});
263+
264+
it("should preserve multiple consecutive slashes with query string (#80)", () => {
265+
const outgoing = {} as any;
266+
common.setupOutgoing(
267+
outgoing,
268+
{ target: "http://localhost:3004" },
269+
{ url: "//test?foo=bar" } as any,
270+
);
271+
expect(outgoing.path).to.eql("//test?foo=bar");
272+
});
273+
254274
it("should not modify the query string", () => {
255275
const outgoing = {} as any;
256276
common.setupOutgoing(

0 commit comments

Comments
 (0)