Skip to content

Commit

Permalink
list todos in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
carloskiki committed Sep 27, 2024
1 parent e92fcf6 commit bc9f013
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ tower-sessions-core = { workspace = true }
# tower-sessions-memory-store = { workspace = true, optional = true }
tracing = { version = "0.1.40", features = ["log"] }
time = { version = "0.3.29", features = ["serde"] }
cookie = { version = "0.18.1", features = ["percent-encode"] }
cookie = "0.18.1"

[dev-dependencies]
async-trait = "0.1.74"
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
</a>
</div>

## TODOs
- [ ] Make the `Store` trait take `R` instead of `&R`.
- [ ] Add `'static` bounds to `Store` trait.
- [ ] Spawn a task when dropping the `SessionData` to save the session data.
- [ ] Complete middleware implementation.
- [ ] Add examples everywhere.
- [ ] Rewrite the in memory store.
- [ ] Rewrite all the tests.

## 🎨 Overview

This crate provides sessions, key-value pairs associated with a site
Expand Down
34 changes: 31 additions & 3 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ where
.into_iter()
.filter_map(|value| value.to_str().ok())
.flat_map(|value| value.split(';'))
.filter_map(|cookie| Cookie::parse_encoded(cookie).ok())
.filter_map(|cookie| Cookie::parse(cookie).ok())
.find(|cookie| cookie.name() == self.config.name);

let id = session_cookie.and_then(|cookie| {
Expand Down Expand Up @@ -229,8 +229,36 @@ where
}
});
match update {
Some(SessionUpdate::Delete) => todo!(),
Some(SessionUpdate::Set(id)) => todo!(),
Some(SessionUpdate::Delete) => {
if let Some(old_id) = this.old_id {
let cookie = this.config.build_cookie(
*old_id,
Some(Expiry::AtDateTime(
// The Year 2000.
time::OffsetDateTime::from_unix_timestamp(946684800)
.expect("year 2000 should be in range"),
)),
);
resp.headers_mut().insert(
http::header::SET_COOKIE,
cookie
.to_string()
.try_into()
.expect("cookie should be valid"),
);
};
}
Some(SessionUpdate::Set(id)) => {
// TODO: This should also accept a user-provided expiry.
let cookie = this.config.build_cookie(id, None);
resp.headers_mut().insert(
http::header::SET_COOKIE,
cookie
.to_string()
.try_into()
.expect("cookie should be valid"),
);
},
None => {}
};

Expand Down
13 changes: 8 additions & 5 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ where
{
fn clone(&self) -> Self {
Self {
id: self.id.clone(),
id: self.id,
store: self.store.clone(),
data: self.data.clone(),
data: self.data,
updater: self.updater.clone(),
}
}
Expand Down Expand Up @@ -112,7 +112,9 @@ impl<R: Send + Sync, Store: SessionStore<R>> LazySession<R, Store> {
}

#[derive(Debug, Clone, Copy)]
struct NoMiddleware;
/// A rejection that is returned from the [`Session`] extractor when the [`SessionManagerLayer`]
/// middleware is not set.
pub struct NoMiddleware;

impl Display for NoMiddleware {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -161,6 +163,7 @@ where
/// store used returned a "hard" error. For example, it could be a connection error, a protocol error,
/// a timeout, etc. A counterexample would be the session not being found in the store, which is
/// not considered an error by the `SessionStore` trait.
#[derive(Debug)]
pub struct Session<R: Send + Sync, Store: SessionStore<R>> {
store: Store,
id: Id,
Expand Down Expand Up @@ -294,9 +297,9 @@ where
Store: SessionStore<R>,
{
fn drop(&mut self) {
let _ = self
let _ = tokio::task::spawn(self
.session
.store
.save(&self.session.id, &self.session.data);
.save(&self.session.id, &self.session.data));
}
}

0 comments on commit bc9f013

Please sign in to comment.