From d77fe50673313e100617bccac17d163f9968cd73 Mon Sep 17 00:00:00 2001 From: 0xtr Date: Sat, 25 Jan 2025 23:09:24 +0100 Subject: [PATCH] Set maximum body size The default size set by Axum was 2MB. This change sets the maximum body size in the Axum router to whatever the server admin configures in the application config. --- src/handlers/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 47b5dff..8ba5d65 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -12,7 +12,7 @@ use crate::utilities::validation::{ use crate::utilities::{bytes_to_mb, get_current_unix_timestamp, split_filehash_and_filetype}; use crate::{utilities, AppState, BlobDescriptor}; use axum::body::Bytes; -use axum::extract::{FromRequestParts, Path, Query, State}; +use axum::extract::{DefaultBodyLimit, FromRequestParts, Path, Query, State}; use axum::http::request::Parts; use axum::http::{HeaderMap, HeaderValue, Method, StatusCode}; use axum::response::{Html, IntoResponse, Response}; @@ -128,6 +128,9 @@ pub async fn create_router(app_state: AppState) -> Router { .allow_headers(Any) .allow_methods(vec![Method::GET, Method::PUT, Method::DELETE, Method::HEAD]); + // Configuration takes MB size so we calculate the amount of bytes + let max_body_limit = app_state.config.upload.max_size * 1024.0 * 1024.0; + // Configure router Router::new() .route("/", get(|| async { Html(include_str!("html/index.html")) })) @@ -144,6 +147,7 @@ pub async fn create_router(app_state: AppState) -> Router { .route("/list/:pubkey", get(list_blobs_handler)) .route("/mirror", put(mirror_blob_handler)) .layer(cors) + .layer(DefaultBodyLimit::max(max_body_limit as usize)) .with_state(app_state) }