Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit c2395cb

Browse files
committed
Set correct Host header with --upstream
1 parent 29ba2a0 commit c2395cb

File tree

4 files changed

+6
-5
lines changed

4 files changed

+6
-5
lines changed

packages/core/src/standards/event.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ export class ServiceWorkerGlobalScope extends WorkerGlobalScope {
405405
}
406406
}
407407

408-
request.headers.delete("host");
409408
// noinspection ES6MissingAwait
410409
const waitUntil = Promise.all(event[kWaitUntil]) as Promise<WaitUntil>;
411410
return withWaitUntil(await fetch(request[kInner]), waitUntil);

packages/http-server/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ export async function convertNodeRequest(
8787
req.headers["cf-ray"] ??= randomHex(16);
8888
req.headers["cf-visitor"] ??= `{"scheme":"${proto}"}`;
8989

90+
// Make sure we're using the correct host header (with custom upstream)
91+
req.headers["host"] = url.host;
92+
9093
// Build Headers object from request
9194
const headers = new Headers();
9295
for (const [name, values] of Object.entries(req.headers)) {

packages/http-server/test/index.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ function request(
102102
}
103103

104104
test("convertNodeRequest: uses request url, with upstream or host as base", async (t) => {
105-
let [, url] = await buildConvertNodeRequest(t, {
105+
// eslint-disable-next-line prefer-const
106+
let [request, url] = await buildConvertNodeRequest(t, {
106107
path: "/test",
107108
upstream: "http://upstream.com",
108109
});
110+
t.is(request.headers.get("host"), "upstream.com");
109111
t.is(url.toString(), "http://upstream.com/test");
110112

111113
[, url] = await buildConvertNodeRequest(t, { path: "/test" });

packages/web-sockets/src/fetch.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ export async function upgradingFetch(
1616
): Promise<Response> {
1717
const request = new Request(input, init);
1818

19-
// Cloudflare ignores request Host
20-
request.headers.delete("host");
21-
2219
// Handle web socket upgrades
2320
if (
2421
request.method === "GET" &&

0 commit comments

Comments
 (0)