Skip to content

Conversation

@dogatuncay
Copy link

I ran into an issue involving unexpected behavior from the URI module. The URI module does not appear to follow the specification with respect to the scheme of a URL. This can cause inconsistencies when operating with other tools and libraries that accept URIs.

According to the spec:

The scheme and path components are required, though the path may be empty (no characters).

However, the URI module doesn't check if the scheme is provided, nor does it set a default scheme. If you don't specify a manual scheme, it comes out like //abc.com. I think it should throw an ArgumentError to match the existing behavior for checking the correctness of the path component before turning the URI into a string.

@chulkilee
Copy link
Contributor

What about empty string? ("") Should it be handled like nil?

We may allow empty string to allow users to build //example.com string... but that's against the spec I guess.

@dogatuncay
Copy link
Author

What about empty string? ("") Should it be handled like nil?

We may allow empty string to allow users to build //example.com string... but that's against the spec I guess.

that's a good point. I'll update my PR.

assert String.Chars.to_string(uri) == "http://google.com"

uri_no_host = URI.parse("/foo/bar")
assert String.Chars.to_string(uri_no_host) == "/foo/bar"
Copy link
Author

@dogatuncay dogatuncay Apr 8, 2021

Choose a reason for hiding this comment

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

removed this test, because it doesn't make sense after the change any more. The path by itself isn't a valid URI.

@wojtekmach
Copy link
Member

Per URI.parse/1 docs

Note this function expects a well-formed URI and does not perform any validation. See the "Examples" section below for examples of how URI.parse/1 can be used to parse a wide range of URIs

So making the function stricter would be a breaking change. For this reason, we are discussing deprecating it in favour of a new function that would perform the proper validations, see #10865

@josevalim
Copy link
Member

The URI module can parse both relative and absolute URLs, see section 4.2. I will clarify this in the docs to avoid further confusion. :) Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants