view src/goodjava/webserver/handlers/HeadersHandler.java @ 1648:224af797b1f9

Mainly small install script improvements - Consistent usage of `$LUANHOME`, removed reliance on current directory. - Made Luan build and install fine (on Linux) without requiring launching it via sudo. Only asks to elevate privileges if installation failed. - Minor spelling mistake fix.
author Fox
date Mon, 28 Mar 2022 18:00:12 +0200
parents 268b2a26e8d7
children
line wrap: on
line source

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");
			if( response.headers.get("Access-Control-Allow-Origin")==null )
				response.headers.put("Access-Control-Allow-Origin","*");
		}
		return response;
	}
}