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

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


The following commit(s) were added to refs/heads/9.0.x by this push:
     new e1bca79d3d Minor fixes
e1bca79d3d is described below

commit e1bca79d3d1579a1a73edc14216eb7c254f03a94
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 f36dfc0892..823efb6c7a 100644
--- a/java/org/apache/catalina/servlets/WebdavServlet.java
+++ b/java/org/apache/catalina/servlets/WebdavServlet.java
@@ -1631,14 +1631,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()))) {
@@ -1731,7 +1731,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;
@@ -1743,23 +1743,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);
+                            }
                         }
                     }
                 }
@@ -1888,13 +1890,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);
     }
 
 
@@ -1930,11 +1926,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;
                                     }
                                 }
@@ -2270,7 +2268,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<>();
@@ -2293,7 +2291,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()) {
@@ -2307,29 +2305,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.
@@ -2354,11 +2329,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);
 
@@ -2369,7 +2339,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));
 
@@ -2396,13 +2366,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