Struct mini_server::HTTPResponse

source ·
pub struct HTTPResponse {
    pub body: Vec<u8>,
    pub headers: Headers,
    pub status: u16,
    pub status_text: String,
    pub http_version: String,
}
Expand description

The HTTPResponse struct represents an HTTP response that the web server can send to clients. It encapsulates various components of an HTTP response, including the response body, headers, status code, status text, and the HTTP version.

use mini_server::{HTTPResponse, Headers};

fn create_http_response() -> HTTPResponse {
    let mut headers = Headers::new();
    headers.insert("Content-Type".into(), "text/plain".into());

    HTTPResponse {
        body: b"Hello, World!".to_vec(),
        headers,
        status: 200,
        status_text: "OK".to_string(),
        http_version: "1.1".to_string(),
    }
}

Fields§

§body: Vec<u8>

The response body as a vector of bytes.

§headers: Headers

A collection of HTTP headers included in the response.

§status: u16

The HTTP status code indicating the outcome of the request.

§status_text: String

The human-readable status text associated with the status code.

§http_version: String

The version of the HTTP protocol used for the response.

Implementations§

source§

impl HTTPResponse

source

pub fn new() -> Self

Get a new HTTPResponse struct

source

pub fn set_body(&mut self, body: Vec<u8>)

Allows updating the body of an HTTPResponse instance with a new vector of bytes (Vec<u8>). Additionally, it automatically updates the “Content-Length” header to reflect the length of the new body.

source

pub fn set_headers(&mut self, headers: Headers)

Update the headers of an HTTPResponse instance with a new set of headers provided as a Headers collection.

source

pub fn set_header(&mut self, k: &str, v: &str)

Insert/Update the HTTPResponse header

source

pub fn set_status(&mut self, status: u16)

The set_status method allows setting the HTTP status code for an HTTPResponse instance. It updates both the numeric status code (status) and the associated human-readable status text.

source

pub fn set_version(&mut self, version: String)

Set the HTTPResponse version (e.g. ‘1.1’)

source

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

The raw method generates the raw representation of an HTTP response, including the status line, headers, and body. It returns the formatted HTTP response as a vector of bytes (Vec<u8>).

use mini_server::HTTPResponse;
fn get_raw_response(response: &mut HTTPResponse) -> Vec<u8> {
    let raw_response = response.raw();

    // Accessing the raw HTTP response
    println!("Raw Response: {:?}", raw_response);
    raw_response
}

Trait Implementations§

source§

impl Debug for HTTPResponse

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for HTTPResponse

source§

fn default() -> Self

Returns the “default value” for a type. 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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.