Skip to content

Commit de24917

Browse files
authored
fix(proxy): pass host for local targets (#946)
1 parent d4ea24b commit de24917

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

docs/2.utils/98.advanced.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Check request caching headers (`If-Modified-Since`) and add caching headers (Las
117117

118118
Make a fetch request with the event's context and headers.
119119

120-
### `getProxyRequestHeaders(event)`
120+
### `getProxyRequestHeaders(event, opts?: { host? })`
121121

122122
Get the request headers object without headers known to cause issues when proxying.
123123

src/utils/proxy.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export async function proxyRequest(
5858

5959
// Headers
6060
const fetchHeaders = mergeHeaders(
61-
getProxyRequestHeaders(event),
61+
getProxyRequestHeaders(event, { host: target.startsWith("/") }),
6262
opts.fetchOptions?.headers,
6363
opts.headers,
6464
);
@@ -174,11 +174,14 @@ export async function sendProxy(
174174
/**
175175
* Get the request headers object without headers known to cause issues when proxying.
176176
*/
177-
export function getProxyRequestHeaders(event: H3Event) {
177+
export function getProxyRequestHeaders(
178+
event: H3Event,
179+
opts?: { host?: boolean },
180+
) {
178181
const headers = Object.create(null);
179182
const reqHeaders = getRequestHeaders(event);
180183
for (const name in reqHeaders) {
181-
if (!ignoredHeaders.has(name)) {
184+
if (!ignoredHeaders.has(name) || (name === "host" && opts?.host)) {
182185
headers[name] = reqHeaders[name];
183186
}
184187
}
@@ -202,7 +205,9 @@ export function fetchWithEvent<
202205
...init,
203206
context: init?.context || event.context,
204207
headers: {
205-
...getProxyRequestHeaders(event),
208+
...getProxyRequestHeaders(event, {
209+
host: typeof req === "string" && req.startsWith("/"),
210+
}),
206211
...init?.headers,
207212
},
208213
});

0 commit comments

Comments
 (0)