Chenjp commented on code in PR #796:
URL: https://github.com/apache/tomcat/pull/796#discussion_r1879213975
##########
java/org/apache/catalina/servlets/DefaultServlet.java:
##########
@@ -2092,36 +2139,51 @@ protected boolean checkSendfile(HttpServletRequest
request, HttpServletResponse
protected boolean checkIfMatch(HttpServletRequest request,
HttpServletResponse response, WebResource resource)
throws IOException {
- String headerValue = request.getHeader("If-Match");
- if (headerValue != null) {
+ String resourceETag = generateETag(resource);
+ if (resourceETag == null) {
+ // if a current representation for the target resource is not
present
+ response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
+ return false;
+ }
- boolean conditionSatisfied;
+ int headerCount =
Collections.list(request.getHeaders("If-Match")).size();
Review Comment:
It is useful later:
```java
if (hasAsteriskValue && headerCount > 1) {
```
@rmaucher another replacement would be:
```java
List<String> headerValues =
Collections.list(request.getHeaders("If-Match"));
int headerCount = headerValues.size();
if (headerCount == 0) {
return true;
}
boolean conditionSatisfied = false;
boolean hasAsteriskValue = false;// check existence of special
header value '*'
for (String headerValue:headerValues) {
if(conditionSatisfied) {
break;
}
if ("*".equals(headerValue)) {
hasAsteriskValue = true;
conditionSatisfied = true;
} else {
// RFC 7232 requires strong comparison for If-Match headers
Boolean matched = EntityTag.compareEntityTag(new
StringReader(headerValue), false, resourceETag);
if (matched == null) {
if (debug > 10) {
log("DefaultServlet.checkIfMatch: Invalid header
value [" + headerValue + "]");
}
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return false;
} else {
conditionSatisfied = matched.booleanValue();
}
}
}
if (hasAsteriskValue && headerCount > 1) {
// Note that an If-Match header field with a list value
containing "*" and other values (including other
// instances of "*") is syntactically invalid (therefore not
allowed to be generated) and furthermore is
// unlikely to be interoperable.
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return false;
}
if (!conditionSatisfied) {
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return false;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]