Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support HEAD and add content-length #326

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions cf/latest-snapshot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ async function get_latest(req_headers: Headers, env: Env, chain: string): Promis

const r = range[0];
headers.set('Content-Range', `${r.type} ${r.start}-${r.end}/${object.size}`);
headers.set('Content-Length', `${r.end - r.start + 1}`);
status = 206; // Partial Content
} else {
headers.set('Content-Length', object.size.toString());
}

object.writeHttpMetadata(headers);
Expand Down Expand Up @@ -78,16 +81,19 @@ export default {
const url = new URL(request.url);
const chain = (url.pathname.match(/\/latest\/(\w*)/) || ['undefined'])[1];

// Disallow any other request method except GET, they are not sensible in the context
// Disallow any other request method except HEAD and GET, they are not sensible in the context
// of fetching a snapshot.
if (request.method !== 'GET') {
return new Response('Method not allowed', {
status: 405,
headers: {
Allow: 'GET',
},
});
switch (request.method) {
case 'HEAD':
case 'GET':
return await get_latest(request.headers, env, chain);
default:
return new Response('Method not allowed', {
status: 405,
headers: {
Allow: 'GET, HEAD',
},
});
}
return await get_latest(request.headers, env, chain);
},
};