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

fix: preserve double slashes in url #70

Merged
merged 7 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ProxyServerOptions } from "./types";
pi0 marked this conversation as resolved.
Show resolved Hide resolved

const upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i;

/**
Expand Down Expand Up @@ -100,7 +102,7 @@ export function setupOutgoing(outgoing, options, req, forward?) {
//
outgoingPath = options.ignorePath ? "" : outgoingPath;

outgoing.path = urlJoin(targetPath, outgoingPath);
outgoing.path = urlJoin(options, targetPath, outgoingPath);

if (options.changeOrigin) {
outgoing.headers.host =
Expand Down Expand Up @@ -176,7 +178,7 @@ export function hasEncryptedConnection(req) {
* @api private
*/

export function urlJoin(...args: string[]) {
export function urlJoin(options: ProxyServerOptions, ...args: string[]) {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
// We do not want to mess with the query string. All we want to touch is the path.
const lastIndex = args.length - 1;
const last = args[lastIndex];
Expand All @@ -188,13 +190,15 @@ export function urlJoin(...args: string[]) {
// Join all strings, but remove empty strings so we don't get extra slashes from
// joining e.g. ['', 'am']
//
let retStr = args.filter(Boolean).join("/");

// Avoid normalize url if option is set
if (options.normalizeUrl) {
retStr = retStr.replace(/\/+/g, "/");
}

const retSegs = [
args
.filter(Boolean)
.join("/")
.replace(/\/+/g, "/")
.replace("http:/", "http://")
.replace("https:/", "https://"),
retStr.replace("http:/", "http://").replace("https:/", "https://"),
];

// Only join the query string if it exists so we don't have trailing a '?'
Expand Down
1 change: 1 addition & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class ProxyServer extends EventEmitter {

this.options = options || {};
this.options.prependPath = options.prependPath !== false;
this.options.normalizeUrl = options.normalizeUrl !== false;
pi0 marked this conversation as resolved.
Show resolved Hide resolved

this.web = _createProxyFn("web", this);
this.ws = _createProxyFn("ws", this);
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ export interface ProxyServerOptions {
selfHandleResponse?: boolean;
/** Buffer */
buffer?: stream.Stream;
/** If set to true, the path will be normalized by removing double slashes. Default: true */
normalizeUrl?: boolean;
}
36 changes: 26 additions & 10 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, it, describe, beforeAll } from "vitest";
import { expect, it, describe } from "vitest";
import { listen, Listener } from "listhen";
import { $fetch } from "ofetch";
import { createProxyServer, ProxyServer } from "../src";
import { createProxyServer, ProxyServer, ProxyServerOptions } from "../src";

describe("httpxy", () => {
let mainListener: Listener;
Expand All @@ -11,7 +11,12 @@ describe("httpxy", () => {
let lastResolved: any;
let lastRejected: any;

beforeAll(async () => {
const maskResponse = (obj: any) => ({
...obj,
headers: { ...obj.headers, connection: "<>", host: "<>" },
});

const makeProxy = async (options: ProxyServerOptions) => {
mainListener = await listen((req, res) => {
res.end(
JSON.stringify({
Expand All @@ -22,7 +27,7 @@ describe("httpxy", () => {
);
});

proxy = createProxyServer({});
proxy = createProxyServer(options);

proxyListener = await listen(async (req, res) => {
lastResolved = false;
Expand All @@ -36,17 +41,13 @@ describe("httpxy", () => {
res.end("Proxy error: " + error.toString());
}
});
});
};

it("works", async () => {
await makeProxy({});
const mainResponse = await $fetch(mainListener.url + "base/test?foo");
const proxyResponse = await $fetch(proxyListener.url + "test?foo");

const maskResponse = (obj: any) => ({
...obj,
headers: { ...obj.headers, connection: "<>", host: "<>" },
});

expect(maskResponse(await mainResponse)).toMatchObject(
maskResponse(proxyResponse),
);
Expand All @@ -56,4 +57,19 @@ describe("httpxy", () => {
expect(lastResolved).toBe(true);
expect(lastRejected).toBe(undefined);
});

it("should avoid normalize url", async () => {
await makeProxy({ normalizeUrl: false });
const mainResponse = await $fetch(mainListener.url + "base//test?foo");
const proxyResponse = await $fetch(proxyListener.url + "test?foo");

expect(maskResponse(await mainResponse)).toMatchObject(
maskResponse(proxyResponse),
);

expect(proxyResponse.path).toBe("/base//test?foo");

expect(lastResolved).toBe(true);
expect(lastRejected).toBe(undefined);
});
});
Loading