[][src]Struct vial::Request

pub struct Request { /* fields omitted */ }

Request contains all the info about a client's request. It's handed to your actions and filters, and is dropped after responding to the client.

The main ways you'll be using Request are:

  • arg(&str): Getting arguments from routes that include parameters, such as GET "/:page.md" => show;. You'd use request.arg("page") in this case.
  • query(&str): Getting decoded query parameters. If a request includes ?id=5&loc=CA you can use request.query("id") and request.query("loc") to get both values.
  • form(&str): Same as above, but with submitted <form> data. Make sure your <input> elements have the right name= attribute.
  • path(): The path requested, starting with an /, not including any ?query.
  • full_path(): The full path starting with /, including ?query.
  • method(): If you need the HTTP method.
  • cache(): The request local cache.

You may also modify a Request in a filter using:

Implementations

impl Request[src]

pub fn new(
    method: Span,
    path: Span,
    headers: Vec<(Span, Span)>,
    body: Span,
    buffer: Vec<u8>
) -> Request
[src]

Create a new Request from a raw one. You probably want default() to get an empty Request.

pub fn default() -> Request[src]

Produce an empty Request.

pub fn from_reader<R: Read>(reader: R) -> Result<Request>[src]

Read a raw HTTP request from reader and create an appropriate Request to represent it.

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

Path requested, starting with / and not including ?query.

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

Full path requested, starting with / and including ?query.

pub fn from_path(path: &str) -> Request[src]

Create a request from an arbitrary path. Used in testing.

pub fn set_path(&mut self, path: &str)[src]

Give a request an arbitrary path. Can be used in tests or with filter.

pub fn with_path(self, path: &str) -> Request[src]

Give a request an arbitrary path. Can be used in tests or with filter.

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

Raw body of HTTP request. If you are using methods like with_path or set_arg this will not accurately represent the raw HTTP request that was made.

pub fn set_body<S: AsRef<str>>(&mut self, body: S)[src]

Give this Request an arbitrary body from a string.

pub fn with_body<S: AsRef<str>>(self, body: S) -> Request[src]

Give this Request an arbitrary body from a string and return the new Request.

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

HTTP Method

pub fn set_method(&mut self, method: &str)[src]

Give this Request a new HTTP Method.

pub fn with_method(self, method: &str) -> Request[src]

Give this Request a new HTTP Method and return the new Request.

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

In a route defined with routes! like "/names/:name", calling request.arg("name") will return Some("peter") when the request is /names/peter.

pub fn set_arg(&mut self, name: String, value: String)[src]

Replace or set a new value for an arbitrary URL argument from a filter or in a test.

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

Get a header value. name is case insensitive.

pub fn has_form(&mut self, name: &str) -> bool[src]

Was the given form value sent?

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

Return a value from the POSTed form data.

pub fn set_form(&mut self, name: &str, value: &str)[src]

Replace or set a new value for an arbitrary URL argument from a filter or in a test.

pub fn parse_form(&mut self)[src]

Parse and decode form POST data into a Hash. Should be called when this Request is created.

pub fn has_query(&self, name: &str) -> bool[src]

Was the given query value sent?

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

Return a value from the ?querystring=

pub fn cache<T, F>(&self, fun: F) -> &T where
    F: FnOnce(&Request) -> T,
    T: Send + Sync + 'static, 
[src]

Request's cache() lives for only a single Request, but can nonethenevertheless be useful to prevent looking up the same data over and over. The cache is based on the return type of the function or closure you pass to cache() using TypeCache, so make sure to create little wrapper structs if you want different functions to return the same common types, like Vec<String>:

struct PageNames(Vec<String>);
struct UserNames(Vec<String>);

Here's an example:

use vial::prelude::*;
use page::Page;

routes! {
    GET "/" => list;
}

struct PageNames(Vec<String>);

fn all_pages(_: &Request) -> Vec<Page> {
    db::lookup("select * from pages")
}

fn page_names(req: &Request) -> PageNames {
    PageNames(req.cache(all_pages)
        .iter()
        .map(|page| page.name.clone())
        .collect::<Vec<_>>())
}

fn list_of_names(req: &Request) -> String {
    req.cache(page_names)
        .0
        .iter()
        .map(|name| format!("<li>{}</li>", name))
        .collect::<Vec<_>>()
        .join("\n")
}

fn list(req: Request) -> impl Responder {
    format!(
        "<html>
            <head><title>{title}</title></head>
            <body>
                <h1>{title}</h1>
                <h3>There are {page_count} pages:</h3>
                <ul>
                    {pages}
                </ul>
            </body>
        </html>",
        title = "List Pages",
        page_count = req.cache(all_pages).len(),
        pages = req.cache(list_of_names),
    )
}

fn main() {
    run!().unwrap();
}

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.