[][src]Struct vial::Request

pub struct Request { /* fields omitted */ }

Contains information about a single request.

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<Cow<'_, 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();
}

pub fn state<T: Send + Sync + 'static>(&self) -> &T[src]

Access to global shared state defined with the vial::use_state! macro before starting your application with vial::run!.

This example is not tested
use std::sync::atomic::{AtomicUsize, Ordering};
use vial::prelude::*;

routes! {
    #[filter(count)]
    GET "/" => |req|
        format!("Hits: {}", req.state::<Counter>.hits());
}

fn count(req: &mut Request) -> Option<Response> {
    req.state::<Counter>().incr();
    None
}

#[derive(Debug, Default)]
struct Counter(AtomicUsize);

impl Counter {
    fn hits(&self) -> usize {
        self.0.load(Ordering::Relaxed)
    }
    fn incr(&self) {
        self.0.fetch_add(1, Ordering::Relaxed);
    }
}

fn main() {
    use_state!(Counter::default());
    run!();
}

Trait Implementations

impl Debug for Request[src]

Auto Trait Implementations

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.