Struct 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 Clone for HTTPResponse

Source§

fn clone(&self) -> HTTPResponse

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
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
Source§

impl Into<HTTPResponse> for &[u8]

Source§

fn into(self) -> HTTPResponse

Converts this type into the (usually inferred) input type.
Source§

impl Into<HTTPResponse> for &str

Source§

fn into(self) -> HTTPResponse

Converts this type into the (usually inferred) input type.
Source§

impl Into<HTTPResponse> for String

Source§

fn into(self) -> HTTPResponse

Converts this type into the (usually inferred) input type.
Source§

impl Into<HTTPResponse> for Vec<u8>

Source§

fn into(self) -> HTTPResponse

Converts this type into the (usually inferred) input type.

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