Skip to content

Commit

Permalink
Refactor. No functional changed.
Browse files Browse the repository at this point in the history
A couple more lines of code but slightly simpler and more aligned with
description in RFC 9110.
  • Loading branch information
markt-asf committed Dec 13, 2024
1 parent 0600912 commit c03ef13
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions java/org/apache/catalina/servlets/DefaultServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -756,10 +756,25 @@ protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws
*/
protected boolean checkIfHeaders(HttpServletRequest request, HttpServletResponse response, WebResource resource)
throws IOException {
return checkIfMatch(request, response, resource)
&& checkIfUnmodifiedSince(request, response, resource)
&& checkIfNoneMatch(request, response, resource)
&& checkIfModifiedSince(request, response, resource);
if (request.getHeader("If-Match") != null) {
if (!checkIfMatch(request, response, resource)) {
return false;
}
} else if (request.getHeader("If-Unmodified-Since") != null) {
if (!checkIfUnmodifiedSince(request, response, resource)) {
return false;
}
}
if (request.getHeader("If-None-Match") != null) {
if (!checkIfNoneMatch(request, response, resource)) {
return false;
}
} else if (request.getHeader("If-Modified-Since") != null) {
if (!checkIfModifiedSince(request, response, resource)) {
return false;
}
}
return true;
}


Expand Down Expand Up @@ -2227,9 +2242,6 @@ protected boolean checkIfMatch(HttpServletRequest request, HttpServletResponse r

boolean conditionSatisfied = false;
Enumeration<String> headerValues = request.getHeaders("If-Match");
if (!headerValues.hasMoreElements()) {
return true;
}
String resourceETag = generateETag(resource);

boolean hasAsteriskValue = false;// check existence of special header value '*'
Expand Down Expand Up @@ -2294,16 +2306,13 @@ protected boolean checkIfModifiedSince(HttpServletRequest request, HttpServletRe
}

long resourceLastModified = resource.getLastModified();
if (resourceLastModified <= -1 || request.getHeader("If-None-Match") != null) {
if (resourceLastModified <= -1) {
// 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;
}

// Must be at least one header for this method to be called
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
Expand Down Expand Up @@ -2345,9 +2354,6 @@ protected boolean checkIfNoneMatch(HttpServletRequest request, HttpServletRespon
String resourceETag = generateETag(resource);

Enumeration<String> headerValues = request.getHeaders("If-None-Match");
if (!headerValues.hasMoreElements()) {
return true;
}
boolean hasAsteriskValue = false;// check existence of special header value '*'
boolean conditionSatisfied = true;
int headerCount = 0;
Expand Down Expand Up @@ -2432,16 +2438,12 @@ protected boolean checkIfUnmodifiedSince(HttpServletRequest request, HttpServlet
WebResource resource) throws IOException {

long resourceLastModified = resource.getLastModified();
if (resourceLastModified <= -1 || request.getHeader("If-Match") != null) {
if (resourceLastModified <= -1) {
// MUST ignore if the resource does not have a modification date available.
// MUST ignore if the request contains an If-Match header field
return true;
}
// Must be at least one header for this method to be called
Enumeration<String> headerEnum = request.getHeaders("If-Unmodified-Since");
if (!headerEnum.hasMoreElements()) {
// If-Unmodified-Since is not present
return true;
}
headerEnum.nextElement();
if (headerEnum.hasMoreElements()) {
// If-Unmodified-Since is a list of dates
Expand Down

0 comments on commit c03ef13

Please sign in to comment.