1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Auth challenge retry logic for sequential downloads.
//
// Mirrors the C++ `HttpSkipResponseCommand::processResponse()` flow
// for 401/407 responses: resolve credentials, retry with Authorization.
use crate::constants;
use crate::error::{Aria2Error, RecoverableError, Result};
use crate::filesystem::resume_helper::ResumeState;
use crate::http::auth::{AuthConfigFactory, AuthResolveOptions};
use crate::http::auth_challenge_handler::{self, AuthChallengeResult};
use crate::http::response::is_redirect_status;
use crate::http::skip_response::AuthScheme;
use super::SequentialDownloader;
fn classify_auth_retry_status(status_code: u16) -> Aria2Error {
if status_code >= 500 || constants::RETRYABLE_HTTP_CODES.contains(&status_code) {
Aria2Error::Recoverable(RecoverableError::ServerError { code: status_code })
} else {
Aria2Error::Recoverable(RecoverableError::HttpProtocolError {
message: format!("HTTP error after auth retry: {status_code}"),
})
}
}
pub(super) enum AuthRetryOutcome {
Completed(Result<()>),
Redirect(String),
}
pub(super) struct AuthRetryRequest<'a> {
pub response: &'a reqwest::Response,
pub uri: &'a str,
pub url_parsed: &'a Option<reqwest::Url>,
pub status_code: u16,
pub authentication_used: bool,
pub allow_origin_auth: bool,
pub resume_state: &'a ResumeState,
pub auth_factory: &'a std::sync::Arc<std::sync::Mutex<AuthConfigFactory>>,
pub auth_opts: &'a AuthResolveOptions,
}
impl SequentialDownloader {
/// Attempt an authentication retry when a 401/407 response is received.
///
/// Returns a completed result or a redirect target after the auth retry.
/// Returns `Some(Err(...))` if the auth retry failed.
/// Returns `None` if auth retry is not possible (no credentials,
/// unsupported scheme, auth already used).
///
/// This mirrors the C++ `HttpSkipResponseCommand::processResponse()` flow
/// for the 401 case: activate BasicCred → prepareForRetry.
pub(in crate::engine::http::sequential_download) async fn try_auth_retry(
&mut self,
request: AuthRetryRequest<'_>,
) -> Option<Result<AuthRetryOutcome>> {
let AuthRetryRequest {
response,
uri,
url_parsed,
status_code,
authentication_used,
allow_origin_auth,
resume_state,
auth_factory,
auth_opts,
} = request;
let is_proxy = status_code == 407;
if !is_proxy && !allow_origin_auth {
tracing::debug!(
status_code,
"Skipping origin auth challenge after cross-origin redirect"
);
return None;
}
let header_name = if is_proxy {
"proxy-authenticate"
} else {
"www-authenticate"
};
// Extract the auth challenge header
let auth_header = response
.headers()
.get(header_name)
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());
// Parse the auth scheme
let scheme = match &auth_header {
Some(h) => AuthScheme::from_header(h),
None => {
// No auth header — if http_auth_challenge is enabled,
// treat as Basic challenge (matches C++ behavior)
if !authentication_used {
Some(AuthScheme::Basic)
} else {
None
}
}
};
let scheme = match scheme {
Some(s) => s,
None => {
tracing::warn!(
status_code,
"Auth challenge received but no supported scheme found"
);
return None;
}
};
// Build HttpAuthChallenge from the response
let challenge = crate::http::skip_response::HttpAuthChallenge {
scheme: scheme.clone(),
realm: auth_header
.as_deref()
.map(crate::http::skip_response::HttpSkipResponseHandler::extract_realm)
.unwrap_or_default(),
is_proxy,
digest_challenge: if scheme == AuthScheme::Digest {
auth_header
.as_deref()
.and_then(|h| crate::http::digest_auth::DigestAuthChallenge::parse(h).ok())
} else {
None
},
};
// Origin 401 retries are opt-in. Proxy credentials are an explicit
// proxy contract and must work independently of that origin option.
if !is_proxy && !auth_opts.http_auth_challenge {
tracing::debug!(
status_code,
"Auth challenge received but http_auth_challenge not enabled"
);
return None;
}
// Use the URL for credential resolution
let url = match url_parsed {
Some(u) => url::Url::parse(u.as_ref()).ok()?,
None => return None,
};
let result = {
let mut auth_factory = auth_factory
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
auth_challenge_handler::handle_auth_challenge(
&challenge,
&mut auth_factory,
&url,
auth_opts,
crate::http::request::HttpMethod::Get,
authentication_used,
1, // nc
)
};
match result {
AuthChallengeResult::RetryWithAuth {
authorization_header,
is_proxy,
} => {
// Build the retry request with Authorization header
// Re-apply the same Range header if we had a resume
let retry_request = if let Some(range_header) =
crate::filesystem::resume_helper::ResumeHelper::build_range_header(resume_state)
{
tracing::debug!("Auth retry: re-applying Range header: {}", range_header);
self.client.get(uri).header("Range", range_header)
} else {
self.client.get(uri)
};
// Add the Authorization or Proxy-Authorization header
let header_name = if is_proxy {
"Proxy-Authorization"
} else {
"Authorization"
};
let cookie_header = url_parsed
.as_ref()
.map(|url| self.cookie_helper.build_cookie_header_from_url(url));
let retry_cookie = cookie_header.as_deref().filter(|value| !value.is_empty());
let retry_request = if is_proxy {
self.request_policy.apply_for_origin_with_proxy_auth(
retry_request,
retry_cookie,
&[],
allow_origin_auth,
Some(&authorization_header),
)
} else {
self.request_policy.apply_for_origin(
retry_request,
retry_cookie,
&[(header_name.to_string(), authorization_header.clone())],
allow_origin_auth,
)
};
tracing::info!(
status_code,
scheme = ?scheme,
"Retrying HTTP request with {} authentication",
if is_proxy { "proxy " } else { "" }
);
// Send the retry request
let retry_response = match retry_request.send().await {
Ok(r) => r,
Err(e) => {
return Some(Err(Aria2Error::Recoverable(
RecoverableError::TemporaryNetworkFailure {
message: format!("Auth retry request failed: {}", e),
},
)));
}
};
self.cookie_helper
.extract_and_store_cookies(uri, &retry_response);
let retry_status = retry_response.status();
let mut effective_resume_state = resume_state.clone();
if retry_status.as_u16() == 200 && Self::resume_requested(&effective_resume_state) {
effective_resume_state = match self
.resume_state_after_failed_request(&effective_resume_state)
.await
{
Ok(state) => state,
Err(error) => return Some(Err(error)),
};
}
if retry_status.is_success() || retry_status.as_u16() == 206 {
// Auth retry succeeded — proceed with the download using
// the retry response
return Some(Ok(AuthRetryOutcome::Completed(
self.download_response_body(retry_response, uri, &effective_resume_state)
.await,
)));
}
if is_redirect_status(retry_status.as_u16()) {
let location = retry_response
.headers()
.get("location")
.and_then(|value| value.to_str().ok());
let location = match location {
Some(location) => location,
None => {
return Some(Err(Aria2Error::Recoverable(
RecoverableError::HttpProtocolError {
message: format!(
"HTTP {} redirect without Location header",
retry_status.as_u16()
),
},
)));
}
};
let base_url = match url_parsed {
Some(url) => url,
None => {
return Some(Err(Aria2Error::Recoverable(
RecoverableError::HttpProtocolError {
message: format!(
"Cannot resolve HTTP redirect from invalid URL: {uri}"
),
},
)));
}
};
let target_url = match base_url.join(location) {
Ok(url) => url.to_string(),
Err(error) => {
return Some(Err(Aria2Error::Recoverable(
RecoverableError::HttpProtocolError {
message: format!(
"Failed to resolve redirect URL '{location}': {error}"
),
},
)));
}
};
return Some(Ok(AuthRetryOutcome::Redirect(target_url)));
}
// Auth retry still failed
if retry_status.as_u16() == 401 || retry_status.as_u16() == 407 {
tracing::warn!(
status_code = retry_status.as_u16(),
"Auth retry still failed — credentials may be incorrect"
);
return Some(Err(Aria2Error::Recoverable(
RecoverableError::HttpAuthFailed {
message: format!("Authentication failed: HTTP {}", retry_status),
},
)));
}
let error = if retry_status.as_u16() == 404 {
self.classify_file_not_found()
} else {
classify_auth_retry_status(retry_status.as_u16())
};
Some(Err(error))
}
AuthChallengeResult::NoCredentials {
status_code,
message,
} => {
tracing::warn!(
status_code,
"Auth challenge but no credentials: {}",
message
);
None // Fall through to normal error handling
}
AuthChallengeResult::UnsupportedScheme {
scheme,
status_code,
} => {
tracing::warn!(status_code, scheme, "Unsupported authentication scheme");
None // Fall through to normal error handling
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn classifies_auth_retry_5xx_as_server_error() {
assert!(matches!(
classify_auth_retry_status(503),
Aria2Error::Recoverable(RecoverableError::ServerError { code: 503 })
));
}
#[test]
fn classifies_configured_4xx_transients_as_server_errors() {
for status_code in [408, 429] {
assert!(matches!(
classify_auth_retry_status(status_code),
Aria2Error::Recoverable(RecoverableError::ServerError { code })
if code == status_code
));
}
}
#[test]
fn classifies_auth_retry_4xx_as_protocol_error() {
assert!(matches!(
classify_auth_retry_status(404),
Aria2Error::Recoverable(RecoverableError::HttpProtocolError { message })
if message.contains("404")
));
}
}