Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion _tools/check_unstable_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getEntrypoints, getPackagesDenoJsons, resolve } from "./utils.ts";
import { partition } from "@std/collections/partition";
import { createGraph } from "@deno/graph";

// These 2 paths are known to include unstable module (net/unstable_get_network_address.ts)
// These paths depend on @std/net/unstable-get-network-address via file_server.ts
// and should be ignored.
const EXCEPTIONS = [
"@std/http/file-server",
Expand Down
2 changes: 0 additions & 2 deletions http/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
"./unstable-file-server": "./unstable_file_server.ts",
"./unstable-formdata-decoder-stream": "./unstable_formdata_decoder_stream.ts",
"./unstable-formdata-encoder-stream": "./unstable_formdata_encoder_stream.ts",
"./unstable-header": "./unstable_header.ts",
"./unstable-method": "./unstable_method.ts",
"./unstable-problem-details": "./unstable_problem_details.ts",
"./negotiation": "./negotiation.ts",
"./server-sent-event-stream": "./server_sent_event_stream.ts",
Expand Down
46 changes: 22 additions & 24 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ import denoConfig from "./deno.json" with { type: "json" };
import { format as formatBytes } from "@std/fmt/bytes";
import { getNetworkAddress } from "@std/net/unstable-get-network-address";
import { escape } from "@std/html/entities";
import { HEADER } from "./unstable_header.ts";
import { METHOD } from "./unstable_method.ts";

interface EntryInfo {
mode: string;
Expand Down Expand Up @@ -186,7 +184,7 @@ export async function serveFile(
): Promise<Response> {
await req.body?.cancel();

if (req.method !== METHOD.Get && req.method !== METHOD.Head) {
if (req.method !== "GET" && req.method !== "HEAD") {
return createStandardResponse(STATUS_CODE.MethodNotAllowed);
}

Expand Down Expand Up @@ -214,22 +212,22 @@ export async function serveFile(

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

// Set mime-type using the file extension in filePath
const contentTypeValue = contentType(extname(filePath));
if (contentTypeValue) {
headers.set(HEADER.ContentType, contentTypeValue);
headers.set("Content-Type", contentTypeValue);
}
const fileSize = fileInfo.size;

if (req.method === METHOD.Head) {
if (req.method === "HEAD") {
// Set content length
headers.set(HEADER.ContentLength, `${fileSize}`);
headers.set("Content-Length", `${fileSize}`);

const status = STATUS_CODE.OK;
return new Response(null, {
Expand All @@ -243,8 +241,8 @@ export async function serveFile(
// 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(HEADER.IfNoneMatch);
const ifModifiedSinceValue = req.headers.get(HEADER.IfModifiedSince);
const ifNoneMatchValue = req.headers.get("If-None-Match");
const ifModifiedSinceValue = req.headers.get("If-Modified-Since");
if (
(!ifNoneMatch(ifNoneMatchValue, etag)) ||
(ifNoneMatchValue === null &&
Expand All @@ -262,7 +260,7 @@ export async function serveFile(
}
}

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

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

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

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

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

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

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

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

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

const headers = createBaseHeaders();
headers.set(HEADER.ContentType, "text/html; charset=UTF-8");
if (req.method === METHOD.Head) {
headers.set("Content-Type", "text/html; charset=UTF-8");
if (req.method === "HEAD") {
const pageSize = new TextEncoder().encode(page).byteLength;
headers.set(HEADER.ContentLength, String(pageSize));
headers.set("Content-Length", String(pageSize));
}

const status = STATUS_CODE.OK;
return new Response(req.method === METHOD.Head ? null : page, {
return new Response(req.method === "HEAD" ? null : page, {
status,
statusText: STATUS_TEXT[status],
headers,
Expand All @@ -434,7 +432,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
[HEADER.AcceptRanges]: "bytes",
["Accept-Ranges"]: "bytes",
});
}

Expand Down Expand Up @@ -665,7 +663,7 @@ export async function serveDir(
req: Request,
opts: ServeDirOptions = {},
): Promise<Response> {
if (req.method !== METHOD.Get && req.method !== METHOD.Head) {
if (req.method !== "GET" && req.method !== "HEAD") {
return createStandardResponse(STATUS_CODE.MethodNotAllowed);
}

Expand All @@ -683,9 +681,9 @@ export async function serveDir(
const isRedirectResponse = isRedirectStatus(response.status);

if (opts.enableCors && !isRedirectResponse) {
response.headers.append(HEADER.AccessControlAllowOrigin, "*");
response.headers.append("Access-Control-Allow-Origin", "*");
response.headers.append(
HEADER.AccessControlAllowHeaders,
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Range",
);
}
Expand Down
Loading
Loading