Sippet v0.2.5 Sippet.Message
Message primitive for composing SIP messages.
Build a SIP message with the Sippet.Message
struct.
request =
Sippet.Message.build_request("INVITE", "sip:joe@example.com")
|> Sippet.Message.put_header(:to,
{"", Sippet.URI.parse("sip:joe@example.com"), %{}})
...
Summary
Functions
Build a SIP request
Build a SIP response
Creates an unique local branch (72-bit random string, 7+12 characters long)
Creates an unique Call-ID (120-bit random string, 20 characters long)
Creates a local tag (48-bit random string, 8 characters long)
Deletes all header
values in message
Deletes the last value of header
in message
Deletes the first value of header
in message
Drops all given headers
from message
Fetches all values for a specific header
and returns it in a tuple
Fetches all values for a specific header
in the given message
, erroring
out if message
doesn’t contain header
Fetches the last value of a specific header
and returns it in a tuple
Fetches the last value of a specific header
in the given message
, erroring
out if message
doesn’t contain header
Fetches the first value of a specific header
and returns it in a tuple
Fetches the first value of a specific header
in the given message
, erroring
out if message
doesn’t contain header
Gets the values from header
and updates it, all in one pass
Gets the last value from header
and updates it, all in one pass
Gets the first value from header
and updates it, all in one pass
Gets all values for a specific header
in message
Gets the last value of a specific header
in message
Gets the first value of a specific header
in message
Returns whether a given header
exists in the given message
Returns a list of all known methods, as a list of uppercase strings
Returns a list of all known transport protocols, as a list of uppercase strings
Parses a SIP message header block as received by the transport layer
Parses a SIP message header block as received by the transport layer
Returns and removes the values associated with header
in message
Returns and removes the last value associated with header
in message
Returns and removes the first value associated with header
in message
Puts the value
under header
on the message
Puts the value
under header
on the message
, as last element
Puts the value
under header
on the message
, as front element
Puts the value
under header
on the message
unless the header
already
exists
Evaluates fun
and puts the result under header
in message
unless
header
is already present
Shortcut to check if the message is a request
Shortcut to check if the message is a response
Returns the iodata representation of the given Sippet.Message
struct
Converts a string representing a known method into an atom, otherwise as an uppercase string
Converts a string representing a known protocol into an atom, otherwise as an uppercase string
Returns the string representation of the given Sippet.Message
struct
Updates the header
in message
with the given function
Updates the last header
value in message
with the given function
Updates the first header
value in message
with the given function
Checks whether a message is valid
Checks whether a message is valid, also checking if it corresponds to the
indicated incoming transport tuple {protocol, host, port}
Types
header :: :accept | :accept_encoding | :accept_language | :alert_info | :allow | :authentication_info | :authorization | :call_id | :call_info | :contact | :content_disposition | :content_encoding | :content_language | :content_length | :content_type | :cseq | :date | :error_info | :expires | :from | :in_reply_to | :max_forwards | :mime_version | :min_expires | :organization | :priority | :proxy_authenticate | :proxy_authorization | :proxy_require | :reason | :record_route | :reply_to | :require | :retry_after | :route | :server | :subject | :supported | :timestamp | :to | :unsupported | :user_agent | :via | :warning | :www_authenticate | binary
method :: :ack | :bye | :cancel | :info | :invite | :message | :notify | :options | :prack | :publish | :pull | :push | :refer | :register | :store | :subscribe | :update | binary
multiple_value() :: token_params | type_subtype_params | uri_params | via_value | auth_params :: %{} | {scheme :: binary, params :: %{}} | {code :: integer, agent :: binary, text :: binary}
protocol :: :amqp | :dccp | :dtls | :sctp | :stomp | :tcp | :tls | :udp | :ws | :wss | binary
request() :: %Sippet.Message{body: term, headers: term, start_line: Sippet.Message.RequestLine.t, target: term}
response() :: %Sippet.Message{body: term, headers: term, start_line: Sippet.Message.StatusLine.t, target: term}
single_value :: binary | integer | {sequence :: integer, method} | {major :: integer, minor :: integer} | token_params | type_subtype_params | uri_params | {delta_seconds :: integer, comment :: binary, params :: %{}} | {timestamp :: integer, delay :: integer} | NativeDateTime.t
t() :: %Sippet.Message{body: String.t | nil, headers: %{optional(header) => value}, start_line: Sippet.Message.RequestLine.t | Sippet.Message.StatusLine.t, target: nil | {protocol :: atom | binary, host :: binary, dport :: integer}}
via_value() :: {{major :: integer, minor :: integer}, protocol, {host :: binary, port :: integer}, params :: %{}}
Functions
Build a SIP request.
Build a SIP response.
build_response(integer | request, integer | String.t | Sippet.Message.StatusLine.t) :: response
Creates an unique local branch (72-bit random string, 7+12 characters long).
Creates an unique Call-ID (120-bit random string, 20 characters long).
Deletes the last value of header
in message
.
Deletes the first value of header
in message
.
Fetches all values for a specific header
and returns it in a tuple.
If the header
does not exist, returns :error
.
Fetches all values for a specific header
in the given message
, erroring
out if message
doesn’t contain header
.
If message
contains the given header
, all corresponding values are
returned in a list. If message
doesn’t contain the header
, a KeyError
exception is raised.
Fetches the last value of a specific header
and returns it in a tuple.
If the header
does not exist, returns :error
. If the header
exists but
it is an empty list, returns {:ok, nil}
.
Fetches the last value of a specific header
in the given message
, erroring
out if message
doesn’t contain header
.
If message
contains the given header
, the last value is returned, which
may be nil
case the values list is empty. If message
doesn’t contain the
header
, a KeyError
exception is raised.
Fetches the first value of a specific header
and returns it in a tuple.
If the header
does not exist, returns :error
. If the header
exists but
it is an empty list, returns {:ok, nil}
.
Fetches the first value of a specific header
in the given message
, erroring
out if message
doesn’t contain header
.
If message
contains the given header
, the first value is returned, which
may be nil
case the values list is empty. If message
doesn’t contain the
header
, a KeyError
exception is raised.
Gets the values from header
and updates it, all in one pass.
fun
is called with the current values under header
in message
(or nil
if key
is not present in message
) and must return a two-element tuple:
the “get” value (the retrieved values, which can be operated on before being
returned) and the new values to be stored under header
in the resulting new
message. fun
may also return :pop
, which means all current values shall
be removed from message
and returned (making this function behave like
Sippet.Message.pop_header(message, header)
. The returned value is a tuple
with the “get” value returned by fun
and a new message with the updated
values under header
.
Gets the last value from header
and updates it, all in one pass.
fun
is called with the current last value under header
in message
(or
nil
if key
is not present in message
) and must return a two-element
tuple: the “get” value (the retrieved value, which can be operated on before
being returned) and the new value to be stored under header
in the
resulting new message. fun
may also return :pop
, which means the current
value shall be removed from message
and returned (making this function
behave like Sippet.Message.pop_header_back(message, header)
. The returned
value is a tuple with the “get” value returned by fun
and a new message
with the updated values under header
.
Gets the first value from header
and updates it, all in one pass.
fun
is called with the current first value under header
in message
(or
nil
if key
is not present in message
) and must return a two-element
tuple: the “get” value (the retrieved value, which can be operated on before
being returned) and the new value to be stored under header
in the
resulting new message. fun
may also return :pop
, which means the current
value shall be removed from message
and returned (making this function
behave like Sippet.Message.pop_header_front(message, header)
. The returned
value is a tuple with the “get” value returned by fun
and a new message
with the updated values under header
.
Gets all values for a specific header
in message
.
If header
is present in message
, then all values are returned in a list.
Otherwise, default
is returned (which is nil
unless specified otherwise).
Gets the last value of a specific header
in message
.
If header
is present in message
, then the last value is returned.
Otherwise, default
is returned (which is nil
unless specified otherwise).
Gets the first value of a specific header
in message
.
If header
is present in message
, then the first value is returned.
Otherwise, default
is returned (which is nil
unless specified otherwise).
Returns whether a given header
exists in the given message
.
Returns a list of all known methods, as a list of uppercase strings.
Example:
iex> Sippet.Message.known_methods()
["ACK", "BYE", "CANCEL", "INFO", "INVITE", "MESSAGE", "NOTIFY", "OPTIONS",
"PRACK", "PUBLISH", "PULL", "PUSH", "REFER", "REGISTER", "STORE", "SUBSCRIBE",
"UPDATE"]
Returns a list of all known transport protocols, as a list of uppercase strings.
Example:
iex> Sippet.Message.known_protocols()
["AMQP", "DCCP", "DTLS", "SCTP", "STOMP", "TCP", "TLS", "UDP", "WS", "WSS"]
Parses a SIP message header block as received by the transport layer.
In order to correctly set the message body, you have to verify the
:content_length
header; if it exists, it reflects the body size and you
have to set it manually on the returned message.
Parses a SIP message header block as received by the transport layer.
Raises if the string is an invalid SIP header.
In order to correctly set the message body, you have to verify the
:content_length
header; if it exists, it reflects the body size and you
have to set it manually on the returned message.
Returns and removes the values associated with header
in message
.
If header
is present in message
with values [value]
, {[value],
new_message}
is returned where new_message
is the result of removing
header
from message
. If header
is not present in message
, {default,
message}
is returned.
Returns and removes the last value associated with header
in message
.
If header
is present in message
with values values
,
{List.last(values), new_message}
is returned where new_message
is the
result of removing List.last(values)
from header
. If header
is not
present in message
or it is an empty list, {default, message}
is
returned. When the header
results in an empty list, message
gets updated
by removing the header.
Returns and removes the first value associated with header
in message
.
If header
is present in message
with values values
,
{List.first(values), new_message}
is returned where new_message
is the
result of removing List.first(values)
from header
. If header
is not
present in message
or it is an empty list, {default, message}
is
returned. When the header
results in an empty list, message
gets updated
by removing the header.
Puts the value
under header
on the message
.
Puts the value
under header
on the message
, as last element.
If the parameter value
is nil
, then the empty list will be appended to
the header
.
Puts the value
under header
on the message
, as front element.
If the parameter value
is nil
, then the empty list will be prefixed to
the header
.
Puts the value
under header
on the message
unless the header
already
exists.
Evaluates fun
and puts the result under header
in message
unless
header
is already present.
This function is useful in case you want to compute the value to put under
header
only if header
is not already present (e.g., the value is
expensive to calculate or generally difficult to setup and teardown again).
Shortcut to check if the message is a request.
Shortcut to check if the message is a response.
Returns the iodata representation of the given Sippet.Message
struct.
Converts a string representing a known method into an atom, otherwise as an uppercase string.
Example:
iex> Sippet.Message.to_method("INVITE")
:invite
iex> Sippet.Message.to_method("InViTe")
:invite
iex> Sippet.Message.to_method("aaa")
"AAA"
Converts a string representing a known protocol into an atom, otherwise as an uppercase string.
Example:
iex> Sippet.Message.to_protocol("UDP")
:udp
iex> Sippet.Message.to_protocol("uDp")
:udp
iex> Sippet.Message.to_protocol("aaa")
"AAA"
Returns the string representation of the given Sippet.Message
struct.
Updates the header
in message
with the given function.
If header
is present in message
with value value
, fun
is invoked
with argument value
and its result is used as the new value of header
.
If header
is not present in message
, initial
is inserted as the value
of header
.
Updates the last header
value in message
with the given function.
If header
is present in message
with value [value]
, fun
is invoked
with for last element of [value]
and its result is used as the new value of
header
back. If header
is not present in message
, or it is an empty
list, initial
is inserted as the single value of header
.
Updates the first header
value in message
with the given function.
If header
is present in message
with value [value]
, fun
is invoked
with for first element of [value]
and its result is used as the new value
of header
front. If header
is not present in message
, or it is an empty
list, initial
is inserted as the single value of header
.
Checks whether a message is valid.