pub struct HttpRequest { /* private fields */ }
Expand description
HTTP Request
Represents an HTTP request
Implementations§
Source§impl HttpRequest
impl HttpRequest
pub fn builder() -> HttpRequestBuilder
Source§impl HttpRequest
impl HttpRequest
Sourcepub fn parse(stream: impl Into<HttpStream>) -> Result<HttpRequest, HttpError>
pub fn parse(stream: impl Into<HttpStream>) -> Result<HttpRequest, HttpError>
Read and parse an HTTP request from the given HttpStream
pub fn keep_alive(self) -> Result<HttpRequest, HttpError>
pub fn stream(&self) -> &HttpStream ⓘ
pub fn set_url(&mut self, url: impl Into<Box<str>>)
pub fn param(&self, key: &str) -> Option<&str>
Sourcepub fn filename(&self) -> Result<Box<str>, HttpError>
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.
Sourcepub fn write_to(&self, f: &mut dyn Write) -> Result<(), HttpError>
pub fn write_to(&self, f: &mut dyn Write) -> Result<(), HttpError>
Writes the request into the given Write object.
Sourcepub fn send_to(&self, stream: HttpStream) -> Result<HttpResponse, HttpError>
pub fn send_to(&self, stream: HttpStream) -> Result<HttpResponse, HttpError>
pub fn method(&self) -> &HttpMethod
pub fn status(&self) -> u16
pub fn set_status(&mut self, status: u16) -> &mut HttpRequest
pub fn version(&self) -> f32
Sourcepub fn content_length(&self) -> usize
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
Sourcepub fn header(&self, key: &str) -> Option<&str>
pub fn header(&self, key: &str) -> Option<&str>
Get the value of the given header key, if present
pub fn headers(&self) -> &HashMap<Box<str>, Box<str>>
pub fn set_header( &mut self, key: impl Into<Box<str>>, value: impl Into<Box<str>>, )
Sourcepub fn body(&mut self) -> Result<Option<&[u8]>, HttpError>
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.
Sourcepub fn has_body(&self) -> Result<bool, HttpError>
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
Sourcepub fn respond(&mut self) -> Result<(), HttpError>
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
Sourcepub fn respond_buf(&mut self, buf: &[u8]) -> Result<(), HttpError>
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
Sourcepub fn respond_str(&mut self, text: &str) -> Result<(), HttpError>
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
Sourcepub fn respond_reader(&mut self, reader: &mut dyn Read) -> Result<(), HttpError>
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
Sourcepub fn respond_chunked(
&mut self,
reader: &mut dyn Read,
) -> Result<(), HttpError>
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
Sourcepub fn respond_error_page(&mut self) -> Result<(), HttpError>
pub fn respond_error_page(&mut self) -> Result<(), HttpError>
Sourcepub fn ok(&mut self) -> Result<(), HttpError>
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
Sourcepub fn forbidden(&mut self) -> Result<(), HttpError>
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
Respond to the request with an 401 UNAUTHORIZED status
§Errors
If some io error is produced while sending the request
Sourcepub fn not_found(&mut self) -> Result<(), HttpError>
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
Sourcepub fn server_error(&mut self) -> Result<(), HttpError>
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
pub fn is_http_ok(&self) -> bool
pub fn is_http_err(&self) -> bool
pub fn status_msg(&self) -> &'static str
Sourcepub fn error_page(&self) -> String
pub fn error_page(&self) -> String
Returns a basic HTML error page of the given status