Skip to content

Commit

Permalink
URI-decode paths to search for
Browse files Browse the repository at this point in the history
  • Loading branch information
TotalVerb committed Sep 30, 2019
1 parent c2a7383 commit 98e8242
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ was not found.
"""
function get_fs_path(req_path::AbstractString)::String
# first element after the split is **always** "/"
r_parts = split(HTTP.URI(req_path).path[2:end], "/")
r_parts = HTTP.URIs.unescapeuri.(split(HTTP.URI(req_path).path[2:end], "/"))
fs_path = joinpath(r_parts...)
if !isempty(CONTENT_DIR[])
fs_path = joinpath(CONTENT_DIR[], fs_path)
Expand Down Expand Up @@ -79,8 +79,12 @@ function serve_file(fw, req::HTTP.Request)
ext = last(splitext(fs_path))[2:end]
content = read(fs_path, String)

# 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, "application/octet-stream")

# if html, add the browser-sync script to it
if ext == "html"
if mime in ["text/html", "application/xhtml+xml"]
end_body_match = match(r"</body>", content)
if end_body_match === nothing
# no </body> tag found, trying to add the reload script at the end; this may fail.
Expand All @@ -96,9 +100,7 @@ function serve_file(fw, req::HTTP.Request)
end
end

# build the response with appropriate mime type (this is inspired from Mux
# https://github.com/JuliaWeb/Mux.jl/blob/master/src/examples/files.jl)
headers = ["Content-Type" => get(MIME_TYPES, ext, "application/octet-stream")]
headers = ["Content-Type" => mime]
resp = HTTP.Response(content)
isempty(headers) || (resp.headers = HTTP.mkheaders(headers))

Expand Down
2 changes: 2 additions & 0 deletions test/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
@test LS.get_fs_path(req) == ""
req = "/test/dummies/index.html"
@test LS.get_fs_path(req) == "test/dummies/index.html"
req = "/test/dummies/r%C3%A9sum%C3%A9/"
@test LS.get_fs_path(req) == "test/dummies/résumé/index.html"
cd(bk)
end

Expand Down

0 comments on commit 98e8242

Please sign in to comment.