Return 304 for not modified SmallResources

When in-memory cached resources are requested using
If-Modified-Since HTTP header, we can then safely
return a 304 without having to transfer the entire
payload over the network.

This change completes the fix [1]: now that we have
the lastModified date into the SmallResource object
we can use it to actually save bandwidth and allow
better client response time.

[1] https://gerrit-review.googlesource.com/#/c/57213/

Change-Id: If268acacc167a2062260008a3b9767e055970d43
This commit is contained in:
Luca Milanesio 2014-05-16 18:43:59 +01:00
parent 0acab4a963
commit c06b6c4615

View File

@ -14,6 +14,7 @@
package com.google.gerrit.httpd.plugins; package com.google.gerrit.httpd.plugins;
import com.google.common.net.HttpHeaders;
import com.google.gerrit.common.Nullable; import com.google.gerrit.common.Nullable;
import java.io.IOException; import java.io.IOException;
@ -55,7 +56,13 @@ final class SmallResource extends Resource {
void send(HttpServletRequest req, HttpServletResponse res) void send(HttpServletRequest req, HttpServletResponse res)
throws IOException { throws IOException {
if (0 < lastModified) { if (0 < lastModified) {
res.setDateHeader("Last-Modified", lastModified); long ifModifiedSince = req.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
if (ifModifiedSince > 0 && ifModifiedSince == lastModified) {
res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
} else {
res.setDateHeader("Last-Modified", lastModified);
}
} }
res.setContentType(contentType); res.setContentType(contentType);
if (characterEncoding != null) { if (characterEncoding != null) {