Prevent panic for wget and http_client on invalid non http URL input

This commit is contained in:
sagie gur ari 2020-01-30 21:16:02 +00:00
parent cf2411dbd8
commit 642727aae9
4 changed files with 28 additions and 3 deletions

View file

@ -17,6 +17,7 @@
* New concat command.
* Improve wget input parsing.
* Modify full named commands.
* Prevent panic for wget and http_client on invalid non http URL input.
### v0.1.8 (2020-01-24)

View file

@ -153,9 +153,14 @@ impl Command for CommandImpl {
let len = arguments.len() - 1;
let url = arguments[len].to_string();
match parse_options(&arguments[0..len].to_vec()) {
Ok(options) => do_request(url, options),
Err(error) => CommandResult::Error(error),
let url_lowercase = url.to_lowercase();
if !url_lowercase.starts_with("http://") && !url_lowercase.starts_with("https://") {
CommandResult::Error(format!("Invalid URL: {} provided.", &url).to_string())
} else {
match parse_options(&arguments[0..len].to_vec()) {
Ok(options) => do_request(url, options),
Err(error) => CommandResult::Error(error),
}
}
}
}

View file

@ -51,3 +51,8 @@ fn run_post() {
CommandValidation::Contains("out".to_string(), "success".to_string()),
);
}
#[test]
fn run_invalid_url() {
test::run_script_and_error(vec![create("")], "out = http_client invalid_url", "out");
}

View file

@ -28,3 +28,17 @@ function test_post
assert ${found}
end
function test_invalid_url
error = get_last_error
empty = is_empty ${error}
assert ${empty}
response = http_client invalid_url
error = get_last_error
empty = is_empty ${error}
assert_false ${empty}
assert_false ${response}
end