From 67d1e0ee2f869e37b886988c50c62894b707bd6b Mon Sep 17 00:00:00 2001 From: perf3ct Date: Mon, 23 Jun 2025 19:39:39 +0000 Subject: [PATCH] feat(webdav): move etag parser to own function, create required migration --- ...0250623000001_normalize_existing_etags.sql | 11 ++++++ src/webdav_xml_parser.rs | 35 +++++++++++++++---- tests/webdav_enhanced_unit_tests.rs | 6 ++-- 3 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 migrations/20250623000001_normalize_existing_etags.sql diff --git a/migrations/20250623000001_normalize_existing_etags.sql b/migrations/20250623000001_normalize_existing_etags.sql new file mode 100644 index 0000000..3198d35 --- /dev/null +++ b/migrations/20250623000001_normalize_existing_etags.sql @@ -0,0 +1,11 @@ +-- Normalize existing ETags in webdav_files table to match new normalization format +-- This migration ensures that existing ETag values are normalized to prevent +-- unnecessary re-downloads of unchanged files after the ETag normalization fix + +-- Update ETags to remove quotes and W/ prefixes +UPDATE webdav_files +SET etag = TRIM(BOTH '"' FROM TRIM(LEADING 'W/' FROM etag)) +WHERE etag LIKE '"%"' OR etag LIKE 'W/%'; + +-- Add a comment to document this normalization +COMMENT ON COLUMN webdav_files.etag IS 'Normalized ETag without quotes or W/ prefix (since migration 20250623000001)'; \ No newline at end of file diff --git a/src/webdav_xml_parser.rs b/src/webdav_xml_parser.rs index ac6a737..328a09c 100644 --- a/src/webdav_xml_parser.rs +++ b/src/webdav_xml_parser.rs @@ -83,13 +83,7 @@ pub fn parse_propfind_response(xml_text: &str) -> Result> { resp.content_type = Some(text.trim().to_string()); } "getetag" => { - // Normalize ETag by removing quotes and weak ETag prefix - let etag = text.trim(); - let normalized = etag - .trim_start_matches("W/") - .trim_matches('"') - .to_string(); - resp.etag = Some(normalized); + resp.etag = Some(normalize_etag(&text)); } "status" if in_propstat => { // Check if status is 200 OK @@ -206,6 +200,20 @@ fn parse_http_date(date_str: &str) -> Option> { }) } +/// Normalize ETag by removing quotes and weak ETag prefix +/// This ensures consistent ETag comparison across different WebDAV servers +/// +/// Examples: +/// - `"abc123"` → `abc123` +/// - `W/"abc123"` → `abc123` +/// - `abc123` → `abc123` +fn normalize_etag(etag: &str) -> String { + etag.trim() + .trim_start_matches("W/") + .trim_matches('"') + .to_string() +} + #[cfg(test)] mod tests { use super::*; @@ -344,4 +352,17 @@ mod tests { let files = parse_propfind_response(xml).unwrap(); assert_eq!(files.len(), 0); } + + #[test] + fn test_normalize_etag() { + // Test various ETag formats that WebDAV servers might return + assert_eq!(normalize_etag("abc123"), "abc123"); + assert_eq!(normalize_etag("\"abc123\""), "abc123"); + assert_eq!(normalize_etag("W/\"abc123\""), "abc123"); + assert_eq!(normalize_etag(" \"abc123\" "), "abc123"); + assert_eq!(normalize_etag("W/\"abc-123-def\""), "abc-123-def"); + assert_eq!(normalize_etag(""), ""); + assert_eq!(normalize_etag("\"\""), ""); + assert_eq!(normalize_etag("W/\"\""), ""); + } } \ No newline at end of file diff --git a/tests/webdav_enhanced_unit_tests.rs b/tests/webdav_enhanced_unit_tests.rs index 112c0f6..d229205 100644 --- a/tests/webdav_enhanced_unit_tests.rs +++ b/tests/webdav_enhanced_unit_tests.rs @@ -201,21 +201,21 @@ fn test_webdav_response_parsing_comprehensive() { let pdf_file = files.iter().find(|f| f.name == "report.pdf").unwrap(); assert_eq!(pdf_file.size, 2048000); assert_eq!(pdf_file.mime_type, "application/pdf"); - assert_eq!(pdf_file.etag, "\"pdf123\""); + assert_eq!(pdf_file.etag, "pdf123"); // ETag should be normalized (quotes removed) assert!(!pdf_file.is_directory); // Verify second file (photo.png) let png_file = files.iter().find(|f| f.name == "photo.png").unwrap(); assert_eq!(png_file.size, 768000); assert_eq!(png_file.mime_type, "image/png"); - assert_eq!(png_file.etag, "\"png456\""); + assert_eq!(png_file.etag, "png456"); // ETag should be normalized (quotes removed) assert!(!png_file.is_directory); // Verify third file (unsupported.docx) let docx_file = files.iter().find(|f| f.name == "unsupported.docx").unwrap(); assert_eq!(docx_file.size, 102400); assert_eq!(docx_file.mime_type, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); - assert_eq!(docx_file.etag, "\"docx789\""); + assert_eq!(docx_file.etag, "docx789"); // ETag should be normalized (quotes removed) assert!(!docx_file.is_directory); }