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

Add content-type header in fileserver #47

Merged
merged 5 commits into from
Dec 31, 2018
Merged
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
71 changes: 71 additions & 0 deletions net/extension_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"": "application/octet-stream",
".aac": "audio/aac",
".abw": "application/x-abiword",
".arc": "application/octet-stream",
".avi": "video/x-msvideo",
".azw": "application/vnd.amazon.ebook",
".bin": "application/octet-stream",
".bmp": "image/bmp",
".bz": "application/x-bzip",
".bz2": "application/x-bzip2",
".csh": "application/x-csh",
".css": "text/css",
".csv": "text/csv",
".doc": "application/msword",
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".eot": "application/vnd.ms-fontobject",
".epub": "application/epub+zip",
".es": "application/ecmascript",
".gif": "image/gif",
".htm": "text/html",
".html": "text/html",
".ico": "image/x-icon",
".ics": "text/calendar",
".jar": "application/java-archive",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".js": "application/javascript",
".json": "application/json",
".mid": "audio/x-midi",
".midi": "audio/x-midi",
".mpeg": "video/mpeg",
".mpkg": "application/vnd.apple.installer+xml",
".odp": "application/vnd.oasis.opendocument.presentation",
".ods": "application/vnd.oasis.opendocument.spreadsheet",
".odt": "application/vnd.oasis.opendocument.text",
".oga": "audio/ogg",
".ogv": "video/ogg",
".ogx": "application/ogg",
".otf": "font/otf",
".png": "image/png",
".pdf": "application/pdf",
".ppt": "application/vnd.ms-powerpoint",
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
".rar": "application/x-rar-compressed",
".rtf": "application/rtf",
".sh": "application/x-sh",
".svg": "image/svg+xml",
".swf": "application/x-shockwave-flash",
".tar": "application/x-tar",
".tif": "image/tiff",
".tiff": "image/tiff",
".ts": "application/typescript",
".ttf": "font/ttf",
".txt": "text/plain",
".vsd": "application/vnd.visio",
".wav": "audio/wav",
".weba": "audio/webm",
".webm": "video/webm",
".webp": "image/webp",
".woff": "font/woff",
".woff2": "font/woff2",
".xhtml": "application/xhtml+xml",
".xls": "application/vnd.ms-excel",
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".xml": "application/xml",
".xul": "application/vnd.mozilla.xul+xml",
".yml": "text/yaml",
".yaml": "text/yaml",
".zip": "application/zip"
}
21 changes: 21 additions & 0 deletions net/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
Response
} from "./http.ts";
import { cwd, DenoError, ErrorKind, args, stat, readDir, open } from "deno";
import { extname } from "../path/index.ts";
import * as extensionsMap from "./extension_map.json";

const dirViewerTemplate = `
<!DOCTYPE html>
Expand Down Expand Up @@ -160,11 +162,30 @@ async function serveDir(req: ServerRequest, dirPath: string, dirName: string) {
return res;
}

function guessContentType(filename: string): string {
let extension = extname(filename);
let contentType = extensionsMap[extension];

if (contentType) {
return contentType;
}

extension = extension.toLowerCase();
contentType = extensionsMap[extension];

if (contentType) {
return contentType;
}

return extensionsMap[''];
}

async function serveFile(req: ServerRequest, filename: string) {
const file = await open(filename);
const fileInfo = await stat(filename);
const headers = new Headers();
headers.set("content-length", fileInfo.len.toString());
headers.set("content-type", guessContentType(filename));

const res = {
status: 200,
Expand Down
1 change: 1 addition & 0 deletions net/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function runTests(serverReadyPromise: Promise<any>) {
const res = await fetch("http://localhost:4500/.travis.yml");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEqual(res.headers.get("content-type"), "text/yaml");
const downloadedFile = await res.text();
const localFile = new TextDecoder().decode(await readFile("./.travis.yml"));
assertEqual(downloadedFile, localFile);
Expand Down