parseRetryAfterSeconds function
Parses the numeric (delta-seconds) form of a Retry-After header into a
Duration, or null when it is not a non-negative integer.
The HTTP-date form (e.g. Wed, 21 Oct 2015 07:28:00 GMT) is intentionally
unsupported and returns null; parsing dates would require a timezone-aware
formatter this dependency-free helper deliberately avoids.
Audited: 2026-06-12 11:26 EDT
Implementation
Duration? parseRetryAfterSeconds(String header) {
final int? seconds = int.tryParse(header.trim());
// Reject the HTTP-date form and any malformed/negative value as null.
if (seconds == null || seconds < 0) {
return null;
}
return Duration(seconds: seconds);
}