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 headersbody
: 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
impl Response
Sourcepub fn new(status: StatusCode) -> Response
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());
Sourcepub fn with_header(self, header: TypedHeader) -> Response
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);
Sourcepub fn set_headers(&mut self, headers: Vec<TypedHeader>)
pub fn set_headers(&mut self, headers: Vec<TypedHeader>)
Sets all headers from a Vec
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);
Sourcepub fn with_reason(self, reason: impl Into<String>) -> Response
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");
Sourcepub fn header(&self, name: &HeaderName) -> Option<&TypedHeader>
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());
Sourcepub fn reason_phrase(&self) -> &str
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");
Sourcepub fn typed_header<T>(&self) -> Option<&T>
pub fn typed_header<T>(&self) -> Option<&T>
Retrieves the first header with the specified type, if any.
Sourcepub fn from(&self) -> Option<&From>
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());
Sourcepub fn to(&self) -> Option<&To>
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());
Sourcepub fn cseq(&self) -> Option<&CSeq>
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);
Sourcepub fn via_headers(&self) -> Vec<Via>
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);
Sourcepub fn first_via(&self) -> Option<Via>
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());
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Sourcepub fn body(&self) -> &[u8] ⓘ
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());
Sourcepub fn from_request(status: StatusCode, request: &Request) -> Response
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 responserequest
: 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));
Sourcepub fn all_headers(&self) -> &[TypedHeader]
pub fn all_headers(&self) -> &[TypedHeader]
Sourcepub fn body_bytes(&self) -> &Bytes
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));
Sourcepub fn status_code(&self) -> u16
pub fn status_code(&self) -> u16
Sourcepub fn via_branch(&self) -> Option<String>
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
Sourcepub fn first_via_branch(&self) -> Option<String>
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
Sourcepub fn all_via_branches(&self) -> Vec<String>
pub fn all_via_branches(&self) -> Vec<String>
Sourcepub fn all_via_transports(&self) -> Vec<String>
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
Sourcepub fn all_via_hosts(&self) -> Vec<String>
pub fn all_via_hosts(&self) -> Vec<String>
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Response
impl<'de> Deserialize<'de> for Response
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Response, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Response, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl HeaderAccess for Response
impl HeaderAccess for Response
Source§fn typed_header<T>(&self) -> Option<&T>
fn typed_header<T>(&self) -> Option<&T>
Source§fn headers(&self, name: &HeaderName) -> Vec<&TypedHeader>
fn headers(&self, name: &HeaderName) -> Vec<&TypedHeader>
Source§fn header(&self, name: &HeaderName) -> Option<&TypedHeader>
fn header(&self, name: &HeaderName) -> Option<&TypedHeader>
Source§fn headers_by_name(&self, name: &str) -> Vec<&TypedHeader>
fn headers_by_name(&self, name: &str) -> Vec<&TypedHeader>
Source§fn raw_header_value(&self, name: &HeaderName) -> Option<String>
fn raw_header_value(&self, name: &HeaderName) -> Option<String>
Source§fn raw_headers(&self, name: &HeaderName) -> Vec<Vec<u8>>
fn raw_headers(&self, name: &HeaderName) -> Vec<Vec<u8>>
Source§fn header_names(&self) -> Vec<HeaderName>
fn header_names(&self) -> Vec<HeaderName>
Source§fn has_header(&self, name: &HeaderName) -> bool
fn has_header(&self, name: &HeaderName) -> bool
Source§impl MessageExtensions for Response
impl MessageExtensions for Response
Source§fn body_string(&self) -> Option<String>
fn body_string(&self) -> Option<String>
Source§impl Serialize for Response
impl Serialize for Response
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl StructuralPartialEq for Response
Auto Trait Implementations§
impl !Freeze for Response
impl RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl UnwindSafe for Response
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> SipJson for Twhere
T: Serialize + DeserializeOwned,
impl<T> SipJson for Twhere
T: Serialize + DeserializeOwned,
Source§fn to_sip_value(&self) -> Result<SipValue, SipJsonError>
fn to_sip_value(&self) -> Result<SipValue, SipJsonError>
Source§fn from_sip_value(value: &SipValue) -> Result<T, SipJsonError>
fn from_sip_value(value: &SipValue) -> Result<T, SipJsonError>
Source§impl<T> SipJsonExt for T
impl<T> SipJsonExt for T
Source§fn path(&self, path: impl AsRef<str>) -> Option<SipValue>
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>
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
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