pub struct Response { /* private fields */ }Expand description
Represents an HTTP response.
§Fields
version: The HTTP version of the response (e.g., HTTP/1.1).status_code: The status code of the response (e.g., 200, 404).reason_phrase: The reason phrase corresponding to the status code (e.g., OK, Not Found).headers: A collection of HTTP headers as key-value pairs.body: The binary body of the response.response: The serialized HTTP response including headers and body.
Implementations§
Source§impl Response
impl Response
Sourcepub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Response
pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Response
Adds a header to the response.
This function inserts a key-value pair into the response headers.
The key and value are converted into String, allowing for efficient handling of both owned and borrowed string data.
§Parameters
key: The header key, which will be converted into aString.value: The value of the header, which will be converted into aString.
§Returns
- Returns a mutable reference to the current instance (
&mut Self), allowing for method chaining.
Sourcepub fn build(&mut self) -> Vec<u8> ⓘ
pub fn build(&mut self) -> Vec<u8> ⓘ
Builds the full HTTP response as a byte vector.
§Returns
- The serialized HTTP response including headers and body.
Sourcepub fn close(&mut self, stream: &TcpStream) -> Result<(), Error>
pub fn close(&mut self, stream: &TcpStream) -> 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 theTcpStreamthat will be closed after sending the response.
§Returns
CloseStreamResult: The result of the operation, indicating whether the closure was successful or if an error occurred.
Sourcepub fn async_close(&mut self, stream: &mut TcpStream) -> Result<(), Error>
pub fn async_close(&mut self, stream: &mut TcpStream) -> 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 theTcpStreamthat will be closed after sending the response.
§Returns
CloseStreamResult: The result of the operation, indicating whether the closure was successful or if an error occurred.
Source§impl Response
impl Response
pub fn get_version(&self) -> &String
pub fn get_mut_version(&mut self) -> &mut String
pub fn set_version(&mut self, val: String) -> &mut Response
pub fn get_status_code(&self) -> &usize
pub fn get_mut_status_code(&mut self) -> &mut usize
pub fn set_status_code(&mut self, val: usize) -> &mut Response
pub fn get_reason_phrase(&self) -> &String
pub fn get_mut_reason_phrase(&mut self) -> &mut String
pub fn set_reason_phrase(&mut self, val: String) -> &mut Response
pub fn get_headers(&self) -> &HashMap<String, String>
pub fn get_mut_headers(&mut self) -> &mut HashMap<String, String>
pub fn set_headers(&mut self, val: HashMap<String, String>) -> &mut Response
pub fn get_body(&self) -> &Vec<u8> ⓘ
pub fn get_mut_body(&mut self) -> &mut Vec<u8> ⓘ
pub fn set_body(&mut self, val: Vec<u8>) -> &mut Response
pub fn get_response(&self) -> &Vec<u8> ⓘ
pub fn get_mut_response(&mut self) -> &mut Vec<u8> ⓘ
pub fn set_response(&mut self, val: Vec<u8>) -> &mut Response
Trait Implementations§
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
Mutably borrows from an owned value. Read more