Polish ServletWebRequest and DefaultServerWebExchange

- The return values of ServletWebRequest.validateIfUnmodifiedSince and
  DefaultServerWebExchange.validateIfUnmodifiedSince are not used. So I
  think that it is better to remove the return statements.

- Add missing @Nullable declarations to eTag method parameters.

- Simplify if statements

Closes gh-29460
This commit is contained in:
divcon 2022-11-10 22:33:01 +09:00 committed by Sam Brannen
parent ca6acfee30
commit c12d93c5d1
2 changed files with 15 additions and 22 deletions

View File

@ -232,10 +232,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
private boolean validateIfMatch(@Nullable String eTag) { private boolean validateIfMatch(@Nullable String eTag) {
Enumeration<String> ifMatchHeaders = getRequest().getHeaders(HttpHeaders.IF_MATCH); Enumeration<String> ifMatchHeaders = getRequest().getHeaders(HttpHeaders.IF_MATCH);
if (SAFE_METHODS.contains(getRequest().getMethod())) { if (SAFE_METHODS.contains(getRequest().getMethod()) || !ifMatchHeaders.hasMoreElements()) {
return false;
}
if (!ifMatchHeaders.hasMoreElements()) {
return false; return false;
} }
this.notModified = matchRequestedETags(ifMatchHeaders, eTag, false); this.notModified = matchRequestedETags(ifMatchHeaders, eTag, false);
@ -308,7 +305,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
return first.equals(second); return first.equals(second);
} }
private void updateResponseStateChanging(String eTag, long lastModifiedTimestamp) { private void updateResponseStateChanging(@Nullable String eTag, long lastModifiedTimestamp) {
if (this.notModified && getResponse() != null) { if (this.notModified && getResponse() != null) {
getResponse().setStatus(HttpStatus.PRECONDITION_FAILED.value()); getResponse().setStatus(HttpStatus.PRECONDITION_FAILED.value());
} }
@ -329,20 +326,18 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
return true; return true;
} }
private boolean validateIfModifiedSince(long lastModifiedTimestamp) { private void validateIfModifiedSince(long lastModifiedTimestamp) {
if (lastModifiedTimestamp < 0) { if (lastModifiedTimestamp < 0) {
return false; return;
} }
long ifModifiedSince = parseDateHeader(HttpHeaders.IF_MODIFIED_SINCE); long ifModifiedSince = parseDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
if (ifModifiedSince == -1) { if (ifModifiedSince != -1) {
return false;
}
// We will perform this validation... // We will perform this validation...
this.notModified = ifModifiedSince >= (lastModifiedTimestamp / 1000 * 1000); this.notModified = ifModifiedSince >= (lastModifiedTimestamp / 1000 * 1000);
return true; }
} }
private void updateResponseIdempotent(String eTag, long lastModifiedTimestamp) { private void updateResponseIdempotent(@Nullable String eTag, long lastModifiedTimestamp) {
if (getResponse() != null) { if (getResponse() != null) {
boolean isHttpGetOrHead = SAFE_METHODS.contains(getRequest().getMethod()); boolean isHttpGetOrHead = SAFE_METHODS.contains(getRequest().getMethod());
if (this.notModified) { if (this.notModified) {
@ -353,7 +348,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
} }
} }
private void addCachingResponseHeaders(String eTag, long lastModifiedTimestamp) { private void addCachingResponseHeaders(@Nullable String eTag, long lastModifiedTimestamp) {
if (SAFE_METHODS.contains(getRequest().getMethod())) { if (SAFE_METHODS.contains(getRequest().getMethod())) {
if (lastModifiedTimestamp > 0 && parseDateValue(getResponse().getHeader(HttpHeaders.LAST_MODIFIED)) == -1) { if (lastModifiedTimestamp > 0 && parseDateValue(getResponse().getHeader(HttpHeaders.LAST_MODIFIED)) == -1) {
getResponse().setDateHeader(HttpHeaders.LAST_MODIFIED, lastModifiedTimestamp); getResponse().setDateHeader(HttpHeaders.LAST_MODIFIED, lastModifiedTimestamp);

View File

@ -346,7 +346,7 @@ public class DefaultServerWebExchange implements ServerWebExchange {
return first.equals(second); return first.equals(second);
} }
private void updateResponseStateChanging(String eTag, Instant lastModified) { private void updateResponseStateChanging(@Nullable String eTag, Instant lastModified) {
if (this.notModified) { if (this.notModified) {
getResponse().setStatusCode(HttpStatus.PRECONDITION_FAILED); getResponse().setStatusCode(HttpStatus.PRECONDITION_FAILED);
} }
@ -401,17 +401,15 @@ public class DefaultServerWebExchange implements ServerWebExchange {
return true; return true;
} }
private boolean validateIfModifiedSince(Instant lastModified) { private void validateIfModifiedSince(Instant lastModified) {
if (lastModified.isBefore(Instant.EPOCH)) { if (lastModified.isBefore(Instant.EPOCH)) {
return false; return;
} }
long ifModifiedSince = getRequestHeaders().getIfModifiedSince(); long ifModifiedSince = getRequestHeaders().getIfModifiedSince();
if (ifModifiedSince == -1) { if (ifModifiedSince != -1) {
return false;
}
// We will perform this validation... // We will perform this validation...
this.notModified = ChronoUnit.SECONDS.between(lastModified, Instant.ofEpochMilli(ifModifiedSince)) >= 0; this.notModified = ChronoUnit.SECONDS.between(lastModified, Instant.ofEpochMilli(ifModifiedSince)) >= 0;
return true; }
} }
@Override @Override