View Source MopURL.ValidateURL (MopURL v0.1.0)

URL validator

usage

Usage:

iex> good_url = cast("google.com")
{:ok, "https://google.com"}
iex> validate_hostname(good_url)
:ok

iex> bad_url = cast("localhost")
{:ok, "https://localhost"}
iex> validate_hostname(bad_url)
{:error, "restricted ip"}

iex> cast("cant-reach-this.com") |> validate_hostname()
{:error, :nxdomain}

Link to this section Summary

Functions

Tries to convert given string to a valid URL.

Checks if a ip is restricted (not in global scope)

Checks url format using a regular expression.

Regex that matches properly formatted URLs. Adapted from https://github.com/spence/valid_url and https://gist.github.com/dperini/729294

Checks if hostname is resolvable.

Checks that given URL has reachable hostname

Link to this section Functions

Tries to convert given string to a valid URL.

examples

Examples

iex> cast("HtTps://GOogLE.cOM/search?q=2+323+32")
{:ok, "https://google.com/search?q=2+323+32"}

iex> cast("localhost")                           
{:ok, "https://localhost"}

iex> cast("instagram.com")
{:ok, "https://instagram.com"}

Checks if a ip is restricted (not in global scope)

examples

Examples

iex> IP.Address.from_string!("127.0.0.1") |> is_restricted_ip()
true

iex> IP.Address.from_string!("0.0.0.0") |> is_restricted_ip()
true

iex> IP.Address.from_string!("142.251.215.238") |> is_restricted_ip()
false

Checks url format using a regular expression.

examples

Examples

iex> is_valid_format("HtTps://GOogLE.cOM/search?q=elixir+fresh")
true

iex> is_valid_format("ipfs://GOogLE.cOM/search?q=elixir+fresh")
false
Link to this function

resolve_hostname(hostname)

View Source

Checks if hostname is resolvable.

Checks that given URL has reachable hostname

Also makes sure that ip of this hostname is not restricted (doesn't point to local network)

examples

Examples

iex> validate_hostname("https://localhost")                           
{:error, "restricted ip"}

iex> validate_hostname("https://google.com")
:ok