Skip to content

Commit ebfae9a

Browse files
committed
Make content_length_limit configurable
1 parent a89b6e6 commit ebfae9a

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ ETHERSCAN_KEY=
99
API_KEY=
1010
# Port to run the simulator on, defaults to 8080
1111
PORT=
12+
# Maximum size for incoming requests (in KB), defaults to 16
13+
MAX_REQUEST_SIZE=

src/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub struct Config {
66
pub fork_url: Option<String>,
77
pub etherscan_key: Option<String>,
88
pub api_key: Option<String>,
9+
pub max_request_size: u64,
910
}
1011

1112
pub fn config() -> Config {
@@ -24,12 +25,18 @@ fn load_config() -> Config {
2425
.ok()
2526
.filter(|k| !k.is_empty());
2627
let api_key = std::env::var("API_KEY").ok().filter(|k| !k.is_empty());
28+
let max_request_size = std::env::var("MAX_REQUEST_SIZE")
29+
.unwrap_or("16".to_string())
30+
.parse::<u64>()
31+
.expect("MAX_REQUEST_SIZE must be a valid u64")
32+
* 1024;
2733

2834
Config {
2935
fork_url,
3036
port,
3137
etherscan_key,
3238
api_key,
39+
max_request_size,
3340
}
3441
}
3542

src/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ pub fn simulate_routes(
2525
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
2626
simulate(config.clone())
2727
.or(simulate_bundle(config.clone()))
28-
.or(simulate_stateful_new(config, state.clone()))
28+
.or(simulate_stateful_new(config.clone(), state.clone()))
2929
.or(simulate_stateful_end(state.clone()))
30-
.or(simulate_stateful(state))
30+
.or(simulate_stateful(config, state))
3131
}
3232

3333
/// POST /simulate
3434
pub fn simulate(config: Config) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
3535
warp::path!("simulate")
3636
.and(warp::post())
37-
.and(json_body::<SimulationRequest>())
37+
.and(json_body::<SimulationRequest>(&config))
3838
.and(with_config(config))
3939
.and_then(simulation::simulate)
4040
}
@@ -45,7 +45,7 @@ pub fn simulate_bundle(
4545
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
4646
warp::path!("simulate-bundle")
4747
.and(warp::post())
48-
.and(json_body())
48+
.and(json_body(&config))
4949
.and(with_config(config))
5050
.and_then(simulation::simulate_bundle)
5151
}
@@ -57,7 +57,7 @@ pub fn simulate_stateful_new(
5757
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
5858
warp::path!("simulate-stateful")
5959
.and(warp::post())
60-
.and(json_body::<StatefulSimulationRequest>())
60+
.and(json_body::<StatefulSimulationRequest>(&config))
6161
.and(with_config(config))
6262
.and(with_state(state))
6363
.and_then(simulation::simulate_stateful_new)
@@ -75,11 +75,12 @@ pub fn simulate_stateful_end(
7575

7676
/// POST /simulate-stateful/{statefulSimulationId}
7777
pub fn simulate_stateful(
78+
config: Config,
7879
state: Arc<SharedSimulationState>,
7980
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
8081
warp::path!("simulate-stateful" / Uuid)
8182
.and(warp::post())
82-
.and(json_body())
83+
.and(json_body(&config))
8384
.and(with_state(state))
8485
.and_then(simulation::simulate_stateful)
8586
}
@@ -97,7 +98,8 @@ fn with_state(
9798
warp::any().map(move || state.clone())
9899
}
99100

100-
fn json_body<T: DeserializeOwned + Send>() -> impl Filter<Extract = (T,), Error = Rejection> + Clone
101-
{
102-
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
101+
fn json_body<T: DeserializeOwned + Send>(
102+
config: &Config,
103+
) -> impl Filter<Extract = (T,), Error = Rejection> + Clone {
104+
warp::body::content_length_limit(config.max_request_size).and(warp::body::json())
103105
}

0 commit comments

Comments
 (0)