Skip to content

Commit

Permalink
fix(proxy): pass host for local targets (#946)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jan 10, 2025
1 parent d4ea24b commit de24917
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/2.utils/98.advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Check request caching headers (`If-Modified-Since`) and add caching headers (Las

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

### `getProxyRequestHeaders(event)`
### `getProxyRequestHeaders(event, opts?: { host? })`

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

Expand Down
13 changes: 9 additions & 4 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function proxyRequest(

// Headers
const fetchHeaders = mergeHeaders(
getProxyRequestHeaders(event),
getProxyRequestHeaders(event, { host: target.startsWith("/") }),
opts.fetchOptions?.headers,
opts.headers,
);
Expand Down Expand Up @@ -174,11 +174,14 @@ export async function sendProxy(
/**
* Get the request headers object without headers known to cause issues when proxying.
*/
export function getProxyRequestHeaders(event: H3Event) {
export function getProxyRequestHeaders(
event: H3Event,
opts?: { host?: boolean },
) {
const headers = Object.create(null);
const reqHeaders = getRequestHeaders(event);
for (const name in reqHeaders) {
if (!ignoredHeaders.has(name)) {
if (!ignoredHeaders.has(name) || (name === "host" && opts?.host)) {
headers[name] = reqHeaders[name];
}
}
Expand All @@ -202,7 +205,9 @@ export function fetchWithEvent<
...init,
context: init?.context || event.context,
headers: {
...getProxyRequestHeaders(event),
...getProxyRequestHeaders(event, {
host: typeof req === "string" && req.startsWith("/"),
}),
...init?.headers,
},
});
Expand Down

0 comments on commit de24917

Please sign in to comment.