Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the absuri function to correctly handle relative location paths #13

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
- windows-latest
arch:
- x64
exclude:
- os: windows-latest
version: 'nightly'
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
Expand Down
17 changes: 16 additions & 1 deletion src/URIs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,22 @@ function absuri(uri::URI, context::URI)
@assert !isempty(context.host)
@assert isempty(uri.port)

return URI(context; path=uri.path, query=uri.query)
if !(context.scheme in ["http", "https"]) || isempty(uri.path) || uri.path[1] == '/'
path = String(uri.path)
else
# > The "Location" header field is used to identify a newly created resource, or to redirect the recipient to a different location for completion of the request.
# > The field value consists of a single URI-reference. When it has the form of a relative reference ([RFC3986], Section 4.2), the final value is computed by resolving it against the effective request URI ([RFC3986], Section 5).
# Sources:
# 1. https://greenbytes.de/tech/webdav/draft-ietf-httpbis-p2-semantics-17.html#header.location
# 2. https://web.archive.org/web/20200926022629/https://greenbytes.de/tech/webdav/draft-ietf-httpbis-p2-semantics-17.html
# 3. https://www.rfc-editor.org/rfc/rfc3986.html#section-4.2
# 4. https://web.archive.org/web/20201106144353/https://www.rfc-editor.org/rfc/rfc3986.html#section-4.2
# 5. https://www.rfc-editor.org/rfc/rfc3986.html#section-5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the official reference https://tools.ietf.org/html/rfc3986?

# 6. https://web.archive.org/web/20201106144353/https://www.rfc-editor.org/rfc/rfc3986.html#section-5
path = String(uristring(normpath(joinpath(URI(; path = context.path), "..", String(uri.path)))))
end

return URI(context; path=path, query=uri.query)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's meant to happen to fragment here?

end

"""
Expand Down
15 changes: 15 additions & 0 deletions test/absuri.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@testset "absuri.jl" begin
cases = [
("https://en.wikipedia.org/api/rest_v1/page/summary/Potatoes", "Potato", "https://en.wikipedia.org/api/rest_v1/page/summary/Potato"),
("https://en.wikipedia.org/api/rest_v1/page/summary/Potatoes", "./Potato", "https://en.wikipedia.org/api/rest_v1/page/summary/Potato"),
("https://en.wikipedia.org/api/rest_v1/page/summary/Potatoes", "/foo/bar/baz", "https://en.wikipedia.org/foo/bar/baz"),
("https://en.wikipedia.org/api/rest_v1/page/summary/Potatoes", "https://julialang.org/foo/bar/baz", "https://julialang.org/foo/bar/baz"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Let's add a test for query handling` as well?

]
for case in cases
url = case[1]
location = case[2]
actual_output = URIs.absuri(location, url)
expected_output = URIs.URI(case[3])
@test actual_output == expected_output
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ using Test

include("uri.jl")
include("url.jl")
include("absuri.jl")