Skip to content

Commit

Permalink
refactor(http): use headers module in file-server module (#5642)
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua committed Aug 6, 2024
1 parent 889a63d commit d99e1f8
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { parseArgs } from "@std/cli/parse-args";
import denoConfig from "./deno.json" with { type: "json" };
import { format as formatBytes } from "@std/fmt/bytes";
import { getNetworkAddress } from "@std/net/get-network-address";
import { HEADER } from "./header.ts";

interface EntryInfo {
mode: string;
Expand Down Expand Up @@ -195,7 +196,7 @@ export async function serveFile(

// Set date header if access timestamp is available
if (fileInfo.atime) {
headers.set("date", fileInfo.atime.toUTCString());
headers.set(HEADER.Date, fileInfo.atime.toUTCString());
}

const etag = fileInfo.mtime
Expand All @@ -204,18 +205,18 @@ export async function serveFile(

// Set last modified header if last modification timestamp is available
if (fileInfo.mtime) {
headers.set("last-modified", fileInfo.mtime.toUTCString());
headers.set(HEADER.LastModified, fileInfo.mtime.toUTCString());
}
if (etag) {
headers.set("etag", etag);
headers.set(HEADER.ETag, etag);
}

if (etag || fileInfo.mtime) {
// If a `if-none-match` header is present and the value matches the tag or
// if a `if-modified-since` header is present and the value is bigger than
// the access timestamp value, then return 304
const ifNoneMatchValue = req.headers.get("if-none-match");
const ifModifiedSinceValue = req.headers.get("if-modified-since");
const ifNoneMatchValue = req.headers.get(HEADER.IfNoneMatch);
const ifModifiedSinceValue = req.headers.get(HEADER.IfModifiedSince);
if (
(!ifNoneMatch(ifNoneMatchValue, etag)) ||
(ifNoneMatchValue === null &&
Expand All @@ -236,12 +237,12 @@ export async function serveFile(
// Set mime-type using the file extension in filePath
const contentTypeValue = contentType(extname(filePath));
if (contentTypeValue) {
headers.set("content-type", contentTypeValue);
headers.set(HEADER.ContentType, contentTypeValue);
}

const fileSize = fileInfo.size;

const rangeValue = req.headers.get("range");
const rangeValue = req.headers.get(HEADER.Range);

// handle range request
// Note: Some clients add a Range header to all requests to limit the size of the response.
Expand All @@ -253,7 +254,7 @@ export async function serveFile(
// Returns 200 OK if parsing the range header fails
if (!parsed) {
// Set content length
headers.set("content-length", `${fileSize}`);
headers.set(HEADER.ContentLength, `${fileSize}`);

const file = await Deno.open(filePath);
const status = STATUS_CODE.OK;
Expand All @@ -271,7 +272,7 @@ export async function serveFile(
fileSize <= parsed.start
) {
// Set the "Content-range" header
headers.set("content-range", `bytes */${fileSize}`);
headers.set(HEADER.ContentRange, `bytes */${fileSize}`);

return createStandardResponse(
STATUS_CODE.RangeNotSatisfiable,
Expand All @@ -284,11 +285,11 @@ export async function serveFile(
const end = Math.min(parsed.end, fileSize - 1);

// Set the "Content-range" header
headers.set("content-range", `bytes ${start}-${end}/${fileSize}`);
headers.set(HEADER.ContentRange, `bytes ${start}-${end}/${fileSize}`);

// Set content length
const contentLength = end - start + 1;
headers.set("content-length", `${contentLength}`);
headers.set(HEADER.ContentLength, `${contentLength}`);

// Return 206 Partial Content
const file = await Deno.open(filePath);
Expand All @@ -304,7 +305,7 @@ export async function serveFile(
}

// Set content length
headers.set("content-length", `${fileSize}`);
headers.set(HEADER.ContentLength, `${fileSize}`);

const file = await Deno.open(filePath);
const status = STATUS_CODE.OK;
Expand Down Expand Up @@ -386,7 +387,7 @@ async function serveDirIndex(
const page = dirViewerTemplate(formattedDirUrl, listEntry);

const headers = createBaseHeaders();
headers.set("content-type", "text/html; charset=UTF-8");
headers.set(HEADER.ContentType, "text/html; charset=UTF-8");

const status = STATUS_CODE.OK;
return new Response(page, {
Expand Down Expand Up @@ -421,7 +422,7 @@ function createBaseHeaders(): Headers {
return new Headers({
server: "deno",
// Set "accept-ranges" so that the client knows it can make range requests on future requests
"accept-ranges": "bytes",
[HEADER.AcceptRanges]: "bytes",
});
}

Expand Down Expand Up @@ -647,9 +648,9 @@ export async function serveDir(
const isRedirectResponse = isRedirectStatus(response.status);

if (opts.enableCors && !isRedirectResponse) {
response.headers.append("access-control-allow-origin", "*");
response.headers.append(HEADER.AccessControlAllowOrigin, "*");
response.headers.append(
"access-control-allow-headers",
HEADER.AccessControlAllowHeaders,
"Origin, X-Requested-With, Content-Type, Accept, Range",
);
}
Expand Down

0 comments on commit d99e1f8

Please sign in to comment.