aria2-protocol 0.3.10

Multi-protocol networking stack for aria2-rust: HTTP/HTTPS client, FTP/SFTP, full BitTorrent (DHT/PEX/MSE), and Metalink V3/V4 parser
Documentation
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! HTTP CONNECT handshake over a caller-established TCP stream.

use std::io;
use std::time::Duration;

use thiserror::Error;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::time::timeout;
use tracing::debug;

use super::auth::basic_authorization;
use super::digest::{DigestChallenge, DigestResponse};
use super::header_processor::{HttpResponseHeadReadError, read_response_head};
use super::request::{
    HttpRequestSerializationError, is_valid_http_field_value, try_serialize_http_request,
};

/// Settings for one HTTP CONNECT handshake.
pub struct HttpConnectOptions<'a> {
    /// Target host used in the CONNECT authority and Host field.
    pub target_host: &'a str,
    /// Target port used in the CONNECT authority and Host field.
    pub target_port: u16,
    /// User-Agent field value.
    pub user_agent: &'a str,
    /// Optional proxy username.
    pub username: Option<&'a str>,
    /// Proxy password, if configured.
    pub password: Option<&'a str>,
    /// Whether to send configured Basic credentials before a challenge.
    pub preemptive_basic_auth: bool,
    /// Maximum time to write each CONNECT request.
    pub write_timeout: Duration,
    /// Maximum time to read each proxy response head.
    pub read_timeout: Duration,
}

/// Authentication and request details for compatibility callers that need a
/// CONNECT retry policy different from the engine default.
#[derive(Debug, Clone, Copy)]
pub struct HttpConnectRetryPolicy {
    /// Maximum number of CONNECT retries after a 407 response.
    pub max_auth_retries: u32,
    /// Prefer a Digest challenge over Basic when the proxy sends several.
    pub prefer_digest: bool,
    /// Fall back to Basic when the proxy advertises only an unknown scheme.
    pub fallback_to_basic: bool,
    /// Include the legacy `Proxy-Connection: keep-alive` request field.
    pub proxy_connection_keep_alive: bool,
}

impl Default for HttpConnectRetryPolicy {
    fn default() -> Self {
        Self {
            max_auth_retries: 1,
            prefer_digest: false,
            fallback_to_basic: false,
            proxy_connection_keep_alive: false,
        }
    }
}

/// Failure while establishing an HTTP CONNECT tunnel.
#[derive(Debug, Error)]
pub enum HttpConnectError {
    /// A CONNECT read or write timed out.
    #[error("HTTP CONNECT operation timed out")]
    Timeout,
    /// A stream read or write failed.
    #[error("HTTP CONNECT stream I/O failed: {0}")]
    Io(#[from] io::Error),
    /// The proxy response could not be parsed.
    #[error("Invalid HTTP CONNECT response: {0}")]
    InvalidResponse(String),
    /// The proxy asked for credentials but none were configured.
    #[error("proxy requires authentication but no credentials were provided")]
    CredentialsRequired,
    /// The proxy requested an authentication scheme that is not supported.
    #[error("unsupported proxy authentication challenge: {0}")]
    UnsupportedAuthentication(String),
    /// Authentication was rejected after the bounded retry.
    #[error("proxy authentication failed")]
    AuthenticationFailed,
    /// The proxy rejected the CONNECT request.
    #[error("proxy rejected CONNECT with status {status_code} {reason_phrase}")]
    Rejected {
        /// HTTP status returned by the proxy.
        status_code: u16,
        /// Reason phrase returned by the proxy.
        reason_phrase: String,
    },
    /// A caller-provided field cannot be serialized safely.
    #[error("invalid HTTP CONNECT request field: {0}")]
    InvalidRequestField(&'static str),
}

struct ProxyResponse {
    status_code: u16,
    reason_phrase: String,
    headers: Vec<(String, String)>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ProxyResponseBodyFraming {
    Chunked,
    ContentLength(usize),
}

/// Complete an HTTP CONNECT handshake without opening or replacing the stream.
///
/// The caller retains DNS, socket creation, binding, and outbound-network
/// policy. This function owns CONNECT framing, informational responses,
/// proxy-auth challenge handling, and response-head reads. It reads exactly
/// through each header terminator so bytes following a successful response
/// remain available to the tunneled protocol.
pub async fn establish_connect_tunnel(
    stream: &mut TcpStream,
    options: &HttpConnectOptions<'_>,
) -> Result<(), HttpConnectError> {
    establish_connect_tunnel_with_retry_policy(stream, options, &HttpConnectRetryPolicy::default())
        .await
}

/// Complete a CONNECT handshake using an explicit proxy-authentication retry
/// policy. This supports public compatibility facades without moving their
/// protocol session logic back into the engine crate.
pub async fn establish_connect_tunnel_with_retry_policy(
    stream: &mut TcpStream,
    options: &HttpConnectOptions<'_>,
    retry_policy: &HttpConnectRetryPolicy,
) -> Result<(), HttpConnectError> {
    if !is_valid_http_field_value(options.user_agent) {
        return Err(HttpConnectError::InvalidRequestField("User-Agent"));
    }
    let preemptive_auth = if options.preemptive_basic_auth {
        options
            .username
            .filter(|username| !username.is_empty())
            .map(|username| basic_authorization(username, options.password.unwrap_or("")))
    } else {
        None
    };
    send_request(
        stream,
        options,
        preemptive_auth.as_deref(),
        retry_policy.proxy_connection_keep_alive,
    )
    .await?;

    let mut auth_retries = 0;
    loop {
        let response = read_proxy_response(stream, options.read_timeout).await?;
        match response.status_code {
            200 => return Ok(()),
            407 if auth_retries < retry_policy.max_auth_retries => {
                auth_retries += 1;
                let username = options
                    .username
                    .filter(|username| !username.is_empty())
                    .ok_or(HttpConnectError::CredentialsRequired)?;
                consume_response_body(stream, &response.headers, options.read_timeout).await?;
                let target = format!("{}:{}", options.target_host, options.target_port);
                let auth = challenged_authentication(
                    username,
                    options.password.unwrap_or(""),
                    &response.headers,
                    &target,
                    auth_retries,
                    retry_policy,
                )?;
                send_request(
                    stream,
                    options,
                    Some(&auth),
                    retry_policy.proxy_connection_keep_alive,
                )
                .await?;
            }
            407 => return Err(HttpConnectError::AuthenticationFailed),
            status_code => {
                return Err(HttpConnectError::Rejected {
                    status_code,
                    reason_phrase: response.reason_phrase,
                });
            }
        }
    }
}

fn challenged_authentication(
    username: &str,
    password: &str,
    headers: &[(String, String)],
    target: &str,
    nonce_count: u32,
    retry_policy: &HttpConnectRetryPolicy,
) -> Result<String, HttpConnectError> {
    let challenges = headers
        .iter()
        .filter(|(name, _)| name.eq_ignore_ascii_case("proxy-authenticate"))
        .map(|(_, value)| value.as_str())
        .collect::<Vec<_>>();
    let challenge = if retry_policy.prefer_digest {
        challenges
            .iter()
            .copied()
            .find(|value| challenge_scheme(value).eq_ignore_ascii_case("digest"))
            .or_else(|| challenges.first().copied())
    } else {
        challenges.first().copied()
    }
    .unwrap_or_default();
    let scheme = challenge_scheme(challenge);
    if scheme.eq_ignore_ascii_case("basic") {
        return Ok(basic_authorization(username, password));
    }
    if scheme.eq_ignore_ascii_case("digest") {
        let Ok(challenge) = DigestChallenge::parse(challenge) else {
            // Preserve the established behavior for malformed or unsupported
            // Digest challenges by falling back to Basic.
            return Ok(basic_authorization(username, password));
        };
        return Ok(DigestResponse::compute(
            username,
            password,
            "CONNECT",
            target,
            &challenge,
            nonce_count,
        )
        .to_header_value());
    }
    if retry_policy.fallback_to_basic && !challenge.is_empty() {
        return Ok(basic_authorization(username, password));
    }
    Err(HttpConnectError::UnsupportedAuthentication(
        challenge.to_string(),
    ))
}

fn challenge_scheme(challenge: &str) -> &str {
    challenge
        .split_ascii_whitespace()
        .next()
        .unwrap_or_default()
}

async fn send_request(
    stream: &mut TcpStream,
    options: &HttpConnectOptions<'_>,
    auth_header: Option<&str>,
    proxy_connection_keep_alive: bool,
) -> Result<(), HttpConnectError> {
    let target = format!("{}:{}", options.target_host, options.target_port);
    let mut headers = Vec::with_capacity(4);
    if !options.user_agent.is_empty() {
        headers.push(("User-Agent", options.user_agent));
    }
    headers.push(("Host", target.as_str()));
    if let Some(auth) = auth_header {
        headers.push(("Proxy-Authorization", auth));
    }
    if proxy_connection_keep_alive {
        headers.push(("Proxy-Connection", "keep-alive"));
    }
    let request = try_serialize_http_request("CONNECT", &target, &headers, None).map_err(
        |error| match error {
            HttpRequestSerializationError::InvalidMethod => {
                HttpConnectError::InvalidRequestField("method")
            }
            HttpRequestSerializationError::InvalidTarget => {
                HttpConnectError::InvalidRequestField("target authority")
            }
            HttpRequestSerializationError::InvalidFieldName => {
                HttpConnectError::InvalidRequestField("header name")
            }
            HttpRequestSerializationError::InvalidFieldValue => {
                HttpConnectError::InvalidRequestField("header value")
            }
        },
    )?;
    debug!(target = %target, authenticated = auth_header.is_some(), "Sending HTTP CONNECT request");
    timeout(options.write_timeout, stream.write_all(&request))
        .await
        .map_err(|_| HttpConnectError::Timeout)??;
    stream.flush().await?;
    Ok(())
}

async fn read_proxy_response(
    stream: &mut TcpStream,
    read_timeout: Duration,
) -> Result<ProxyResponse, HttpConnectError> {
    loop {
        let response =
            read_response_head(stream, read_timeout)
                .await
                .map_err(|error| match error {
                    HttpResponseHeadReadError::Timeout => HttpConnectError::Timeout,
                    HttpResponseHeadReadError::Io(error) => HttpConnectError::Io(error),
                    HttpResponseHeadReadError::HeaderTooLarge => {
                        HttpConnectError::InvalidResponse("Too large HTTP header block".to_string())
                    }
                    HttpResponseHeadReadError::InvalidResponse(message) => {
                        HttpConnectError::InvalidResponse(message)
                    }
                })?;
        if (100..200).contains(&response.status_code) {
            debug!(
                status = response.status_code,
                "Skipping HTTP CONNECT informational response"
            );
            continue;
        }
        let headers = response
            .iter_headers()
            .map(|(name, value)| (name.to_string(), value.to_string()))
            .collect();
        return Ok(ProxyResponse {
            status_code: response.status_code,
            reason_phrase: response.reason_phrase,
            headers,
        });
    }
}

async fn consume_response_body(
    stream: &mut TcpStream,
    headers: &[(String, String)],
    read_timeout: Duration,
) -> Result<(), HttpConnectError> {
    const MAX_AUTH_RESPONSE_BODY_BYTES: usize = 64 * 1024;
    match proxy_response_body_framing(headers)? {
        ProxyResponseBodyFraming::Chunked => {
            timeout(read_timeout, consume_chunked_body(stream))
                .await
                .map_err(|_| HttpConnectError::Timeout)??;
            Ok(())
        }
        ProxyResponseBodyFraming::ContentLength(0) => Ok(()),
        ProxyResponseBodyFraming::ContentLength(length)
            if length <= MAX_AUTH_RESPONSE_BODY_BYTES =>
        {
            let mut remaining = length;
            let mut buffer = [0; 4096];
            let result = timeout(read_timeout, async {
                while remaining > 0 {
                    let chunk_length = remaining.min(buffer.len());
                    stream.read_exact(&mut buffer[..chunk_length]).await?;
                    remaining -= chunk_length;
                }
                Ok::<(), io::Error>(())
            })
            .await;
            result
                .map_err(|_| HttpConnectError::Timeout)?
                .map_err(HttpConnectError::Io)?;
            debug!(length, "Consumed proxy authentication response body");
            Ok(())
        }
        ProxyResponseBodyFraming::ContentLength(_) => Err(HttpConnectError::InvalidResponse(
            "proxy authentication response body exceeds the discard limit".to_string(),
        )),
    }
}

fn proxy_response_body_framing(
    headers: &[(String, String)],
) -> Result<ProxyResponseBodyFraming, HttpConnectError> {
    let transfer_encodings = headers
        .iter()
        .filter(|(name, _)| name.eq_ignore_ascii_case("transfer-encoding"))
        .flat_map(|(_, value)| value.split(','))
        .map(str::trim)
        .collect::<Vec<_>>();

    if !transfer_encodings.is_empty() {
        let chunked_count = transfer_encodings
            .iter()
            .filter(|coding| coding.eq_ignore_ascii_case("chunked"))
            .count();
        if transfer_encodings.iter().any(|coding| coding.is_empty())
            || chunked_count != 1
            || !transfer_encodings
                .last()
                .is_some_and(|coding| coding.eq_ignore_ascii_case("chunked"))
        {
            return Err(HttpConnectError::InvalidResponse(
                "unsupported proxy authentication response transfer coding".to_string(),
            ));
        }
        return Ok(ProxyResponseBodyFraming::Chunked);
    }

    let content_lengths = headers
        .iter()
        .filter(|(name, _)| name.eq_ignore_ascii_case("content-length"))
        .flat_map(|(_, value)| value.split(','))
        .map(str::trim)
        .map(|value| {
            value.parse::<usize>().map_err(|_| {
                HttpConnectError::InvalidResponse(
                    "invalid proxy authentication response Content-Length".to_string(),
                )
            })
        })
        .collect::<Result<Vec<_>, _>>()?;

    let Some(&length) = content_lengths.first() else {
        return Err(HttpConnectError::InvalidResponse(
            "proxy authentication response has no body framing".to_string(),
        ));
    };
    if content_lengths.iter().any(|candidate| *candidate != length) {
        return Err(HttpConnectError::InvalidResponse(
            "conflicting proxy authentication response Content-Length values".to_string(),
        ));
    }
    Ok(ProxyResponseBodyFraming::ContentLength(length))
}

async fn consume_chunked_body(stream: &mut TcpStream) -> Result<(), HttpConnectError> {
    const MAX_CHUNKED_BODY_BYTES: usize = 1024 * 1024;
    const MAX_CHUNK_LINE_BYTES: usize = 1024;

    let mut consumed = 0usize;
    loop {
        let line = read_crlf_line(stream, MAX_CHUNK_LINE_BYTES).await?;
        let size_text = std::str::from_utf8(&line[..line.len() - 2])
            .map_err(|error| HttpConnectError::InvalidResponse(error.to_string()))?;
        let size_text = size_text.split(';').next().unwrap_or_default().trim();
        let chunk_size = usize::from_str_radix(size_text, 16)
            .map_err(|error| HttpConnectError::InvalidResponse(error.to_string()))?;
        if chunk_size == 0 {
            let mut trailer_bytes = 0usize;
            loop {
                let trailer = read_crlf_line(stream, MAX_CHUNK_LINE_BYTES).await?;
                trailer_bytes = trailer_bytes.saturating_add(trailer.len());
                if trailer_bytes > MAX_CHUNKED_BODY_BYTES {
                    return Err(HttpConnectError::InvalidResponse(
                        "proxy authentication response trailers exceed the discard limit"
                            .to_string(),
                    ));
                }
                if trailer == b"\r\n" {
                    return Ok(());
                }
            }
        }

        consumed = consumed.saturating_add(chunk_size);
        if consumed > MAX_CHUNKED_BODY_BYTES {
            return Err(HttpConnectError::InvalidResponse(
                "proxy authentication response body exceeds the discard limit".to_string(),
            ));
        }
        let mut remaining = chunk_size;
        let mut buffer = [0; 4096];
        while remaining > 0 {
            let length = remaining.min(buffer.len());
            stream.read_exact(&mut buffer[..length]).await?;
            remaining -= length;
        }
        let mut terminator = [0; 2];
        stream.read_exact(&mut terminator).await?;
        if terminator != *b"\r\n" {
            return Err(HttpConnectError::InvalidResponse(
                "invalid proxy authentication response chunk terminator".to_string(),
            ));
        }
    }
}

async fn read_crlf_line(
    stream: &mut TcpStream,
    max_line_bytes: usize,
) -> Result<Vec<u8>, HttpConnectError> {
    let mut line = Vec::with_capacity(32);
    while line.len() < max_line_bytes {
        let mut byte = [0; 1];
        stream.read_exact(&mut byte).await?;
        line.push(byte[0]);
        if line.ends_with(b"\r\n") {
            return Ok(line);
        }
    }
    Err(HttpConnectError::InvalidResponse(
        "proxy authentication response chunk line exceeds the limit".to_string(),
    ))
}

#[cfg(test)]
mod tests;