Correctly appending UTF-8 formatting to text only files

canary
WolverinDEV 2020-08-05 19:05:24 +02:00
parent 0725bea8dd
commit 226f3d50d0
1 changed files with 5 additions and 5 deletions

10
file.ts
View File

@ -292,9 +292,9 @@ namespace server {
server = https.createServer({ server = https.createServer({
key: await fs.readFile(key_file), key: await fs.readFile(key_file),
cert: await fs.readFile(cert_file), cert: await fs.readFile(cert_file),
}, handle_request); }, handleHTTPRequest);
} else { } else {
server = http.createServer(handle_request); server = http.createServer(handleHTTPRequest);
} }
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
server.on('error', reject); server.on('error', reject);
@ -322,12 +322,12 @@ namespace server {
return; return;
} }
let type = mt.lookup(path.extname(file)) || "text/html"; let type: string = mt.lookup(path.extname(file)) || "text/html";
console.log("[SERVER] Serving file %s", file, type); console.log("[SERVER] Serving file %s", file, type);
const fis = fs.createReadStream(file); const fis = fs.createReadStream(file);
response.writeHead(200, "success", { response.writeHead(200, "success", {
"Content-Type": type + "; charset=utf-8" "Content-Type": type + (type.startsWith("text/") ? "; charset=utf-8" : "")
}); });
fis.on("end", () => response.end()); fis.on("end", () => response.end());
@ -360,7 +360,7 @@ namespace server {
response.end(); response.end();
} }
function handle_request(request: http.IncomingMessage, response: http.ServerResponse) { function handleHTTPRequest(request: http.IncomingMessage, response: http.ServerResponse) {
let url: url_utils.UrlWithParsedQuery; let url: url_utils.UrlWithParsedQuery;
try { try {
url = url_utils.parse(request.url, true); url = url_utils.parse(request.url, true);