Check if a URL is valid in elixir

4.5k views Asked by At

I want to check if a given URL is valid, ideally if the url resolves too.

Firstly, how would I go about just checking the string validity (i.e. regex)

and secondly, is there a way that I can see if the URL actually resolves to a resource on the internet?

Thanks

1

There are 1 answers

11
Yoshiki On BEST ANSWER

Instead of reaching out for a regex I would use the URI package to textually validate the URI, and then check if the host name resolves through :inet.gethostbyname:

iex(1)> URI.parse("http://google.com/")
%URI{authority: "google.com", fragment: nil, host: "google.com",
path: "/", port: 80, query: nil, scheme: "http", userinfo: nil}

Note the "host" field of the URI struct. If it's a relative resource then this will be nil. Additionally scheme will be nil if the scheme, i.e. http://, or ftp:// is missing. The path should also be there("/") even if it's just the root path of the site. Your validation then is whether any of these are nil or not, something like this:

defmodule Validation do
  def validate_uri(str) do
    uri = URI.parse(str)
    case uri do
      %URI{scheme: nil} -> {:error, uri}
      %URI{host: nil} -> {:error, uri}
      %URI{path: nil} -> {:error, uri}
      uri -> {:ok, uri}
    end 
  end 
end

{:ok, uri} = Validation.validate_uri("http://google.com/")

You can then pass this "valid" uri to :inet.gethostbyname/1

iex(18)> :inet.gethostbyname(to_char_list a.host)
{:ok, {:hostent, 'google.com', [], :inet, 4, [{216, 58, 217, 46}]}}

If for whatever reason this fails :inet.gethostbyname/1 will return {:error, :nxdomain}