Skip to content

Commit

Permalink
Implement support for HEAD requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
karlpauls committed Feb 12, 2024
1 parent 3003f45 commit 1f393ea
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
46 changes: 46 additions & 0 deletions src/handlers/head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { getSource } from '../routes/source.js';
import getList from '../routes/list.js';
import { getProperties } from '../routes/properties.js';

function get404() {
return { status: 404, contentLength: 0 };
}

function getRobots() {
const body = 'User-agent: *\nDisallow: /';
return { contentLength: body.length, status: 200 };
}

export default async function headHandler({ env, daCtx, head }) {
const { path } = daCtx;

if (path.startsWith('/favicon.ico')) return get404();
if (path.startsWith('/robots.txt')) return getRobots();

if (path.startsWith('/source')) return getSource({ env, daCtx, head });
if (path.startsWith('/list')) {
const { body, contentType, status } = await getList({ env, daCtx });
return {
contentLength: body.length,
contentType,
status,
};
}
if (path.startsWith('/properties')) {
const { body, status, contentType } = getProperties();
return { status, contentType, contentLength: body.length };
}

return undefined;
}
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import getDaCtx from './utils/daCtx.js';
import daResp from './utils/daResp.js';

import headHandler from './handlers/head.js';
import getHandler from './handlers/get.js';
import postHandler from './handlers/post.js';
import deleteHandler from './handlers/delete.js';
Expand All @@ -26,6 +27,9 @@ export default {

let respObj;
switch (req.method) {
case 'HEAD':
respObj = await headHandler({ env, daCtx });
break;
case 'GET':
respObj = await getHandler({ env, daCtx });
break;
Expand Down
8 changes: 5 additions & 3 deletions src/storage/object/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import {
S3Client,
GetObjectCommand,
HeadObjectCommand,
} from '@aws-sdk/client-s3';

import getS3Config from '../utils/config.js';
Expand All @@ -21,21 +22,22 @@ function buildInput({ org, key }) {
return { Bucket, Key: key };
}

export default async function getObject(env, { org, key }) {
export default async function getObject(env, { org, key, head }) {
const config = getS3Config(env);
const client = new S3Client(config);

const input = buildInput({ org, key });
const command = new GetObjectCommand(input);
const command = head ? new HeadObjectCommand(input) : new GetObjectCommand(input);

try {
const resp = await client.send(command);
return {
body: resp.Body,
status: resp.$metadata.httpStatusCode,
contentType: resp.ContentType,
contentLength: resp.ContentLength,
};
} catch (e) {
return { body: '', status: 404 };
return { body: '', status: 404, contentLength: 0 };
}
}
12 changes: 10 additions & 2 deletions src/utils/daResp.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
export default function daResp({ status, body = '', contentType = 'application/json' }) {
export default function daResp({
status,
body = '',
contentType = 'application/json',
contentLength,
}) {
const headers = new Headers();
headers.append('Access-Control-Allow-Origin', '*');
headers.append('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
headers.append('Access-Control-Allow-Methods', 'HEAD, GET, PUT, POST, DELETE');
headers.append('Access-Control-Allow-Headers', '*');
headers.append('Content-Type', contentType);
if (contentLength) {
headers.append('Content-Length', contentLength);
}

return new Response(body, { status, headers });
}

0 comments on commit 1f393ea

Please sign in to comment.