use tracing::{debug, info, warn};
use crate::error::Result;
use super::types::{FtpClient, FtpResponse};
#[derive(Debug, Clone, Default)]
pub struct FtpFeatures {
pub utf8: bool,
pub mlsd: bool,
pub mlst: bool,
pub pret: bool,
pub mfmt: bool,
pub tvfs: bool,
pub auth_tls: bool,
pub auth_ssl: bool,
pub epsv: bool,
pub eprt: bool,
pub size: bool,
pub rest_stem: bool,
pub features: Vec<String>,
}
impl FtpFeatures {
pub fn parse(body: &str) -> Self {
let mut feat = Self::default();
for line in body.lines() {
let feature = if line.starts_with(' ') {
line.trim()
} else {
continue;
};
if feature.is_empty() {
continue;
}
let keyword = feature
.split(|c: char| c.is_whitespace() || c == ';')
.next()
.unwrap_or(feature)
.trim();
feat.apply_keyword(keyword, feature);
}
info!(
"FEAT parsed: utf8={}, mlsd={}, mlst={}, pret={}, mfmt={}, tvfs={}, \
auth_tls={}, auth_ssl={}, epsv={}, eprt={}, size={}, rest_stem={}",
feat.utf8,
feat.mlsd,
feat.mlst,
feat.pret,
feat.mfmt,
feat.tvfs,
feat.auth_tls,
feat.auth_ssl,
feat.epsv,
feat.eprt,
feat.size,
feat.rest_stem,
);
feat
}
fn apply_keyword(&mut self, keyword: &str, full_line: &str) {
match keyword.to_uppercase().as_str() {
"UTF8" => {
self.utf8 = true;
debug!("FEAT: UTF8 support detected");
}
"MLSD" => {
self.mlsd = true;
debug!("FEAT: MLSD support detected");
}
"MLST" => {
self.mlst = true;
debug!("FEAT: MLST support detected");
}
"PRET" => {
self.pret = true;
debug!("FEAT: PRET support detected");
}
"MFMT" => {
self.mfmt = true;
debug!("FEAT: MFMT support detected");
}
"TVFS" => {
self.tvfs = true;
debug!("FEAT: TVFS support detected");
}
"AUTH" => {
let upper = full_line.to_uppercase();
if upper.contains("TLS") {
self.auth_tls = true;
debug!("FEAT: AUTH TLS support detected");
}
if upper.contains("SSL") {
self.auth_ssl = true;
debug!("FEAT: AUTH SSL support detected");
}
if !upper.contains("TLS") && !upper.contains("SSL") {
debug!(
"FEAT: AUTH support detected (type unspecified): {}",
full_line
);
}
}
"EPSV" => {
self.epsv = true;
debug!("FEAT: EPSV support detected");
}
"EPRT" => {
self.eprt = true;
debug!("FEAT: EPRT support detected");
}
"SIZE" => {
self.size = true;
debug!("FEAT: SIZE support detected");
}
"REST" => {
let upper = full_line.to_uppercase();
if upper.contains("STREAM") {
self.rest_stem = true;
debug!("FEAT: REST STREAM support detected");
} else {
debug!("FEAT: REST support detected (variant: {})", full_line);
}
}
_ => {
debug!("FEAT: unknown feature '{}'", keyword);
self.features.push(full_line.to_string());
}
}
}
}
impl FtpClient {
pub async fn send_feat(&mut self) -> Result<FtpFeatures> {
debug!("Sending FEAT command");
self.send_command("FEAT").await?;
let resp = self.read_response().await?;
if resp.code != 211 {
info!(
"FEAT not supported (response: {} {}), using defaults",
resp.code, resp.message
);
let feat = FtpFeatures::default();
self.features = Some(feat.clone());
return Ok(feat);
}
let feat = FtpFeatures::parse(&resp.message);
self.features = Some(feat.clone());
Ok(feat)
}
pub async fn send_cdup(&mut self) -> Result<FtpResponse> {
debug!("Sending CDUP command");
self.send_command("CDUP").await?;
let resp = self.read_response().await?;
if resp.is_positive_completion() {
debug!("CDUP successful");
} else {
warn!("CDUP failed: {} {}", resp.code, resp.message);
}
Ok(resp)
}
pub async fn send_mkd(&mut self, path: &str) -> Result<FtpResponse> {
debug!("Sending MKD command for: {}", path);
self.send_command(&format!("MKD {}", path)).await?;
let resp = self.read_response().await?;
if resp.code == 257 {
debug!("MKD successful: {}", path);
} else if resp.code == 550 {
debug!("MKD returned 550 (directory may already exist): {}", path);
} else {
warn!("MKD failed for {}: {} {}", path, resp.code, resp.message);
}
Ok(resp)
}
pub async fn send_opts_utf8(&mut self) -> Result<FtpResponse> {
debug!("Sending OPTS UTF8 ON");
self.send_command("OPTS UTF8 ON").await?;
let resp = self.read_response().await?;
if resp.is_positive_completion() {
info!("UTF-8 path encoding enabled (OPTS UTF8 ON accepted)");
} else {
warn!(
"OPTS UTF8 ON rejected ({} {}), continuing with default encoding",
resp.code, resp.message
);
}
Ok(resp)
}
pub async fn send_auth_tls(&mut self) -> Result<FtpResponse> {
debug!("Sending AUTH TLS command");
self.send_command("AUTH TLS").await?;
let resp = self.read_response().await?;
match resp.code {
234 => {
info!("AUTH TLS accepted (234), connection ready for TLS upgrade");
}
_ => {
info!(
"AUTH TLS not supported by server ({} {})",
resp.code, resp.message
);
}
}
Ok(resp)
}
pub fn features(&self) -> Option<&FtpFeatures> {
self.features.as_ref()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_feat_full() {
let response = "\
211-Features:
UTF8
MLST type*;size*;modify*;
MLSD
SIZE
MDTM
EPSV
EPRT
TVFS
PRET
MFMT
REST STREAM
AUTH TLS
211 End";
let feat = FtpFeatures::parse(response);
assert!(feat.utf8);
assert!(feat.mlst);
assert!(feat.mlsd);
assert!(feat.size);
assert!(feat.epsv);
assert!(feat.eprt);
assert!(feat.tvfs);
assert!(feat.pret);
assert!(feat.mfmt);
assert!(feat.rest_stem);
assert!(feat.auth_tls);
assert!(!feat.auth_ssl);
}
#[test]
fn test_parse_feat_minimal() {
let response = "\
211-Features:
UTF8
211 End";
let feat = FtpFeatures::parse(response);
assert!(feat.utf8);
assert!(!feat.mlsd);
assert!(!feat.size);
}
#[test]
fn test_parse_feat_empty() {
let response = "211 End";
let feat = FtpFeatures::parse(response);
assert!(!feat.utf8);
assert!(!feat.mlst);
}
#[test]
fn test_parse_feat_case_insensitive() {
let response = "\
211-Features:
utf8
Mlst type*;size*;
211 End";
let feat = FtpFeatures::parse(response);
assert!(feat.utf8);
assert!(feat.mlst);
}
#[test]
fn test_parse_feat_auth_ssl() {
let response = "\
211-Features:
AUTH SSL
211 End";
let feat = FtpFeatures::parse(response);
assert!(feat.auth_ssl);
assert!(!feat.auth_tls);
}
#[test]
fn test_parse_feat_rest_stream() {
let response = "\
211-Features:
REST STREAM
211 End";
let feat = FtpFeatures::parse(response);
assert!(feat.rest_stem);
}
#[test]
fn test_parse_feat_unknown_features_collected() {
let response = "\
211-Features:
UTF8
XCURSOR
SOME_UNKNOWN_FEATURE
211 End";
let feat = FtpFeatures::parse(response);
assert!(feat.utf8);
assert_eq!(feat.features.len(), 2);
}
#[test]
fn test_feat_default() {
let feat = FtpFeatures::default();
assert!(!feat.utf8);
assert!(!feat.mlsd);
assert!(!feat.mlst);
assert!(!feat.pret);
assert!(!feat.mfmt);
assert!(!feat.tvfs);
assert!(!feat.auth_tls);
assert!(!feat.auth_ssl);
assert!(!feat.epsv);
assert!(!feat.eprt);
assert!(!feat.size);
assert!(!feat.rest_stem);
assert!(feat.features.is_empty());
}
}