Skip to content

Commit

Permalink
feat: add --no-request-size-limit option to anvil (#8209)
Browse files Browse the repository at this point in the history
* feat: add --no-request-size-limit option to anvil

* chore: flip logic, fmt, improve cli help

* nit

---------

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
  • Loading branch information
frontier159 and DaniPopes authored Jun 20, 2024
1 parent ebe4731 commit bde40a8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
34 changes: 16 additions & 18 deletions crates/anvil/server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,39 @@ use std::str::FromStr;
#[cfg_attr(feature = "clap", derive(clap::Parser), command(next_help_heading = "Server options"))]
pub struct ServerConfig {
/// The cors `allow_origin` header
#[cfg_attr(
feature = "clap",
arg(
long,
help = "Set the CORS allow_origin",
default_value = "*",
value_name = "ALLOW_ORIGIN"
)
)]
#[cfg_attr(feature = "clap", arg(long, default_value = "*"))]
pub allow_origin: HeaderValueWrapper,
/// Whether to enable CORS
#[cfg_attr(
feature = "clap",
arg(long, help = "Disable CORS", conflicts_with = "allow_origin")
)]

/// Disable CORS.
#[cfg_attr(feature = "clap", arg(long, conflicts_with = "allow_origin"))]
pub no_cors: bool,

/// Disable the default request body size limit. At time of writing the default limit is 2MB.
#[cfg_attr(feature = "clap", arg(long))]
pub no_request_size_limit: bool,
}

impl ServerConfig {
/// Sets the "allow origin" header for cors
/// Sets the "allow origin" header for CORS.
pub fn with_allow_origin(mut self, allow_origin: impl Into<HeaderValueWrapper>) -> Self {
self.allow_origin = allow_origin.into();
self
}

/// Whether to enable CORS
/// Whether to enable CORS.
pub fn set_cors(mut self, cors: bool) -> Self {
self.no_cors = cors;
self.no_cors = !cors;
self
}
}

impl Default for ServerConfig {
fn default() -> Self {
Self { allow_origin: "*".parse::<HeaderValue>().unwrap().into(), no_cors: false }
Self {
allow_origin: "*".parse::<HeaderValue>().unwrap().into(),
no_cors: false,
no_request_size_limit: false,
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion crates/anvil/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use anvil_rpc::{
response::{ResponseResult, RpcResponse},
};
use axum::{
extract::DefaultBodyLimit,
http::{header, HeaderValue, Method},
routing::{post, MethodRouter},
Router,
Expand Down Expand Up @@ -56,7 +57,7 @@ fn router_inner<S: Clone + Send + Sync + 'static>(
root_method_router: MethodRouter<S>,
state: S,
) -> Router {
let ServerConfig { allow_origin, no_cors } = config;
let ServerConfig { allow_origin, no_cors, no_request_size_limit } = config;

let mut router = Router::new()
.route("/", root_method_router)
Expand All @@ -72,6 +73,9 @@ fn router_inner<S: Clone + Send + Sync + 'static>(
.allow_methods([Method::GET, Method::POST]),
);
}
if no_request_size_limit {
router = router.layer(DefaultBodyLimit::disable());
}
router
}

Expand Down

0 comments on commit bde40a8

Please sign in to comment.