Skip to content

Commit

Permalink
fix: api permacache validate content length header
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed May 19, 2022
1 parent 295489d commit b893863
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/api/src/perma-cache/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,33 @@ export async function permaCachePost(request, env, ctx) {

if (!r2Object) {
// Fetch Response from provided URL
const response = await getResponse(request, env, ctx, normalizedUrl)
let response = await getResponse(request, env, ctx, normalizedUrl)
if (!response.ok) {
throw new HTTPError(
'Failed to get response from provided URL',
response.status
)
}

// Content length is mandatory given R2 needs a known size of the readable stream
// TODO: We need public gateways to set content-length for JSON content
const contentLengthMb = response.headers.get('content-length')
if (
!contentLengthMb &&
response.headers.get('content-type') === 'application/json'
) {
const headers = response.headers
const content = await response.json()
response = new Response(content, {
headers: headers,
})
} else {
throw new HTTPError(
'Failed to get response with content length header',
400
)
}

// TODO: Validate headers per https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
// Store in R2 and add to Database if not existent
r2Object = await env.SUPERHOT.put(r2Key, response.body, {
Expand Down

0 comments on commit b893863

Please sign in to comment.