Struct rouille::Request[][src]

pub struct Request { /* fields omitted */ }

Represents a request that your handler must answer to.

This can be either a real request (received by the HTTP server) or a mock object created with one of the fake_* constructors.

Implementations

impl Request[src]

pub fn fake_http<U, M>(
    method: M,
    url: U,
    headers: Vec<(String, String)>,
    data: Vec<u8>
) -> Request where
    U: Into<String>,
    M: Into<String>, 
[src]

Builds a fake HTTP request to be used during tests.

The remote address of the client will be 127.0.0.1:12345. Use fake_http_from to specify what the client’s address should be.

pub fn fake_http_from<U, M>(
    from: SocketAddr,
    method: M,
    url: U,
    headers: Vec<(String, String)>,
    data: Vec<u8>
) -> Request where
    U: Into<String>,
    M: Into<String>, 
[src]

Builds a fake HTTP request to be used during tests.

pub fn fake_https<U, M>(
    method: M,
    url: U,
    headers: Vec<(String, String)>,
    data: Vec<u8>
) -> Request where
    U: Into<String>,
    M: Into<String>, 
[src]

Builds a fake HTTPS request to be used during tests.

The remote address of the client will be 127.0.0.1:12345. Use fake_https_from to specify what the client’s address should be.

pub fn fake_https_from<U, M>(
    from: SocketAddr,
    method: M,
    url: U,
    headers: Vec<(String, String)>,
    data: Vec<u8>
) -> Request where
    U: Into<String>,
    M: Into<String>, 
[src]

Builds a fake HTTPS request to be used during tests.

pub fn remove_prefix(&self, prefix: &str) -> Option<Request>[src]

If the decoded URL of the request starts with prefix, builds a new Request that is the same as the original but without that prefix.

Example

fn handle(request: &Request) -> Response {
    if let Some(request) = request.remove_prefix("/static") {
        return rouille::match_assets(&request, "/static");
    }

    // ...
}

pub fn is_secure(&self) -> bool[src]

Returns true if the request uses HTTPS, and false if it uses HTTP.

Example

use rouille::{Request, Response};

fn handle(request: &Request) -> Response {
    if !request.is_secure() {
        return Response::redirect_303(format!("https://example.com"));
    }

    // ...
}

pub fn method(&self) -> &str[src]

Returns the method of the request (GET, POST, etc.).

pub fn raw_url(&self) -> &str[src]

Returns the raw URL requested by the client. It is not decoded and thus can contain strings such as %20, and the query parameters such as ?p=hello.

See also url().

Example

use rouille::Request;

let request = Request::fake_http("GET", "/hello%20world?foo=bar", vec![], vec![]);
assert_eq!(request.raw_url(), "/hello%20world?foo=bar");

pub fn raw_query_string(&self) -> &str[src]

Returns the raw query string requested by the client. In other words, everything after the first ? in the raw url.

Returns the empty string if no query string.

pub fn url(&self) -> String[src]

Returns the URL requested by the client.

Contrary to raw_url, special characters have been decoded and the query string (eg ?p=hello) has been removed.

If there is any non-unicode character in the URL, it will be replaced with U+FFFD.

Note: This function will decode the token %2F will be decoded as /. However the official speficiations say that such a token must not count as a delimiter for URL paths. In other words, /hello/world is not the same as /hello%2Fworld.

Example

use rouille::Request;

let request = Request::fake_http("GET", "/hello%20world?foo=bar", vec![], vec![]);
assert_eq!(request.url(), "/hello world");

pub fn get_param(&self, param_name: &str) -> Option<String>[src]

Returns the value of a GET parameter. TODO: clumbsy

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

Returns the value of a header of the request.

Returns None if no such header could be found.

pub fn headers(&self) -> HeadersIter<'_>

Notable traits for HeadersIter<'a>

impl<'a> Iterator for HeadersIter<'a> type Item = (&'a str, &'a str);
[src]

Returns a list of all the headers of the request.

pub fn do_not_track(&self) -> Option<bool>[src]

Returns the state of the DNT (Do Not Track) header.

If the header is missing or is malformed, None is returned. If the header exists, Some(true) is returned if DNT is 1 and Some(false) is returned if DNT is 0.

Example

use rouille::{Request, Response};

fn handle(request: &Request) -> Response {
    if !request.do_not_track().unwrap_or(false) {
        track_user(&request);
    }

    // ...
}

pub fn data(&self) -> Option<RequestBody<'_>>[src]

Returns the body of the request.

The body can only be retrieved once. Returns None is the body has already been retreived before.

Example

use std::io::Read;
use rouille::{Request, Response, ResponseBody};

fn echo(request: &Request) -> Response {
    let mut data = request.data().expect("Oops, body already retrieved, problem \
                                          in the server");

    let mut buf = Vec::new();
    match data.read_to_end(&mut buf) {
        Ok(_) => (),
        Err(_) => return Response::text("Failed to read body")
    };

    Response {
        data: ResponseBody::from_data(buf),
        .. Response::text("")
    }
}

pub fn remote_addr(&self) -> &SocketAddr[src]

Returns the address of the client that made this request.

Example

use rouille::{Request, Response};

fn handle(request: &Request) -> Response {
    Response::text(format!("Your IP is: {:?}", request.remote_addr()))
}

Trait Implementations

impl Debug for Request[src]

Auto Trait Implementations

impl RefUnwindSafe for Request

impl Send for Request

impl Sync for Request

impl Unpin for Request

impl UnwindSafe for Request

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.