mirror of
https://gitlab.com/famedly/conduit.git
synced 2025-09-05 18:41:00 +00:00
fix: Respond with HTTP code 413, when request size is too big
This commit is contained in:
parent
1b32cfe955
commit
0772a7c151
2 changed files with 8 additions and 16 deletions
|
@ -1,11 +1,10 @@
|
||||||
use std::{collections::BTreeMap, iter::FromIterator, str};
|
use std::{collections::BTreeMap, error::Error as _, iter::FromIterator, str};
|
||||||
use std::error::Error as _;
|
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
extract::{FromRequest, Path},
|
extract::{FromRequest, Path},
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
RequestExt, RequestPartsExt,
|
RequestPartsExt,
|
||||||
};
|
};
|
||||||
use axum_extra::{
|
use axum_extra::{
|
||||||
headers::{authorization::Bearer, Authorization},
|
headers::{authorization::Bearer, Authorization},
|
||||||
|
@ -49,8 +48,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
let (mut parts, mut body) = {
|
let (mut parts, mut body) = {
|
||||||
let limited_req = req.with_limited_body();
|
let (parts, body) = req.into_parts();
|
||||||
let (parts, body) = limited_req.into_parts();
|
|
||||||
let body = axum::body::to_bytes(
|
let body = axum::body::to_bytes(
|
||||||
body,
|
body,
|
||||||
services()
|
services()
|
||||||
|
@ -61,13 +59,13 @@ where
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
if err.source()
|
if err
|
||||||
.as_ref()
|
.source()
|
||||||
.and_then(|err| err.source())
|
|
||||||
.is_some_and(|err| err.is::<http_body_util::LengthLimitError>())
|
.is_some_and(|err| err.is::<http_body_util::LengthLimitError>())
|
||||||
{
|
{
|
||||||
Error::BadRequest(ErrorKind::TooLarge, "Reached maximum request size")
|
Error::BadRequest(ErrorKind::TooLarge, "Reached maximum request size")
|
||||||
} else {
|
} else {
|
||||||
|
error!("An unknown error has occurred: {err}");
|
||||||
Error::BadRequest(ErrorKind::Unknown, "An unknown error has occurred")
|
Error::BadRequest(ErrorKind::Unknown, "An unknown error has occurred")
|
||||||
}
|
}
|
||||||
})?;
|
})?;
|
||||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -2,7 +2,7 @@ use std::{future::Future, io, net::SocketAddr, sync::atomic, time::Duration};
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
extract::{DefaultBodyLimit, FromRequestParts, MatchedPath},
|
extract::{FromRequestParts, MatchedPath},
|
||||||
middleware::map_response,
|
middleware::map_response,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
routing::{any, get, on, MethodFilter},
|
routing::{any, get, on, MethodFilter},
|
||||||
|
@ -240,13 +240,7 @@ async fn run_server() -> io::Result<()> {
|
||||||
])
|
])
|
||||||
.max_age(Duration::from_secs(86400)),
|
.max_age(Duration::from_secs(86400)),
|
||||||
)
|
)
|
||||||
.layer(map_response(set_csp_header))
|
.layer(map_response(set_csp_header));
|
||||||
.layer(DefaultBodyLimit::max(
|
|
||||||
config
|
|
||||||
.max_request_size
|
|
||||||
.try_into()
|
|
||||||
.expect("failed to convert max request size"),
|
|
||||||
));
|
|
||||||
|
|
||||||
let app = routes(config).layer(middlewares).into_make_service();
|
let app = routes(config).layer(middlewares).into_make_service();
|
||||||
let handle = ServerHandle::new();
|
let handle = ServerHandle::new();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue