Skip to content

Commit 34708a1

Browse files
authored
Merge branch 'main' into enhancement/gemini-grounding
2 parents f3ca35f + a66c3a3 commit 34708a1

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

src/handlers/handlerUtils.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,34 @@ export function constructRequest(
4343
method: string,
4444
forwardHeaders: string[],
4545
requestHeaders: Record<string, string>,
46-
fn: endpointStrings
46+
fn: endpointStrings,
47+
c: Context
4748
) {
49+
let proxyHeaders: Record<string, string> = {};
50+
// Handle proxy headers
51+
if (fn === 'proxy') {
52+
const poweredByHeadersPattern = `x-${POWERED_BY}-`;
53+
const headersToAvoidForCloudflare = ['expect'];
54+
const headersToIgnore = [
55+
...(env(c).CUSTOM_HEADERS_TO_IGNORE ?? []),
56+
...headersToAvoidForCloudflare,
57+
];
58+
headersToIgnore.push('content-length');
59+
Object.keys(requestHeaders).forEach((key: string) => {
60+
if (
61+
!headersToIgnore.includes(key) &&
62+
!key.startsWith(poweredByHeadersPattern)
63+
) {
64+
proxyHeaders[key] = requestHeaders[key];
65+
}
66+
});
67+
// Remove brotli from accept-encoding because cloudflare has problems with it
68+
if (proxyHeaders['accept-encoding']?.includes('br'))
69+
proxyHeaders['accept-encoding'] = proxyHeaders[
70+
'accept-encoding'
71+
]?.replace('br', '');
72+
}
73+
4874
let baseHeaders: any = {
4975
'content-type': 'application/json',
5076
};
@@ -69,7 +95,7 @@ export function constructRequest(
6995
...baseHeaders,
7096
...headers,
7197
...forwardHeadersMap,
72-
...(fn === 'proxy' ? requestHeaders : {}),
98+
...(fn === 'proxy' && proxyHeaders),
7399
};
74100

75101
let fetchOptions: RequestInit = {
@@ -269,7 +295,8 @@ export async function tryPost(
269295
method,
270296
forwardHeaders,
271297
requestHeaders,
272-
fn
298+
fn,
299+
c
273300
);
274301

275302
const headerContentType = headers[HEADER_KEYS.CONTENT_TYPE];

src/handlers/proxyHandler.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { Context } from 'hono';
2-
import { CONTENT_TYPES, POWERED_BY } from '../globals';
2+
import { CONTENT_TYPES } from '../globals';
33
import {
44
constructConfigFromRequestHeaders,
55
tryTargetsRecursively,
66
} from './handlerUtils';
77
import { RouterError } from '../errors/RouterError';
8-
import { env } from 'hono/adapter';
98

109
async function getRequestData(request: Request, contentType: string) {
1110
let finalRequest: any;
@@ -24,34 +23,6 @@ async function getRequestData(request: Request, contentType: string) {
2423
return finalRequest;
2524
}
2625

27-
function headersToSend(
28-
headersObj: Record<string, string>,
29-
customHeadersToIgnore: Array<string>
30-
): Record<string, string> {
31-
let final: Record<string, string> = {};
32-
const poweredByHeadersPattern = `x-${POWERED_BY}-`;
33-
const headersToAvoidForCloudflare = ['expect'];
34-
const headersToAvoid = [
35-
...customHeadersToIgnore,
36-
...headersToAvoidForCloudflare,
37-
];
38-
headersToAvoid.push('content-length');
39-
Object.keys(headersObj).forEach((key: string) => {
40-
if (
41-
!headersToAvoid.includes(key) &&
42-
!key.startsWith(poweredByHeadersPattern)
43-
) {
44-
final[key] = headersObj[key];
45-
}
46-
});
47-
48-
// Remove brotli from accept-encoding because cloudflare has problems with it
49-
if (final['accept-encoding']?.includes('br'))
50-
final['accept-encoding'] = final['accept-encoding']?.replace('br', '');
51-
52-
return final;
53-
}
54-
5526
export async function proxyHandler(c: Context): Promise<Response> {
5627
try {
5728
let requestHeaders = Object.fromEntries(c.req.raw.headers);
@@ -65,7 +36,7 @@ export async function proxyHandler(c: Context): Promise<Response> {
6536
c,
6637
camelCaseConfig,
6738
request,
68-
headersToSend(requestHeaders, env(c).CUSTOM_HEADERS_TO_IGNORE ?? []),
39+
requestHeaders,
6940
'proxy',
7041
c.req.method,
7142
'config'

0 commit comments

Comments
 (0)