pub struct Request { /* private fields */ }
Expand description
Contains information about a single request.
Implementations§
Source§impl Request
impl Request
Sourcepub fn new(
method: Span,
path: Span,
headers: Vec<(Span, Span)>,
body: Span,
buffer: Vec<u8>,
) -> Request
pub fn new( method: Span, path: Span, headers: Vec<(Span, Span)>, body: Span, buffer: Vec<u8>, ) -> Request
Create a new Request from a raw one. You probably want
default()
to get an empty Request
.
Sourcepub fn from_reader<R: Read>(reader: R) -> Result<Request>
pub fn from_reader<R: Read>(reader: R) -> Result<Request>
Read a raw HTTP request from reader
and create an
appropriate Request
to represent it.
Sourcepub fn from_path(path: &str) -> Request
pub fn from_path(path: &str) -> Request
Create a request from an arbitrary path. Used in testing.
Sourcepub fn set_path(&mut self, path: &str)
pub fn set_path(&mut self, path: &str)
Give a request an arbitrary path
. Can be used in tests or
with filter
.
Sourcepub fn with_path(self, path: &str) -> Request
pub fn with_path(self, path: &str) -> Request
Give a request an arbitrary path
. Can be used in tests or
with filter
.
Sourcepub fn body(&self) -> &str
pub fn body(&self) -> &str
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.
Sourcepub fn set_body<S: AsRef<str>>(&mut self, body: S)
pub fn set_body<S: AsRef<str>>(&mut self, body: S)
Give this Request an arbitrary body from a string.
Sourcepub fn with_body<S: AsRef<str>>(self, body: S) -> Request
pub fn with_body<S: AsRef<str>>(self, body: S) -> Request
Give this Request an arbitrary body from a string and return the new Request.
Sourcepub fn set_method(&mut self, method: &str)
pub fn set_method(&mut self, method: &str)
Give this Request a new HTTP Method.
Sourcepub fn with_method(self, method: &str) -> Request
pub fn with_method(self, method: &str) -> Request
Give this Request a new HTTP Method and return the new Request.
Sourcepub fn arg(&self, name: &str) -> Option<&str>
pub fn arg(&self, name: &str) -> Option<&str>
In a route defined with routes!
like "/names/:name"
,
calling request.arg("name")
will return Some("peter")
when
the request is /names/peter
.
Sourcepub fn set_arg(&mut self, name: String, value: String)
pub fn set_arg(&mut self, name: String, value: String)
Replace or set a new value for an arbitrary URL argument from
a filter
or in a test.
Sourcepub fn header(&self, name: &str) -> Option<Cow<'_, str>>
pub fn header(&self, name: &str) -> Option<Cow<'_, str>>
Get a header value. name
is case insensitive.
Sourcepub fn set_form(&mut self, name: &str, value: &str)
pub fn set_form(&mut self, name: &str, value: &str)
Replace or set a new value for an arbitrary URL argument from
a filter
or in a test.
Sourcepub fn parse_form(&mut self)
pub fn parse_form(&mut self)
Parse and decode form POST data into a Hash. Should be called when this Request is created.
Sourcepub fn cache<T, F>(&self, fun: F) -> &T
pub fn cache<T, F>(&self, fun: F) -> &T
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();
}
Sourcepub fn state<T: Send + Sync + 'static>(&self) -> &T
pub fn state<T: Send + Sync + 'static>(&self) -> &T
Access to global shared state defined with the
vial::use_state!
macro before
starting your application with vial::run!
.
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!();
}