diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/webserver/handlers/HeadersHandler.java	Fri Apr 30 20:23:28 2021 -0600
@@ -0,0 +1,35 @@
+package goodjava.webserver.handlers;
+
+import goodjava.webserver.Handler;
+import goodjava.webserver.Request;
+import goodjava.webserver.Response;
+
+
+public class HeadersHandler implements Handler {
+	private final Handler handler;
+
+	public HeadersHandler(Handler handler) {
+		this.handler = handler;
+	}
+
+	public Response handle(Request request) {
+		Response response = handler.handle(request);
+		if( response!=null ) {
+			if( response.headers.get("Last-Modified")!=null
+				&& response.headers.get("Cache-Control")==null
+			) {
+				String contentType = (String)response.headers.get("Content-Type");
+				if( contentType!=null
+					&& !contentType.startsWith("image/")
+					&& !contentType.startsWith("video/")
+				)
+					response.headers.put("Cache-Control","max-age=3600");
+			}
+			if( response.headers.get("Last-Modified")!=null
+				&& response.headers.get("X-Accel-Expires")==null
+			)
+				response.headers.put("X-Accel-Expires","1");
+		}
+		return response;
+	}
+}