use tracing::{debug, info, warn};
use crate::error::Result;
use crate::ftp::connection::negotiation::control::FreshControl;
#[derive(Debug, Clone, Default)]
pub struct ServerCapabilities {
pub utf8: bool,
pub mlst_mlsd: bool,
pub size: bool,
pub mdtm: bool,
pub epsv: bool,
pub eprt: bool,
pub tvfs: bool,
pub syst: Option<String>,
}
impl ServerCapabilities {
pub fn new() -> Self {
Self::default()
}
pub fn parse_feat_response(&mut self, response_body: &str) {
for line in response_body.lines() {
let feature = if line.starts_with(' ') {
line.trim()
} else {
continue;
};
if feature.is_empty() {
continue;
}
let first_token = feature
.split(|c: char| c.is_whitespace() || c == ';')
.next()
.unwrap_or(feature)
.trim();
let keyword = first_token;
self.apply_feature(keyword, feature);
}
info!(
"FEAT parsed: utf8={}, mlst_mlsd={}, size={}, mdtm={}, epsv={}, eprt={}, tvfs={}",
self.utf8, self.mlst_mlsd, self.size, self.mdtm, self.epsv, self.eprt, self.tvfs
);
}
fn apply_feature(&mut self, keyword: &str, _full_line: &str) {
match keyword.to_uppercase().as_str() {
"UTF8" => {
self.utf8 = true;
debug!("FEAT: UTF8 support detected");
}
"MLST" | "MLSD" => {
self.mlst_mlsd = true;
debug!("FEAT: MLST/MLSD support detected");
}
"SIZE" => {
self.size = true;
debug!("FEAT: SIZE support detected");
}
"MDTM" => {
self.mdtm = true;
debug!("FEAT: MDTM support detected");
}
"EPSV" => {
self.epsv = true;
debug!("FEAT: EPSV support detected");
}
"EPRT" => {
self.eprt = true;
debug!("FEAT: EPRT support detected");
}
"TVFS" => {
self.tvfs = true;
debug!("FEAT: TVFS support detected");
}
other => {
debug!("FEAT: unknown feature '{}'", other);
}
}
}
}
pub async fn query_feat(ctrl: &mut FreshControl) -> Result<ServerCapabilities> {
debug!("Sending FEAT command");
let resp = ctrl.command("FEAT").await?;
if resp.0 != 211 {
info!(
"FEAT command not supported (response: {} {}), using defaults",
resp.0, resp.1
);
return Ok(ServerCapabilities::new());
}
let mut caps = ServerCapabilities::new();
caps.parse_feat_response(&resp.1);
Ok(caps)
}
pub async fn send_opts_utf8_on(ctrl: &mut FreshControl) -> Result<bool> {
debug!("Sending OPTS UTF8 ON");
let resp = ctrl.command("OPTS UTF8 ON").await?;
if (200..300).contains(&resp.0) {
info!("UTF-8 path encoding enabled (OPTS UTF8 ON accepted)");
Ok(true)
} else {
warn!(
"OPTS UTF8 ON rejected ({} {}), continuing with default encoding",
resp.0, resp.1
);
Ok(false)
}
}
pub async fn query_syst(ctrl: &mut FreshControl) -> Result<Option<String>> {
debug!("Sending SYST command");
let resp = ctrl.command("SYST").await?;
if resp.0 != 215 {
info!("SYST command returned non-215: {} {}", resp.0, resp.1);
return Ok(None);
}
let msg = resp.1.trim();
let systype = if let Some(stripped) = msg.strip_prefix("215") {
stripped.trim().to_string()
} else {
msg.to_string()
};
info!("Server system type: {}", systype);
Ok(Some(systype))
}
#[allow(dead_code)] pub async fn send_cdup(ctrl: &mut FreshControl) -> Result<bool> {
debug!("Sending CDUP command");
let resp = ctrl.command("CDUP").await?;
if (200..300).contains(&resp.0) {
debug!("CDUP successful");
Ok(true)
} else {
warn!("CDUP failed: {} {}", resp.0, resp.1);
Ok(false)
}
}
#[allow(dead_code)] pub async fn send_mkd(ctrl: &mut FreshControl, path: &str) -> Result<bool> {
debug!("Sending MKD command for: {}", path);
let resp = ctrl.command(&format!("MKD {}", path)).await?;
if resp.0 == 257 {
debug!("MKD successful: {}", path);
Ok(true)
} else if resp.0 == 550 {
debug!("MKD returned 550 (directory may already exist): {}", path);
Ok(false)
} else {
warn!("MKD failed for {}: {} {}", path, resp.0, resp.1);
Ok(false)
}
}
#[allow(dead_code)] pub async fn send_noop(ctrl: &mut FreshControl) -> Result<bool> {
debug!("Sending NOOP command");
let resp = ctrl.command("NOOP").await?;
if resp.0 == 200 {
debug!("NOOP successful, connection alive");
Ok(true)
} else {
warn!("NOOP unexpected response: {} {}", resp.0, resp.1);
Ok(false)
}
}
#[allow(dead_code)] pub async fn send_auth_tls(ctrl: &mut FreshControl) -> Result<bool> {
debug!("Sending AUTH TLS command");
let resp = ctrl.command("AUTH TLS").await?;
match resp.0 {
234 => {
warn!(
"AUTH TLS accepted by server (234), but TLS upgrade is not yet implemented. \
Continuing without TLS."
);
Ok(false)
}
_ => {
info!("AUTH TLS not supported by server ({} {})", resp.0, resp.1);
Ok(false)
}
}
}
#[allow(dead_code)] pub async fn send_pbsz_prot(ctrl: &mut FreshControl) -> Result<bool> {
debug!("Sending PBSZ 0 command");
let pbsz_resp = ctrl.command("PBSZ 0").await?;
if !(200..300).contains(&pbsz_resp.0) {
warn!("PBSZ 0 failed: {} {}", pbsz_resp.0, pbsz_resp.1);
return Ok(false);
}
debug!("Sending PROT P command");
let prot_resp = ctrl.command("PROT P").await?;
if !(200..300).contains(&prot_resp.0) {
warn!("PROT P failed: {} {}", prot_resp.0, prot_resp.1);
return Ok(false);
}
info!("TLS data channel protection configured (PBSZ 0 + PROT P)");
Ok(true)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_feat_response_full() {
let response = "\
211-Features:
UTF8
MLST type*;size*;modify*;
SIZE
MDTM
EPSV
EPRT
TVFS
211 End";
let mut caps = ServerCapabilities::new();
caps.parse_feat_response(response);
assert!(caps.utf8);
assert!(caps.mlst_mlsd);
assert!(caps.size);
assert!(caps.mdtm);
assert!(caps.epsv);
assert!(caps.eprt);
assert!(caps.tvfs);
}
#[test]
fn test_parse_feat_response_minimal() {
let response = "\
211-Features:
UTF8
211 End";
let mut caps = ServerCapabilities::new();
caps.parse_feat_response(response);
assert!(caps.utf8);
assert!(!caps.mlst_mlsd);
assert!(!caps.size);
assert!(!caps.mdtm);
}
#[test]
fn test_parse_feat_response_empty() {
let response = "211 End";
let mut caps = ServerCapabilities::new();
caps.parse_feat_response(response);
assert!(!caps.utf8);
assert!(!caps.mlst_mlsd);
}
#[test]
fn test_parse_feat_response_case_insensitive() {
let response = "\
211-Features:
utf8
Mlst type*;size*;
211 End";
let mut caps = ServerCapabilities::new();
caps.parse_feat_response(response);
assert!(caps.utf8);
assert!(caps.mlst_mlsd);
}
#[test]
fn test_parse_feat_mlst_with_semicolons() {
let response = "\
211-Features:
MLST type*;size*;modify*;perm*;
211 End";
let mut caps = ServerCapabilities::new();
caps.parse_feat_response(response);
assert!(caps.mlst_mlsd);
}
#[test]
fn test_parse_feat_unknown_feature() {
let response = "\
211-Features:
UTF8
SOME_UNKNOWN_FEATURE
211 End";
let mut caps = ServerCapabilities::new();
caps.parse_feat_response(response);
assert!(caps.utf8);
}
#[test]
fn test_server_capabilities_default() {
let caps = ServerCapabilities::new();
assert!(!caps.utf8);
assert!(!caps.mlst_mlsd);
assert!(!caps.size);
assert!(!caps.mdtm);
assert!(!caps.epsv);
assert!(!caps.eprt);
assert!(!caps.tvfs);
assert!(caps.syst.is_none());
}
#[test]
fn test_parse_feat_mlsd_separate() {
let response = "\
211-Features:
MLST type*;size*;
MLSD
211 End";
let mut caps = ServerCapabilities::new();
caps.parse_feat_response(response);
assert!(caps.mlst_mlsd);
}
}