diff --git a/net/extension_map.json b/net/extension_map.json new file mode 100644 index 0000000000..2fbe0c8854 --- /dev/null +++ b/net/extension_map.json @@ -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" +} \ No newline at end of file diff --git a/net/file_server.ts b/net/file_server.ts index 3d14934db4..aaaec64d53 100755 --- a/net/file_server.ts +++ b/net/file_server.ts @@ -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 = ` @@ -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, diff --git a/net/file_server_test.ts b/net/file_server_test.ts index b751b1145a..40bec1c4b2 100644 --- a/net/file_server_test.ts +++ b/net/file_server_test.ts @@ -25,6 +25,7 @@ export function runTests(serverReadyPromise: Promise) { 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);