-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implicit accept header can be overridden by format query (#36)
* fix: implicit accept header can be overridden by format query * chore: some cleanup and optimizations * chore: forgot to add files * test: uncomment accept header in test * chore: apply suggestions from code review Co-authored-by: Alex Potsides <[email protected]> * chore: logger is always set, should not be optional --------- Co-authored-by: Alex Potsides <[email protected]>
- Loading branch information
1 parent
e00d41c
commit 75c0b75
Showing
5 changed files
with
144 additions
and
20 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
packages/verified-fetch/src/utils/get-resolved-accept-header.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { isExplicitAcceptHeader, isExplicitFormatQuery, isExplicitIpldAcceptRequest } from './is-accept-explicit.js' | ||
import { queryFormatToAcceptHeader } from './select-output-type.js' | ||
import type { ParsedUrlStringResults } from './parse-url-string.js' | ||
import type { ComponentLogger } from '@libp2p/interface' | ||
|
||
export interface ResolvedAcceptHeaderOptions { | ||
query?: ParsedUrlStringResults['query'] | ||
headers?: RequestInit['headers'] | ||
logger: ComponentLogger | ||
} | ||
|
||
export function getResolvedAcceptHeader ({ query, headers, logger }: ResolvedAcceptHeaderOptions): string | undefined { | ||
const log = logger.forComponent('helia:verified-fetch:get-resolved-accept-header') | ||
const requestHeaders = new Headers(headers) | ||
const incomingAcceptHeader = requestHeaders.get('accept') ?? undefined | ||
|
||
if (incomingAcceptHeader != null) { | ||
log('incoming accept header "%s"', incomingAcceptHeader) | ||
} | ||
|
||
if (!isExplicitIpldAcceptRequest({ query, headers: requestHeaders })) { | ||
log('no explicit IPLD content-type requested, returning incoming accept header %s', incomingAcceptHeader) | ||
return incomingAcceptHeader | ||
} | ||
|
||
const queryFormatMapping = queryFormatToAcceptHeader(query?.format) | ||
|
||
if (query?.format != null) { | ||
log('incoming query format "%s", mapped to %s', query.format, queryFormatMapping) | ||
} | ||
|
||
let acceptHeader = incomingAcceptHeader | ||
// if the incomingAcceptHeader is autogenerated by the requesting client (browser/curl/fetch/etc) then we may need to override it if query.format is specified | ||
if (!isExplicitAcceptHeader(requestHeaders) && isExplicitFormatQuery(query)) { | ||
log('accept header not recognized, but query format provided, setting accept header to %s', queryFormatMapping) | ||
acceptHeader = queryFormatMapping | ||
} | ||
|
||
log('resolved accept header to "%s"', acceptHeader) | ||
|
||
return acceptHeader | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { FORMAT_TO_MIME_TYPE } from './select-output-type.js' | ||
import type { ParsedUrlStringResults } from './parse-url-string.js' | ||
|
||
export interface IsAcceptExplicitOptions { | ||
query?: ParsedUrlStringResults['query'] | ||
headers: Headers | ||
} | ||
|
||
export function isExplicitAcceptHeader (headers: Headers): boolean { | ||
const incomingAcceptHeader = headers.get('accept') | ||
if (incomingAcceptHeader != null && Object.values(FORMAT_TO_MIME_TYPE).includes(incomingAcceptHeader)) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
export function isExplicitFormatQuery (query?: ParsedUrlStringResults['query']): boolean { | ||
const formatQuery = query?.format | ||
if (formatQuery != null && Object.keys(FORMAT_TO_MIME_TYPE).includes(formatQuery)) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
/** | ||
* The user can provide an explicit `accept` header in the request headers or a `format` query parameter in the URL. | ||
* If either of these are provided, this function returns true. | ||
*/ | ||
export function isExplicitIpldAcceptRequest ({ query, headers }: IsAcceptExplicitOptions): boolean { | ||
return isExplicitAcceptHeader(headers) || isExplicitFormatQuery(query) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters