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

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


The following commit(s) were added to refs/heads/10.1.x by this push:
     new 80249fe9f5 Fix a bug handling multiple If-Modified-Since preconditions
80249fe9f5 is described below

commit 80249fe9f5bca6d01c115403b2778397e0f8ac67
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Dec 12 12:12:14 2024 +0000

    Fix a bug handling multiple If-Modified-Since preconditions
    
    Expand tests to cover If-Modified-Since.
    Expand tests for multiple values.
    Fix bug by aligning code for If-Modified-Since with If-Unmodified-Since
---
 .../apache/catalina/servlets/DefaultServlet.java   | 34 +++++++++++++++-------
 ...efaultServletRfc9110Section13Parameterized.java | 22 ++++++++++++++
 2 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/java/org/apache/catalina/servlets/DefaultServlet.java 
b/java/org/apache/catalina/servlets/DefaultServlet.java
index eb1bf46fa9..f47abd4441 100644
--- a/java/org/apache/catalina/servlets/DefaultServlet.java
+++ b/java/org/apache/catalina/servlets/DefaultServlet.java
@@ -2213,21 +2213,33 @@ public class DefaultServlet extends HttpServlet {
         if (!"GET".equals(method) && !"HEAD".equals(method)) {
             return true;
         }
+
         long resourceLastModified = resource.getLastModified();
+        if (resourceLastModified <= -1 || request.getHeader("If-None-Match") 
!= null) {
+            // MUST ignore if the resource does not have a modification date 
available.
+            // MUST ignore if the request contains an If-None-Match header 
field
+            return true;
+        }
+        Enumeration<String> headerEnum = 
request.getHeaders("If-Modified-Since");
+        if (!headerEnum.hasMoreElements()) {
+            // If-Modified-Since is not present
+            return true;
+        }
+        headerEnum.nextElement();
+        if (headerEnum.hasMoreElements()) {
+            // If-Modified-Since is a list of dates
+            return true;
+        }
 
         try {
+            // Header is present so -1 will be not returned. Only a valid date 
or an IAE are possible.
             long headerValue = request.getDateHeader("If-Modified-Since");
-            if (headerValue != -1) {
-
-                // If an If-None-Match header has been specified, if modified 
since
-                // is ignored.
-                if ((request.getHeader("If-None-Match") == null) && 
(resourceLastModified < headerValue + 1000)) {
-                    // The entity has not been modified since the date
-                    // specified by the client. This is not an error case.
-                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
-                    response.setHeader("ETag", generateETag(resource));
-                    return false;
-                }
+            if (resourceLastModified < (headerValue + 1000)) {
+                // The entity has not been modified since the date
+                // specified by the client. This is not an error case.
+                response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+                response.setHeader("ETag", generateETag(resource));
+                return false;
             }
         } catch (IllegalArgumentException illegalArgument) {
             return true;
diff --git 
a/test/org/apache/catalina/servlets/TestDefaultServletRfc9110Section13Parameterized.java
 
b/test/org/apache/catalina/servlets/TestDefaultServletRfc9110Section13Parameterized.java
index 4aa70b5711..eb14b5029c 100644
--- 
a/test/org/apache/catalina/servlets/TestDefaultServletRfc9110Section13Parameterized.java
+++ 
b/test/org/apache/catalina/servlets/TestDefaultServletRfc9110Section13Parameterized.java
@@ -97,6 +97,8 @@ public class TestDefaultServletRfc9110Section13Parameterized 
extends TomcatBaseT
                         Boolean.FALSE, SC_200 });
                 parameterSets.add(new Object[] { useStrongEtag, task, null, 
DatePrecondition.MULTI_IN, null, null, null,
                         Boolean.FALSE, SC_200 });
+                parameterSets.add(new Object[] { useStrongEtag, task, null, 
DatePrecondition.MULTI_IN_REV, null, null, null,
+                        Boolean.FALSE, SC_200 });
                 parameterSets.add(new Object[] { useStrongEtag, task, null, 
DatePrecondition.INVALID, null, null, null,
                         Boolean.FALSE, SC_200 });
 
@@ -110,6 +112,20 @@ public class 
TestDefaultServletRfc9110Section13Parameterized extends TomcatBaseT
                 // Both
                 parameterSets.add(new Object[] { useStrongEtag, task, null, 
DatePrecondition.LT, null,
                         DatePrecondition.GT, null, Boolean.FALSE, SC_412 });
+
+                // RFC 9110, Section 13.2.2, Step 4, HEAD: If-Unmodified-Since 
only
+                parameterSets.add(new Object[] { useStrongEtag, task, null, 
null, null, DatePrecondition.EQ, null,
+                        Boolean.FALSE, SC_304 });
+                parameterSets.add(new Object[] { useStrongEtag, task, null, 
null, null, DatePrecondition.LT, null,
+                        Boolean.FALSE, SC_200 });
+                parameterSets.add(new Object[] { useStrongEtag, task, null, 
null, null, DatePrecondition.GT, null,
+                        Boolean.FALSE, SC_304 });
+                parameterSets.add(new Object[] { useStrongEtag, task, null, 
null, null, DatePrecondition.MULTI_IN, null,
+                        Boolean.FALSE, SC_200 });
+                parameterSets.add(new Object[] { useStrongEtag, task, null, 
null, null, DatePrecondition.MULTI_IN_REV, null,
+                        Boolean.FALSE, SC_200 });
+                parameterSets.add(new Object[] { useStrongEtag, task, null, 
null, null, DatePrecondition.INVALID, null,
+                        Boolean.FALSE, SC_200 });
             }
         }
 
@@ -191,6 +207,7 @@ public class 
TestDefaultServletRfc9110Section13Parameterized extends TomcatBaseT
          */
         LT,
         MULTI_IN,
+        MULTI_IN_REV,
         /**
          * not a valid HTTP-date
          */
@@ -268,6 +285,11 @@ public class 
TestDefaultServletRfc9110Section13Parameterized extends TomcatBaseT
                 
headerValues.add(FastHttpDateFormat.formatDate(lastModifiedTimestamp));
                 
headerValues.add(FastHttpDateFormat.formatDate(lastModifiedTimestamp + 30000L));
                 break;
+            case MULTI_IN_REV:
+                
headerValues.add(FastHttpDateFormat.formatDate(lastModifiedTimestamp + 30000L));
+                
headerValues.add(FastHttpDateFormat.formatDate(lastModifiedTimestamp));
+                
headerValues.add(FastHttpDateFormat.formatDate(lastModifiedTimestamp - 30000L));
+                break;
             case INVALID:
                 headerValues.add("2024.12.09 GMT");
                 break;


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

Reply via email to