Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie Improvement #170

Merged
merged 8 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ __More Examples__
- [Body Types](https://github.com/rustasync/tide/blob/master/examples/body_types.rs)
- [Multipart Form](https://github.com/rustasync/tide/tree/master/examples/multipart-form/main.rs)
- [Catch All](https://github.com/rustasync/tide/tree/master/examples/catch_all.rs)
- [Cookie Extractor](https://github.com/rustasync/tide/tree/master/examples/cookie_extractor.rs)
- [Cookies](https://github.com/rustasync/tide/tree/master/examples/cookies.rs)
- [Default Headers](https://github.com/rustasync/tide/tree/master/examples/default_headers.rs)
- [GraphQL](https://github.com/rustasync/tide/tree/master/examples/graphql.rs)

Expand Down
5 changes: 5 additions & 0 deletions src/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ impl CookieData {
pub trait CookiesExt {
/// returns a `Cookie` by name of the cookie
fn get_cookie(&mut self, name: &str) -> Result<Option<Cookie<'static>>, StringError>;

/// Add cookie to the cookie jar
fn set_cookie(&mut self, cookie: Cookie<'static>) -> Result<(), StringError>;

/// Removes the cookie. This instructs the `CookiesMiddleware` to send a cookie with empty value
/// in the response.
fn remove_cookie(&mut self, cookie: Cookie<'static>) -> Result<(), StringError>;
}

Expand Down
7 changes: 6 additions & 1 deletion src/middleware/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use crate::{
Context, Response,
};

/// `CookiesMiddleware middleware is required for `CookiesExt` implementation on `Context` to work.
mmrath marked this conversation as resolved.
Show resolved Hide resolved
/// This middleware parses cookies from request and caches them in the extension. Once the request
/// is processed by endpoints and other middlewares, all the added and removed cookies are set on
/// on the respone. You will need to add this middle before any other middlewares that might need to
/// access Cookies.
#[derive(Clone, Default)]
pub struct CookiesMiddleware {}

Expand Down Expand Up @@ -38,7 +43,7 @@ impl<Data: Send + Sync + 'static> Middleware<Data> for CookiesMiddleware {
if let Ok(val) = hv {
headers.append(http::header::SET_COOKIE, val);
} else {
//TODO Log error here
// It would be useful to log this error here.
mmrath marked this conversation as resolved.
Show resolved Hide resolved
return http::Response::builder()
.status(http::status::StatusCode::INTERNAL_SERVER_ERROR)
.header("Content-Type", "text/plain; charset=utf-8")
Expand Down