Skip to content

Commit

Permalink
fix: normalize accept-encoding header
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed Jan 23, 2025
1 parent 5b9ad99 commit a98a18d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,6 @@ dist

#vim
.vim

# custom
src/list.ts
4 changes: 0 additions & 4 deletions src/create-main-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
cloneHeaders,
cloneRequest,
denyHeaders,
filterUpstreamHeaders,
getBuildTarget,
isForbidden,
isJsResponse,
Expand Down Expand Up @@ -82,9 +81,7 @@ export function createMainHandler(
return async function requestHandler(
request: Request,
): Promise<Response> {
// Step: tracing
const rootSpan = otel.trace.getActiveSpan();
rootSpan?.addEvent(Deno.env.get('DENO_REGION') ?? '');
const originalUserAgent = request.headers.get('user-agent') ?? '';
const [buildTarget, upstreamUserAgent] = getBuildTarget(
originalUserAgent,
Expand Down Expand Up @@ -165,7 +162,6 @@ export function createMainHandler(
headers: cloneHeaders(
request.headers,
denyHeaders,
filterUpstreamHeaders,
(
pair: [string, string] | null,
) => (pair && pair[0] === 'user-agent'
Expand Down
16 changes: 16 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ if (config.CACHE) {
if (headerName === 'origin') {
return '*';
}
if (headerName === 'accept-encoding') {
// Weighted ("Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5"): leave it as-is
if (headerValue?.includes('=')) {
return headerValue;
}
if (headerValue?.includes('br')) {
return 'br';
}
if (headerValue?.includes('gzip')) {
return 'gzip';
}
if (headerValue?.includes('deflate')) {
return 'deflate';
}
return '';
}
return headerValue ?? '';
};
let persistenceFactory: CachePersistenceFactory | undefined;
Expand Down
16 changes: 5 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,20 @@ export const cloneHeaders = (

const denyHeadersList = [
'access-control-expose-headers',
'accept-encoding',
'accept-ranges',
'age',
'date',
'alt-svc',
'cf-cache-status',
'cf-ray',
'content-length',
'date',
'host',
'last-modified',
'nel',
'report-to',
'server',
'server-timing',
'via',
'x-amz-cf-id',
'x-amz-cf-pop',
Expand All @@ -93,16 +97,6 @@ export const denyHeaders = (
pair: [string, string] | null,
) => (pair !== null && denyHeadersList.includes(pair[0]) ? null : pair);

export const filterUpstreamHeaders = (
pair: [string, string] | null,
) => (pair !== null && ['accept-encoding'].includes(pair[0])
? [
pair[0],
pair[1].split(',').map((i) => i.trim()).filter((i) => i !== 'zstd')
.join(','),
] as [string, string]
: pair);

export const isJsResponse = (response: Response): boolean => {
return !!(response.headers.get('content-type')?.startsWith(
'application/javascript',
Expand Down

0 comments on commit a98a18d

Please sign in to comment.