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

is_valid_url? #64

Open
ndrean opened this issue Oct 8, 2023 · 9 comments · May be fixed by #68
Open

is_valid_url? #64

ndrean opened this issue Oct 8, 2023 · 9 comments · May be fixed by #68
Labels
discuss Share your constructive thoughts on how to make progress with this issue enhancement New feature or request priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished question Further information is requested technical A technical issue that requires understanding of the code, infrastructure or dependencies

Comments

@ndrean
Copy link
Contributor

ndrean commented Oct 8, 2023

I used this a few times:

def is_valid_url?(string) do
  [:scheme, :host, :port]
  |> Enum.map(&Map.get(URI.parse(string), &1))
  |> Enum.all?()
end
test "is_valid_url?/1" do
  assert is_valid_url?("http://www.google.com") == true
  assert is_valid_url?("http//google.com") == false
  assert s_valid_url?("ftp://google.com") == true
  assert is_valid_url?("https://google.com/api&url=ok") == true
  assert is_valid_url?("http://localhost:3000") == true
  assert is_valid_url?("https://localhost") == true
  assert is_valid_url?("htt:/google") == false
  assert is_valid_url?("www.google.com") == false
end
LuchoTurtle added a commit that referenced this issue Nov 12, 2023
LuchoTurtle added a commit that referenced this issue Nov 12, 2023
LuchoTurtle added a commit that referenced this issue Nov 12, 2023
@LuchoTurtle LuchoTurtle linked a pull request Nov 12, 2023 that will close this issue
@nelsonic
Copy link
Member

Why is www.google.com not valid? 💭
You will often see urls represented without the scheme e.g. on billboards IRL.
Or just google.com which everyone knows is not valid but is widely accepted as a URL ...
Just curious what context you are using the is_valid_url? function to know.

BTW: Agree that it's useful. 😉

@nelsonic
Copy link
Member

Asking because in RFC 3986 allows the scheme to be optional. 💭

See Link.valid?/1 🔗
https://github.com/dwyl/link/blob/ca72089c0a56a0e1a96360ccba424b49b8165e3b/lib/link.ex#L118-L161

@nelsonic nelsonic added enhancement New feature or request question Further information is requested priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished discuss Share your constructive thoughts on how to make progress with this issue technical A technical issue that requires understanding of the code, infrastructure or dependencies labels Nov 14, 2023
@LuchoTurtle
Copy link
Member

I can't think of any other reason other than to enforce strictness.
But if dwyl/link@ca72089/lib/link.ex#L118-L161 already does this, maybe #68 is useless.

@ndrean
Copy link
Contributor Author

ndrean commented Nov 14, 2023

My one euro input. Suppose you have an API and an endpoint that takes an URL. If you don't put a constraint on the scheme, are you assured of have the redirection? www.google. to goggle is casi automatic by any website. However, I don't think the browser redirects from HTTP to HTTPS, but instead, it should be enabled by the website DNS rules.

@ndrean
Copy link
Contributor Author

ndrean commented Nov 14, 2023

An example. You want to download a file and you have an endpoint that accepts URLs. If you don't specify the scheme, how do you know if it is ftp or http?

Another example: try in the browser:

fetch("google.com").then(console.log)
// and compare to
fetch("http://google.com").then(console.log)

@nelsonic
Copy link
Member

Good point. 💭
If the person supplied an ftp url it would be in the form: ftp://ftp.funet.fi/pub/standards/RFC/rfc959.txt
But in the case that someone supplies a valid URL that doesn't match the correct scheme it will error, right? ❌
e.g: they supply https://google.com/file.txt the scheme is correct, the URL is valid, but there's no file. 🤷‍♂️
So the application has to handle that error scenario. 🙃

@ndrean
Copy link
Contributor Author

ndrean commented Nov 14, 2023

The postman brings a letter to the correct address and finds that the house has been demolished. Yes 😏

@nelsonic
Copy link
Member

The act of delivering the letter is what triggers the destruction ... 🤯 😜

whitehouse-explode-independence-day

@ndrean
Copy link
Contributor Author

ndrean commented Nov 14, 2023

Who was (!) the president?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss Share your constructive thoughts on how to make progress with this issue enhancement New feature or request priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished question Further information is requested technical A technical issue that requires understanding of the code, infrastructure or dependencies
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants