Allow gitweb assets to be cached by browser

Instead of always serving these, allow the browser to cache the
resources for up to 5 minutes before asking again if they are
up-to-date.  When it does so, we also now check If-Modified-Since
to allow returning a 304 Not Modified response if the file hasn't
been modified, allowing the user agent to reuse its cached copy.

Change-Id: Icdd792f8c626cc30aa2d5d8dd3b7d91ad5afc1a2
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce 2010-04-24 16:21:34 -07:00
parent 526490a8b8
commit 4ba0b8a7cb
3 changed files with 61 additions and 4 deletions

View File

@ -20,8 +20,10 @@ import com.google.inject.Singleton;
import org.eclipse.jgit.util.IO;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
@ -31,18 +33,27 @@ import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
@Singleton
class GitLogoServlet extends HttpServlet {
private static final long MAX_AGE =
TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES);
private static final String CACHE_CTRL =
"public, max-age=" + (MAX_AGE / 1000L);
private final long modified;
private final byte[] raw;
@Inject
GitLogoServlet(final GitWebConfig gitWebConfig) throws IOException {
byte[] png;
if (gitWebConfig.getGitLogoPNG() != null) {
final File src = gitWebConfig.getGitLogoPNG();
if (src != null) {
try {
png = IO.readFully(gitWebConfig.getGitLogoPNG());
png = IO.readFully(src);
} catch (FileNotFoundException e) {
png = null;
}
modified = src.lastModified();
} else {
modified = -1;
png = null;
}
raw = png;
@ -52,8 +63,13 @@ class GitLogoServlet extends HttpServlet {
protected void doGet(final HttpServletRequest req,
final HttpServletResponse rsp) throws IOException {
if (raw != null) {
final long now = System.currentTimeMillis();
rsp.setContentType("image/png");
rsp.setContentLength(raw.length);
rsp.setHeader("Cache-Control", CACHE_CTRL);
rsp.setDateHeader("Date", now);
rsp.setDateHeader("Expires", now + MAX_AGE);
rsp.setDateHeader("Last-Modified", modified);
final ServletOutputStream os = rsp.getOutputStream();
try {

View File

@ -23,6 +23,7 @@ import com.google.inject.Singleton;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
@ -50,6 +51,12 @@ abstract class GitWebCssServlet extends HttpServlet {
}
private static final String ENC = "UTF-8";
private static final long MAX_AGE =
TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES);
private static final String CACHE_CTRL =
"public, max-age=" + (MAX_AGE / 1000L);
private final long modified;
private final byte[] raw_css;
private final byte[] gz_css;
@ -60,22 +67,31 @@ abstract class GitWebCssServlet extends HttpServlet {
final String name = src.getName();
final String raw = HtmlDomUtil.readFile(dir, name);
if (raw != null) {
modified = src.lastModified();
raw_css = raw.getBytes(ENC);
gz_css = HtmlDomUtil.compress(raw_css);
} else {
modified = -1L;
raw_css = null;
gz_css = null;
}
} else {
modified = -1;
raw_css = null;
gz_css = null;
}
}
@Override
protected long getLastModified(final HttpServletRequest req) {
return modified;
}
@Override
protected void doGet(final HttpServletRequest req,
final HttpServletResponse rsp) throws IOException {
if (raw_css != null) {
final long now = System.currentTimeMillis();
rsp.setContentType("text/css");
rsp.setCharacterEncoding(ENC);
final byte[] toSend;
@ -86,6 +102,10 @@ abstract class GitWebCssServlet extends HttpServlet {
toSend = raw_css;
}
rsp.setContentLength(toSend.length);
rsp.setHeader("Cache-Control", CACHE_CTRL);
rsp.setDateHeader("Date", now);
rsp.setDateHeader("Expires", now + MAX_AGE);
rsp.setDateHeader("Last-Modified", modified);
final ServletOutputStream os = rsp.getOutputStream();
try {

View File

@ -20,8 +20,10 @@ import com.google.inject.Singleton;
import org.eclipse.jgit.util.IO;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
@ -31,29 +33,48 @@ import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
@Singleton
class GitWebJavaScriptServlet extends HttpServlet {
private static final long MAX_AGE =
TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES);
private static final String CACHE_CTRL =
"public, max-age=" + (MAX_AGE / 1000L);
private final long modified;
private final byte[] raw;
@Inject
GitWebJavaScriptServlet(final GitWebConfig gitWebConfig) throws IOException {
byte[] png;
if (gitWebConfig.getGitwebJS() != null) {
final File src = gitWebConfig.getGitwebJS();
if (src != null) {
try {
png = IO.readFully(gitWebConfig.getGitwebJS());
png = IO.readFully(src);
} catch (FileNotFoundException e) {
png = null;
}
modified = src.lastModified();
} else {
modified = -1;
png = null;
}
raw = png;
}
@Override
protected long getLastModified(final HttpServletRequest req) {
return modified;
}
@Override
protected void doGet(final HttpServletRequest req,
final HttpServletResponse rsp) throws IOException {
if (raw != null) {
final long now = System.currentTimeMillis();
rsp.setContentType("text/javascript");
rsp.setContentLength(raw.length);
rsp.setHeader("Cache-Control", CACHE_CTRL);
rsp.setDateHeader("Date", now);
rsp.setDateHeader("Expires", now + MAX_AGE);
rsp.setDateHeader("Last-Modified", modified);
final ServletOutputStream os = rsp.getOutputStream();
try {