Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ pub fn validate(cli: &Cli) -> Result<(), FetchError> {
{
return Err("min-tls must be less than or equal to max-tls".into());
}
if let Some(retry_count) = cli.retry {
crate::http::total_attempts_for_retry(retry_count)?;
}
Ok(())
}

Expand Down Expand Up @@ -672,11 +675,10 @@ fn parse_duration_seconds(
value: &str,
usage: &str,
) -> Result<f64, String> {
const MAX_SECONDS: f64 = i64::MAX as f64 / 1_000_000_000_f64;
let seconds = value
.parse::<f64>()
.map_err(|_| value_error(path, line_num, option, value, usage))?;
if !seconds.is_finite() || !(0.0..=MAX_SECONDS).contains(&seconds) {
if !seconds.is_finite() || !(0.0..=crate::http::MAX_DURATION_SECONDS).contains(&seconds) {
return Err(value_error(path, line_num, option, value, usage));
}
Ok(seconds)
Expand Down Expand Up @@ -1259,6 +1261,29 @@ mod tests {
}
}

#[test]
fn validate_rejects_retry_count_that_cannot_add_initial_attempt() {
let mut cli = Cli::try_parse_from([
"fetch",
"--retry",
&usize::MAX.to_string(),
"https://example.com",
])
.unwrap();

let err = validate(&cli).unwrap_err();

assert_eq!(
err.to_string(),
format!(
"invalid value '{}' for option '--retry': must be less than the maximum usize value",
usize::MAX
)
);
cli.retry = Some(usize::MAX - 1);
validate(&cli).unwrap();
}

#[test]
fn parse_duration_seconds_matches_go_validation() {
let path = PathBuf::from("test/config");
Expand Down
55 changes: 52 additions & 3 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl RequestBodyPayload {

const MAX_BUFFERED_RESPONSE_BYTES: usize = 16 * 1024 * 1024;
const MAX_DISCARDED_RESPONSE_BYTES: usize = 1024 * 1024;
pub(crate) const MAX_DURATION_SECONDS: f64 = i64::MAX as f64 / 1_000_000_000_f64;

pub async fn execute(cli: &Cli) -> Result<i32, FetchError> {
let http_version =
Expand Down Expand Up @@ -128,7 +129,7 @@ pub async fn execute(cli: &Cli) -> Result<i32, FetchError> {
session: session.as_ref(),
connect_timing: Some(&connect_timing),
};
let initial_client = client::build_client_for_url(cli, &url, &client_build).await?;
let mut initial_client = client::build_client_for_url(cli, &url, &client_build).await?;
if cli.grpc && grpc_method.is_none() && grpc_request_requires_schema(cli) {
let schema =
crate::grpc::reflection::schema_for_call(cli, &url, &initial_client.client).await?;
Expand Down Expand Up @@ -188,7 +189,7 @@ pub async fn execute(cli: &Cli) -> Result<i32, FetchError> {

let retry_count = cli.retry();
let retry_delay = duration_from_seconds("retry-delay", cli.retry_delay())?;
let total_attempts = retry_count + 1;
let total_attempts = total_attempts_for_retry(retry_count)?;
let mut attempt = 0;
let result = loop {
let mut request_method = method.clone();
Expand Down Expand Up @@ -327,6 +328,16 @@ pub async fn execute(cli: &Cli) -> Result<i32, FetchError> {
);
drain_response_body_bounded(response).await;
tokio::time::sleep(delay).await;
let retry_client_build = client::ClientBuildContext {
mode: client_build.mode,
request_timeout,
connect_timeout,
request_start: Instant::now(),
session: session.as_ref(),
connect_timing: Some(&connect_timing),
};
initial_client =
client::build_client_for_url(cli, &url, &retry_client_build).await?;
attempt += 1;
continue;
}
Expand Down Expand Up @@ -2246,12 +2257,22 @@ pub(crate) fn request_target(url: &Url) -> String {
}

pub(crate) fn duration_from_seconds(flag: &str, seconds: f64) -> Result<Duration, FetchError> {
if !seconds.is_finite() || seconds < 0.0 {
if !seconds.is_finite() || !(0.0..=MAX_DURATION_SECONDS).contains(&seconds) {
return Err(format!("{flag} must be a non-negative number").into());
}
Ok(Duration::from_secs_f64(seconds))
}

pub(crate) fn total_attempts_for_retry(retry_count: usize) -> Result<usize, FetchError> {
retry_count.checked_add(1).ok_or_else(|| {
FetchError::invalid_value(
"--retry",
retry_count.to_string(),
"must be less than the maximum usize value",
)
})
}

pub(crate) fn normalize_url(raw: &str) -> Result<Url, FetchError> {
if raw.is_empty() {
return Err("empty URL provided".into());
Expand Down Expand Up @@ -3634,6 +3655,34 @@ mod tests {
assert_eq!(format_delay(Duration::from_secs(1)), "1.0s");
}

#[test]
fn duration_from_seconds_rejects_values_outside_supported_range() {
assert_eq!(
duration_from_seconds("timeout", 1.5).unwrap(),
Duration::from_millis(1500)
);

for seconds in [-1.0, f64::NAN, f64::INFINITY, 1e100] {
let err = duration_from_seconds("timeout", seconds).unwrap_err();
assert_eq!(err.to_string(), "timeout must be a non-negative number");
}
}

#[test]
fn total_attempts_for_retry_rejects_overflow() {
assert_eq!(total_attempts_for_retry(0).unwrap(), 1);
assert_eq!(total_attempts_for_retry(3).unwrap(), 4);

let err = total_attempts_for_retry(usize::MAX).unwrap_err();
assert_eq!(
err.to_string(),
format!(
"invalid value '{}' for option '--retry': must be less than the maximum usize value",
usize::MAX
)
);
}

#[test]
fn parse_retry_after_matches_go_integer_and_date_cases() {
let mut headers = HeaderMap::new();
Expand Down
Loading