diff options
Diffstat (limited to 'net')
| -rw-r--r-- | net/extension_map.json | 85 | ||||
| -rwxr-xr-x | net/file_server.ts | 22 | ||||
| -rw-r--r-- | net/file_server_test.ts | 2 |
3 files changed, 3 insertions, 106 deletions
diff --git a/net/extension_map.json b/net/extension_map.json deleted file mode 100644 index b02517d6b..000000000 --- a/net/extension_map.json +++ /dev/null @@ -1,85 +0,0 @@ -{ - "": "application/octet-stream", - ".7z": "application/x-7z-compressed", - ".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", - ".gz": "application/gzip", - ".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", - ".md": "text/markdown", - ".mid": "audio/x-midi", - ".midi": "audio/x-midi", - ".mp3": "audio/mpeg", - ".mp4": "video/mpeg", - ".mpeg": "video/mpeg", - ".mpkg": "application/vnd.apple.installer+xml", - ".less": "text/less", - ".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", - ".ppm": "image/x-portable-pixmap", - ".pgm": "image/x-portable-graymap", - ".pmm": "image/x-portable-bitmap", - ".pnm": "image/x-portable-anymap", - ".ppt": "application/vnd.ms-powerpoint", - ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", - ".rar": "application/x-rar-compressed", - ".rtf": "application/rtf", - ".sh": "application/x-sh", - ".sass": "text/x-sass", - ".scss": "text/x-scss", - ".svg": "image/svg+xml", - ".swf": "application/x-shockwave-flash", - ".tar": "application/x-tar", - ".tar.gz": "application/tar+gzip", - ".tif": "image/tiff", - ".tiff": "image/tiff", - ".toml": "application/toml", - ".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" -} diff --git a/net/file_server.ts b/net/file_server.ts index bebb93969..72432abdd 100755 --- a/net/file_server.ts +++ b/net/file_server.ts @@ -13,7 +13,7 @@ import { } from "./http.ts"; import { cwd, DenoError, ErrorKind, args, stat, readDir, open } from "deno"; import { extname } from "../fs/path.ts"; -import * as extensionsMap from "./extension_map.json"; +import { contentType } from "../media_types/mod.ts"; const dirViewerTemplate = ` <!DOCTYPE html> @@ -162,30 +162,12 @@ 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)); + headers.set("content-type", contentType(extname(filename)) || "text/plain"); const res = { status: 200, diff --git a/net/file_server_test.ts b/net/file_server_test.ts index 28357c912..bd00d749b 100644 --- a/net/file_server_test.ts +++ b/net/file_server_test.ts @@ -21,7 +21,7 @@ export function runTests(serverReadyPromise: Promise<any>) { const res = await fetch("http://localhost:4500/azure-pipelines.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"); + assertEqual(res.headers.get("content-type"), "text/yaml; charset=utf-8"); const downloadedFile = await res.text(); const localFile = new TextDecoder().decode( await readFile("./azure-pipelines.yml") |
