forked from actix/actix-extras
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cookie adapter through actix-cookie subcrate
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "actix-cookie" | ||
version = "0.1.0" | ||
authors = [ | ||
"Near Inc <hello@nearprotocol.com>", | ||
] | ||
description = "Actix adapter for cookies functionality" | ||
repository = "https://github.com/near/actix-extras.git" | ||
license = "MIT OR Apache-2.0" | ||
edition = "2018" | ||
|
||
[lib] | ||
name = "actix_cookie" | ||
path = "src/lib.rs" | ||
|
||
[dependencies] | ||
actix-web = { version = "4.0.0-beta.6", default-features = false, features = ["cookies"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::cell::Ref; | ||
|
||
use actix_web::{ | ||
cookie::{Cookie, ParseError as CookieParseError}, | ||
dev::ServiceRequest, | ||
http::header, | ||
HttpMessage, | ||
}; | ||
|
||
struct Cookies(Vec<Cookie<'static>>); | ||
|
||
pub trait Cookied: HttpMessage { | ||
fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, CookieParseError> { | ||
if self.extensions().get::<Cookies>().is_none() { | ||
let mut cookies = Vec::new(); | ||
for hdr in self.headers().get_all(header::COOKIE) { | ||
let s = std::str::from_utf8(hdr.as_bytes()) | ||
.map_err(CookieParseError::from)?; | ||
for cookie_str in s.split(';').map(|s| s.trim()) { | ||
if !cookie_str.is_empty() { | ||
cookies.push(Cookie::parse_encoded(cookie_str)?.into_owned()); | ||
} | ||
} | ||
} | ||
self.extensions_mut().insert(Cookies(cookies)); | ||
} | ||
|
||
Ok(Ref::map(self.extensions(), |ext| { | ||
&ext.get::<Cookies>().unwrap().0 | ||
})) | ||
} | ||
|
||
/// Return request cookie. | ||
fn cookie(&self, name: &str) -> Option<Cookie<'static>> { | ||
if let Ok(cookies) = self.cookies() { | ||
for cookie in cookies.iter() { | ||
if cookie.name() == name { | ||
return Some(cookie.to_owned()); | ||
} | ||
} | ||
} | ||
None | ||
} | ||
} | ||
|
||
impl Cookied for ServiceRequest {} |