From 9aa8e20bd5552916c6b382659224f1003b02dbc7 Mon Sep 17 00:00:00 2001 From: bptato Date: Sat, 16 Mar 2024 20:11:53 +0100 Subject: container: fall back to text/plain instead of application/octet-stream This has its own problems, but application/octet-stream has the horrible consequence that opening any local file with an unrecognized type automatically quits the browser. (FWIW, w3m also falls back to text/plain, so it's not such an unreasonable default.) The proper solution would be to a) fix the bug that makes the browser auto-quit and b) show a "what to do" prompt for unrecognized file types (and allow users to override it, preferably on a per-protocol basis.) --- src/local/container.nim | 3 ++- src/utils/mimeguess.nim | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/local/container.nim b/src/local/container.nim index 011c7279..62799c56 100644 --- a/src/local/container.nim +++ b/src/local/container.nim @@ -1412,7 +1412,8 @@ proc applyResponse*(container: Container; response: Response; if container.contentType.isNone: var contentType = response.getContentType() if contentType == "application/octet-stream": - contentType = mimeTypes.guessContentType(container.url.pathname) + contentType = mimeTypes.guessContentType(container.url.pathname, + "text/plain") container.contentType = some(contentType) # setup charsets: # * override charset diff --git a/src/utils/mimeguess.nim b/src/utils/mimeguess.nim index 4b8df086..f13eaf89 100644 --- a/src/utils/mimeguess.nim +++ b/src/utils/mimeguess.nim @@ -8,7 +8,8 @@ const DefaultGuess* = block: let ss = newStringStream(staticRead"res/mime.types") parseMimeTypes(ss) -func guessContentType*(mimeTypes: MimeTypes; path: string): string = +func guessContentType*(mimeTypes: MimeTypes; path: string; + fallback = "application/octet-stream"): string = var n = 0 for i in countdown(path.high, 0): if path[i] == '/': @@ -20,7 +21,7 @@ func guessContentType*(mimeTypes: MimeTypes; path: string): string = let ext = path.substr(n + 1) if ext in mimeTypes: return mimeTypes[ext] - return "application/octet-stream" + return fallback const JavaScriptTypes = [ "application/ecmascript", -- cgit 1.4.1-2-gfad0