Skip to content

Commit

Permalink
fix: URL validation and error message
Browse files Browse the repository at this point in the history
for env var P_INGESTOR_ENDPOINT
corrected error messages for socket address and URL validations
  • Loading branch information
nikhilsinhaparseable committed Jun 10, 2024
1 parent 36aa929 commit 9389b4b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
3 changes: 2 additions & 1 deletion server/src/handlers/airplane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ pub fn server() -> impl Future<Output = Result<(), Box<dyn std::error::Error + S
.parseable
.address
.parse()
.expect("valid socket address");
.unwrap_or_else(|err| panic!("{}, failed to parse `{}` as a socket address. Please set the environment variable `P_ADDR` to `<ip address>:<port>` without the scheme (e.g., 192.168.1.1:8000)",
CONFIG.parseable.address, err));
addr.set_port(CONFIG.parseable.flight_port);

let service = AirServiceImpl {};
Expand Down
40 changes: 31 additions & 9 deletions server/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,21 @@ pub fn get_url() -> Url {
CONFIG.parseable.address
)
.parse::<Url>() // if the value was improperly set, this will panic before hand
.expect("Valid URL");
.unwrap_or_else(|err| panic!("{}, failed to parse `{}` as Url. Please set the environment variable `P_ADDR` to `<ip address>:<port>` without the scheme (e.g., 192.168.1.1:8000)",
err, CONFIG.parseable.address));
}

let ingestor_endpoint = &CONFIG.parseable.ingestor_endpoint;

if ingestor_endpoint.starts_with("http") {
panic!("Invalid value `{}`, please set the environement variable `P_INGESTOR_ENDPOINT` to `<ip address / DNS>:<port>` without the scheme (e.g., 192.168.1.1:8000 or example.com:8000)", ingestor_endpoint);
}

let addr_from_env = ingestor_endpoint.split(':').collect::<Vec<&str>>();

if addr_from_env.len() != 2 {
panic!("Invalid value `{}`, please set the environement variable `P_INGESTOR_ENDPOINT` to `<ip address / DNS>:<port>` without the scheme (e.g., 192.168.1.1:8000 or example.com:8000)", ingestor_endpoint);
}
let addr_from_env = CONFIG
.parseable
.ingestor_endpoint
.split(':')
.collect::<Vec<&str>>();

let mut hostname = addr_from_env[0].to_string();
let mut port = addr_from_env[1].to_string();
Expand All @@ -254,15 +262,29 @@ pub fn get_url() -> Url {
if hostname.starts_with('$') {
let var_hostname = hostname[1..].to_string();
hostname = get_from_env(&var_hostname);
}
if !hostname.starts_with("http") {
hostname = format!("{}://{}", CONFIG.parseable.get_scheme(), hostname);

if hostname.is_empty() {
panic!("The environement variable `{}` is not set, please set as <ip address / DNS> without the scheme (e.g., 192.168.1.1 or example.com)", var_hostname);
}
if hostname.starts_with("http") {
panic!("Invalid value `{}`, please set the environement variable `{}` to `<ip address / DNS>` without the scheme (e.g., 192.168.1.1 or example.com)", hostname, var_hostname);
} else {
hostname = format!("{}://{}", CONFIG.parseable.get_scheme(), hostname);
}
}

if port.starts_with('$') {
let var_port = port[1..].to_string();
port = get_from_env(&var_port);

if port.is_empty() {
panic!(
"Port is not set in the environement variable `{}`",
var_port
);
}
}

format!("{}:{}", hostname, port)
.parse::<Url>()
.expect("Valid URL")
Expand Down

0 comments on commit 9389b4b

Please sign in to comment.