Skip to content

Commit f90d7f9

Browse files
move retry_after() to lib
1 parent d4daea6 commit f90d7f9

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

src/account.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ use serde::{Deserialize, Serialize};
1414

1515
#[cfg(feature = "hyper-rustls")]
1616
use crate::DefaultClient;
17-
use crate::order::{Order, retry_after};
17+
use crate::order::Order;
1818
use crate::types::{
1919
AccountCredentials, AuthorizationStatus, Empty, Header, JoseJson, Jwk, KeyOrKeyId, NewAccount,
2020
NewAccountPayload, NewOrder, OrderState, Problem, ProfileMeta, RevocationRequest, Signer,
2121
SigningAlgorithm,
2222
};
2323
#[cfg(feature = "time")]
2424
use crate::types::{CertificateIdentifier, RenewalInfo};
25-
use crate::{BytesResponse, Client, Error, HttpClient, crypto, nonce_from_response};
25+
use crate::{BytesResponse, Client, Error, HttpClient, crypto, nonce_from_response, retry_after};
2626

2727
/// An ACME account as described in RFC 8555 (section 7.1.2)
2828
///

src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ use std::error::Error as StdError;
88
use std::fmt;
99
use std::future::Future;
1010
use std::pin::Pin;
11+
use std::str::FromStr;
12+
use std::time::{Duration, SystemTime};
1113

1214
use async_trait::async_trait;
1315
use bytes::Bytes;
14-
use http::header::CONTENT_TYPE;
16+
use http::header::{CONTENT_TYPE, RETRY_AFTER};
1517
use http::{Method, Request, Response, StatusCode};
1618
use http_body_util::{BodyExt, Full};
19+
use httpdate::HttpDate;
1720
#[cfg(feature = "hyper-rustls")]
1821
use hyper_util::client::legacy::Client as HyperClient;
1922
#[cfg(feature = "hyper-rustls")]
@@ -159,6 +162,28 @@ fn nonce_from_response(rsp: &BytesResponse) -> Option<String> {
159162
.and_then(|hv| String::from_utf8(hv.as_ref().to_vec()).ok())
160163
}
161164

165+
/// Parse the `Retry-After` header from the response
166+
///
167+
/// <https://httpwg.org/specs/rfc9110.html#field.retry-after>
168+
///
169+
/// # Syntax
170+
///
171+
/// Retry-After = HTTP-date / delay-seconds
172+
/// delay-seconds = 1*DIGIT
173+
fn retry_after(rsp: &BytesResponse) -> Option<SystemTime> {
174+
let value = rsp.parts.headers.get(RETRY_AFTER)?.to_str().ok()?.trim();
175+
if value.is_empty() {
176+
return None;
177+
}
178+
179+
Some(match u64::from_str(value) {
180+
// `delay-seconds` is a number of seconds to wait
181+
Ok(secs) => SystemTime::now() + Duration::from_secs(secs),
182+
// `HTTP-date` looks like `Fri, 31 Dec 1999 23:59:59 GMT`
183+
Err(_) => SystemTime::from(HttpDate::from_str(value).ok()?),
184+
})
185+
}
186+
162187
#[cfg(feature = "hyper-rustls")]
163188
struct DefaultClient(HyperClient<hyper_rustls::HttpsConnector<HttpConnector>, Full<Bytes>>);
164189

src/order.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use std::ops::{ControlFlow, Deref};
2-
use std::str::FromStr;
32
use std::sync::Arc;
43
use std::time::{Duration, Instant, SystemTime};
54
use std::{fmt, slice};
65

76
use base64::prelude::{BASE64_URL_SAFE_NO_PAD, Engine};
8-
use http::header::RETRY_AFTER;
9-
use httpdate::HttpDate;
107
#[cfg(feature = "rcgen")]
118
use rcgen::{CertificateParams, DistinguishedName, KeyPair};
129
use serde::Serialize;
@@ -17,7 +14,7 @@ use crate::types::{
1714
Authorization, AuthorizationState, AuthorizationStatus, AuthorizedIdentifier, Challenge,
1815
ChallengeType, Empty, FinalizeRequest, OrderState, OrderStatus, Problem,
1916
};
20-
use crate::{BytesResponse, Error, Key, crypto, nonce_from_response};
17+
use crate::{Error, Key, crypto, nonce_from_response, retry_after};
2118

2219
/// An ACME order as described in RFC 8555 (section 7.1.3)
2320
///
@@ -613,25 +610,3 @@ impl RetryState {
613610
}
614611
}
615612
}
616-
617-
/// Parse the `Retry-After` header from the response
618-
///
619-
/// <https://httpwg.org/specs/rfc9110.html#field.retry-after>
620-
///
621-
/// # Syntax
622-
///
623-
/// Retry-After = HTTP-date / delay-seconds
624-
/// delay-seconds = 1*DIGIT
625-
pub(crate) fn retry_after(rsp: &BytesResponse) -> Option<SystemTime> {
626-
let value = rsp.parts.headers.get(RETRY_AFTER)?.to_str().ok()?.trim();
627-
if value.is_empty() {
628-
return None;
629-
}
630-
631-
Some(match u64::from_str(value) {
632-
// `delay-seconds` is a number of seconds to wait
633-
Ok(secs) => SystemTime::now() + Duration::from_secs(secs),
634-
// `HTTP-date` looks like `Fri, 31 Dec 1999 23:59:59 GMT`
635-
Err(_) => SystemTime::from(HttpDate::from_str(value).ok()?),
636-
})
637-
}

0 commit comments

Comments
 (0)