Struct HttpRequest

Source
pub struct HttpRequest { /* private fields */ }
Expand description

HTTP Request

Represents an HTTP request

Implementations§

Source§

impl HttpRequest

Source§

impl HttpRequest

Source

pub fn parse(stream: impl Into<HttpStream>) -> Result<HttpRequest, HttpError>

Read and parse an HTTP request from the given HttpStream

Source

pub fn keep_alive(self) -> Result<HttpRequest, HttpError>

Source

pub fn stream(&self) -> &HttpStream

Source

pub fn url(&self) -> &str

Url of the request

Source

pub fn set_url(&mut self, url: impl Into<Box<str>>)

Source

pub fn params(&self) -> &HashMap<Box<str>, Box<str>>

Get the query parameters

Source

pub fn param(&self, key: &str) -> Option<&str>

Source

pub fn filename(&self) -> Result<Box<str>, HttpError>

Get the filename for the request

It computes the path in the server corresponding to the request’s url.

Source

pub fn write_to(&self, f: &mut dyn Write) -> Result<(), HttpError>

Writes the request into the given Write object.

Source

pub fn send_to(&self, stream: HttpStream) -> Result<HttpResponse, HttpError>

Sends the HttpRequest to a stream

§Errors

If the transfer fails, returns the error

Source

pub fn method(&self) -> &HttpMethod

Source

pub fn status(&self) -> u16

Source

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

Source

pub fn version(&self) -> f32

Source

pub fn content_length(&self) -> usize

Get the value of the Content-Length HTTP header

If the header is not present, or if it fails to parse it’s value, it returns 0

Source

pub fn header(&self, key: &str) -> Option<&str>

Get the value of the given header key, if present

Source

pub fn headers(&self) -> &HashMap<Box<str>, Box<str>>

Source

pub fn set_header( &mut self, key: impl Into<Box<str>>, value: impl Into<Box<str>>, )

Source

pub fn body(&mut self) -> Result<Option<&[u8]>, HttpError>

Reads the body from the stream into the request’s buffer.

§NOTE

This loads the whole body of the request into memory, and it’ll stick with the request for it’s lifetime. It’s not very efficient memory-wise for requests with big bodies.

For a nicer way to process a request’s body, see the read_body function.

§Errors

If some IO error happens when reading the body from the stream

§Returns

And option of &u8. A None variant means the request doesn’t have a body. For example, GET request don’t usually have a body.

Source

pub fn has_body(&self) -> Result<bool, HttpError>

Returns true if the stream has a body, and false if it’s empty.

This method is preferred to check the presence of a body, over calling body and checking the returned Option, since this function doesn’t allocate memory, nor mutates the request.

§Errors

If some IO error happens in the process of checking the stream’s availability

Source

pub fn read_body(&mut self, writer: &mut dyn Write) -> Result<(), HttpError>

Reads the request body into writer

§Errors

If, while reading or writing, some io Error is found

Source

pub fn respond(&mut self) -> Result<(), HttpError>

Respond to the request without a body

§Errors

If some io error is produced while sending the request

Source

pub fn respond_buf(&mut self, buf: &[u8]) -> Result<(), HttpError>

Respond to the request with the data of buf as a body

§Errors

If some io error is produced while sending the request

Source

pub fn respond_str(&mut self, text: &str) -> Result<(), HttpError>

Respond to the request with the given string

§Errors

If some io error is produced while sending the request

Source

pub fn respond_reader(&mut self, reader: &mut dyn Read) -> Result<(), HttpError>

Respond to the request with the data read from reader as a body

§Errors

If some io error is produced while sending the request

Source

pub fn respond_chunked( &mut self, reader: &mut dyn Read, ) -> Result<(), HttpError>

Respond to the request as a chunked transfer

This means that the Content-Length of the request doen’t need to be known.

§Errors

If some io error is produced while sending the request

Source

pub fn respond_error_page(&mut self) -> Result<(), HttpError>

Respond with a basic HTML error page

§Errors

If some io error is produced while sending the request

Source

pub fn ok(&mut self) -> Result<(), HttpError>

Respond to the request with an 200 OK status

§Errors

If some io error is produced while sending the request

Source

pub fn forbidden(&mut self) -> Result<(), HttpError>

Respond to the request with an 403 FORBIDDEN status

§Errors

If some io error is produced while sending the request

Source

pub fn unauthorized(&mut self) -> Result<(), HttpError>

Respond to the request with an 401 UNAUTHORIZED status

§Errors

If some io error is produced while sending the request

Source

pub fn not_found(&mut self) -> Result<(), HttpError>

Respond to the request with an 404 NOT FOUND status

§Errors

If some io error is produced while sending the request

Source

pub fn server_error(&mut self) -> Result<(), HttpError>

Respond to the request with an 500 INTERNAL SERVER ERROR status

§Errors

If some io error is produced while sending the request

Source

pub fn is_http_ok(&self) -> bool

Source

pub fn is_http_err(&self) -> bool

Source

pub fn status_msg(&self) -> &'static str

Source

pub fn error_page(&self) -> String

Returns a basic HTML error page of the given status

Trait Implementations§

Source§

impl Debug for HttpRequest

Source§

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

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

impl PartialEq for HttpRequest

Source§

fn eq(&self, other: &HttpRequest) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

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

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.