comparison 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
comparison
equal deleted inserted replaced
1606:7c7f28c724e8 1607:fa066aaa068c
1 package goodjava.webserver.handlers;
2
3 import goodjava.webserver.Handler;
4 import goodjava.webserver.Request;
5 import goodjava.webserver.Response;
6
7
8 public class HeadersHandler implements Handler {
9 private final Handler handler;
10
11 public HeadersHandler(Handler handler) {
12 this.handler = handler;
13 }
14
15 public Response handle(Request request) {
16 Response response = handler.handle(request);
17 if( response!=null ) {
18 if( response.headers.get("Last-Modified")!=null
19 && response.headers.get("Cache-Control")==null
20 ) {
21 String contentType = (String)response.headers.get("Content-Type");
22 if( contentType!=null
23 && !contentType.startsWith("image/")
24 && !contentType.startsWith("video/")
25 )
26 response.headers.put("Cache-Control","max-age=3600");
27 }
28 if( response.headers.get("Last-Modified")!=null
29 && response.headers.get("X-Accel-Expires")==null
30 )
31 response.headers.put("X-Accel-Expires","1");
32 }
33 return response;
34 }
35 }