Sippet v0.6.2 Sippet.URI 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
Link to this section Functions
char_hnv_unreserved?(char) View Source
Checks if the character is an "hnv-unreserved" character in a SIP-URI.
Examples
iex> Sippet.URI.char_hnv_unreserved?(?:)
true
char_param_unreserved?(char) View Source
Checks if the character is an "param-unreserved" character in a SIP-URI.
Examples
iex> Sippet.URI.char_param_unreserved?(?~)
false
char_unreserved?(char) View Source
Checks if the character is an "unreserved" character in a SIP-URI.
Examples
iex> Sippet.URI.char_unreserved?(?~)
true
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"}
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"}
default_port(scheme)
View Source
default_port(binary()) :: nil | non_neg_integer()
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
encode_headers(enumerable) View Source
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]
encode_hnvchar(string) View Source
Encodes a string as "hname" / "hvalue".
Example
iex> Sippet.URI.encode_hnvchar("put: it+й")
"put:%20it+%D0%B9"
encode_paramchar(string) View Source
Encodes a string as "paramchar".
Example
iex> Sippet.URI.encode_paramchar("put: it+й")
"put:%20it+%D0%B9"
encode_parameters(enumerable) View Source
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]
equivalent(a, b) View Source
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
headers_decoder(headers)
View Source
headers_decoder(binary()) :: Enumerable.t()
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"}]
lazy_equivalent(a, b) View Source
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
parameters_decoder(parameters)
View Source
parameters_decoder(binary()) :: Enumerable.t()
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"}]
parse(uri) View Source
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}}
parse!(uri) View Source
Parses a well-formed SIP-URI reference into its components.
If invalid, raises an exception.
percent_unescape(string) View Source
Decodes an encoded string, transforming any percent encoding back to corresponding characters.
Examples
iex> Sippet.URI.percent_unescape("%3Call%20in%2F")
"<all in/"
to_string(uri) View Source
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"