Skip to content

Commit

Permalink
Merge pull request #65 from rarecrumb/content-type
Browse files Browse the repository at this point in the history
fix(balancer): Insert content-type header if not set to json
  • Loading branch information
makemake-kbo authored Mar 14, 2024
2 parents 4bc33a9 + 858a174 commit 6fa1737
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 2 additions & 0 deletions example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ ma_length = 100
sort_on_startup = true
# Enable health checking
health_check = true
# Enable content type header checking
header_check = true
# Acceptable time to wait for a response in ms
ttl = 30
# How many times to retry a request before giving up
Expand Down
22 changes: 14 additions & 8 deletions src/balancer/accept_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl ConnectionParams {
struct RequestParams {
ttl: u128,
max_retries: u32,
header_check: bool,
}

#[derive(Debug)]
Expand Down Expand Up @@ -284,14 +285,18 @@ async fn forward_body(
Option<usize>,
) {
// Check if body has application/json
if tx.headers().get("content-type") != Some(&HeaderValue::from_static("application/json")) {
return (
Ok(hyper::Response::builder()
.status(400)
.body(Full::new(Bytes::from("Improper content-type header")))
.unwrap()),
None,
);
//
// Can be toggled via the config. Should be on if we want blutgang to be JSON-RPC compliant.
if params.header_check {
if tx.headers().get("content-type") != Some(&HeaderValue::from_static("application/json")) {
return (
Ok(hyper::Response::builder()
.status(400)
.body(Full::new(Bytes::from("Improper content-type header")))
.unwrap()),
None,
);
}
}

// Convert incoming body to serde value
Expand Down Expand Up @@ -421,6 +426,7 @@ pub async fn accept_request(
RequestParams {
ttl: config_guard.ttl,
max_retries: config_guard.max_retries,
header_check: config_guard.header_check,
}
};

Expand Down
10 changes: 10 additions & 0 deletions src/config/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub struct Settings {
pub do_clear: bool,
pub address: SocketAddr,
pub health_check: bool,
pub header_check: bool,
pub ttl: u128,
pub expected_block_time: u64,
pub supress_rpc_check: bool,
Expand All @@ -82,6 +83,7 @@ impl Default for Settings {
do_clear: false,
address: "127.0.0.1:3000".parse::<SocketAddr>().unwrap(),
health_check: false,
header_check: true,
ttl: 1000,
expected_block_time: 12500,
supress_rpc_check: true,
Expand Down Expand Up @@ -169,6 +171,11 @@ impl Settings {
.expect("\x1b[31mErr:\x1b[0m Missing health_check toggle!")
.as_bool()
.expect("\x1b[31mErr:\x1b[0m Could not parse health_check as bool!");
let header_check = blutgang_table
.get("header_check")
.expect("\x1b[31mErr:\x1b[0m Missing header_check toggle!")
.as_bool()
.expect("\x1b[31mErr:\x1b[0m Could not parse header_check as bool!");
let ttl = blutgang_table
.get("ttl")
.expect("\x1b[31mErr:\x1b[0m Missing ttl!")
Expand Down Expand Up @@ -406,6 +413,7 @@ impl Settings {
do_clear,
address,
health_check,
header_check,
ttl,
expected_block_time,
max_retries,
Expand Down Expand Up @@ -494,6 +502,7 @@ impl Settings {
.flush_every_ms(Some(flush_every_ms));

let health_check = matches.get_occurrences::<String>("health_check").is_some();
let header_check = matches.get_occurrences::<String>("header_check").is_some();

let ttl = matches
.get_one::<String>("ttl")
Expand Down Expand Up @@ -554,6 +563,7 @@ impl Settings {
do_clear: clear,
address,
health_check,
header_check,
ttl,
supress_rpc_check,
expected_block_time,
Expand Down

0 comments on commit 6fa1737

Please sign in to comment.