Sippet.URI (Sippet URI v0.1.0) View Source

Utilities for working with SIP-URIs.

This module provides functions for working with URIs (for example, parsing SIP-URIs, encoding parameters or header strings).

Link to this section Summary

Functions

Checks if the character is an "hnv-unreserved" character in a SIP-URI.

Checks if the character is an "param-unreserved" character in a SIP-URI.

Checks if the character is an "unreserved" character in a SIP-URI.

Decodes a "headers" string into a map.

Decodes a "uri-parameters" string into a map.

Returns the default port for a given SIP scheme. If the scheme is unknown to the Sippet.URI module, this function returns nil.

Encodes an enumerable into a "headers" string.

Encodes a string as "hname" / "hvalue".

Encodes a string as "paramchar".

Encodes an enumerable into a "uri-parameters" string.

Checks whether two SIP-URIs are equivalent.

Returns a stream of two-element tuples representing key-value pairs in the given headers.

Checks whether two SIP-URIs are equivalent, but using more lazy rules.

Returns a stream of two-element tuples representing key-value pairs in the given parameters.

Parses a well-formed SIP-URI reference into its components.

Parses a well-formed SIP-URI reference into its components.

Decodes an encoded string, transforming any percent encoding back to corresponding characters.

Returns the string representation of the given Sippet.URI struct.

Link to this section Types

Specs

t() :: %Sippet.URI{
  authority: nil | binary(),
  headers: nil | binary(),
  host: nil | binary(),
  parameters: nil | binary(),
  port: nil | :inet.port_number(),
  scheme: nil | binary(),
  userinfo: nil | binary()
}

Link to this section Functions

Link to this function

char_hnv_unreserved?(char)

View Source

Specs

char_hnv_unreserved?(char()) :: boolean()

Checks if the character is an "hnv-unreserved" character in a SIP-URI.

Examples

iex> Sippet.URI.char_hnv_unreserved?(?:)
true
Link to this function

char_param_unreserved?(char)

View Source

Specs

char_param_unreserved?(char()) :: boolean()

Checks if the character is an "param-unreserved" character in a SIP-URI.

Examples

iex> Sippet.URI.char_param_unreserved?(?~)
false

Specs

char_unreserved?(char()) :: boolean()

Checks if the character is an "unreserved" character in a SIP-URI.

Examples

iex> Sippet.URI.char_unreserved?(?~)
true
Link to this function

decode_headers(headers, map \\ %{})

View Source

Decodes a "headers" string into a map.

Given a "headers" string of the form of ?key1=value1&key2=value2..., this function inserts each key-value pair in the query string as one entry in the given map. Keys and values in the resulting map will be binaries. Keys and values will be percent-unescaped.

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

Examples

iex> Sippet.URI.decode_headers("?foo=1&bar=2")
%{"bar" => "2", "foo" => "1"}
Link to this function

decode_parameters(parameters, map \\ %{})

View Source

Decodes a "uri-parameters" string into a map.

Given a "uri-parameters" string of the form of ;key1=value1;key2=value2..., this function inserts each key-value pair in the query string as one entry in the given map. Keys and values in the resulting map will be binaries. Keys and values will be percent-unescaped.

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

Examples

iex> Sippet.URI.decode_parameters(";foo=1;bar=2")
%{"bar" => "2", "foo" => "1"}

Specs

default_port(binary()) :: nil | non_neg_integer()

Returns the default port for a given SIP scheme. If the scheme is unknown to the Sippet.URI module, this function returns nil.

Examples

iex> Sippet.URI.default_port("sip")
5060
iex> Sippet.URI.default_port("ponzi")
nil
Link to this function

encode_headers(enumerable)

View Source

Specs

encode_headers(term()) :: binary()

Encodes an enumerable into a "headers" string.

Takes an enumerable that enumerates as a list of two-element tuples (e.g., a map or a keyword list) and returns a string in the form of ?key1=value1&key2=value2... where keys and values are encoded as per encode_header/1. Keys and values can be any term that implements the String.Chars protocol, except lists which are explicitly forbidden.

Examples

iex> hd = %{"foo" => 1, "bar" => 2}
iex> Sippet.URI.encode_headers(hd)
"?bar=2&foo=1"
iex> headers = %{"key" => "value with spaces"}
iex> Sippet.URI.encode_headers(headers)
"?key=value%20with%20spaces"
iex> Sippet.URI.encode_headers %{key: [:a, :list]}
** (ArgumentError) encode_headers/1 values cannot be lists, got: [:a, :list]

Specs

encode_hnvchar(binary()) :: binary()

Encodes a string as "hname" / "hvalue".

Example

iex> Sippet.URI.encode_hnvchar("put: it+й")
"put:%20it+%D0%B9"
Link to this function

encode_paramchar(string)

View Source

Specs

encode_paramchar(binary()) :: binary()

Encodes a string as "paramchar".

Example

iex> Sippet.URI.encode_paramchar("put: it+й")
"put:%20it+%D0%B9"
Link to this function

encode_parameters(enumerable)

View Source

Specs

encode_parameters(term()) :: binary()

Encodes an enumerable into a "uri-parameters" string.

Takes an enumerable that enumerates as a list of two-element tuples (e.g., a map or a keyword list) and returns a string in the form of ;key1=value1;key2=value2... where keys and values are encoded as per encode_paramchar/1. Keys and values can be any term that implements the String.Chars protocol, except lists which are explicitly forbidden.

Examples

iex> hd = %{"foo" => 1, "bar" => 2}
iex> Sippet.URI.encode_parameters(hd)
";bar=2;foo=1"
iex> parameters = %{"key" => "value with spaces"}
iex> Sippet.URI.encode_parameters(parameters)
";key=value%20with%20spaces"
iex> Sippet.URI.encode_parameters %{key: [:a, :list]}
** (ArgumentError) encode_parameters/1 values cannot be lists, got: [:a, :list]

Specs

equivalent(t(), t()) :: boolean()

Checks whether two SIP-URIs are equivalent.

This function follows the RFC 3261 rules specified in section 19.1.4.

Examples

iex> a = Sippet.URI.parse!("sip:%61lice@atlanta.com;transport=TCP")
iex> b = Sippet.URI.parse!("sip:alice@atlanta.com;transport=tcp")
iex> Sippet.URI.equivalent(a, b)
true
Link to this function

headers_decoder(headers)

View Source

Specs

headers_decoder(binary()) :: Enumerable.t()

Returns a stream of two-element tuples representing key-value pairs in the given headers.

Key and value in each tuple will be binaries and will be percent-unescaped.

Examples

iex> Sippet.URI.headers_decoder("?foo=1&bar=2") |> Enum.to_list()
[{"foo", "1"}, {"bar", "2"}]

Specs

lazy_equivalent(t(), t()) :: boolean()

Checks whether two SIP-URIs are equivalent, but using more lazy rules.

Examples

iex> a = Sippet.URI.parse!("sip:atlanta.com;transport=UDP")
iex> b = Sippet.URI.parse!("sip:atlanta.com:5060")
iex> Sippet.URI.lazy_equivalent(a, b)
true
Link to this function

parameters_decoder(parameters)

View Source

Specs

parameters_decoder(binary()) :: Enumerable.t()

Returns a stream of two-element tuples representing key-value pairs in the given parameters.

Key and value in each tuple will be binaries and will be percent-unescaped.

Examples

iex> Sippet.URI.parameters_decoder(";foo=1;bar=2") |> Enum.to_list()
[{"foo", "1"}, {"bar", "2"}]

Specs

parse(t() | binary()) :: {:ok, t()} | {:error, reason :: term()}

Parses a well-formed SIP-URI reference into its components.

Note this function expects a well-formed SIP-URI and does not perform any validation. See the "Examples" section below for examples of how Sippet.URI.parse/1 can be used to parse a wide range of URIs. When a SIP-URI is given without a port, the value returned by Sippet.URI.default_port/1 for the SIP-URI's scheme is used for the :port field. If a %Sippet.URI{} struct is given to this function, this function returns it unmodified.

Examples

iex> Sippet.URI.parse("sip:user@host?Call-Info=%3Chttp://www.foo.com%3E&Subject=foo")
{:ok, %Sippet.URI{scheme: "sip", userinfo: "user", authority: "user@host",
            host: "host", port: 5060, parameters: nil,
            headers: "?Call-Info=%3Chttp://www.foo.com%3E&Subject=foo"}}
iex> Sippet.URI.parse("sip:user@host;transport=FOO")
{:ok, %Sippet.URI{scheme: "sip", userinfo: "user", authority: "user@host",
            host: "host", port: 5060, parameters: ";transport=FOO",
            headers: nil}}
iex> Sippet.URI.parse("sip:user@host")
{:ok, %Sippet.URI{scheme: "sip", userinfo: "user", authority: "user@host",
            host: "host", port: 5060, parameters: nil,
            headers: nil}}

Specs

parse!(t() | binary()) :: t() | no_return()

Parses a well-formed SIP-URI reference into its components.

If invalid, raises an exception.

Link to this function

percent_unescape(string)

View Source

Specs

percent_unescape(binary()) :: binary()

Decodes an encoded string, transforming any percent encoding back to corresponding characters.

Examples

iex> Sippet.URI.percent_unescape("%3Call%20in%2F")
"<all in/"

Specs

to_string(t()) :: binary()

Returns the string representation of the given Sippet.URI struct.

Examples

iex> Sippet.URI.to_string(Sippet.URI.parse!("sip:foo@bar.com"))
"sip:foo@bar.com"
iex> Sippet.URI.to_string(%URI{scheme: "foo", host: "bar.baz"})
"foo:bar.baz"