Struct Response

Source
pub struct Response {
    pub version: Version,
    pub status: StatusCode,
    pub reason: Option<String>,
    pub headers: Vec<TypedHeader>,
    pub body: Bytes,
}
Expand description

A SIP response message

Represents a SIP response sent from a server to a client, containing a status code, reason phrase, headers, and optional body. SIP responses acknowledge requests and provide information about their processing.

§Standard RFC Compliance

This implementation follows RFC 3261, which defines the Session Initiation Protocol.

§Fields

  • version: The SIP protocol version (typically SIP/2.0)
  • status: The response status code (e.g., 200 for OK, 404 for Not Found)
  • reason: Optional custom reason phrase (overrides the default for the status code)
  • headers: A list of message headers
  • body: The message body (optional)

§Examples

use rvoip_sip_core::prelude::*;

// Create a 200 OK response
let response = Response::new(StatusCode::Ok)
    .with_header(TypedHeader::From(From::new(Address::new_with_display_name("Alice", "sip:alice@example.com".parse().unwrap()))))
    .with_header(TypedHeader::To(To::new(Address::new_with_display_name("Bob", "sip:bob@example.com".parse().unwrap()))));

// Or use a convenience method for common responses
let trying = Response::trying();

Fields§

§version: Version

The SIP version

§status: StatusCode

The status code

§reason: Option<String>

Custom reason phrase (overrides the default for the status code)

§headers: Vec<TypedHeader>

The headers of the response (now typed)

§body: Bytes

The body of the response

Implementations§

Source§

impl Response

Source

pub fn new(status: StatusCode) -> Response

Creates a new SIP response with the specified status code

This initializes a response with SIP/2.0 version, the given status code, default reason phrase, empty headers, and empty body.

§Parameters
  • status: The SIP status code
§Returns

A new Response instance

§Examples
use rvoip_sip_core::prelude::*;

// Create a 404 Not Found response
let response = Response::new(StatusCode::NotFound);

assert_eq!(response.status, StatusCode::NotFound);
assert_eq!(response.reason_phrase(), "Not Found");
assert!(response.headers.is_empty());
assert!(response.body.is_empty());
Source

pub fn trying() -> Response

Creates a SIP 100 Trying response

§Returns

A new Response with 100 Trying status

§Examples
use rvoip_sip_core::prelude::*;

let response = Response::trying();
assert_eq!(response.status, StatusCode::Trying);
Source

pub fn ringing() -> Response

Creates a SIP 180 Ringing response

§Returns

A new Response with 180 Ringing status

§Examples
use rvoip_sip_core::prelude::*;

let response = Response::ringing();
assert_eq!(response.status, StatusCode::Ringing);
Source

pub fn ok() -> Response

Creates a SIP 200 OK response

§Returns

A new Response with 200 OK status

§Examples
use rvoip_sip_core::prelude::*;

let response = Response::ok();
assert_eq!(response.status, StatusCode::Ok);
Source

pub fn with_header(self, header: TypedHeader) -> Response

Adds a typed header to the response

§Parameters
  • header: The typed header to add
§Returns

Self for method chaining

§Examples
use rvoip_sip_core::prelude::*;

let response = Response::new(StatusCode::Ok)
    .with_header(TypedHeader::CallId(CallId::new("abc123")))
    .with_header(TypedHeader::MaxForwards(MaxForwards::new(70)));

assert_eq!(response.headers.len(), 2);
Source

pub fn set_headers(&mut self, headers: Vec<TypedHeader>)

Sets all headers from a Vec (used by parser)

This method replaces all existing headers with the provided ones.

§Parameters
  • headers: Vector of typed headers to set
§Examples
use rvoip_sip_core::prelude::*;

let mut response = Response::new(StatusCode::Ok);
let headers = vec![
    TypedHeader::CallId(CallId::new("abc123")),
    TypedHeader::MaxForwards(MaxForwards::new(70))
];

response.set_headers(headers);
assert_eq!(response.headers.len(), 2);
Source

pub fn with_reason(self, reason: impl Into<String>) -> Response

Sets a custom reason phrase for the response

§Parameters
  • reason: The custom reason phrase
§Returns

Self for method chaining

§Examples
use rvoip_sip_core::prelude::*;

let response = Response::new(StatusCode::Ok)
    .with_reason("Everything is Awesome");

assert_eq!(response.reason_phrase(), "Everything is Awesome");
Source

pub fn with_body(self, body: impl Into<Bytes>) -> Response

Sets the body of the response

This method also automatically adds or updates the Content-Length header.

§Parameters
  • body: The body content
§Returns

Self for method chaining

Source

pub fn header(&self, name: &HeaderName) -> Option<&TypedHeader>

Retrieves the first typed header with the specified name, if any

§Parameters
  • name: The header name to look for
§Returns

An optional reference to the first matching header

§Examples
use rvoip_sip_core::prelude::*;

let response = Response::new(StatusCode::Ok)
    .with_header(TypedHeader::CallId(CallId::new("abc123")));

let header = response.header(&HeaderName::CallId);
assert!(header.is_some());
Source

pub fn reason_phrase(&self) -> &str

Returns the reason phrase for this response

§Returns

A string slice with the reason phrase

§Examples
use rvoip_sip_core::prelude::*;

let response = Response::new(StatusCode::Ok);
assert_eq!(response.reason_phrase(), "OK");

// With custom reason
let custom = Response::new(StatusCode::Ok).with_reason("Everything is Awesome");
assert_eq!(custom.reason_phrase(), "Everything is Awesome");
Source

pub fn typed_header<T>(&self) -> Option<&T>
where T: TypedHeaderTrait + Debug + 'static, <T as TypedHeaderTrait>::Name: Debug,

Retrieves the first header with the specified type, if any.

Source

pub fn call_id(&self) -> Option<&CallId>

Get the Call-ID header value, if present

§Returns

An optional reference to the CallId

Source

pub fn from(&self) -> Option<&From>

Retrieves the From header value, if present

§Returns

An optional reference to the From header

§Examples
use rvoip_sip_core::prelude::*;
use std::str::FromStr;

// Create a URI for the address
let uri = Uri::from_str("sip:alice@atlanta.com").unwrap();
// Create an address with display name
let address = Address::new_with_display_name("Alice", uri);
// Create a From header with the address
let mut from = From::new(address);
// Add a tag parameter
from.set_tag("1928301774");
 
let response = Response::new(StatusCode::Ok)
    .with_header(TypedHeader::From(from.clone()));

let retrieved = response.from();
assert!(retrieved.is_some());
Source

pub fn to(&self) -> Option<&To>

Retrieves the To header value, if present

§Returns

An optional reference to the To header

§Examples
use rvoip_sip_core::prelude::*;

let to = To::new(Address::new_with_display_name("Bob", "sip:bob@example.com".parse().unwrap()));
let response = Response::new(StatusCode::Ok)
    .with_header(TypedHeader::To(to.clone()));

let retrieved = response.to();
assert!(retrieved.is_some());
Source

pub fn cseq(&self) -> Option<&CSeq>

Retrieves the CSeq header value, if present

§Returns

An optional reference to the CSeq header

§Examples
use rvoip_sip_core::prelude::*;

let cseq = CSeq::new(1, Method::Invite);
let response = Response::new(StatusCode::Ok)
    .with_header(TypedHeader::CSeq(cseq.clone()));

let retrieved = response.cseq();
assert!(retrieved.is_some());
assert_eq!(retrieved.unwrap().method().clone(), Method::Invite);
Source

pub fn via_headers(&self) -> Vec<Via>

Get all Via headers as structured Via objects

§Returns

A vector of all Via headers in the response

§Examples
use rvoip_sip_core::prelude::*;

let via = Via::new(
    "SIP", "2.0", "UDP",
    "example.com", Some(5060),
    vec![Param::branch("z9hG4bK123456")]
).unwrap();
let response = Response::new(StatusCode::Ok)
    .with_header(TypedHeader::Via(via.clone()));

let vias = response.via_headers();
assert_eq!(vias.len(), 1);
Source

pub fn first_via(&self) -> Option<Via>

Get the first Via header as a structured Via object

§Returns

The first Via header if present, or None

§Examples
use rvoip_sip_core::prelude::*;

let via = Via::new(
    "SIP", "2.0", "UDP",
    "example.com", Some(5060),
    vec![Param::branch("z9hG4bK123456")]
).unwrap();
let response = Response::new(StatusCode::Ok)
    .with_header(TypedHeader::Via(via.clone()));

let first_via = response.first_via();
assert!(first_via.is_some());
Source

pub fn status(&self) -> StatusCode

Returns the status code of the response

§Returns

The response status code

§Examples
use rvoip_sip_core::prelude::*;

let response = Response::new(StatusCode::Ok);
assert_eq!(response.status(), StatusCode::Ok);
Source

pub fn body(&self) -> &[u8]

Returns a reference to the body content

§Returns

A slice reference to the message body bytes

§Examples
use rvoip_sip_core::prelude::*;
use bytes::Bytes;

let body_content = "test body";
let response = Response::new(StatusCode::Ok)
    .with_body(Bytes::from(body_content));

assert_eq!(response.body(), body_content.as_bytes());
Source

pub fn from_request(status: StatusCode, request: &Request) -> Response

Creates a new response with all essential headers copied from a request

This is a convenience method that creates a response with:

  • From header (copied from request)
  • To header (copied from request)
  • Call-ID header (copied from request)
  • CSeq header (copied from request)
  • Via headers (copied from request)
§Parameters
  • status: The status code for the response
  • request: The request to copy headers from
§Returns

A well-formed Response with all required headers

§Examples
use rvoip_sip_core::prelude::*;
use rvoip_sip_core::types::headers::HeaderAccess;
use std::str::FromStr;

// Create a URI for the addresses
let alice_uri = Uri::from_str("sip:alice@example.com").unwrap();
let alice_addr = Address::new(alice_uri);
let from = From::new(alice_addr);

let bob_uri = Uri::from_str("sip:bob@example.com").unwrap();
let bob_addr = Address::new(bob_uri);
let to = To::new(bob_addr);

let request = Request::new(Method::Invite, "sip:bob@example.com".parse().unwrap())
    .with_header(TypedHeader::From(from))
    .with_header(TypedHeader::To(to))
    .with_header(TypedHeader::CallId(CallId::new("abc123")))
    .with_header(TypedHeader::CSeq(CSeq::new(1, Method::Invite)));

let response = Response::from_request(StatusCode::Ok, &request);

assert_eq!(response.status, StatusCode::Ok);
assert!(response.has_header(&HeaderName::From));
assert!(response.has_header(&HeaderName::To));
assert!(response.has_header(&HeaderName::CallId));
assert!(response.has_header(&HeaderName::CSeq));
Source

pub fn version(&self) -> Version

Returns the SIP version

§Returns

A clone of the response’s SIP version

§Examples
use rvoip_sip_core::prelude::*;

let response = Response::new(StatusCode::Ok);
assert_eq!(response.version(), Version::sip_2_0());
Source

pub fn all_headers(&self) -> &[TypedHeader]

Returns a reference to the response headers

§Returns

A slice of all TypedHeader objects in the response

§Examples
use rvoip_sip_core::prelude::*;

let response = Response::new(StatusCode::Ok)
    .with_header(TypedHeader::CallId(CallId::new("abc123")));

assert_eq!(response.all_headers().len(), 1);
Source

pub fn reason(&self) -> Option<&str>

Returns the custom reason if set

§Returns

An optional reference to the custom reason string

§Examples
use rvoip_sip_core::prelude::*;

let response = Response::new(StatusCode::Ok)
    .with_reason("Everything is Awesome");

assert_eq!(response.reason(), Some("Everything is Awesome"));
Source

pub fn body_bytes(&self) -> &Bytes

Returns a reference to the response body as Bytes

§Returns

A reference to the response body Bytes

§Examples
use rvoip_sip_core::prelude::*;
use bytes::Bytes;

let body_content = "test body";
let response = Response::new(StatusCode::Ok)
    .with_body(Bytes::from(body_content));

assert_eq!(response.body_bytes(), &Bytes::from(body_content));
Source

pub fn status_code(&self) -> u16

Returns the status code as a u16

§Returns

The numeric status code value

§Examples
use rvoip_sip_core::prelude::*;

let response = Response::new(StatusCode::Ok);
assert_eq!(response.status_code(), 200);
Source

pub fn from_tag(&self) -> Option<String>

Retrieves the From tag, if present

§Returns

An optional From tag string

Source

pub fn to_tag(&self) -> Option<String>

Retrieves the To tag, if present

§Returns

An optional To tag string

Source

pub fn via_branch(&self) -> Option<String>

Retrieves the Via branch from the first (topmost) Via header

§Returns

An optional Via branch string from the first Via header

Source

pub fn first_via_branch(&self) -> Option<String>

Retrieves the Via branch from the first (topmost) Via header

§Returns

An optional Via branch string from the first Via header

Source

pub fn all_via_branches(&self) -> Vec<String>

Retrieves all Via branches in order

§Returns

A vector of branch strings from all Via headers

Source

pub fn all_via_transports(&self) -> Vec<String>

Retrieves all Via transport protocols in order

§Returns

A vector of transport protocol strings from all Via headers

Source

pub fn all_via_hosts(&self) -> Vec<String>

Retrieves all Via hosts in order

§Returns

A vector of host strings from all Via headers

Trait Implementations§

Source§

impl Clone for Response

Source§

fn clone(&self) -> Response

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Response

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Response

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Response, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Response

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl HeaderAccess for Response

Source§

fn typed_headers<T>(&self) -> Vec<&T>
where T: TypedHeaderTrait + Debug + 'static, <T as TypedHeaderTrait>::Name: Debug,

Returns all headers with the specified type. Read more
Source§

fn typed_header<T>(&self) -> Option<&T>
where T: TypedHeaderTrait + Debug + 'static, <T as TypedHeaderTrait>::Name: Debug,

Returns the first header with the specified type, if any. Read more
Source§

fn headers(&self, name: &HeaderName) -> Vec<&TypedHeader>

Returns all headers with the specified name. Read more
Source§

fn header(&self, name: &HeaderName) -> Option<&TypedHeader>

Returns the first header with the specified name, if any. Read more
Source§

fn headers_by_name(&self, name: &str) -> Vec<&TypedHeader>

Returns all headers with the specified name as a string. Read more
Source§

fn raw_header_value(&self, name: &HeaderName) -> Option<String>

Returns the raw value of the first header with the specified name, if any. Read more
Source§

fn raw_headers(&self, name: &HeaderName) -> Vec<Vec<u8>>

Returns all raw values for headers with the specified name. Read more
Source§

fn header_names(&self) -> Vec<HeaderName>

Returns all header names present in the message. Read more
Source§

fn has_header(&self, name: &HeaderName) -> bool

Checks if a header with the specified name is present. Read more
Source§

impl MessageExtensions for Response

Source§

fn body_string(&self) -> Option<String>

Extract body as string if present
Source§

impl PartialEq for Response

Source§

fn eq(&self, other: &Response) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Response

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Response

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SipJson for T

Source§

fn to_sip_value(&self) -> Result<SipValue, SipJsonError>

Convert this type to a SipValue. Read more
Source§

fn from_sip_value(value: &SipValue) -> Result<T, SipJsonError>

Create this type from a SipValue. Read more
Source§

impl<T> SipJsonExt for T

Source§

fn path(&self, path: impl AsRef<str>) -> Option<SipValue>

Simple path accessor that returns an Option directly

Source§

fn path_str(&self, path: impl AsRef<str>) -> Option<String>

Get a string value at the given path

Source§

fn path_str_or(&self, path: impl AsRef<str>, default: &str) -> String

Get a string value at the given path, or return the default value if not found

Source§

fn to_sip_value(&self) -> Result<SipValue, SipJsonError>

Convert to a SipValue. Read more
Source§

fn from_sip_value(value: &SipValue) -> Result<T, SipJsonError>

Convert from a SipValue. Read more
Source§

fn get_path(&self, path: impl AsRef<str>) -> SipValue

Access a value via path notation (e.g., “headers.from.tag”). Read more
Source§

fn path_accessor(&self) -> PathAccessor

Get a PathAccessor for chained access to fields. Read more
Source§

fn query(&self, query_str: impl AsRef<str>) -> Vec<SipValue>

Query for values using a JSONPath-like syntax. Read more
Source§

fn to_json_string(&self) -> Result<String, SipJsonError>

Convert to a JSON string. Read more
Source§

fn to_json_string_pretty(&self) -> Result<String, SipJsonError>

Convert to a pretty-printed JSON string. Read more
Source§

fn from_json_str(json_str: &str) -> Result<T, SipJsonError>

Create from a JSON string. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> SipMessageJson for T
where T: SipJsonExt,