aria2-core 0.3.1

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! FTP connection establishment and mode configuration
//!
//! Contains methods for connecting to an FTP server, logging in,
//! setting transfer mode, and establishing passive/active data connections.
//! Supports both plain FTP and FTPS (FTP over TLS) per RFC 4217.

use crate::error::{Aria2Error, Result};
use tokio::net::TcpStream;
use tokio::time::{Duration, timeout};
use tracing::{debug, info, warn};

use super::tls;
use super::types::{FtpClient, FtpControlStream, FtpMode, FtpTlsMode, FtpsConfig};

impl FtpClient {
    /// Default connection timeout: 30 seconds
    const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(30);
    /// Default read timeout: 30 seconds
    const DEFAULT_READ_TIMEOUT: Duration = Duration::from_secs(30);

    /// Connect to an FTP server
    ///
    /// # Arguments
    ///
    /// - `host`: FTP server address (domain name or IP)
    /// - `port`: FTP server port (typically 21)
    /// - `mode`: Data connection mode (passive or active)
    ///
    /// # Errors
    ///
    /// - Connection timeout
    /// - Network error
    /// - Server refused connection
    pub async fn connect(host: &str, port: u16, mode: FtpMode) -> Result<Self> {
        info!("FTP connecting: {}:{}", host, port);

        let stream = timeout(
            Self::DEFAULT_CONNECT_TIMEOUT,
            TcpStream::connect((host, port)),
        )
        .await
        .map_err(|_| Aria2Error::Recoverable(crate::error::RecoverableError::Timeout))?
        .map_err(|e| Aria2Error::Network(format!("FTP connection failed: {}", e)))?;

        let control_peer = stream
            .peer_addr()
            .map_err(|e| {
                Aria2Error::Network(format!("FTP control peer address unavailable: {}", e))
            })?
            .ip();
        let mut client = Self {
            control_stream: tokio::io::BufReader::new(FtpControlStream::Plain(stream)),
            mode,
            binary_mode: false,
            host: host.to_string(),
            control_peer,
            port,
            connect_timeout: Self::DEFAULT_CONNECT_TIMEOUT,
            read_timeout: Self::DEFAULT_READ_TIMEOUT,
            base_working_dir: "/".to_string(),
            features: None,
            tls_mode: FtpTlsMode::None,
            data_channel_protected: false,
            ftps_config: None,
        };

        // Read welcome message
        let welcome = client.read_response().await?;
        if !welcome.is_positive_completion() && !welcome.is_positive_preliminary() {
            return Err(Aria2Error::DownloadFailed(format!(
                "FTP server refused connection: {} {}",
                welcome.code, welcome.message
            )));
        }

        debug!("FTP connected successfully: {}", welcome.message.trim());
        Ok(client)
    }

    /// Connect to an FTPS server with explicit TLS upgrade (AUTH TLS).
    ///
    /// After the 220 greeting, sends `AUTH TLS`, upgrades the control stream
    /// to TLS, then sends `PBSZ 0` and `PROT P` to protect both control and
    /// data channels per RFC 4217.
    ///
    /// # Arguments
    ///
    /// - `host`: FTP server address (domain name or IP)
    /// - `port`: FTP server port (typically 21 for explicit FTPS)
    /// - `mode`: Data connection mode (passive or active)
    /// - `config`: FTPS TLS configuration (certificate verification, CA path)
    ///
    /// # Errors
    ///
    /// - Connection timeout
    /// - AUTH TLS rejected (non-234 response)
    /// - TLS handshake failed
    /// - PBSZ 0 or PROT P rejected
    pub async fn connect_ftps_explicit(
        host: &str,
        port: u16,
        mode: FtpMode,
        config: &FtpsConfig,
    ) -> Result<Self> {
        info!("FTPS (explicit) connecting: {}:{}", host, port);

        // Step 1: Connect in plaintext and read greeting
        let stream = timeout(
            Self::DEFAULT_CONNECT_TIMEOUT,
            TcpStream::connect((host, port)),
        )
        .await
        .map_err(|_| Aria2Error::Recoverable(crate::error::RecoverableError::Timeout))?
        .map_err(|e| Aria2Error::Network(format!("FTPS connection failed: {}", e)))?;

        let control_peer = stream
            .peer_addr()
            .map_err(|e| {
                Aria2Error::Network(format!("FTPS control peer address unavailable: {}", e))
            })?
            .ip();
        // Wrap in BufReader to read the 220 greeting, then recover the stream
        let mut buf_reader = tokio::io::BufReader::new(stream);
        let greeting = read_single_response(&mut buf_reader, Self::DEFAULT_READ_TIMEOUT).await?;

        if greeting.code != 220 {
            return Err(Aria2Error::DownloadFailed(format!(
                "FTPS server refused connection (expected 220): {} {}",
                greeting.code, greeting.message
            )));
        }
        info!("FTPS greeting received: {}", greeting.message.trim());

        // Recover the plain TCP stream from the BufReader for TLS upgrade
        let stream = buf_reader.into_inner();

        // Step 2: AUTH TLS + TLS handshake + PBSZ 0 + PROT P
        let tls_stream = tls::upgrade_control_stream(stream, host, config)
            .await
            .map_err(|e| Aria2Error::Network(format!("FTPS upgrade failed: {}", e)))?;

        let client = Self {
            control_stream: tokio::io::BufReader::new(FtpControlStream::Tls(Box::new(tls_stream))),
            mode,
            binary_mode: false,
            host: host.to_string(),
            control_peer,
            port,
            connect_timeout: Self::DEFAULT_CONNECT_TIMEOUT,
            read_timeout: Self::DEFAULT_READ_TIMEOUT,
            base_working_dir: "/".to_string(),
            features: None,
            tls_mode: FtpTlsMode::Explicit,
            data_channel_protected: true,
            ftps_config: Some(config.clone()),
        };

        info!("FTPS explicit connection established with {}", host);
        Ok(client)
    }

    /// Connect to an FTPS server with implicit TLS (TLS from the start).
    ///
    /// Used for legacy implicit FTPS on port 990 where the TCP connection
    /// is immediately wrapped in TLS before any FTP protocol exchange.
    /// No `AUTH TLS` command is sent.
    ///
    /// # Arguments
    ///
    /// - `host`: FTP server address (domain name or IP)
    /// - `port`: FTP server port (typically 990 for implicit FTPS)
    /// - `mode`: Data connection mode (passive or active)
    /// - `config`: FTPS TLS configuration
    ///
    /// # Errors
    ///
    /// - Connection timeout
    /// - TLS handshake failed
    /// - Server refused connection (non-220 greeting)
    pub async fn connect_ftps_implicit(
        host: &str,
        port: u16,
        mode: FtpMode,
        config: &FtpsConfig,
    ) -> Result<Self> {
        info!("FTPS (implicit) connecting: {}:{}", host, port);

        // Step 1: Connect and immediately perform TLS handshake
        let stream = timeout(
            Self::DEFAULT_CONNECT_TIMEOUT,
            TcpStream::connect((host, port)),
        )
        .await
        .map_err(|_| Aria2Error::Recoverable(crate::error::RecoverableError::Timeout))?
        .map_err(|e| Aria2Error::Network(format!("FTPS implicit connection failed: {}", e)))?;

        let control_peer = stream
            .peer_addr()
            .map_err(|e| {
                Aria2Error::Network(format!("FTPS control peer address unavailable: {}", e))
            })?
            .ip();
        let tls_stream = tls::perform_tls_handshake(stream, host, config)
            .await
            .map_err(|e| Aria2Error::Network(format!("FTPS implicit TLS failed: {}", e)))?;

        let mut client = Self {
            control_stream: tokio::io::BufReader::new(FtpControlStream::Tls(Box::new(tls_stream))),
            mode,
            binary_mode: false,
            host: host.to_string(),
            control_peer,
            port,
            connect_timeout: Self::DEFAULT_CONNECT_TIMEOUT,
            read_timeout: Self::DEFAULT_READ_TIMEOUT,
            base_working_dir: "/".to_string(),
            features: None,
            tls_mode: FtpTlsMode::Implicit,
            data_channel_protected: true,
            ftps_config: Some(config.clone()),
        };

        // Step 2: Read the 220 greeting (now over TLS)
        // For implicit FTPS, we still need to send PBSZ 0 + PROT P
        // after receiving the greeting.
        let greeting = client.read_response().await?;
        if greeting.code != 220 {
            return Err(Aria2Error::DownloadFailed(format!(
                "FTPS implicit server refused connection: {} {}",
                greeting.code, greeting.message
            )));
        }

        // Send PBSZ 0 + PROT P on the TLS control stream
        // (no AUTH TLS needed for implicit FTPS)
        send_pbsz_prot_on_client(&mut client).await?;

        info!("FTPS implicit connection established with {}", host);
        Ok(client)
    }

    /// Whether the control connection is TLS-encrypted.
    pub fn is_tls(&self) -> bool {
        self.tls_mode != FtpTlsMode::None
    }

    /// Whether data connections will be TLS-protected (PROT P negotiated).
    pub fn is_data_channel_protected(&self) -> bool {
        self.data_channel_protected
    }

    /// Log in to the FTP server
    ///
    /// # Arguments
    ///
    /// - `username`: Username (use "anonymous" for anonymous login)
    /// - `password`: Password (use email for anonymous login)
    ///
    /// # Errors
    ///
    /// - 530 Not logged in (authentication failed)
    pub async fn login(&mut self, username: &str, password: &str) -> Result<()> {
        debug!("Sending USER command: {}", username);
        self.send_command(&format!("USER {}", username)).await?;
        let resp = self.read_response().await?;

        match resp.code {
            230 => {
                // No password required, login successful
                info!("FTP login successful (no password required)");
                Ok(())
            }
            331 | 332 => {
                // Password required
                debug!("Password authentication required, sending PASS command");
                self.send_command(&format!("PASS {}", password)).await?;
                let pass_resp = self.read_response().await?;

                if pass_resp.code == 230 || pass_resp.code == 202 {
                    info!("FTP login successful");
                    Ok(())
                } else if pass_resp.code == 530 {
                    Err(Aria2Error::Recoverable(
                        crate::error::RecoverableError::ServerError { code: 530 },
                    ))
                } else {
                    Err(Aria2Error::DownloadFailed(format!(
                        "FTP login failed: {} {}",
                        pass_resp.code, pass_resp.message
                    )))
                }
            }
            530 => Err(Aria2Error::Recoverable(
                crate::error::RecoverableError::ServerError { code: 530 },
            )),
            _ => {
                if resp.is_positive_completion() {
                    info!("FTP login successful");
                    Ok(())
                } else {
                    Err(Aria2Error::DownloadFailed(format!(
                        "FTP login failed: {} {}",
                        resp.code, resp.message
                    )))
                }
            }
        }
    }

    /// Set transfer mode (binary/ASCII)
    ///
    /// # Arguments
    ///
    /// - `enabled`: true for binary mode (TYPE I), false for ASCII mode (TYPE A)
    ///
    /// # Errors
    ///
    /// - 504 Unsupported transfer mode
    pub async fn set_binary_mode(&mut self, enabled: bool) -> Result<()> {
        let type_cmd = if enabled { "TYPE I" } else { "TYPE A" };
        debug!("Setting transfer type: {}", type_cmd);
        self.send_command(type_cmd).await?;
        let resp = self.read_response().await?;

        if resp.is_positive_completion() {
            self.binary_mode = enabled;
            debug!(
                "Transfer mode set to: {}",
                if enabled { "Binary" } else { "ASCII" }
            );
            Ok(())
        } else if resp.code == 504 {
            Err(Aria2Error::DownloadFailed(format!(
                "Unsupported transfer mode: {}",
                resp.message
            )))
        } else {
            Err(Aria2Error::DownloadFailed(format!(
                "TYPE command failed: {} {}",
                resp.code, resp.message
            )))
        }
    }

    /// Enter passive mode and establish data connection.
    ///
    /// If PROT P has been negotiated (FTPS mode), the data stream is
    /// also upgraded to TLS before being returned.
    ///
    /// Tries EPSV (Extended Passive Mode) first, falls back to PASV if
    /// the server does not support it.
    ///
    /// # Returns
    ///
    /// Returns the data connection TcpStream (TLS-wrapped if PROT P).
    ///
    /// # Errors
    ///
    /// - 425 Cannot open data connection
    /// - Timeout error
    pub async fn passive_mode(&mut self) -> Result<TcpStream> {
        debug!("Requesting passive mode data connection");

        // Try EPSV first
        self.send_command("EPSV").await?;
        let resp = self.read_response().await?;

        if resp.code == 229 {
            // Parse EPSV response: Entering Extended Passive Mode (|||port|)
            if let Some(port) = Self::parse_epsv_response(&resp.message) {
                debug!("EPSV data channel port: {}", port);
                let data_stream = timeout(
                    self.connect_timeout,
                    TcpStream::connect(std::net::SocketAddr::new(self.control_peer, port)),
                )
                .await
                .map_err(|_| Aria2Error::Recoverable(crate::error::RecoverableError::Timeout))?
                .map_err(|e| Aria2Error::Network(format!("EPSV data connection failed: {}", e)))?;

                return self.maybe_upgrade_data_stream(data_stream).await;
            }
        }

        // Fall back to PASV
        warn!("EPSV unavailable, falling back to PASV mode");
        self.send_command("PASV").await?;
        let pasv_resp = self.read_response().await?;

        if pasv_resp.code != 227 {
            return Err(Aria2Error::Recoverable(
                crate::error::RecoverableError::FtpProtocolError {
                    message: format!("PASV failed: {} {}", pasv_resp.code, pasv_resp.message),
                },
            ));
        }

        // Parse PASV response
        let (advertised_host, data_port) = Self::parse_pasv_response(&pasv_resp.message)?;
        debug!(
            advertised_host = %advertised_host,
            control_peer = %self.control_peer,
            port = data_port,
            "PASV data channel; using control peer address"
        );
        let data_stream = timeout(
            self.connect_timeout,
            TcpStream::connect(std::net::SocketAddr::new(self.control_peer, data_port)),
        )
        .await
        .map_err(|_| Aria2Error::Recoverable(crate::error::RecoverableError::Timeout))?
        .map_err(|e| Aria2Error::Network(format!("PASV data connection failed: {}", e)))?;

        self.maybe_upgrade_data_stream(data_stream).await
    }

    /// Enter active mode and establish data connection.
    ///
    /// If PROT P has been negotiated (FTPS mode), the accepted data
    /// stream is also upgraded to TLS before being returned.
    ///
    /// Sends a PORT or EPRT command to inform the server of the client's
    /// data port, then listens on that port for the server to connect.
    ///
    /// # Returns
    ///
    /// Returns the accepted data connection TcpStream (TLS-wrapped if PROT P).
    ///
    /// # Errors
    ///
    /// - 425 Cannot open data connection
    /// - 500/501/502 Command syntax error
    pub async fn active_mode(&mut self) -> Result<TcpStream> {
        debug!("Requesting active mode data connection");

        // Get local address from the underlying TCP stream
        let local_addr = self
            .control_stream
            .get_ref()
            .get_ref()
            .ok_or_else(|| {
                Aria2Error::Network("Cannot get local address from TLS stream".to_string())
            })?
            .local_addr()
            .map_err(|e| Aria2Error::Network(format!("Failed to get local address: {}", e)))?;

        let local_ip = local_addr.ip();

        // Bind listener on the appropriate wildcard address for the address family
        // IPv4 -> 0.0.0.0:0, IPv6 -> [::]:0
        let bind_addr = match local_ip {
            std::net::IpAddr::V4(_) => "0.0.0.0:0",
            std::net::IpAddr::V6(_) => "[::]:0",
        };
        let listener = tokio::net::TcpListener::bind(bind_addr)
            .await
            .map_err(|e| Aria2Error::Network(format!("Failed to bind data port: {}", e)))?;
        let data_port = listener
            .local_addr()
            .map_err(|e| Aria2Error::Network(format!("Failed to get listen port: {}", e)))?
            .port();

        // EPRT protocol number: |1| for IPv4, |2| for IPv6 (RFC 2428)
        let proto_num = match local_ip {
            std::net::IpAddr::V4(_) => 1,
            std::net::IpAddr::V6(_) => 2,
        };
        let eprt_cmd = format!("EPRT |{}|{}|{}|", proto_num, local_ip, data_port);
        debug!("Sending EPRT command: {}", eprt_cmd);
        self.send_command(&eprt_cmd).await?;
        let resp = self.read_response().await?;

        if resp.code != 200 && resp.code != 500 && resp.code != 501 && resp.code != 502 {
            return Err(Aria2Error::DownloadFailed(format!(
                "EPRT command failed: {} {}",
                resp.code, resp.message
            )));
        }

        // If EPRT failed, try PORT command
        if !resp.is_positive_completion() {
            warn!("EPRT unavailable, falling back to PORT mode");

            // Convert IP address to PORT command format (h1,h2,h3,h4,p1,p2)
            // Only supports IPv4 (PORT command does not support IPv6)

            let ipv4_addr = match local_ip {
                std::net::IpAddr::V4(v4) => v4,
                std::net::IpAddr::V6(_) => {
                    // IPv6 does not support PORT command, return error
                    return Err(Aria2Error::DownloadFailed(
                        "IPv6 does not support active mode PORT command, please use passive mode"
                            .to_string(),
                    ));
                }
            };
            let ip_bytes = ipv4_addr.octets();
            let p1 = data_port / 256;
            let p2 = data_port % 256;
            let port_cmd = format!(
                "PORT {},{},{},{},{},{}",
                ip_bytes[0], ip_bytes[1], ip_bytes[2], ip_bytes[3], p1, p2
            );

            debug!("Sending PORT command: {}", port_cmd);
            self.send_command(&port_cmd).await?;
            let port_resp = self.read_response().await?;

            if !port_resp.is_positive_completion() {
                return Err(Aria2Error::Recoverable(
                    crate::error::RecoverableError::ServerError { code: 425 },
                ));
            }
        }

        // Wait for server connection (with timeout)
        debug!("Waiting for server to connect to data port: {}", data_port);
        let (data_stream, _addr) = timeout(self.connect_timeout, listener.accept())
            .await
            .map_err(|_| Aria2Error::Recoverable(crate::error::RecoverableError::Timeout))?
            .map_err(|e| Aria2Error::Network(format!("Failed to accept data connection: {}", e)))?;

        debug!("Active mode data connection established successfully");
        self.maybe_upgrade_data_stream(data_stream).await
    }

    // ========================================================================
    // FTPS internal helpers
    // ========================================================================

    /// Conditionally upgrade a data stream to TLS if PROT P is in effect.
    ///
    /// For plain FTP, returns the TcpStream as-is. For FTPS with PROT P,
    /// upgrades to TLS but returns the inner TcpStream since the old
    /// FtpClient API only supports TcpStream. Use FtpNegotiator for full
    /// FTPS data channel encryption support.
    async fn maybe_upgrade_data_stream(&self, stream: TcpStream) -> Result<TcpStream> {
        if !self.data_channel_protected {
            return Ok(stream);
        }

        let config = self.ftps_config.as_ref().ok_or_else(|| {
            Aria2Error::Network(
                "FTPS data channel protected but no FTPS config available".to_string(),
            )
        })?;

        debug!("Upgrading FTP data connection to TLS for {}", self.host);
        let tls_stream = tls::upgrade_data_stream(stream, &self.host, config)
            .await
            .map_err(|e| Aria2Error::Network(format!("FTPS data TLS upgrade failed: {}", e)))?;

        // NOTE: The old FtpClient API returns TcpStream. For full FTPS data
        // channel encryption, use the newer FtpNegotiator which supports
        // polymorphic streams via FtpControlStream.
        warn!(
            "FTPS data TLS negotiated but FtpClient returns TcpStream. \
             Use FtpNegotiator for full FTPS data channel encryption."
        );
        let (tcp_stream, _) = tls_stream.into_inner();
        Ok(tcp_stream)
    }
}

// =============================================================================
// Internal helpers for FTPS negotiation on the FtpClient control stream
// =============================================================================

/// Send PBSZ 0 and PROT P on an existing TLS-encrypted FtpClient.
/// Used for implicit FTPS where AUTH TLS is not needed but PBSZ/PROT
/// are still required to protect the data channel.
async fn send_pbsz_prot_on_client(client: &mut FtpClient) -> Result<()> {
    // Send PBSZ 0
    debug!("Sending PBSZ 0 (implicit FTPS)");
    client.send_command("PBSZ 0").await?;
    let pbsz_resp = client.read_response().await?;
    if pbsz_resp.code != 200 {
        warn!(
            "PBSZ 0 rejected by server: {} {} (continuing without data protection)",
            pbsz_resp.code, pbsz_resp.message
        );
        // Not fatal: control channel is still encrypted
        return Ok(());
    }

    // Send PROT P
    debug!("Sending PROT P (implicit FTPS)");
    client.send_command("PROT P").await?;
    let prot_resp = client.read_response().await?;
    if prot_resp.code != 200 {
        warn!(
            "PROT P rejected by server: {} {} (data channel will NOT be encrypted)",
            prot_resp.code, prot_resp.message
        );
        client.data_channel_protected = false;
    } else {
        client.data_channel_protected = true;
        info!("PROT P accepted — data channel will be TLS-protected");
    }

    Ok(())
}

/// Read a single FTP response before the `FtpClient` is fully constructed.
async fn read_single_response<R: tokio::io::AsyncBufReadExt + Unpin>(
    reader: &mut R,
    timeout_dur: Duration,
) -> Result<super::types::FtpResponse> {
    let mut line = String::new();
    timeout(timeout_dur, reader.read_line(&mut line))
        .await
        .map_err(|_| Aria2Error::Recoverable(crate::error::RecoverableError::Timeout))?
        .map_err(|e| Aria2Error::Network(format!("Failed to read FTP response: {}", e)))?;

    let trimmed = line.trim_end();
    let (code, message) = if trimmed.len() >= 4 {
        let code: u16 = trimmed[..3].parse().unwrap_or(0);
        let sep = trimmed.as_bytes()[3];
        let msg = if sep == b' ' || sep == b'-' {
            trimmed[4..].to_string()
        } else {
            trimmed[3..].to_string()
        };
        (code, msg)
    } else if trimmed.len() >= 3 {
        let code: u16 = trimmed[..3].parse().unwrap_or(0);
        (code, String::new())
    } else {
        (0, line)
    };

    Ok(super::types::FtpResponse { code, message })
}