Mercurial Hosting > luan
diff src/goodjava/webserver/handlers/ContentTypeHandler.java @ 1607:fa066aaa068c
nginx caching
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 30 Apr 2021 20:23:28 -0600 |
parents | 20375c20289d |
children | fe611f6e3c28 |
line wrap: on
line diff
--- a/src/goodjava/webserver/handlers/ContentTypeHandler.java Tue Apr 20 18:06:50 2021 -0600 +++ b/src/goodjava/webserver/handlers/ContentTypeHandler.java Fri Apr 30 20:23:28 2021 -0600 @@ -2,56 +2,59 @@ import java.util.Map; import java.util.HashMap; +import goodjava.logging.Logger; +import goodjava.logging.LoggerFactory; import goodjava.webserver.Handler; import goodjava.webserver.Request; import goodjava.webserver.Response; public class ContentTypeHandler implements Handler { - private final Handler handler; + private static final Logger logger = LoggerFactory.getLogger(ContentTypeHandler.class); // maps extension to content-type // key must be lower case - public final Map<String,String> map = new HashMap<String,String>(); - - // set to null for none - public String contentTypeForNoExtension; - - public ContentTypeHandler(Handler handler) { - this(handler,"utf-8"); + public static final Map<String,String> map = new HashMap<String,String>(); + static { + String textType = "text/plain; charset=utf-8"; + map.put( "txt", textType ); + map.put( "luan", textType ); + map.put( "luano", textType ); + map.put( "log", textType ); + map.put( "html", "text/html; charset=utf-8" ); + map.put( "css", "text/css; charset=utf-8" ); + map.put( "js", "application/javascript; charset=utf-8" ); + map.put( "json", "application/json; charset=utf-8" ); + map.put( "mp4", "video/mp4" ); + map.put( "jpg", "image/jpeg" ); + map.put( "jpeg", "image/jpeg" ); + map.put( "png", "image/png" ); + // add more as need } - public ContentTypeHandler(Handler handler,String charset) { + public static String getExtension(String path) { + int iSlash = path.lastIndexOf('/'); + int iDot = path.lastIndexOf('.'); + return iDot > iSlash ? path.substring(iDot+1).toLowerCase() : null; + } + + private final Handler handler; + + public ContentTypeHandler(Handler handler) { this.handler = handler; - String attrs = charset== null ? "" : "; charset="+charset; - String htmlType = "text/html" + attrs; - String textType = "text/plain" + attrs; - contentTypeForNoExtension = htmlType; - map.put( "html", htmlType ); - map.put( "txt", textType ); - map.put( "luan", textType ); - map.put( "css", "text/css" ); - map.put( "js", "application/javascript" ); - map.put( "json", "application/json" + attrs ); - map.put( "mp4", "video/mp4" ); - // add more as need } public Response handle(Request request) { Response response = handler.handle(request); - if( response!=null && !response.headers.containsKey("content-type") ) { - String path = request.path; - int iSlash = path.lastIndexOf('/'); - int iDot = path.lastIndexOf('.'); - String type; - if( iDot < iSlash ) { // no extension - type = contentTypeForNoExtension; - } else { // extension - String extension = path.substring(iDot+1); - type = map.get( extension.toLowerCase() ); + if( response!=null && response.status.code==200 && !response.headers.containsKey("Content-Type") ) { + String extension = getExtension(request.path); + if( extension != null ) { + String type = map.get(extension); + if( type != null ) + response.headers.put("Content-Type",type); + else + logger.info("no type defined for extension: "+extension); } - if( type != null ) - response.headers.put("content-type",type); } return response; }