mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-28 01:52:57 +01:00
store cookies in extensions
This commit is contained in:
parent
ef89430f9b
commit
b9d870645f
@ -39,7 +39,6 @@ pub struct HttpInnerMessage {
|
|||||||
pub headers: HeaderMap,
|
pub headers: HeaderMap,
|
||||||
pub extensions: Extensions,
|
pub extensions: Extensions,
|
||||||
pub params: Params<'static>,
|
pub params: Params<'static>,
|
||||||
pub cookies: Option<Vec<Cookie<'static>>>,
|
|
||||||
pub addr: Option<SocketAddr>,
|
pub addr: Option<SocketAddr>,
|
||||||
pub payload: Option<Payload>,
|
pub payload: Option<Payload>,
|
||||||
pub info: Option<ConnectionInfo<'static>>,
|
pub info: Option<ConnectionInfo<'static>>,
|
||||||
@ -48,6 +47,7 @@ pub struct HttpInnerMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Query(Params<'static>);
|
struct Query(Params<'static>);
|
||||||
|
struct Cookies(Vec<Cookie<'static>>);
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
enum RouterResource {
|
enum RouterResource {
|
||||||
@ -65,7 +65,6 @@ impl Default for HttpInnerMessage {
|
|||||||
flags: MessageFlags::empty(),
|
flags: MessageFlags::empty(),
|
||||||
params: Params::new(),
|
params: Params::new(),
|
||||||
addr: None,
|
addr: None,
|
||||||
cookies: None,
|
|
||||||
payload: None,
|
payload: None,
|
||||||
extensions: Extensions::new(),
|
extensions: Extensions::new(),
|
||||||
info: None,
|
info: None,
|
||||||
@ -90,7 +89,6 @@ impl HttpInnerMessage {
|
|||||||
self.addr = None;
|
self.addr = None;
|
||||||
self.info = None;
|
self.info = None;
|
||||||
self.flags = MessageFlags::empty();
|
self.flags = MessageFlags::empty();
|
||||||
self.cookies = None;
|
|
||||||
self.payload = None;
|
self.payload = None;
|
||||||
self.prefix = 0;
|
self.prefix = 0;
|
||||||
self.resource = RouterResource::Notset;
|
self.resource = RouterResource::Notset;
|
||||||
@ -124,7 +122,6 @@ impl HttpRequest<()> {
|
|||||||
payload,
|
payload,
|
||||||
params: Params::new(),
|
params: Params::new(),
|
||||||
extensions: Extensions::new(),
|
extensions: Extensions::new(),
|
||||||
cookies: None,
|
|
||||||
addr: None,
|
addr: None,
|
||||||
info: None,
|
info: None,
|
||||||
prefix: 0,
|
prefix: 0,
|
||||||
@ -402,7 +399,7 @@ impl<S> HttpRequest<S> {
|
|||||||
|
|
||||||
/// Load request cookies.
|
/// Load request cookies.
|
||||||
pub fn cookies(&self) -> Result<&Vec<Cookie<'static>>, CookieParseError> {
|
pub fn cookies(&self) -> Result<&Vec<Cookie<'static>>, CookieParseError> {
|
||||||
if self.as_ref().cookies.is_none() {
|
if let None = self.extensions().get::<Query>() {
|
||||||
let msg = self.as_mut();
|
let msg = self.as_mut();
|
||||||
let mut cookies = Vec::new();
|
let mut cookies = Vec::new();
|
||||||
for hdr in msg.headers.get_all(header::COOKIE) {
|
for hdr in msg.headers.get_all(header::COOKIE) {
|
||||||
@ -413,9 +410,9 @@ impl<S> HttpRequest<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
msg.cookies = Some(cookies);
|
msg.extensions.insert(Cookies(cookies));
|
||||||
}
|
}
|
||||||
Ok(&self.as_ref().cookies.as_ref().unwrap())
|
Ok(&self.extensions().get::<Cookies>().unwrap().0)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return request cookie.
|
/// Return request cookie.
|
||||||
@ -430,6 +427,12 @@ impl<S> HttpRequest<S> {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn set_cookies(&mut self, cookies: Option<Vec<Cookie<'static>>>) {
|
||||||
|
if let Some(cookies) = cookies {
|
||||||
|
self.extensions_mut().insert(Cookies(cookies));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get a reference to the Params object.
|
/// Get a reference to the Params object.
|
||||||
///
|
///
|
||||||
/// Params is a container for url parameters.
|
/// Params is a container for url parameters.
|
||||||
|
15
src/test.rs
15
src/test.rs
@ -355,12 +355,7 @@ impl<S: 'static> TestApp<S> {
|
|||||||
|
|
||||||
/// Register handler for "/"
|
/// Register handler for "/"
|
||||||
pub fn handler<H: Handler<S>>(&mut self, handler: H) {
|
pub fn handler<H: Handler<S>>(&mut self, handler: H) {
|
||||||
self.app = Some(
|
self.app = Some(self.app.take().unwrap().resource("/", |r| r.h(handler)));
|
||||||
self.app
|
|
||||||
.take()
|
|
||||||
.unwrap()
|
|
||||||
.resource("/", |r| r.h(handler)),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register middleware
|
/// Register middleware
|
||||||
@ -562,8 +557,8 @@ impl<S: 'static> TestRequest<S> {
|
|||||||
cookies,
|
cookies,
|
||||||
payload,
|
payload,
|
||||||
} = self;
|
} = self;
|
||||||
let req = HttpRequest::new(method, uri, version, headers, payload);
|
let mut req = HttpRequest::new(method, uri, version, headers, payload);
|
||||||
req.as_mut().cookies = cookies;
|
req.set_cookies(cookies);
|
||||||
req.as_mut().params = params;
|
req.as_mut().params = params;
|
||||||
let (router, _) = Router::new::<S>("/", ServerSettings::default(), Vec::new());
|
let (router, _) = Router::new::<S>("/", ServerSettings::default(), Vec::new());
|
||||||
req.with_state(Rc::new(state), router)
|
req.with_state(Rc::new(state), router)
|
||||||
@ -583,8 +578,8 @@ impl<S: 'static> TestRequest<S> {
|
|||||||
payload,
|
payload,
|
||||||
} = self;
|
} = self;
|
||||||
|
|
||||||
let req = HttpRequest::new(method, uri, version, headers, payload);
|
let mut req = HttpRequest::new(method, uri, version, headers, payload);
|
||||||
req.as_mut().cookies = cookies;
|
req.set_cookies(cookies);
|
||||||
req.as_mut().params = params;
|
req.as_mut().params = params;
|
||||||
req.with_state(Rc::new(state), router)
|
req.with_state(Rc::new(state), router)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user