[−][src]Struct tiny_http::Request
Represents an HTTP request made by a client.
A Request
object is what is produced by the server, and is your what
your code must analyse and answer.
This object implements the Send
trait, therefore you can dispatch your requests to
worker threads.
Pipelining
If a client sends multiple requests in a row (without waiting for the response), then you will
get multiple Request
objects simultaneously. This is called requests pipelining.
Tiny-http automatically reorders the responses so that you don't need to worry about the order
in which you call respond
or into_writer
.
This mechanic is disabled if:
- The body of a request is large enough (handling requires pipelining requires storing the body of the request in a buffer ; if the body is too big, tiny-http will avoid doing that)
- A request sends a
Expect: 100-continue
header (which means that the client waits to know whether its body will be processed before sending it) - A request sends a
Connection: close
header orConnection: upgrade
header (used for websockets), which indicates that this is the last request that will be received on this connection
Automatic cleanup
If a Request
object is destroyed without into_writer
or respond
being called,
an empty response with a 500 status code (internal server error) will automatically be
sent back to the client.
This means that if your code fails during the handling of a request, this "internal server
error" response will automatically be sent during the stack unwinding.
Methods
impl Request
[src]
pub fn secure(&self) -> bool
[src]
Returns true if the request was made through HTTPS.
pub fn method(&self) -> &Method
[src]
Returns the method requested by the client (eg. GET
, POST
, etc.).
pub fn url(&self) -> &str
[src]
Returns the resource requested by the client.
pub fn headers(&self) -> &[Header]
[src]
Returns a list of all headers sent by the client.
pub fn http_version(&self) -> &HTTPVersion
[src]
Returns the HTTP version of the request.
pub fn body_length(&self) -> Option<usize>
[src]
Returns the length of the body in bytes.
Returns None
if the length is unknown.
pub fn remote_addr(&self) -> &SocketAddr
[src]
Returns the address of the client that sent this request.
Note that this is gathered from the socket. If you receive the request from a proxy, this function will return the address of the proxy and not the address of the actual user.
pub fn upgrade<R: Read>(
self,
protocol: &str,
response: Response<R>
) -> Box<dyn ReadWrite + Send>
[src]
self,
protocol: &str,
response: Response<R>
) -> Box<dyn ReadWrite + Send>
Sends a response with a Connection: upgrade
header, then turns the Request
into a Stream
.
The main purpose of this function is to support websockets. If you detect that the request wants to use some kind of protocol upgrade, you can call this function to obtain full control of the socket stream.
If you call this on a non-websocket request, tiny-http will wait until this Stream
object
is destroyed before continuing to read or write on the socket. Therefore you should always
destroy it as soon as possible.
pub fn as_reader(&mut self) -> &mut dyn Read
[src]
Allows to read the body of the request.
Example
let mut request = server.recv().unwrap(); if get_content_type(&request) == "application/json" { let mut content = String::new(); request.as_reader().read_to_string(&mut content).unwrap(); let json: Json = content.parse().unwrap(); }
If the client sent a Expect: 100-continue
header with the request, calling this
function will send back a 100 Continue
response.
pub fn into_writer(self) -> Box<dyn Write + Send + 'static>
[src]
Turns the Request
into a writer.
The writer has a raw access to the stream to the user. This function is useful for things like CGI.
Note that the destruction of the Writer
object may trigger
some events. For exemple if a client has sent multiple requests and the requests
have been processed in parallel, the destruction of a writer will trigger
the writing of the next response.
Therefore you should always destroy the Writer
as soon as possible.
pub fn respond<R>(self, response: Response<R>) -> Result<(), IoError> where
R: Read,
[src]
R: Read,
Sends a response to this request.
Trait Implementations
Auto Trait Implementations
impl Unpin for Request
impl !Sync for Request
impl Send for Request
impl !UnwindSafe for Request
impl !RefUnwindSafe for Request
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,