Skip to content

Commit

Permalink
feat: add host validation in --distributed
Browse files Browse the repository at this point in the history
  • Loading branch information
cestef committed Oct 9, 2024
1 parent 6e97426 commit 5f35990
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/cli/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ pub fn parse_url(s: &str) -> Result<String, String> {
}
}

pub fn parse_host(s: &str) -> Result<String, String> {
let url = Url::parse(&format!("http://{}", s));

match url {
Ok(url) => {
if url.host().is_none() {
return Err("Invalid host".to_string());
}
Ok(url.host_str().unwrap().to_string())
}
Err(_) => Err("Invalid host".to_string()),
}
}

pub fn parse_header(s: &str) -> Result<String, String> {
// key: value
let parts = s.split(':').collect::<Vec<_>>();
Expand Down
11 changes: 9 additions & 2 deletions src/cli/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::utils::{
use serde::{Deserialize, Serialize};

use super::helpers::{
parse_cookie, parse_header, parse_method, parse_url, parse_wordlist, KeyOrKeyVal,
parse_cookie, parse_header, parse_host, parse_method, parse_url, parse_wordlist, KeyOrKeyVal,
KeyOrKeyValParser, KeyVal, KeyValParser,
};
use clap::Parser;
Expand Down Expand Up @@ -160,7 +160,14 @@ pub struct Opts {
pub insecure: bool,

/// Distribute the requests to multiple hosts
#[clap(long, env, hide_env = true, value_delimiter = ',')]
#[clap(
long,
env,
hide_env = true,
value_delimiter = ',',
visible_alias = "distribute",
value_parser = parse_host
)]
#[merge(strategy = merge::vec::overwrite_empty)]
#[serde(default)]
pub distributed: Vec<String>,
Expand Down

0 comments on commit 5f35990

Please sign in to comment.