Mercurial Hosting > luan
comparison src/luan/webserver/handlers/ContentTypeHandler.java @ 1165:668f29bc52ea
clean up content-type
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 07 Feb 2018 23:16:12 -0700 |
parents | 49fb4e83484f |
children | 7e7c2d0c3b99 |
comparison
equal
deleted
inserted
replaced
1164:1f9d34a6f308 | 1165:668f29bc52ea |
---|---|
6 import luan.webserver.Request; | 6 import luan.webserver.Request; |
7 import luan.webserver.Response; | 7 import luan.webserver.Response; |
8 | 8 |
9 | 9 |
10 public class ContentTypeHandler implements Handler { | 10 public class ContentTypeHandler implements Handler { |
11 public final static String CONTENT_TYPE = "content-type"; | |
12 | |
13 private final Handler handler; | 11 private final Handler handler; |
14 | 12 |
15 // maps extension to content-type | 13 // maps extension to content-type |
16 // key must be lower case | 14 // key must be lower case |
17 public final Map<String,String> map = new HashMap<String,String>(); | 15 public final Map<String,String> map = new HashMap<String,String>(); |
18 | 16 |
19 // set to null for none | 17 // set to null for none |
20 public String contentTypeForNoExtension; | 18 public String contentTypeForNoExtension; |
21 | 19 |
22 public ContentTypeHandler(Handler handler) { | 20 public ContentTypeHandler(Handler handler) { |
23 this(handler,"UTF-8"); | 21 this(handler,"utf-8"); |
24 } | 22 } |
25 | 23 |
26 public ContentTypeHandler(Handler handler,String charset) { | 24 public ContentTypeHandler(Handler handler,String charset) { |
27 this.handler = handler; | 25 this.handler = handler; |
28 String htmlType = "text/html; charset=" + charset; | 26 String attrs = charset== null ? "" : "; charset="+charset; |
29 String textType = "text/plain; charset=" + charset; | 27 String htmlType = "text/html" + attrs; |
28 String textType = "text/plain" + attrs; | |
30 contentTypeForNoExtension = htmlType; | 29 contentTypeForNoExtension = htmlType; |
31 map.put( "html", htmlType ); | 30 map.put( "html", htmlType ); |
32 map.put( "txt", textType ); | 31 map.put( "txt", textType ); |
33 // add more as need | 32 // add more as need |
34 } | 33 } |
35 | 34 |
36 public Response handle(Request request) { | 35 public Response handle(Request request) { |
37 Response response = handler.handle(request); | 36 Response response = handler.handle(request); |
38 if( response!=null && !response.headers.containsKey(CONTENT_TYPE) ) { | 37 if( response!=null && !response.headers.containsKey("content-type") ) { |
39 String path = request.path; | 38 String path = request.path; |
40 int iSlash = path.lastIndexOf('/'); | 39 int iSlash = path.lastIndexOf('/'); |
41 int iDot = path.lastIndexOf('.'); | 40 int iDot = path.lastIndexOf('.'); |
42 String type; | 41 String type; |
43 if( iDot < iSlash ) { // no extension | 42 if( iDot < iSlash ) { // no extension |
45 } else { // extension | 44 } else { // extension |
46 String extension = path.substring(iDot+1); | 45 String extension = path.substring(iDot+1); |
47 type = map.get( extension.toLowerCase() ); | 46 type = map.get( extension.toLowerCase() ); |
48 } | 47 } |
49 if( type != null ) | 48 if( type != null ) |
50 response.headers.put(CONTENT_TYPE,type); | 49 response.headers.put("content-type",type); |
51 } | 50 } |
52 return response; | 51 return response; |
53 } | 52 } |
54 } | 53 } |