Implement body size limit middleware with dynamic config reload

Add body_limit middleware that reads limit from ArcSwap<DynamicConfig>
on each request, enabling runtime config changes without restart.
Uses Content-Length header check for fast rejection and http_body_util::Limited
for streaming body enforcement. Default limit: 100 MB (104,857,600 bytes).
Returns 413 Payload Too Large when exceeded.
This commit is contained in:
2026-06-11 13:02:59 +00:00
parent 994ce0fb66
commit 5fa0fc600e
5 changed files with 285 additions and 0 deletions

View File

@@ -1,3 +1,20 @@
pub mod body_limit;
pub mod error;
pub mod handler;
pub mod headers;
use std::sync::Arc;
use arc_swap::ArcSwap;
use crate::config::DynamicConfig;
pub fn router_with_body_limit(
router: axum::Router,
config: Arc<ArcSwap<DynamicConfig>>,
) -> axum::Router {
router.layer(axum::middleware::from_fn_with_state(
config,
body_limit::body_limit_middleware,
))
}