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

remm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new 62473af6a0 Minor fixes
62473af6a0 is described below

commit 62473af6a02e6d36d8ba6a3bc4e205e22eb0ea86
Author: remm <r...@apache.org>
AuthorDate: Thu Oct 24 09:34:05 2024 +0200

    Minor fixes
    
    Stop using the Lock-Token header for isLocked (clearly not used for
    that).
    More accurate matching of the token (append : and >).
    Cleanup the unlock for shared locks, although no real change (unless
    there's a token with no shared lock anymore, which should not really
    happen).
    Drop old unlockResource method (the shared lock unlock probably had some
    issue since it seemed to only remove the first one), and replace it with
    a much simpler deletedResource.
---
 .../apache/catalina/servlets/WebdavServlet.java    | 83 +++++++++-------------
 1 file changed, 32 insertions(+), 51 deletions(-)

diff --git a/java/org/apache/catalina/servlets/WebdavServlet.java 
b/java/org/apache/catalina/servlets/WebdavServlet.java
index 0d8474892e..c6573aed43 100644
--- a/java/org/apache/catalina/servlets/WebdavServlet.java
+++ b/java/org/apache/catalina/servlets/WebdavServlet.java
@@ -1634,14 +1634,14 @@ public class WebdavServlet extends DefaultServlet 
implements PeriodicEventListen
                     } else {
                         if ((parentPath != path && parentLock.depth > 0) || 
parentPath == path) {
                             if (parentLock.isExclusive()) {
-                                if (ifHeader.contains(parentLock.token) && 
(parentLock.principal == null ||
+                                if (ifHeader.contains(":" + parentLock.token + 
">") && (parentLock.principal == null ||
                                         
parentLock.principal.equals(req.getRemoteUser()))) {
                                     toRenew = parentLock;
                                     break;
                                 }
                             } else {
                                 for (String token : parentLock.sharedTokens) {
-                                    if (ifHeader.contains(token)) {
+                                    if (ifHeader.contains(":" + token + ">")) {
                                         LockInfo sharedLock = 
sharedLocks.get(token);
                                         if (sharedLock != null && 
(sharedLock.principal == null ||
                                                 
sharedLock.principal.equals(req.getRemoteUser()))) {
@@ -1734,7 +1734,7 @@ public class WebdavServlet extends DefaultServlet 
implements PeriodicEventListen
                 } else {
                     if ((parentPath != path && parentLock.depth > 0) || 
parentPath == path) {
                         if (parentLock.isExclusive()) {
-                            if (lockTokenHeader.contains(parentLock.token) && 
(parentLock.principal == null ||
+                            if (lockTokenHeader.contains(":" + 
parentLock.token + ">") && (parentLock.principal == null ||
                                     
parentLock.principal.equals(req.getRemoteUser()))) {
                                 resourceLocks.remove(parentPath);
                                 unlocked = true;
@@ -1746,23 +1746,25 @@ public class WebdavServlet extends DefaultServlet 
implements PeriodicEventListen
                             }
                         } else {
                             for (String token : parentLock.sharedTokens) {
-                                if (lockTokenHeader.contains(token)) {
+                                if (lockTokenHeader.contains(":" + token + 
">")) {
                                     LockInfo lock = sharedLocks.get(token);
-                                    if (lock == null || lock.principal == null 
||
-                                            
lock.principal.equals(req.getRemoteUser())) {
-                                        if ((parentPath != path && lock != 
null && lock.depth > 0) ||
-                                                parentPath == path) {
+                                    if (lock == null) {
+                                        parentLock.sharedTokens.remove(token);
+                                    } else if (lock.principal == null || 
lock.principal.equals(req.getRemoteUser())) {
+                                        // The shared lock might not have the 
same depth
+                                        if ((parentPath != path && lock.depth 
> 0) || parentPath == path) {
                                             
parentLock.sharedTokens.remove(token);
-                                            if 
(parentLock.sharedTokens.isEmpty()) {
-                                                
resourceLocks.remove(parentPath);
-                                            }
                                             sharedLocks.remove(token);
                                             unlocked = true;
-                                            break;
                                         }
                                     }
+                                    // Unlike the if header, this can only 
match one token
+                                    break;
                                 }
                             }
+                            if (parentLock.sharedTokens.isEmpty()) {
+                                resourceLocks.remove(parentPath);
+                            }
                         }
                     }
                 }
@@ -1891,13 +1893,7 @@ public class WebdavServlet extends DefaultServlet 
implements PeriodicEventListen
             ifHeader = "";
         }
 
-        // Normally a lock token is not sent using this header
-        String lockTokenHeader = req.getHeader("Lock-Token");
-        if (lockTokenHeader == null) {
-            lockTokenHeader = "";
-        }
-
-        return isLocked(path, req.getRemoteUser(), ifHeader + lockTokenHeader);
+        return isLocked(path, req.getRemoteUser(), ifHeader);
     }
 
 
@@ -1933,11 +1929,13 @@ public class WebdavServlet extends DefaultServlet 
implements PeriodicEventListen
                             for (String token : parentLock.sharedTokens) {
                                 LockInfo lock = sharedLocks.get(token);
                                 if (lock != null) {
+                                    // The shared lock might not have the same 
depth
                                     if ((parentPath != path && lock.depth > 0) 
|| parentPath == path) {
                                         if (ifHeader.contains(":" + token + 
">") &&
                                                 (lock.principal == null || 
lock.principal.equals(principal))) {
                                             return false;
                                         }
+                                        // Since it is a shared lock, continue 
to look up the tree but note that there was a lock
                                         unmatchedSharedLock = true;
                                     }
                                 }
@@ -2273,7 +2271,7 @@ public class WebdavServlet extends DefaultServlet 
implements PeriodicEventListen
                 sendNotAllowed(req, resp);
                 return false;
             }
-            unlockResource(path, null);
+            deletedResource(path);
         } else {
 
             Map<String,Integer> errorList = new LinkedHashMap<>();
@@ -2296,7 +2294,7 @@ public class WebdavServlet extends DefaultServlet 
implements PeriodicEventListen
                     errorList.put(path, 
Integer.valueOf(WebdavStatus.SC_METHOD_NOT_ALLOWED));
                 }
             } else {
-                unlockResource(path, null);
+                deletedResource(path);
             }
 
             if (!errorList.isEmpty()) {
@@ -2310,29 +2308,6 @@ public class WebdavServlet extends DefaultServlet 
implements PeriodicEventListen
         return true;
     }
 
-    private void unlockResource(String path, String lockToken) {
-        LockInfo lock = resourceLocks.get(path);
-        if (lock != null) {
-            if (lock.isExclusive()) {
-                if (lockToken == null || lockToken.contains(lock.token)) {
-                    resourceLocks.remove(path);
-                }
-            } else {
-                for (String token : lock.sharedTokens) {
-                    if (lockToken == null || lockToken.contains(token)) {
-                        lock.sharedTokens.remove(token);
-                        if (lock.sharedTokens.isEmpty()) {
-                            resourceLocks.remove(lock.path);
-                        }
-                        sharedLocks.remove(token);
-                        break;
-                    }
-                }
-            }
-        }
-        store.delete(path);
-    }
-
 
     /**
      * Deletes a collection.
@@ -2357,11 +2332,6 @@ public class WebdavServlet extends DefaultServlet 
implements PeriodicEventListen
         if (ifHeader == null) {
             ifHeader = "";
         }
-        String lockTokenHeader = req.getHeader("Lock-Token");
-        if (lockTokenHeader == null) {
-            lockTokenHeader = "";
-        }
-        String lockHeader = ifHeader + lockTokenHeader;
 
         String[] entries = resources.list(path);
 
@@ -2372,7 +2342,7 @@ public class WebdavServlet extends DefaultServlet 
implements PeriodicEventListen
             }
             childName += entry;
 
-            if (isLocked(childName, req.getRemoteUser(), lockHeader)) {
+            if (isLocked(childName, req.getRemoteUser(), ifHeader)) {
 
                 errorList.put(childName, 
Integer.valueOf(WebdavStatus.SC_LOCKED));
 
@@ -2399,13 +2369,24 @@ public class WebdavServlet extends DefaultServlet 
implements PeriodicEventListen
                         errorList.put(childName, 
Integer.valueOf(WebdavStatus.SC_METHOD_NOT_ALLOWED));
                     }
                 } else {
-                    unlockResource(childName, null);
+                    deletedResource(childName);
                 }
             }
         }
     }
 
 
+    private void deletedResource(String path) {
+        LockInfo lock = resourceLocks.remove(path);
+        if (lock != null && !lock.isExclusive()) {
+            for (String token : lock.sharedTokens) {
+                sharedLocks.remove(token);
+            }
+        }
+        store.delete(path);
+    }
+
+
     /**
      * Send a multistatus element containing a complete error report to the 
client.
      *


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to