From 91e9e9ead216bb08c5a5bf9e85e555f1b5f0f641 Mon Sep 17 00:00:00 2001 From: Thibaut Lienart Date: Tue, 15 Mar 2022 10:17:29 +0100 Subject: [PATCH] fixing mime types and trailing backslashes --- Project.toml | 2 +- src/mimetypes.jl | 14 +++++++------- src/server.jl | 19 ++++++++++--------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Project.toml b/Project.toml index b65ceb6..5093058 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "LiveServer" uuid = "16fef848-5104-11e9-1b77-fb7a48bbb589" authors = ["Jonas Asprion "] -version = "0.7.3" +version = "0.7.4" [deps] Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f" diff --git a/src/mimetypes.jl b/src/mimetypes.jl index 929752e..058b632 100644 --- a/src/mimetypes.jl +++ b/src/mimetypes.jl @@ -147,7 +147,7 @@ const MIME_TYPES = Dict{String,String}([ ("cmx", "image/x-cmx"), ("cod", "application/vnd.rim.cod"), ("com", "application/x-msdownload"), - ("conf", "text/plain"), + ("conf", "text/plain; charset=utf-8"), ("cpio", "application/x-cpio"), ("cpp", "text/x-c"), ("cpt", "application/mac-compactpro"), @@ -177,7 +177,7 @@ const MIME_TYPES = Dict{String,String}([ ("dd2", "application/vnd.oma.dd2+xml"), ("ddd", "application/vnd.fujixerox.ddd"), ("deb", "application/x-debian-package"), - ("def", "text/plain"), + ("def", "text/plain; charset=utf-8"), ("deploy", "application/octet-stream"), ("der", "application/x-x509-ca-cert"), ("dfac", "application/vnd.dreamfactory"), @@ -354,7 +354,7 @@ const MIME_TYPES = Dict{String,String}([ ("iif", "application/vnd.shana.informed.interchange"), ("imp", "application/vnd.accpac.simply.imp"), ("ims", "application/vnd.ms-ims"), - ("in", "text/plain"), + ("in", "text/plain; charset=utf-8"), ("ink", "application/inkml+xml"), ("inkml", "application/inkml+xml"), ("install", "application/x-install-instructions"), @@ -409,11 +409,11 @@ const MIME_TYPES = Dict{String,String}([ ("les", "application/vnd.hhe.lesson-player"), ("lha", "application/x-lzh-compressed"), ("link66", "application/vnd.route66.link66+xml"), - ("list", "text/plain"), + ("list", "text/plain; charset=utf-8"), ("list3820", "application/vnd.ibm.modcap"), ("listafp", "application/vnd.ibm.modcap"), ("lnk", "application/x-ms-shortcut"), - ("log", "text/plain"), + ("log", "text/plain; charset=utf-8"), ("lostxml", "application/lost+xml"), ("lrf", "application/octet-stream"), ("lrm", "application/vnd.ms-lrm"), @@ -807,7 +807,7 @@ const MIME_TYPES = Dict{String,String}([ ("tex", "application/x-tex"), ("texi", "application/x-texinfo"), ("texinfo", "application/x-texinfo"), - ("text", "text/plain"), + ("text", "text/plain; charset=utf-8"), ("tfi", "application/thraud+xml"), ("tfm", "application/x-tex-tfm"), ("tga", "image/x-tga"), @@ -830,7 +830,7 @@ const MIME_TYPES = Dict{String,String}([ ("twds", "application/vnd.simtech-mindmapper"), ("txd", "application/vnd.genomatix.tuxedo"), ("txf", "application/vnd.mobius.txf"), - ("txt", "text/plain"), + ("txt", "text/plain; charset=utf-8"), ("u32", "application/x-authorware-bin"), ("udeb", "application/x-debian-package"), ("ufd", "application/vnd.ufdl"), diff --git a/src/server.jl b/src/server.jl index a61a251..218fc6b 100644 --- a/src/server.jl +++ b/src/server.jl @@ -88,14 +88,15 @@ function get_fs_path(req_path::AbstractString)::String fs_path = joinpath(CONTENT_DIR[], fs_path) end - # if it ends with `/` try to see if there's an index.html - if endswith(uri.path, '/') - tmp = joinpath(fs_path, "index.html") - isfile(tmp) && return tmp - end - # otherwise return the path if it exists (file or dir) - (isfile(fs_path) || isdir(fs_path)) && return fs_path - # otherwise --> this will lead to a 404 + isfile(fs_path) && return fs_path + + tmp = joinpath(fs_path, "index.html") + isfile(tmp) && return tmp + + # content of the dir will be shown + isdir(fs_path) && return fs_path + + # 404 will be shown return "" end @@ -266,7 +267,7 @@ function serve_file( # build the response with appropriate mime type (this is inspired from Mux # https://github.com/JuliaWeb/Mux.jl/blob/master/src/examples/files.jl) - mime = get(MIME_TYPES, ext, "text/plain") + mime = get(MIME_TYPES, ext, HTTP.sniff(content)) # if html-like, try adding the browser-sync script to it inject_reload = inject_browser_reload_script &&