Skip to content

Commit

Permalink
chore(core): update deps sessions 0.6.0, path-tree 0.7.4, form-data 0…
Browse files Browse the repository at this point in the history
….5.3, handlebars 5
  • Loading branch information
fundon committed Jan 2, 2024
1 parent 3e0569f commit c143cdf
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ sync_wrapper = "0.1.2"
thiserror = "1.0"

# router
path-tree = "0.7.3"
path-tree = "0.7.4"

# http
headers = "0.4"
Expand Down
3 changes: 2 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ Here you can find a lot of small crabs 🦀.
* [tera](templates/tera)
* [maud](templates/maud)
* [minijinja](templates/minijinja)
* [handlebars(htmlx)
* [Tracing aka logging](tracing)
* [htmlx](htmlx)
* [htmlx + handlebars](htmlx)
* [Tower Services](tower)

## Usage
Expand Down
2 changes: 1 addition & 1 deletion examples/htmlx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }

handlebars = { version = "4.5", features = ["dir_source"] }
handlebars = { version = "5", features = ["dir_source"] }
once_cell = "1.19"
4 changes: 2 additions & 2 deletions examples/htmlx/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// #![deny(warnings)]

use handlebars::Handlebars;
use handlebars::{DirectorySourceOptions, Handlebars};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use serde_json::json;
Expand Down Expand Up @@ -28,7 +28,7 @@ struct Todo {
static TPLS: Lazy<Handlebars> = Lazy::new(|| {
let dir = env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap();
let mut h = Handlebars::new();
h.register_templates_directory(".html", dir.join("templates"))
h.register_templates_directory(dir.join("templates"), DirectorySourceOptions::default())
.unwrap();
h
});
Expand Down
2 changes: 1 addition & 1 deletion examples/session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ viz = { workspace = true, features = ["session"] }

tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }

sessions = { version = "0.5.0", features = ["memory"] }
sessions = { version = "0.6.0", features = ["memory"] }
nano-id = "0.3"
4 changes: 2 additions & 2 deletions viz-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ mime.workspace = true

rfc7239 = "0.1" # realip
cookie = { version = "0.18", features = ["percent-encode"], optional = true }
form-data = { version = "0.5.1", optional = true }
form-data = { version = "0.5.3", optional = true }
serde = { workspace = true, features = ["derive"], optional = true }
serde_json = { workspace = true, optional = true }
serde_urlencoded = { workspace = true, optional = true }
sessions-core = { version = "0.5.1", optional = true }
sessions-core = { version = "0.6", optional = true }

# CSRF
getrandom = { version = "0.2", optional = true }
Expand Down
12 changes: 2 additions & 10 deletions viz-core/src/into_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,13 @@ impl IntoResponse for &'static str {

impl IntoResponse for &'static [u8] {
fn into_response(self) -> Response {
Response::builder()
.header(CONTENT_TYPE, mime::APPLICATION_OCTET_STREAM.as_ref())
.header(CONTENT_LENGTH, self.len())
.body(Full::from(self).into())
.unwrap()
bytes::Bytes::into_response(self.into())
}
}

impl IntoResponse for Vec<u8> {
fn into_response(self) -> Response {
Response::builder()
.header(CONTENT_TYPE, mime::APPLICATION_OCTET_STREAM.as_ref())
.header(CONTENT_LENGTH, self.len())
.body(Full::from(self).into())
.unwrap()
bytes::Bytes::into_response(self.into())
}
}

Expand Down
5 changes: 4 additions & 1 deletion viz-core/src/middleware/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use std::fmt;

use crate::{
header::{HeaderValue, COOKIE, SET_COOKIE},
types::{Cookie, CookieJar, CookieKey, Cookies},
types::{Cookie, CookieJar, Cookies},
Handler, IntoResponse, Request, Response, Result, Transform,
};

#[cfg(any(feature = "cookie-signed", feature = "cookie-private"))]
use crate::types::CookieKey;

/// A configure for [`CookieMiddleware`].
pub struct Config {
#[cfg(any(feature = "cookie-signed", feature = "cookie-private"))]
Expand Down
32 changes: 7 additions & 25 deletions viz-core/src/middleware/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@ use std::{
use crate::{
middleware::helper::{CookieOptions, Cookieable},
types::{Cookie, Session},
Error, Handler, IntoResponse, Request, RequestExt, Response, Result, StatusCode, Transform,
Handler, IntoResponse, Request, RequestExt, Response, Result, Transform,
};

use super::{Error as SessionError, Storage, Store, PURGED, RENEWED, UNCHANGED};
use super::{Storage, Store, PURGED, RENEWED, UNCHANGED};

/// A configuration for [`SessionMiddleware`].
pub struct Config<S, G, V>(Arc<(Store<S, G, V>, CookieOptions)>);

impl<S, G, V> Config<S, G, V>
where
S: Send + Sync,
G: Send + Sync,
V: Send + Sync,
{
impl<S, G, V> Config<S, G, V> {
/// Creates a new configuration with the [`Store`] and [`CookieOptions`].
#[must_use]
pub fn new(store: Store<S, G, V>, cookie: CookieOptions) -> Self {
Expand Down Expand Up @@ -102,7 +97,7 @@ where
async fn call(&self, mut req: Request) -> Self::Output {
let Self { h, config } = self;

let cookies = req.cookies().map_err(Error::from)?;
let cookies = req.cookies()?;
let cookie = config.get_cookie(&cookies);

let mut session_id = cookie.as_ref().map(Cookie::value).map(ToString::to_string);
Expand All @@ -126,7 +121,7 @@ where

if status == PURGED {
if let Some(sid) = &session_id {
config.store().remove(sid).await.map_err(Error::from)?;
config.store().remove(sid).await?;
config.remove_cookie(&cookies);
}

Expand All @@ -135,7 +130,7 @@ where

if status == RENEWED {
if let Some(sid) = &session_id.take() {
config.store().remove(sid).await.map_err(Error::from)?;
config.store().remove(sid).await?;
}
}

Expand All @@ -148,8 +143,7 @@ where
config
.store()
.set(&sid, session.data()?, &config.ttl().unwrap_or_else(max_age))
.await
.map_err(Error::from)?;
.await?;

resp
}
Expand All @@ -158,15 +152,3 @@ where
fn max_age() -> Duration {
Duration::from_secs(CookieOptions::MAX_AGE)
}

impl From<SessionError> for Error {
fn from(e: SessionError) -> Self {
Self::Responder(e.into_response())
}
}

impl IntoResponse for SessionError {
fn into_response(self) -> Response {
(StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response()
}
}
1 change: 1 addition & 0 deletions viz-core/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ impl RequestExt for Request {
.collect()
.await
.map_err(|err| {
#[cfg(feature = "limits")]
if err.is::<LengthLimitError>() {
return PayloadError::TooLarge;
}
Expand Down
5 changes: 2 additions & 3 deletions viz-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl Router {
#[must_use]
pub fn with<T>(self, t: T) -> Self
where
T: Transform<BoxHandler<Request, Result<Response>>>,
T: Transform<BoxHandler>,
T::Output: Handler<Request, Output = Result<Response>> + Clone,
{
self.map_handler(|handler| t.transform(handler).boxed())
Expand All @@ -174,8 +174,7 @@ impl Router {
#[must_use]
pub fn with_handler<H>(self, f: H) -> Self
where
H: Handler<Next<Request, BoxHandler<Request, Result<Response>>>, Output = Result<Response>>
+ Clone,
H: Handler<Next<Request, BoxHandler>, Output = Result<Response>> + Clone,
{
self.map_handler(|handler| handler.around(f.clone()).boxed())
}
Expand Down

0 comments on commit c143cdf

Please sign in to comment.