Struct Response

Source
pub struct Response { /* private fields */ }
Expand description

Represents an HTTP response.

§Fields

  • version: The HTTP version of the response.
  • status_code: The status code of the response.
  • reason_phrase: The reason phrase corresponding to the status code.
  • headers: A collection of HTTP headers as key-value pairs.
  • body: The binary body of the response.

Implementations§

Source§

impl Response

Source

pub fn new() -> Response

Creates a new instance of Response.

§Returns
  • An initialized Response with default values.
Source

pub fn get_header<K>(&self, key: K) -> Option<String>
where K: Into<String>,

Retrieves the value of a response header by its key.

§Parameters
  • key: The header’s key, which can be of any type that implements Into<ResponseHeadersKey>.
§Returns
  • OptionResponseHeadersValue: Returns Some(value) if the key exists in the response headers, or None if the key does not exist.
Source

pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Response
where K: Into<String>, V: Into<String>,

Adds a header to the response.

This function inserts a key-value pair into the response headers. The key and value are converted into ResponseHeadersKey, allowing for efficient handling of both owned and borrowed string data.

§Parameters
  • key: The header key, which will be converted into a ResponseHeadersKey.
  • value: The value of the header, which will be converted into a ResponseHeadersValue.
§Returns
  • Returns a mutable reference to the current instance (&mut Self), allowing for method chaining.
Source

pub fn set_body<T>(&mut self, body: T) -> &mut Response
where T: Into<Vec<u8>>,

Set the body of the response.

This method allows you to set the body of the response by converting the provided value into a ResponseBody type. The body is updated with the converted value, and the method returns a mutable reference to the current instance for method chaining.

§Parameters
  • body: The body of the response to be set. It can be any type that can be converted into a ResponseBody using the Into trait.
§Return Value
  • Returns a mutable reference to the current instance of the struct, enabling method chaining. Set the body of the response.

This method allows you to set the body of the response by converting the provided value into a ResponseBody type. The body is updated with the converted value, and the method returns a mutable reference to the current instance for method chaining.

§Parameters
  • body: The body of the response to be set. It can be any type that can be converted into a ResponseBody using the Into trait.
§Return Value
  • Returns a mutable reference to the current instance of the struct, enabling method chaining.
Source

pub fn set_reason_phrase<T>(&mut self, reason_phrase: T) -> &mut Response
where T: Into<String>,

Set the reason phrase of the response.

This method allows you to set the reason phrase of the response by converting the provided value into a ResponseReasonPhrase type. The reason_phrase is updated with the converted value, and the method returns a mutable reference to the current instance for method chaining.

§Parameters
  • reason_phrase: The reason phrase to be set for the response. It can be any type that can be converted into a ResponseReasonPhrase using the Into trait.
§Return Value
  • Returns a mutable reference to the current instance of the struct, enabling method chaining.
Source

pub async fn send_body( &mut self, stream_lock: &ArcRwLockStream, is_websocket: bool, ) -> Result<(), Error>

Sends the HTTP response body over a TCP stream.

§Parameters
  • stream: A mutable reference to the TcpStream to send the response.
  • is_websocket: Is websocket
§Returns
  • Ok: If the response body is successfully sent.
  • Err: If an error occurs during sending.
Source

pub async fn send(&mut self, stream_lock: &ArcRwLockStream) -> Result<(), Error>

Sends the HTTP response over a TCP stream.

§Parameters
  • stream: A mutable reference to the TcpStream to send the response.
§Returns
  • Ok: If the response is successfully sent.
  • Err: If an error occurs during sending.
Source

pub async fn flush( &mut self, stream_lock: &ArcRwLockStream, ) -> Result<(), Error>

Flush the TCP stream.

  • stream_lock: A reference to an ArcRwLockStream that manages the TCP stream.

  • Returns: A ResponseResult indicating success or failure.

Source

pub async fn close( &mut self, stream_lock: &ArcRwLockStream, ) -> Result<(), Error>

Closes the stream after sending the response.

This function is responsible for:

  • Building the response using the build() method.
  • Setting the response using the set_response() method.
  • Shutting down the write half of the TCP stream to indicate no more data will be sent.
§Parameters
  • stream: A reference to the TcpStream that will be closed after sending the response.
§Returns
  • ResponseResult: The result of the operation, indicating whether the closure was successful or if an error occurred.
Source

pub fn get_string(&self) -> String

Converts the response to a formatted string representation.

  • Returns: A String containing formatted response details.
Source§

impl Response

Source

pub fn get_version(&self) -> &String

Source

pub fn get_mut_version(&mut self) -> &mut String

Source

pub fn get_status_code(&self) -> &usize

Source

pub fn get_mut_status_code(&mut self) -> &mut usize

Source

pub fn set_status_code(&mut self, val: usize) -> &mut Response

Source

pub fn get_reason_phrase(&self) -> &String

Source

pub fn get_mut_reason_phrase(&mut self) -> &mut String

Source

pub fn get_headers( &self, ) -> &HashMap<String, String, BuildHasherDefault<Hasher>>

Source

pub fn get_mut_headers( &mut self, ) -> &mut HashMap<String, String, BuildHasherDefault<Hasher>>

Source

pub fn set_headers( &mut self, val: HashMap<String, String, BuildHasherDefault<Hasher>>, ) -> &mut Response

Source

pub fn get_body(&self) -> &Vec<u8>

Source

pub fn get_mut_body(&mut self) -> &mut Vec<u8>

Trait Implementations§

Source§

impl Clone for Response

Source§

fn clone(&self) -> Response

Returns a copy 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 Default for Response

Source§

fn default() -> Response

Returns the “default value” for a type. 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

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, 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> 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> AnySend for T
where T: Any + Send,

Source§

impl<T> AnySendClone for T
where T: Any + Send + Clone,

Source§

impl<T> AnySendSync for T
where T: Any + Send + Sync,

Source§

impl<T> AnySendSyncClone for T
where T: Any + Send + Sync + Clone,

Source§

impl<T> AnySync for T
where T: Any + Sync,

Source§

impl<T> AnySyncClone for T
where T: Any + Sync + Clone,

Source§

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

Source§

impl<T> MaybeSendSync for T