URI

Utilities for working with and creating URIs.

Source

Summary

decode(uri)

Percent-unescape a URI

decode(arg1, uri)
decode_query(q, dict \\ HashDict.new())

Decodes a query string into a HashDict

default_port(scheme)

Returns the default port for a given scheme

default_port(scheme, port)

Registers a scheme with a default port

encode(s)

Percent-escape a URI

encode_query(l)

Encodes an enumerable into a query string

normalize_scheme(scheme)

Normalizes the scheme according to the spec by downcasing it

parse(s)

Parses a URI into components

query_decoder(q)

Returns an iterator function over the query string that decodes the query string in steps

Functions

decode(uri)

Percent-unescape a URI.

Examples

iex> URI.decode("http%3A%2F%2Felixir-lang.com")
"http://elixir-lang.com"
Source
decode(arg1, uri)
Source
decode_query(q, dict \\ HashDict.new())

Decodes a query string into a HashDict.

Given a query string of the form "key1=value1&key2=value2...", produces a HashDict with one entry for each key-value pair. Each key and value will be a binary. Keys and values will be percent-unescaped.

Use query_decoder/1 if you want to iterate over each value manually.

Examples

iex> URI.decode_query("foo=1&bar=2") |> Dict.to_list
[{"bar", "2"}, {"foo", "1"}]

iex> hd = HashDict.new()
iex> URI.decode_query("foo=1&bar=2", hd) |> HashDict.keys
["bar", "foo"]
iex> URI.decode_query("foo=1&bar=2", hd) |> HashDict.values
["2", "1"]
Source
default_port(scheme)

Returns the default port for a given scheme.

If the scheme is unknown to URI, returns nil. Any scheme may be registered via default_port/2.

Examples

iex> URI.default_port("ftp")
21

iex> URI.default_port("ponzi")
nil
Source
default_port(scheme, port)

Registers a scheme with a default port.

Source
encode(s)

Percent-escape a URI.

Example

iex> URI.encode("http://elixir-lang.com/getting_started/2.html")
"http%3A%2F%2Felixir-lang.com%2Fgetting_started%2F2.html"
Source
encode_query(l)

Encodes an enumerable into a query string.

Takes an enumerable (containing a sequence of two-item tuples) and returns a string of the form "key1=value1&key2=value2..." where keys and values are URL encoded as per encode/1. Keys and values can be any term that implements the String.Chars protocol (i.e. can be converted to a binary).

Examples

iex> hd = HashDict.new([{"foo", 1}, {"bar", "2"}])
iex> URI.encode_query(hd)
"bar=2&foo=1"
Source
normalize_scheme(scheme)

Normalizes the scheme according to the spec by downcasing it.

Source
parse(s)

Parses a URI into components.

URIs have portions that are handled specially for the particular scheme of the URI. For example, http and https have different default ports. Such values can be accessed and registered via URI.default_port/1 and URI.default_port/2.

Examples

iex> URI.parse("http://elixir-lang.org/")
URI.Info[scheme: "http", path: "/", query: nil, fragment: nil,
         authority: "elixir-lang.org", userinfo: nil,
         host: "elixir-lang.org", port: 80]
Source
query_decoder(q)

Returns an iterator function over the query string that decodes the query string in steps.

Examples

iex> URI.query_decoder("foo=1&bar=2") |> Enum.map &(&1)
[{"foo", "1"}, {"bar", "2"}]
Source