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: u16
The HTTP status code (e.g., 200 for OK, 404 for Not Found).
status_text: String
The 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<W: Write>(&self, stream: &mut W) -> Result<(), ServerError>
pub fn send<W: Write>(&self, stream: &mut W) -> Result<(), ServerError>
Sends the response over the provided Write
stream.
This method writes the HTTP status line, headers, and body to the stream, ensuring the client receives the complete response.
§Arguments
stream
- A mutable reference to any stream that implementsWrite
.
§Returns
Ok(())
- If the response is successfully sent.Err(ServerError)
- If an error occurs while sending the response.