This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch xuanwo/fix-dav-server-mkcol-root-and-move
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit 0c441a74c511e290058c759ba2d762cc9597106d
Author: Xuanwo <[email protected]>
AuthorDate: Sun Nov 22 22:17:27 2026 +0100

    fix(integrations/dav-server): fix MKCOL / panic and MOVE on non-empty 
collections
    
    MKCOL / panicked on Path::new("/").parent().unwrap() before reaching the
    existence check. Skip the parent-existence check for paths without a
    parent so MKCOL / returns FsError::Exists (HTTP 405) per RFC 4918 9.3.1,
    while a missing parent still fails with 409 Conflict.
    
    MOVE on a non-empty collection returned 501 even on backends whose
    native Operator::rename moves directories atomically. Attempt the native
    rename when capability().rename is advertised, and keep the existing
    empty-collection fallback otherwise.
    
    Add HTTP loopback coverage for MKCOL /, MKCOL with a missing parent, and
    MOVE of a non-empty collection including Overwrite F/T semantics.
---
 integrations/dav-server/src/fs.rs                |  51 +++++++--
 integrations/dav-server/tests/webdav_loopback.rs | 126 +++++++++++++++++++++++
 2 files changed, 166 insertions(+), 11 deletions(-)

diff --git a/integrations/dav-server/src/fs.rs 
b/integrations/dav-server/src/fs.rs
index 25db711d9d..c8a034e4c9 100644
--- a/integrations/dav-server/src/fs.rs
+++ b/integrations/dav-server/src/fs.rs
@@ -158,19 +158,25 @@ impl DavFileSystem for OpendalFs {
             // check if the parent path is exist.
             // During MKCOL processing, a server MUST make the Request-URI a 
member of its parent collection, unless the Request-URI is "/".  If no such 
ancestor exists, the method MUST fail.
             // refer to 
https://datatracker.ietf.org/doc/html/rfc2518#section-8.3.1
-            let parent = Path::new(&path).parent().unwrap();
-            match self
-                .op
-                .exists(format!("{}/", parent.display()).as_str())
-                .await
+            // The root and paths without a parent separator have no parent; 
skip
+            // the check so they hit the existence check below instead.
+            if let Some(parent) = Path::new(&path).parent()
+                && parent != Path::new("/")
+                && !parent.as_os_str().is_empty()
             {
-                Ok(exist) => {
-                    if !exist && parent != Path::new("/") {
-                        return Err(FsError::NotFound);
+                match self
+                    .op
+                    .exists(format!("{}/", parent.display()).as_str())
+                    .await
+                {
+                    Ok(exist) => {
+                        if !exist {
+                            return Err(FsError::NotFound);
+                        }
+                    }
+                    Err(e) => {
+                        return Err(convert_error(e));
                     }
-                }
-                Err(e) => {
-                    return Err(convert_error(e));
                 }
             }
 
@@ -225,6 +231,29 @@ impl DavFileSystem for OpendalFs {
                     return Err(FsError::Exists);
                 }
 
+                // Services with native rename can move a non-empty collection
+                // atomically. Fall back to the empty-collection move when the
+                // service cannot rename this collection. `Operator::rename`
+                // takes file paths, so drop the collection trailing slashes.
+                if self.op.info().capability().rename {
+                    match self
+                        .op
+                        .rename(
+                            from_path.trim_end_matches('/'),
+                            to_path.trim_end_matches('/'),
+                        )
+                        .await
+                    {
+                        Ok(()) => return Ok(()),
+                        // A missing collection on a rename-capable backend can
+                        // look like a missing source; the listing below 
decides.
+                        Err(e)
+                            if matches!(e.kind(), ErrorKind::Unsupported | 
ErrorKind::NotFound) => {
+                        }
+                        Err(e) => return Err(convert_error(e)),
+                    }
+                }
+
                 let mut lister = self
                     .op
                     .lister_with(from_path.as_str())
diff --git a/integrations/dav-server/tests/webdav_loopback.rs 
b/integrations/dav-server/tests/webdav_loopback.rs
index 39997eaced..c819402695 100644
--- a/integrations/dav-server/tests/webdav_loopback.rs
+++ b/integrations/dav-server/tests/webdav_loopback.rs
@@ -313,3 +313,129 @@ async fn nested_utf8_file_lifecycle() -> Result<()> {
     );
     Ok(())
 }
+
+#[tokio::test]
+async fn mkcol_root_and_missing_parent() -> Result<()> {
+    // MKCOL / must fail with 405 instead of panicking on the missing parent
+    // of "/"; the connection and server stay usable afterwards.
+    let dav = WebDav::new().await?;
+    dav.send(
+        dav.request("MKCOL", "/"),
+        b"",
+        StatusCode::METHOD_NOT_ALLOWED,
+    )
+    .await?;
+
+    dav.send(
+        dav.request("MKCOL", "/missing/child/"),
+        b"",
+        StatusCode::CONFLICT,
+    )
+    .await?;
+    dav.send(dav.request("MKCOL", "/alive/"), b"", StatusCode::CREATED)
+        .await?;
+    assert_eq!(
+        dav.propfind("/", "1").await?,
+        [
+            ("/".to_string(), Resource::Collection),
+            ("/alive/".to_string(), Resource::Collection)
+        ]
+    );
+    Ok(())
+}
+
+#[tokio::test]
+async fn move_non_empty_collection() -> Result<()> {
+    // The fs backend supports native rename, so MOVE on a non-empty
+    // collection must relocate it instead of failing with 501.
+    let dav = WebDav::new().await?;
+    dav.send(dav.request("MKCOL", "/dir/"), b"", StatusCode::CREATED)
+        .await?;
+    dav.send(
+        dav.request("MKCOL", "/dir/nested/"),
+        b"",
+        StatusCode::CREATED,
+    )
+    .await?;
+    let content = b"nested file";
+    dav.send(
+        dav.request("PUT", "/dir/nested/blob.bin"),
+        content,
+        StatusCode::CREATED,
+    )
+    .await?;
+    dav.send(
+        dav.request("PUT", "/dir/top.bin"),
+        b"top",
+        StatusCode::CREATED,
+    )
+    .await?;
+
+    let move_request = dav
+        .request("MOVE", "/dir/")
+        .header("Destination", format!("http://{}/moved/";, dav.addr));
+    dav.send(move_request, b"", StatusCode::CREATED).await?;
+
+    dav.send(
+        dav.request("PROPFIND", "/dir/").header("Depth", "0"),
+        b"",
+        StatusCode::NOT_FOUND,
+    )
+    .await?;
+    assert_eq!(
+        dav.propfind("/moved/", "1").await?,
+        [
+            ("/moved/".to_string(), Resource::Collection),
+            ("/moved/nested/".to_string(), Resource::Collection),
+            ("/moved/top.bin".to_string(), Resource::File(3))
+        ]
+    );
+    let get = dav
+        .send(
+            dav.request("GET", "/moved/nested/blob.bin"),
+            b"",
+            StatusCode::OK,
+        )
+        .await?;
+    assert_eq!(get.body().as_ref(), content);
+
+    // MOVE with `Overwrite: F` onto an existing collection fails with 412.
+    dav.send(dav.request("MKCOL", "/occupied/"), b"", StatusCode::CREATED)
+        .await?;
+    let overwrite_f = dav
+        .request("MOVE", "/moved/")
+        .header("Destination", format!("http://{}/occupied/";, dav.addr))
+        .header("Overwrite", "F");
+    dav.send(overwrite_f, b"", StatusCode::PRECONDITION_FAILED)
+        .await?;
+
+    // With `Overwrite: T` the destination is replaced atomically (204).
+    let overwrite_t = dav
+        .request("MOVE", "/moved/")
+        .header("Destination", format!("http://{}/occupied/";, dav.addr))
+        .header("Overwrite", "T");
+    dav.send(overwrite_t, b"", StatusCode::NO_CONTENT).await?;
+    dav.send(
+        dav.request("PROPFIND", "/moved/").header("Depth", "0"),
+        b"",
+        StatusCode::NOT_FOUND,
+    )
+    .await?;
+    assert_eq!(
+        dav.propfind("/occupied/", "1").await?,
+        [
+            ("/occupied/".to_string(), Resource::Collection),
+            ("/occupied/nested/".to_string(), Resource::Collection),
+            ("/occupied/top.bin".to_string(), Resource::File(3))
+        ]
+    );
+    let get = dav
+        .send(
+            dav.request("GET", "/occupied/nested/blob.bin"),
+            b"",
+            StatusCode::OK,
+        )
+        .await?;
+    assert_eq!(get.body().as_ref(), content);
+    Ok(())
+}

Reply via email to