Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
1606:7c7f28c724e8 | 1607:fa066aaa068c |
---|---|
1 package goodjava.webserver.handlers; | 1 package goodjava.webserver.handlers; |
2 | 2 |
3 import java.util.Map; | 3 import java.util.Map; |
4 import java.util.HashMap; | 4 import java.util.HashMap; |
5 import goodjava.logging.Logger; | |
6 import goodjava.logging.LoggerFactory; | |
5 import goodjava.webserver.Handler; | 7 import goodjava.webserver.Handler; |
6 import goodjava.webserver.Request; | 8 import goodjava.webserver.Request; |
7 import goodjava.webserver.Response; | 9 import goodjava.webserver.Response; |
8 | 10 |
9 | 11 |
10 public class ContentTypeHandler implements Handler { | 12 public class ContentTypeHandler implements Handler { |
11 private final Handler handler; | 13 private static final Logger logger = LoggerFactory.getLogger(ContentTypeHandler.class); |
12 | 14 |
13 // maps extension to content-type | 15 // maps extension to content-type |
14 // key must be lower case | 16 // key must be lower case |
15 public final Map<String,String> map = new HashMap<String,String>(); | 17 public static final Map<String,String> map = new HashMap<String,String>(); |
18 static { | |
19 String textType = "text/plain; charset=utf-8"; | |
20 map.put( "txt", textType ); | |
21 map.put( "luan", textType ); | |
22 map.put( "luano", textType ); | |
23 map.put( "log", textType ); | |
24 map.put( "html", "text/html; charset=utf-8" ); | |
25 map.put( "css", "text/css; charset=utf-8" ); | |
26 map.put( "js", "application/javascript; charset=utf-8" ); | |
27 map.put( "json", "application/json; charset=utf-8" ); | |
28 map.put( "mp4", "video/mp4" ); | |
29 map.put( "jpg", "image/jpeg" ); | |
30 map.put( "jpeg", "image/jpeg" ); | |
31 map.put( "png", "image/png" ); | |
32 // add more as need | |
33 } | |
16 | 34 |
17 // set to null for none | 35 public static String getExtension(String path) { |
18 public String contentTypeForNoExtension; | 36 int iSlash = path.lastIndexOf('/'); |
37 int iDot = path.lastIndexOf('.'); | |
38 return iDot > iSlash ? path.substring(iDot+1).toLowerCase() : null; | |
39 } | |
40 | |
41 private final Handler handler; | |
19 | 42 |
20 public ContentTypeHandler(Handler handler) { | 43 public ContentTypeHandler(Handler handler) { |
21 this(handler,"utf-8"); | |
22 } | |
23 | |
24 public ContentTypeHandler(Handler handler,String charset) { | |
25 this.handler = handler; | 44 this.handler = handler; |
26 String attrs = charset== null ? "" : "; charset="+charset; | |
27 String htmlType = "text/html" + attrs; | |
28 String textType = "text/plain" + attrs; | |
29 contentTypeForNoExtension = htmlType; | |
30 map.put( "html", htmlType ); | |
31 map.put( "txt", textType ); | |
32 map.put( "luan", textType ); | |
33 map.put( "css", "text/css" ); | |
34 map.put( "js", "application/javascript" ); | |
35 map.put( "json", "application/json" + attrs ); | |
36 map.put( "mp4", "video/mp4" ); | |
37 // add more as need | |
38 } | 45 } |
39 | 46 |
40 public Response handle(Request request) { | 47 public Response handle(Request request) { |
41 Response response = handler.handle(request); | 48 Response response = handler.handle(request); |
42 if( response!=null && !response.headers.containsKey("content-type") ) { | 49 if( response!=null && response.status.code==200 && !response.headers.containsKey("Content-Type") ) { |
43 String path = request.path; | 50 String extension = getExtension(request.path); |
44 int iSlash = path.lastIndexOf('/'); | 51 if( extension != null ) { |
45 int iDot = path.lastIndexOf('.'); | 52 String type = map.get(extension); |
46 String type; | 53 if( type != null ) |
47 if( iDot < iSlash ) { // no extension | 54 response.headers.put("Content-Type",type); |
48 type = contentTypeForNoExtension; | 55 else |
49 } else { // extension | 56 logger.info("no type defined for extension: "+extension); |
50 String extension = path.substring(iDot+1); | |
51 type = map.get( extension.toLowerCase() ); | |
52 } | 57 } |
53 if( type != null ) | |
54 response.headers.put("content-type",type); | |
55 } | 58 } |
56 return response; | 59 return response; |
57 } | 60 } |
58 } | 61 } |