mirror of
https://github.com/denoland/deno.git
synced 2024-11-23 15:16:54 -05:00
Add content-type header to file_server (denoland/deno_std#47)
Original: ab27371a01
This commit is contained in:
parent
95e378a28b
commit
b65727734e
3 changed files with 93 additions and 0 deletions
71
net/extension_map.json
Normal file
71
net/extension_map.json
Normal file
|
@ -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"
|
||||
}
|
|
@ -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>
|
||||
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue