From ab1a57d98894847f8e06493cfaf3ea9d154533a6 Mon Sep 17 00:00:00 2001 From: DeathwingTheBoss <ozcanbarisucar@gmail.com> Date: Tue, 21 Mar 2023 22:29:40 +0300 Subject: [PATCH] - Limit batch calls with 100 calls - Change error codes. - Increase payload limit to 100KB. - Version bump --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 17 +++++++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a2a461a..69134df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -574,7 +574,7 @@ checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" [[package]] name = "drone" -version = "0.1.9" +version = "0.2.0" dependencies = [ "actix-cors", "actix-web", diff --git a/Cargo.toml b/Cargo.toml index a1cefd2..71d0c8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drone" -version = "0.1.9" +version = "0.2.0" edition = "2021" authors = ["Deathwing <hi@deathwing.me>"] description = "A caching reverse-proxy application for the Hive blockchain." diff --git a/src/main.rs b/src/main.rs index 8a8d749..2ff6b2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -188,7 +188,7 @@ async fn handle_request( Ok(response) => response, Err(err) => { return Err(ErrorStructure { - code: 1000, + code: -32700, message: format!("Unable to send request to endpoint."), error: ErrorField::Message(err.to_string()), }) @@ -198,7 +198,7 @@ async fn handle_request( Ok(text) => text, Err(err) => { return Err(ErrorStructure { - code: 2000, + code: -32600, message: format!("Received an invalid response from the endpoint."), error: ErrorField::Message(err.to_string()), }) @@ -216,7 +216,7 @@ async fn handle_request( }; if json_body["error"].is_object() { return Err(ErrorStructure { - code: 4000, + code: -32602, message: format!("Endpoint returned an error."), error: ErrorField::Object(json_body["error"].clone()), }); @@ -284,6 +284,15 @@ async fn api_call( } APICall::Batch(requests) => { let mut responses = Vec::new(); + // If there's over 100 in the batch, return an error. + if requests.len() > 100 { + return HttpResponse::InternalServerError().json(ErrorStructure { + code: -32600, + message: "Internal Server Error".to_string(), + error: ErrorField::Message("Batch size too large.".to_string()), + }); + } + for request in requests { let result = handle_request(&request, &data, &user_ip).await; match result { @@ -365,7 +374,7 @@ async fn main() -> std::io::Result<()> { .app_data(web::JsonConfig::default() .content_type(|_| true) .content_type_required(false) - .limit(1024)) + .limit(1024 * 100)) // 100kb .app_data(_cache.clone()) .route("/", web::get().to(index)) .route("/", web::post().to(api_call)) -- GitLab