playframework / playframework/play1

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

Open
#1,206 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.6k
Forks
671
Avg merge
12d 15h
Merged PRs (30d)
1

Description

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:
https://github.com/playframework/play1/blob/b90a6770427b8ec19149e4ae3af7bbf19b25ca8f/framework/src/play/server/PlayHandler.java#L873-L883
ServletWrapper.java only reads the configuration for "serveStatic", but not in copyResponse:
https://github.com/playframework/play1/blob/b90a6770427b8ec19149e4ae3af7bbf19b25ca8f/framework/src/play/server/ServletWrapper.java#L436-L438

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in framework/src/play/server/ServletWrapper.java at copyResponse and compare its handling with the http.cacheControl logic in framework/src/play/server/PlayHandler.java. Reproduce renderBinary(file) from a WAR on Jetty or Tomcat, then verify that the response honors the configured cache duration instead of always sending no-cache.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.