-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Basic cookie extractor * Update futures-preview and pin-utils * Clean up as discussed in the PR * minimal docs added * change as per new extract trait * fix cookie extractor example
- Loading branch information
1 parent
bf1e3d8
commit fd57592
Showing
5 changed files
with
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ dist/ | |
npm-debug.log* | ||
Cargo.lock | ||
.DS_Store | ||
.idea |
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,18 @@ | ||
#![feature(async_await, futures_api)] | ||
|
||
use tide::Cookies; | ||
|
||
/// Tide will use the the `Cookies`'s `Extract` implementation to build this parameter. | ||
/// | ||
async fn hello_cookies(cookies: Cookies) -> String { | ||
format!("hello cookies: {:?}", cookies) | ||
} | ||
|
||
fn main() { | ||
let mut app = tide::App::new(()); | ||
app.at("/").get(hello_cookies); | ||
|
||
let address = "127.0.0.1:8000".to_owned(); | ||
println!("Server is listening on http://{}", address); | ||
app.serve(); | ||
} |
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,55 @@ | ||
use cookie::{Cookie, CookieJar, ParseError}; | ||
use futures::future; | ||
|
||
use crate::{configuration::Store, response::IntoResponse, Extract, Request, Response, RouteMatch}; | ||
|
||
/// A representation of cookies which wraps `CookieJar` from `cookie` crate | ||
/// | ||
/// Currently this only exposes getting cookie by name but future enhancements might allow more | ||
/// operation. `Cookies` implements`Extract` so that handler methods can have a `Cookies` parameter. | ||
/// | ||
#[derive(Clone, Debug)] | ||
pub struct Cookies { | ||
content: CookieJar, | ||
} | ||
|
||
impl Cookies { | ||
/// returns a `Cookie` by name of the cookie | ||
#[inline] | ||
pub fn get(&self, name: &str) -> Option<&Cookie<'static>> { | ||
self.content.get(name) | ||
} | ||
} | ||
|
||
impl<S: 'static> Extract<S> for Cookies { | ||
type Fut = future::Ready<Result<Self, Response>>; | ||
|
||
fn extract( | ||
data: &mut S, | ||
req: &mut Request, | ||
params: &Option<RouteMatch<'_>>, | ||
store: &Store, | ||
) -> Self::Fut { | ||
let cookie_jar = match req.headers().get("Cookie") { | ||
Some(raw_cookies) => parse_from_header(raw_cookies.to_str().unwrap()), | ||
_ => Ok(CookieJar::new()), | ||
}; | ||
let resp = cookie_jar | ||
.map(|c| Cookies { content: c }) | ||
.map_err(|_e| http::status::StatusCode::BAD_REQUEST.into_response()); | ||
|
||
future::ready(resp) | ||
} | ||
} | ||
|
||
fn parse_from_header(s: &str) -> Result<CookieJar, ParseError> { | ||
let mut jar = CookieJar::new(); | ||
|
||
s.split(';').try_for_each(|s| -> Result<_, ParseError> { | ||
jar.add(Cookie::parse(s.trim().to_owned())?); | ||
|
||
Ok(()) | ||
})?; | ||
|
||
Ok(jar) | ||
} |
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