Fix Handler::with_state not working if request body was changed via layer (#1536)

Previously

```rust
handler.layer(RequestBodyLimitLayer::new(...)).with_state(...)
```

didn't work because we required the same request body all the way
through.
This commit is contained in:
David Pedersen
2022-11-18 11:00:52 +01:00
committed by GitHub
parent b1f894a500
commit 2e8a7e51a1
7 changed files with 56 additions and 24 deletions
+1
View File
@@ -15,5 +15,6 @@ tower-http = { version = "0.3.0", features = [
"limit",
"trace",
] }
tower-layer = "0.3.2"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
+7 -5
View File
@@ -26,7 +26,7 @@ use std::{
use tower::{BoxError, ServiceBuilder};
use tower_http::{
auth::RequireAuthorizationLayer, compression::CompressionLayer, limit::RequestBodyLimitLayer,
trace::TraceLayer,
trace::TraceLayer, ServiceBuilderExt,
};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
@@ -50,10 +50,12 @@ async fn main() {
get(kv_get.layer(CompressionLayer::new()))
// But don't compress `kv_set`
.post_service(
ServiceBuilder::new()
.layer(DefaultBodyLimit::disable())
.layer(RequestBodyLimitLayer::new(1024 * 5_000 /* ~5mb */))
.service(kv_set.with_state(Arc::clone(&shared_state))),
kv_set
.layer((
DefaultBodyLimit::disable(),
RequestBodyLimitLayer::new(1024 * 5_000 /* ~5mb */),
))
.with_state(Arc::clone(&shared_state)),
),
)
.route("/keys", get(list_keys))