Skip to content

Commit

Permalink
fix: limits should be sorted (#68)
Browse files Browse the repository at this point in the history
* fix: limits should be sorted

* fix: fmt

* fix: fmt

* fix: cargo path

* fix: doc ci
  • Loading branch information
fundon authored Feb 8, 2023
1 parent a7713dc commit 83b0f54
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 35 deletions.
3 changes: 2 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ members = [
"static-routes",
"routing/todos",
"otel/*",
"compression"
"compression",
"limits"
]
exclude = ["tls", "target"]

Expand Down
14 changes: 14 additions & 0 deletions examples/limits/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "limits"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
viz = { path = "../../viz", features = ["limits", "json"] }

serde = {version = "1.0", features = ["derive"] }
tokio = { version = "1.0", features = [
"rt-multi-thread",
"macros",
] }
31 changes: 31 additions & 0 deletions examples/limits/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#![deny(warnings)]

use std::net::SocketAddr;
use viz::{middleware::limits, Request, RequestExt, Result, Router, Server, ServiceMaker};

async fn echo(mut req: Request) -> Result<String> {
Ok(format!("len: {}", req.text().await?.len()))
}

#[tokio::main]
async fn main() -> Result<()> {
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("listening on {addr}");

let limits = viz::types::Limits::new()
.insert("payload", 1000 * 1024 * 1024)
.insert("text", 1000 * 1024 * 1024)
.insert("bytes", 1000 * 1024 * 1024)
.insert("json", 1000 * 1024 * 1024);

let app = Router::new()
.post("/", echo)
// limit body size
.with(limits::Config::default().limits(limits));

if let Err(err) = Server::bind(&addr).serve(ServiceMaker::from(app)).await {
println!("{err}");
}

Ok(())
}
13 changes: 6 additions & 7 deletions viz-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "viz-core"
version = "0.4.4"
version = "0.4.5"
edition = "2021"
rust-version = "1.60"
license = "MIT OR Apache-2.0"
Expand All @@ -10,7 +10,6 @@ homepage = "https://viz.rs"
repository = "https://github.com/viz-rs/viz"
documentation = "https://docs.rs/viz"
description = "The core traits and types in for the Viz"
publish = true

[features]
default = [
Expand Down Expand Up @@ -53,10 +52,10 @@ otel-tracing = ["otel", "opentelemetry?/trace"]
otel-metrics = ["otel", "opentelemetry?/metrics"]

[dependencies]
async-trait = "0.1.61"
bytes = "1.3"
async-trait = "0.1.64"
bytes = "1.4"
dyn-clone = "1.0.8"
futures-util = "0.3.25"
futures-util = "0.3.26"
headers = "0.3.8"
http = "0.2.7"
http-body = "0.4.5"
Expand All @@ -65,7 +64,7 @@ mime = "0.3.16"
thiserror = "1.0.35"

rfc7239 = "0.1.0" # realip
cookie = { version = "0.16.1", features = ["percent-encode"], optional = true }
cookie = { version = "0.17.0", features = ["percent-encode"], optional = true }
form-data = { version = "0.4.1", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true }
Expand All @@ -83,7 +82,7 @@ async-compression = { version = "0.3.15", features = ["tokio", "gzip", "brotli",
opentelemetry = { version = "0.18.0", default-features = false, optional = true }
opentelemetry-semantic-conventions = { version = "0.10.0", optional = true }

tokio = { version = "1.24", optional = true }
tokio = { version = "1.25", optional = true }
tokio-tungstenite = { version = "0.18", optional = true }
tokio-stream = { version = "0.1.11", optional = true }
tokio-util = { version = "0.7.4", optional = true }
Expand Down
7 changes: 2 additions & 5 deletions viz-core/src/middleware/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ where
match cookies.jar().lock() {
Ok(c) => {
c.delta()
.into_iter()
.filter_map(|cookie| {
HeaderValue::from_str(&cookie.encoded().to_string()).ok()
})
Expand All @@ -128,10 +127,8 @@ where

#[inline]
fn add_cookie(mut jar: types::CookieJar, value: &str) -> types::CookieJar {
value
.split(types::Cookies::SPLITER)
.map(str::trim)
.filter_map(|v| types::Cookie::parse_encoded(v).ok())
types::Cookie::split_parse_encoded(value)
.filter_map(Result::ok)
.for_each(|cookie| jar.add_original(cookie.into_owned()));
jar
}
2 changes: 1 addition & 1 deletion viz-core/src/middleware/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Config {

/// Sets a limits for the Text/Bytes/Form.
pub fn limits(mut self, limits: types::Limits) -> Self {
self.limits = limits;
self.limits = limits.sort();
self
}

Expand Down
2 changes: 0 additions & 2 deletions viz-core/src/types/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ impl Clone for Cookies {
}

impl Cookies {
pub(crate) const SPLITER: char = ';';

/// Creates a new Cookies with the [`CookieJar`].
pub fn new(cookie_jar: CookieJar) -> Self {
Self {
Expand Down
13 changes: 10 additions & 3 deletions viz-core/src/types/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ pub struct Limits {
impl Default for Limits {
fn default() -> Self {
let limits = Limits::new()
.insert("bytes", Limits::NORMAL)
.insert("payload", Limits::NORMAL)
.insert("text", Limits::NORMAL)
.insert("bytes", Limits::NORMAL);
.insert("text", Limits::NORMAL);

#[cfg(feature = "json")]
let limits = limits.insert("json", <Json as Payload>::LIMIT);

#[cfg(feature = "form")]
let limits = limits.insert("form", <Form as Payload>::LIMIT);

limits
limits.sort()
}
}

Expand Down Expand Up @@ -61,6 +61,13 @@ impl Limits {
.map(|i| self.inner[i].1)
.ok()
}

/// Sorts the limits for binary search.
#[must_use]
pub fn sort(mut self) -> Self {
Arc::make_mut(&mut self.inner).sort_by_key(|a| a.0);
self
}
}

#[async_trait]
Expand Down
7 changes: 3 additions & 4 deletions viz-handlers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "viz-handlers"
version = "0.4.4"
version = "0.4.5"
edition = "2021"
rust-version = "1.60"
license = "MIT OR Apache-2.0"
Expand All @@ -9,7 +9,6 @@ homepage = "https://viz.rs"
repository = "https://github.com/viz-rs/viz"
documentation = "https://docs.rs/viz-handlers"
description = "Viz handlers"
publish = true

[features]
default = ["serve"]
Expand All @@ -28,7 +27,7 @@ embed = ["dep:hex", "dep:mime_guess", "dep:rust-embed"]
prometheus = ["opentelemetry/metrics", "dep:opentelemetry-prometheus"]

[dependencies]
viz-core = { path = "../viz-core", version = '0.4.4' }
viz-core = { path = "../viz-core", version = '0.4.5' }

# required!
thiserror = "1.0"
Expand All @@ -38,7 +37,7 @@ mime_guess = { version = "2.0", optional = true }

# serve
percent-encoding = { version = "2.2.0", optional = true }
tokio = { version = "1.24", optional = true }
tokio = { version = "1.25", optional = true }
tokio-stream = { version = "0.1.11", optional = true }
tokio-util = { version = "0.7.4", optional = true }

Expand Down
7 changes: 3 additions & 4 deletions viz-router/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "viz-router"
version = "0.4.4"
version = "0.4.5"
edition = "2021"
rust-version = "1.60"
license = "MIT OR Apache-2.0"
Expand All @@ -9,20 +9,19 @@ homepage = "https://viz.rs"
repository = "https://github.com/viz-rs/viz"
documentation = "https://docs.rs/viz-router"
description = "Viz Router"
publish = true

[features]
default = []

[dependencies]
viz-core = { path = "../viz-core", version = "0.4.4" }
viz-core = { path = "../viz-core", version = "0.4.5" }
path-tree = { version = "0.7" }
serde = "1.0"
thiserror = "1.0"

[dev-dependencies]
anyhow = "1.0"
bytes = "1.0"
hyper = "0.14.20"
hyper = "0.14"
serde_derive = "1.0"
tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] }
16 changes: 8 additions & 8 deletions viz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "viz"
version = "0.4.4"
version = "0.4.5"
edition = "2021"
rust-version = "1.60"
readme = "README.md"
Expand Down Expand Up @@ -68,18 +68,18 @@ rustls = ["dep:rustls-pemfile", "dep:tokio-rustls", "dep:futures-util"]
native-tls = ["dep:tokio-native-tls", "dep:futures-util"]

[dependencies]
viz-core = { path = "../viz-core", version = "0.4.4" }
viz-router = { path = "../viz-router", version = "0.4.4" }
viz-handlers = { path = "../viz-handlers", version = "0.4.4", default-features = false, optional = true }
viz-core = { path = "../viz-core", version = "0.4.5" }
viz-router = { path = "../viz-router", version = "0.4.5" }
viz-handlers = { path = "../viz-handlers", version = "0.4.5", default-features = false, optional = true }
viz-macros = { path = "../viz-macros", version = "0.1.0", optional = true }

hyper = { version = "0.14", features = ["server", "stream", "tcp"] }
tokio = { version = "1.24", features = ["net"] }
tokio = { version = "1.25", features = ["net"] }

futures-util = { version = "0.3.25", optional = true }
rustls-pemfile = { version = "1.0.0", optional = true }
futures-util = { version = "0.3.26", optional = true }
rustls-pemfile = { version = "1.0.2", optional = true }
tokio-rustls = { version = "0.23.4", optional = true }
tokio-native-tls = { version = "0.3.0", optional = true }
tokio-native-tls = { version = "0.3.1", optional = true }

[dev-dependencies]
tokio = { version = "1.0", features = ["macros", "rt", "rt-multi-thread"] }
Expand Down

0 comments on commit 83b0f54

Please sign in to comment.