annotate src/goodjava/webserver/handlers/HeadersHandler.java @ 1810:3c43b07e12b7

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 12 May 2024 22:00:42 -0600
parents 268b2a26e8d7
children
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");
1609
268b2a26e8d7 minor - cors
Franklin Schmidt <fschmidt@gmail.com>
parents: 1607
diff changeset
32 if( response.headers.get("Access-Control-Allow-Origin")==null )
268b2a26e8d7 minor - cors
Franklin Schmidt <fschmidt@gmail.com>
parents: 1607
diff changeset
33 response.headers.put("Access-Control-Allow-Origin","*");
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 return response;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 }