sippet v0.1.8 Sippet.URI
Utilities for working with SIP-URIs. This module provides functions for working with URIs (for example, parsing SIP-URIs, encoding parameters or header strings).
Summary
Functions
Checks if the character is an “hnv-unreserved” character in a SIP-URI.
Examples
iex> Sippet.URI.char_hnv_unreserved?(?:)
true
Checks if the character is an “param-unreserved” character in a SIP-URI.
Examples
iex> Sippet.URI.char_param_unreserved?(?~)
false
Checks if the character is an “unreserved” character in a SIP-URI.
Examples
iex> Sippet.URI.char_unreserved?(?~)
true
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"}
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"}
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
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]
Encodes a string as “hname” / “hvalue”.
Example
iex> Sippet.URI.encode_hnvchar("put: it+й")
"put:%20it%2B%D0%B9"
Encodes a string as “paramchar”.
Example
iex> Sippet.URI.encode_paramchar("put: it+й")
"put:%20it%2B%D0%B9"
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]
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"}]
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"}]
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}}
Parses a well-formed SIP-URI reference into its components
Decodes an encoded string, transforming any percent encoding back to corresponding characters.
Examples
iex> Sippet.URI.percent_unescape("%3Call%20in%2F")
"<all in/"
Returns the string representation of the given Sippet.URI
struct.
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"
Types
Functions
Checks if the character is an “hnv-unreserved” character in a SIP-URI.
Examples
iex> Sippet.URI.char_hnv_unreserved?(?:)
true
Checks if the character is an “param-unreserved” character in a SIP-URI.
Examples
iex> Sippet.URI.char_param_unreserved?(?~)
false
Checks if the character is an “unreserved” character in a SIP-URI.
Examples
iex> Sippet.URI.char_unreserved?(?~)
true
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"}
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"}
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
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]
Encodes a string as “hname” / “hvalue”.
Example
iex> Sippet.URI.encode_hnvchar("put: it+й")
"put:%20it%2B%D0%B9"
Encodes a string as “paramchar”.
Example
iex> Sippet.URI.encode_paramchar("put: it+й")
"put:%20it%2B%D0%B9"
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]
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"}]
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"}]
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}}
Parses a well-formed SIP-URI reference into its components.
If invalid, raises an exception.
Decodes an encoded string, transforming any percent encoding back to corresponding characters.
Examples
iex> Sippet.URI.percent_unescape("%3Call%20in%2F")
"<all in/"
Returns the string representation of the given Sippet.URI
struct.
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"