pub struct Response {
pub status_code: u16,
pub status_text: String,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
}Expand description
Represents an HTTP response, including the status code, status text, headers, and body.
Fields§
§status_code: u16The HTTP status code (e.g., 200 for OK, 404 for Not Found).
status_text: StringThe HTTP status text associated with the status code (e.g., “OK”, “Not Found”).
headers: Vec<(String, String)>A list of headers in the response, each represented as a tuple containing the header name and its corresponding value.
body: Vec<u8>The body of the response, represented as a vector of bytes.
Implementations§
source§impl Response
impl Response
sourcepub fn new(status_code: u16, status_text: &str, body: Vec<u8>) -> Self
pub fn new(status_code: u16, status_text: &str, body: Vec<u8>) -> Self
Creates a new Response with the given status code, status text, and body.
The headers are initialized as an empty list and can be added later using the add_header method.
§Arguments
status_code- The HTTP status code for the response.status_text- The status text corresponding to the status code.body- The body of the response, represented as a vector of bytes.
§Returns
A new Response instance with the specified status code, status text, and body.
sourcepub fn add_header(&mut self, name: &str, value: &str)
pub fn add_header(&mut self, name: &str, value: &str)
Adds a header to the response.
This method allows you to add custom headers to the response, which will be included in the HTTP response when it is sent to the client.
§Arguments
name- The name of the header (e.g., “Content-Type”).value- The value of the header (e.g., “text/html”).
sourcepub fn send(&self, stream: &mut TcpStream) -> Result<(), ServerError>
pub fn send(&self, stream: &mut TcpStream) -> Result<(), ServerError>
Sends the response over the provided TcpStream.
This method writes the HTTP status line, headers, and body to the stream, ensuring
the client receives the complete response. It uses the write! macro and the TcpStream
to communicate with the client.
§Arguments
stream- A mutable reference to theTcpStreamover which the response will be sent.
§Returns
Ok(())- If the response is successfully sent.Err(ServerError)- If an error occurs while sending the response.
§Errors
This function returns a ServerError if there is any issue writing to the stream or
sending the response data.
Trait Implementations§
source§impl<'de> Deserialize<'de> for Response
impl<'de> Deserialize<'de> for Response
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for Response
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)