mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
load cookies automatically
This commit is contained in:
parent
0abb3863dc
commit
d595dd850e
@ -23,12 +23,10 @@ struct HttpMessage {
|
|||||||
version: Version,
|
version: Version,
|
||||||
method: Method,
|
method: Method,
|
||||||
uri: Uri,
|
uri: Uri,
|
||||||
prefix: usize,
|
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
extensions: Extensions,
|
extensions: Extensions,
|
||||||
params: Params<'static>,
|
params: Params<'static>,
|
||||||
cookies: Vec<Cookie<'static>>,
|
cookies: Option<Vec<Cookie<'static>>>,
|
||||||
cookies_loaded: bool,
|
|
||||||
addr: Option<SocketAddr>,
|
addr: Option<SocketAddr>,
|
||||||
payload: Payload,
|
payload: Payload,
|
||||||
info: Option<ConnectionInfo<'static>>,
|
info: Option<ConnectionInfo<'static>>,
|
||||||
@ -41,12 +39,10 @@ impl Default for HttpMessage {
|
|||||||
HttpMessage {
|
HttpMessage {
|
||||||
method: Method::GET,
|
method: Method::GET,
|
||||||
uri: Uri::default(),
|
uri: Uri::default(),
|
||||||
prefix: 0,
|
|
||||||
version: Version::HTTP_11,
|
version: Version::HTTP_11,
|
||||||
headers: HeaderMap::new(),
|
headers: HeaderMap::new(),
|
||||||
params: Params::default(),
|
params: Params::default(),
|
||||||
cookies: Vec::new(),
|
cookies: None,
|
||||||
cookies_loaded: false,
|
|
||||||
addr: None,
|
addr: None,
|
||||||
payload: Payload::empty(),
|
payload: Payload::empty(),
|
||||||
extensions: Extensions::new(),
|
extensions: Extensions::new(),
|
||||||
@ -68,12 +64,10 @@ impl HttpRequest<()> {
|
|||||||
Rc::new(HttpMessage {
|
Rc::new(HttpMessage {
|
||||||
method: method,
|
method: method,
|
||||||
uri: uri,
|
uri: uri,
|
||||||
prefix: 0,
|
|
||||||
version: version,
|
version: version,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
params: Params::default(),
|
params: Params::default(),
|
||||||
cookies: Vec::new(),
|
cookies: None,
|
||||||
cookies_loaded: false,
|
|
||||||
addr: None,
|
addr: None,
|
||||||
payload: payload,
|
payload: payload,
|
||||||
extensions: Extensions::new(),
|
extensions: Extensions::new(),
|
||||||
@ -119,14 +113,13 @@ impl<S> HttpRequest<S> {
|
|||||||
&mut self.as_mut().extensions
|
&mut self.as_mut().extensions
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn set_prefix(&mut self, idx: usize) {
|
|
||||||
self.as_mut().prefix = idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn prefix_len(&self) -> usize {
|
pub fn prefix_len(&self) -> usize {
|
||||||
self.0.prefix
|
if let Some(router) = self.router() {
|
||||||
|
router.prefix().len()
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read the Request Uri.
|
/// Read the Request Uri.
|
||||||
@ -225,37 +218,34 @@ impl<S> HttpRequest<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return request cookies.
|
/// Load request cookies.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn cookies(&self) -> &Vec<Cookie<'static>> {
|
pub fn cookies(&self) -> Result<&Vec<Cookie<'static>>, CookieParseError> {
|
||||||
&self.0.cookies
|
if self.0.cookies.is_none() {
|
||||||
}
|
|
||||||
|
|
||||||
/// Return request cookie.
|
|
||||||
pub fn cookie(&self, name: &str) -> Option<&Cookie> {
|
|
||||||
for cookie in &self.0.cookies {
|
|
||||||
if cookie.name() == name {
|
|
||||||
return Some(cookie)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Load cookies
|
|
||||||
pub fn load_cookies(&mut self) -> Result<&Vec<Cookie<'static>>, CookieParseError>
|
|
||||||
{
|
|
||||||
if !self.0.cookies_loaded {
|
|
||||||
let msg = self.as_mut();
|
let msg = self.as_mut();
|
||||||
msg.cookies_loaded = true;
|
let mut cookies = Vec::new();
|
||||||
if let Some(val) = msg.headers.get(header::COOKIE) {
|
if let Some(val) = msg.headers.get(header::COOKIE) {
|
||||||
let s = str::from_utf8(val.as_bytes())
|
let s = str::from_utf8(val.as_bytes())
|
||||||
.map_err(CookieParseError::from)?;
|
.map_err(CookieParseError::from)?;
|
||||||
for cookie in s.split("; ") {
|
for cookie in s.split("; ") {
|
||||||
msg.cookies.push(Cookie::parse_encoded(cookie)?.into_owned());
|
cookies.push(Cookie::parse_encoded(cookie)?.into_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
msg.cookies = Some(cookies)
|
||||||
|
}
|
||||||
|
Ok(self.0.cookies.as_ref().unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return request cookie.
|
||||||
|
pub fn cookie(&self, name: &str) -> Option<&Cookie> {
|
||||||
|
if let Ok(cookies) = self.cookies() {
|
||||||
|
for cookie in cookies {
|
||||||
|
if cookie.name() == name {
|
||||||
|
return Some(cookie)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(&self.0.cookies)
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a reference to the Params object.
|
/// Get a reference to the Params object.
|
||||||
@ -535,7 +525,7 @@ mod tests {
|
|||||||
let mut headers = HeaderMap::new();
|
let mut headers = HeaderMap::new();
|
||||||
headers.insert(header::HOST,
|
headers.insert(header::HOST,
|
||||||
header::HeaderValue::from_static("www.rust-lang.org"));
|
header::HeaderValue::from_static("www.rust-lang.org"));
|
||||||
let mut req = HttpRequest::new(
|
let req = HttpRequest::new(
|
||||||
Method::GET, Uri::from_str("/").unwrap(),
|
Method::GET, Uri::from_str("/").unwrap(),
|
||||||
Version::HTTP_11, headers, Payload::empty());
|
Version::HTTP_11, headers, Payload::empty());
|
||||||
|
|
||||||
@ -550,7 +540,7 @@ mod tests {
|
|||||||
assert_eq!(req.url_for("unknown", &["test"]),
|
assert_eq!(req.url_for("unknown", &["test"]),
|
||||||
Err(UrlGenerationError::RouterNotAvailable));
|
Err(UrlGenerationError::RouterNotAvailable));
|
||||||
|
|
||||||
let mut req = req.with_state(Rc::new(()), router);
|
let req = req.with_state(Rc::new(()), router);
|
||||||
|
|
||||||
assert_eq!(req.url_for("unknown", &["test"]),
|
assert_eq!(req.url_for("unknown", &["test"]),
|
||||||
Err(UrlGenerationError::ResourceNotFound));
|
Err(UrlGenerationError::ResourceNotFound));
|
||||||
@ -573,7 +563,7 @@ mod tests {
|
|||||||
let router = Router::new("", map);
|
let router = Router::new("", map);
|
||||||
assert!(!router.has_route("https://youtube.com/watch/unknown"));
|
assert!(!router.has_route("https://youtube.com/watch/unknown"));
|
||||||
|
|
||||||
let mut req = req.with_state(Rc::new(()), router);
|
let req = req.with_state(Rc::new(()), router);
|
||||||
let url = req.url_for("youtube", &["oHg5SJYRHA0"]);
|
let url = req.url_for("youtube", &["oHg5SJYRHA0"]);
|
||||||
assert_eq!(url.ok().unwrap().as_str(), "https://youtube.com/watch/oHg5SJYRHA0");
|
assert_eq!(url.ok().unwrap().as_str(), "https://youtube.com/watch/oHg5SJYRHA0");
|
||||||
}
|
}
|
||||||
|
@ -259,7 +259,7 @@ impl CookieSessionInner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn load(&self, req: &mut HttpRequest) -> HashMap<String, String> {
|
fn load(&self, req: &mut HttpRequest) -> HashMap<String, String> {
|
||||||
if let Ok(cookies) = req.load_cookies() {
|
if let Ok(cookies) = req.cookies() {
|
||||||
for cookie in cookies {
|
for cookie in cookies {
|
||||||
if cookie.name() == self.name {
|
if cookie.name() == self.name {
|
||||||
let mut jar = CookieJar::new();
|
let mut jar = CookieJar::new();
|
||||||
|
@ -54,7 +54,7 @@ impl<S> Router<S> {
|
|||||||
|
|
||||||
/// Router prefix
|
/// Router prefix
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn prefix(&self) -> &str {
|
pub fn prefix(&self) -> &str {
|
||||||
&self.0.prefix
|
&self.0.prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +74,6 @@ impl<S> Router<S> {
|
|||||||
|
|
||||||
if let Some(idx) = idx {
|
if let Some(idx) = idx {
|
||||||
let path: &str = unsafe{ mem::transmute(&req.path()[self.0.prefix.len()..]) };
|
let path: &str = unsafe{ mem::transmute(&req.path()[self.0.prefix.len()..]) };
|
||||||
req.set_prefix(self.prefix().len());
|
|
||||||
self.0.patterns[idx].update_match_info(path, req);
|
self.0.patterns[idx].update_match_info(path, req);
|
||||||
return Some(&self.0.resources[idx])
|
return Some(&self.0.resources[idx])
|
||||||
} else {
|
} else {
|
||||||
|
@ -20,12 +20,10 @@ fn test_debug() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_no_request_cookies() {
|
fn test_no_request_cookies() {
|
||||||
let mut req = HttpRequest::new(
|
let req = HttpRequest::new(
|
||||||
Method::GET, Uri::from_str("/").unwrap(),
|
Method::GET, Uri::from_str("/").unwrap(),
|
||||||
Version::HTTP_11, HeaderMap::new(), Payload::empty());
|
Version::HTTP_11, HeaderMap::new(), Payload::empty());
|
||||||
assert!(req.cookies().is_empty());
|
assert!(req.cookies().unwrap().is_empty());
|
||||||
let _ = req.load_cookies();
|
|
||||||
assert!(req.cookies().is_empty());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -34,12 +32,11 @@ fn test_request_cookies() {
|
|||||||
headers.insert(header::COOKIE,
|
headers.insert(header::COOKIE,
|
||||||
header::HeaderValue::from_static("cookie1=value1; cookie2=value2"));
|
header::HeaderValue::from_static("cookie1=value1; cookie2=value2"));
|
||||||
|
|
||||||
let mut req = HttpRequest::new(
|
let req = HttpRequest::new(
|
||||||
Method::GET, Uri::from_str("/").unwrap(),
|
Method::GET, Uri::from_str("/").unwrap(),
|
||||||
Version::HTTP_11, headers, Payload::empty());
|
Version::HTTP_11, headers, Payload::empty());
|
||||||
assert!(req.cookies().is_empty());
|
|
||||||
{
|
{
|
||||||
let cookies = req.load_cookies().unwrap();
|
let cookies = req.cookies().unwrap();
|
||||||
assert_eq!(cookies.len(), 2);
|
assert_eq!(cookies.len(), 2);
|
||||||
assert_eq!(cookies[0].name(), "cookie1");
|
assert_eq!(cookies[0].name(), "cookie1");
|
||||||
assert_eq!(cookies[0].value(), "value1");
|
assert_eq!(cookies[0].value(), "value1");
|
||||||
|
@ -14,9 +14,9 @@ fn test_response_cookies() {
|
|||||||
headers.insert(header::COOKIE,
|
headers.insert(header::COOKIE,
|
||||||
header::HeaderValue::from_static("cookie1=value1; cookie2=value2"));
|
header::HeaderValue::from_static("cookie1=value1; cookie2=value2"));
|
||||||
|
|
||||||
let mut req = HttpRequest::new(
|
let req = HttpRequest::new(
|
||||||
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, Payload::empty());
|
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, Payload::empty());
|
||||||
let cookies = req.load_cookies().unwrap();
|
let cookies = req.cookies().unwrap();
|
||||||
|
|
||||||
let resp = httpcodes::HTTPOk
|
let resp = httpcodes::HTTPOk
|
||||||
.build()
|
.build()
|
||||||
|
Loading…
Reference in New Issue
Block a user