mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-24 00:00:16 +02:00
Remove ContentLengthLimit (#1400)
* feat: remove ContentLengthLimit * feat: remove ContentLengthLimit rejections * fix: update multipart docs * fix: typo * feat: add wip extractor code * feat: revert "feat: add wip extractor code" * fix: update Multipart docs * fix: update examples * fix: missing import in an example * fix: broken import yet again * fix: disable default body limit for example * fix: key value store example * fix: update expected debug_handler output * chore: update CHANGELOG * Update axum/CHANGELOG.md Co-authored-by: David Pedersen <[email protected]>
This commit is contained in:
co-authored by
David Pedersen
parent
c3f3db79ec
commit
896ffc5fba
@@ -8,6 +8,12 @@ publish = false
|
||||
axum = { path = "../../axum" }
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
tower = { version = "0.4", features = ["util", "timeout", "load-shed", "limit"] }
|
||||
tower-http = { version = "0.3.0", features = ["add-extension", "auth", "compression-full", "trace"] }
|
||||
tower-http = { version = "0.3.0", features = [
|
||||
"add-extension",
|
||||
"auth",
|
||||
"compression-full",
|
||||
"limit",
|
||||
"trace",
|
||||
] }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
use axum::{
|
||||
body::Bytes,
|
||||
error_handling::HandleErrorLayer,
|
||||
extract::{ContentLengthLimit, Path, State},
|
||||
extract::{DefaultBodyLimit, Path, State},
|
||||
handler::Handler,
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
@@ -25,7 +25,8 @@ use std::{
|
||||
};
|
||||
use tower::{BoxError, ServiceBuilder};
|
||||
use tower_http::{
|
||||
auth::RequireAuthorizationLayer, compression::CompressionLayer, trace::TraceLayer,
|
||||
auth::RequireAuthorizationLayer, compression::CompressionLayer, limit::RequestBodyLimitLayer,
|
||||
trace::TraceLayer,
|
||||
};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
@@ -48,7 +49,12 @@ async fn main() {
|
||||
// Add compression to `kv_get`
|
||||
get(kv_get.layer(CompressionLayer::new()))
|
||||
// But don't compress `kv_set`
|
||||
.post(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))),
|
||||
),
|
||||
)
|
||||
.route("/keys", get(list_keys))
|
||||
// Nest our admin routes under `/admin`
|
||||
@@ -94,11 +100,7 @@ async fn kv_get(
|
||||
}
|
||||
}
|
||||
|
||||
async fn kv_set(
|
||||
Path(key): Path<String>,
|
||||
State(state): State<SharedState>,
|
||||
ContentLengthLimit(bytes): ContentLengthLimit<Bytes, { 1024 * 5_000 }>, // ~5mb
|
||||
) {
|
||||
async fn kv_set(Path(key): Path<String>, State(state): State<SharedState>, bytes: Bytes) {
|
||||
state.write().unwrap().db.insert(key, bytes);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,6 @@ publish = false
|
||||
[dependencies]
|
||||
axum = { path = "../../axum", features = ["multipart"] }
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
tower-http = { version = "0.3.0", features = ["trace"] }
|
||||
tower-http = { version = "0.3.0", features = ["limit", "trace"] }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
||||
@@ -5,12 +5,13 @@
|
||||
//! ```
|
||||
|
||||
use axum::{
|
||||
extract::{ContentLengthLimit, Multipart},
|
||||
extract::{DefaultBodyLimit, Multipart},
|
||||
response::Html,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
use tower_http::limit::RequestBodyLimitLayer;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
#[tokio::main]
|
||||
@@ -26,6 +27,10 @@ async fn main() {
|
||||
// build our application with some routes
|
||||
let app = Router::new()
|
||||
.route("/", get(show_form).post(accept_form))
|
||||
.layer(DefaultBodyLimit::disable())
|
||||
.layer(RequestBodyLimitLayer::new(
|
||||
250 * 1024 * 1024, /* 250mb */
|
||||
))
|
||||
.layer(tower_http::trace::TraceLayer::new_for_http());
|
||||
|
||||
// run it with hyper
|
||||
@@ -58,14 +63,7 @@ async fn show_form() -> Html<&'static str> {
|
||||
)
|
||||
}
|
||||
|
||||
async fn accept_form(
|
||||
ContentLengthLimit(mut multipart): ContentLengthLimit<
|
||||
Multipart,
|
||||
{
|
||||
250 * 1024 * 1024 /* 250mb */
|
||||
},
|
||||
>,
|
||||
) {
|
||||
async fn accept_form(mut multipart: Multipart) {
|
||||
while let Some(field) = multipart.next_field().await.unwrap() {
|
||||
let name = field.name().unwrap().to_string();
|
||||
let file_name = field.file_name().unwrap().to_string();
|
||||
|
||||
Reference in New Issue
Block a user