annotate src/goodjava/webserver/handlers/HeadersHandler.java @ 1607:fa066aaa068c

nginx caching
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 30 Apr 2021 20:23:28 -0600
parents
children 268b2a26e8d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.webserver.handlers;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import goodjava.webserver.Handler;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import goodjava.webserver.Request;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import goodjava.webserver.Response;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 public class HeadersHandler implements Handler {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 private final Handler handler;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 public HeadersHandler(Handler handler) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 this.handler = handler;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 public Response handle(Request request) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 Response response = handler.handle(request);
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 if( response!=null ) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 if( response.headers.get("Last-Modified")!=null
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 && response.headers.get("Cache-Control")==null
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 ) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 String contentType = (String)response.headers.get("Content-Type");
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 if( contentType!=null
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 && !contentType.startsWith("image/")
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 && !contentType.startsWith("video/")
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 )
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 response.headers.put("Cache-Control","max-age=3600");
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 if( response.headers.get("Last-Modified")!=null
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 && response.headers.get("X-Accel-Expires")==null
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 )
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 response.headers.put("X-Accel-Expires","1");
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 return response;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 }