New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

renderBinary responses always has "no-cache" headers when packaged as WAR #1206

Open
fnicollet opened this Issue Nov 15, 2017 · 0 comments

Comments

Projects
None yet
1 participant
@fnicollet

fnicollet commented Nov 15, 2017

Hello,

We noticed that renderBinary(file) behaves differently when Play is run via the Play server and when it is packaged as WAR and used in an app server (Jetty or Tomcat).
The documentation indicates:

# HTTP Response headers control for static files
# ~~~~~
# Set the default max-age, telling the user's browser how long it should cache the page.
# Default is 3600 (one hour). Set it to 0 to send no-cache.
# This is only read in prod mode, in dev mode the cache is disabled.
# http.cacheControl=3600

The behaviour is correct when running Play in prod mode, the Cache-Control headers are correctly added.
When using a WAR, the Cache-Control header in the response is always "no-cache", which prevent the browser from caching the file

The code to reproduce the issue is quite simple

File file = new File(Play.applicationPath, "public/javascripts/jquery-1.6.4.min.js");
renderBinary(file);

The difference in behaviour comes from the fact that the code for serving binary file uses PlayHandler.java when using the Play server, and ServletWrapper.java when using a WAR file.
While PlayHandler makes an effort to use the values in the configuration file:

private static HttpResponse addEtag(HttpRequest nettyRequest, HttpResponse httpResponse, File file) {
if (Play.mode == Play.Mode.DEV) {
httpResponse.setHeader(CACHE_CONTROL, "no-cache");
} else {
String maxAge = Play.configuration.getProperty("http.cacheControl", "3600");
if (maxAge.equals("0")) {
httpResponse.setHeader(CACHE_CONTROL, "no-cache");
} else {
httpResponse.setHeader(CACHE_CONTROL, "max-age=" + maxAge);
}
}

ServletWrapper.java only reads the configuration for "serveStatic", but not in copyResponse:
if (!response.headers.containsKey("cache-control")) {
servletResponse.setHeader("Cache-Control", "no-cache");
}

There is a simple workaround, just by using cacheFor on the response

long last = file.lastModified();
String etag = "\"" + last + "-" + file.hashCode() + "\"";
response.cacheFor(etag, "30d", last);
renderBinary(file);

But I believe it should be fixed in ServletWrapper directly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment