Mercurial Hosting > luan
changeset 1023:27f3dc761452
MimeTypes uses StringCache
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 01 Nov 2016 03:44:46 -0600 |
parents | 3718afd99988 |
children | be50ffc0ad5c |
files | src/org/eclipse/jetty/http/MimeTypes.java src/org/eclipse/jetty/http/encoding.properties src/org/eclipse/jetty/io/StringCache.java src/org/eclipse/jetty/server/AbstractHttpConnection.java src/org/eclipse/jetty/server/Response.java src/org/eclipse/jetty/server/handler/ResourceHandler.java |
diffstat | 6 files changed, 116 insertions(+), 159 deletions(-) [+] |
line wrap: on
line diff
diff -r 3718afd99988 -r 27f3dc761452 src/org/eclipse/jetty/http/MimeTypes.java --- a/src/org/eclipse/jetty/http/MimeTypes.java Tue Nov 01 01:04:46 2016 -0600 +++ b/src/org/eclipse/jetty/http/MimeTypes.java Tue Nov 01 03:44:46 2016 -0600 @@ -26,18 +26,13 @@ import java.util.ResourceBundle; import org.eclipse.jetty.io.Buffer; -import org.eclipse.jetty.io.BufferCache; -import org.eclipse.jetty.io.BufferCache.CachedBuffer; +import org.eclipse.jetty.io.StringCache; import org.eclipse.jetty.util.StringUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/* ------------------------------------------------------------ */ -/** - * - */ -public class MimeTypes +public final class MimeTypes { private static final Logger LOG = LoggerFactory.getLogger(MimeTypes.class); @@ -70,13 +65,13 @@ TEXT_JSON__UTF_8="text/json; charset=UTF-8"; private final static int - FORM_ENCODED_ORDINAL=1, + FORM_ENCODED_ORDINAL=1, MESSAGE_HTTP_ORDINAL=2, MULTIPART_BYTERANGES_ORDINAL=3, TEXT_HTML_ORDINAL=4, - TEXT_PLAIN_ORDINAL=5, - TEXT_XML_ORDINAL=6, + TEXT_PLAIN_ORDINAL=5, + TEXT_XML_ORDINAL=6, TEXT_JSON_ORDINAL=7, TEXT_HTML_8859_1_ORDINAL=8, @@ -90,41 +85,39 @@ private static int __index=15; - public final static BufferCache CACHE = new BufferCache(); + public final static StringCache CACHE = new StringCache(); - public final static CachedBuffer - FORM_ENCODED_BUFFER=CACHE.add(FORM_ENCODED,FORM_ENCODED_ORDINAL), - MESSAGE_HTTP_BUFFER=CACHE.add(MESSAGE_HTTP, MESSAGE_HTTP_ORDINAL), - MULTIPART_BYTERANGES_BUFFER=CACHE.add(MULTIPART_BYTERANGES,MULTIPART_BYTERANGES_ORDINAL), + public final static byte[] + FORM_ENCODED_BYTES=CACHE.add(FORM_ENCODED,FORM_ENCODED_ORDINAL), + MESSAGE_HTTP_BYTES=CACHE.add(MESSAGE_HTTP, MESSAGE_HTTP_ORDINAL), + MULTIPART_BYTERANGES_BYTES=CACHE.add(MULTIPART_BYTERANGES,MULTIPART_BYTERANGES_ORDINAL), - TEXT_HTML_BUFFER=CACHE.add(TEXT_HTML,TEXT_HTML_ORDINAL), - TEXT_PLAIN_BUFFER=CACHE.add(TEXT_PLAIN,TEXT_PLAIN_ORDINAL), - TEXT_XML_BUFFER=CACHE.add(TEXT_XML,TEXT_XML_ORDINAL), - TEXT_JSON_BUFFER=CACHE.add(TEXT_JSON,TEXT_JSON_ORDINAL), + TEXT_HTML_BYTES=CACHE.add(TEXT_HTML,TEXT_HTML_ORDINAL), + TEXT_PLAIN_BYTES=CACHE.add(TEXT_PLAIN,TEXT_PLAIN_ORDINAL), + TEXT_XML_BYTES=CACHE.add(TEXT_XML,TEXT_XML_ORDINAL), + TEXT_JSON_BYTES=CACHE.add(TEXT_JSON,TEXT_JSON_ORDINAL), - TEXT_HTML_8859_1_BUFFER=CACHE.add(TEXT_HTML_8859_1,TEXT_HTML_8859_1_ORDINAL), - TEXT_PLAIN_8859_1_BUFFER=CACHE.add(TEXT_PLAIN_8859_1,TEXT_PLAIN_8859_1_ORDINAL), - TEXT_XML_8859_1_BUFFER=CACHE.add(TEXT_XML_8859_1,TEXT_XML_8859_1_ORDINAL), + TEXT_HTML_8859_1_BYTES=CACHE.add(TEXT_HTML_8859_1,TEXT_HTML_8859_1_ORDINAL), + TEXT_PLAIN_8859_1_BYTES=CACHE.add(TEXT_PLAIN_8859_1,TEXT_PLAIN_8859_1_ORDINAL), + TEXT_XML_8859_1_BYTES=CACHE.add(TEXT_XML_8859_1,TEXT_XML_8859_1_ORDINAL), - TEXT_HTML_UTF_8_BUFFER=CACHE.add(TEXT_HTML_UTF_8,TEXT_HTML_UTF_8_ORDINAL), - TEXT_PLAIN_UTF_8_BUFFER=CACHE.add(TEXT_PLAIN_UTF_8,TEXT_PLAIN_UTF_8_ORDINAL), - TEXT_XML_UTF_8_BUFFER=CACHE.add(TEXT_XML_UTF_8,TEXT_XML_UTF_8_ORDINAL), - TEXT_JSON_UTF_8_BUFFER=CACHE.add(TEXT_JSON_UTF_8,TEXT_JSON_UTF_8_ORDINAL), + TEXT_HTML_UTF_8_BYTES=CACHE.add(TEXT_HTML_UTF_8,TEXT_HTML_UTF_8_ORDINAL), + TEXT_PLAIN_UTF_8_BYTES=CACHE.add(TEXT_PLAIN_UTF_8,TEXT_PLAIN_UTF_8_ORDINAL), + TEXT_XML_UTF_8_BYTES=CACHE.add(TEXT_XML_UTF_8,TEXT_XML_UTF_8_ORDINAL), + TEXT_JSON_UTF_8_BYTES=CACHE.add(TEXT_JSON_UTF_8,TEXT_JSON_UTF_8_ORDINAL), - TEXT_HTML__8859_1_BUFFER=CACHE.add(TEXT_HTML__8859_1,TEXT_HTML_8859_1_ORDINAL), - TEXT_PLAIN__8859_1_BUFFER=CACHE.add(TEXT_PLAIN__8859_1,TEXT_PLAIN_8859_1_ORDINAL), - TEXT_XML__8859_1_BUFFER=CACHE.add(TEXT_XML__8859_1,TEXT_XML_8859_1_ORDINAL), + TEXT_HTML__8859_1_BYTES=CACHE.add(TEXT_HTML__8859_1,TEXT_HTML_8859_1_ORDINAL), + TEXT_PLAIN__8859_1_BYTES=CACHE.add(TEXT_PLAIN__8859_1,TEXT_PLAIN_8859_1_ORDINAL), + TEXT_XML__8859_1_BYTES=CACHE.add(TEXT_XML__8859_1,TEXT_XML_8859_1_ORDINAL), - TEXT_HTML__UTF_8_BUFFER=CACHE.add(TEXT_HTML__UTF_8,TEXT_HTML_UTF_8_ORDINAL), - TEXT_PLAIN__UTF_8_BUFFER=CACHE.add(TEXT_PLAIN__UTF_8,TEXT_PLAIN_UTF_8_ORDINAL), - TEXT_XML__UTF_8_BUFFER=CACHE.add(TEXT_XML__UTF_8,TEXT_XML_UTF_8_ORDINAL), - TEXT_JSON__UTF_8_BUFFER=CACHE.add(TEXT_JSON__UTF_8,TEXT_JSON_UTF_8_ORDINAL); + TEXT_HTML__UTF_8_BYTES=CACHE.add(TEXT_HTML__UTF_8,TEXT_HTML_UTF_8_ORDINAL), + TEXT_PLAIN__UTF_8_BYTES=CACHE.add(TEXT_PLAIN__UTF_8,TEXT_PLAIN_UTF_8_ORDINAL), + TEXT_XML__UTF_8_BYTES=CACHE.add(TEXT_XML__UTF_8,TEXT_XML_UTF_8_ORDINAL), + TEXT_JSON__UTF_8_BYTES=CACHE.add(TEXT_JSON__UTF_8,TEXT_JSON_UTF_8_ORDINAL); - /* ------------------------------------------------------------ */ - /* ------------------------------------------------------------ */ - private final static Map __dftMimeMap = new HashMap(); - private final static Map __encodings = new HashMap(); + private final static Map<String,String> __dftMimeMap = new HashMap<String,String>(); + private final static Map<String,String> __encodings = new HashMap<String,String>(); static { try @@ -135,7 +128,8 @@ { String ext = (String)i.nextElement(); String m = mime.getString(ext); - __dftMimeMap.put(StringUtil.asciiToLowerCase(ext),normalizeMimeType(m)); +// __dftMimeMap.put(ext.toLowerCase(),normalizeMimeType(m)); + __dftMimeMap.put(ext.toLowerCase(),m); } } catch(MissingResourceException e) @@ -144,23 +138,12 @@ LOG.debug("",e); } - try - { - ResourceBundle encoding = ResourceBundle.getBundle("org/eclipse/jetty/http/encoding"); - Enumeration i = encoding.getKeys(); - while(i.hasMoreElements()) - { - Buffer type = normalizeMimeType((String)i.nextElement()); - __encodings.put(type,encoding.getString(type.toString())); - } - } - catch(MissingResourceException e) - { - LOG.warn(e.toString()); - LOG.debug("",e); - } + __encodings.put("text/html","ISO-8859-1"); + __encodings.put("text/plain","ISO-8859-1"); + __encodings.put("text/xml","UTF-8"); + __encodings.put("text/json","UTF-8"); - +/* TEXT_HTML_BUFFER.setAssociate("ISO-8859-1",TEXT_HTML_8859_1_BUFFER); TEXT_HTML_BUFFER.setAssociate("ISO_8859_1",TEXT_HTML_8859_1_BUFFER); TEXT_HTML_BUFFER.setAssociate("iso-8859-1",TEXT_HTML_8859_1_BUFFER); @@ -187,56 +170,54 @@ TEXT_JSON_BUFFER.setAssociate("UTF8",TEXT_JSON_UTF_8_BUFFER); TEXT_JSON_BUFFER.setAssociate("utf8",TEXT_JSON_UTF_8_BUFFER); TEXT_JSON_BUFFER.setAssociate("utf-8",TEXT_JSON_UTF_8_BUFFER); +*/ + } + + // replace setAssociate() + + private static final Map<String,String> association = new HashMap<String,String>(); + + private static String key(String type,String charEncoding) { + return (type+'~'+charEncoding).toLowerCase(); + } + + private static void setAssociate(String type,String charEncoding,String result) { + association.put( key(type,charEncoding), result ); + } + + public static String getAssociate(String type,String charEncoding) { + return association.get( key(type,charEncoding) ); + } + + static { + setAssociate(TEXT_HTML,"ISO-8859-1",TEXT_HTML_8859_1); + setAssociate(TEXT_HTML,"ISO_8859_1",TEXT_HTML_8859_1); + setAssociate(TEXT_PLAIN,"ISO-8859-1",TEXT_PLAIN_8859_1); + setAssociate(TEXT_PLAIN,"ISO_8859_1",TEXT_PLAIN_8859_1); + setAssociate(TEXT_XML,"ISO-8859-1",TEXT_XML_8859_1); + setAssociate(TEXT_XML,"ISO_8859_1",TEXT_XML_8859_1); + + setAssociate(TEXT_HTML,"UTF-8",TEXT_HTML_UTF_8); + setAssociate(TEXT_HTML,"UTF8",TEXT_HTML_UTF_8); + setAssociate(TEXT_PLAIN,"UTF-8",TEXT_PLAIN_UTF_8); + setAssociate(TEXT_PLAIN,"UTF8",TEXT_PLAIN_UTF_8); + setAssociate(TEXT_XML,"UTF-8",TEXT_XML_UTF_8); + setAssociate(TEXT_XML,"UTF8",TEXT_XML_UTF_8); + setAssociate(TEXT_JSON,"UTF-8",TEXT_JSON_UTF_8); + setAssociate(TEXT_JSON,"UTF8",TEXT_JSON_UTF_8); } - /* ------------------------------------------------------------ */ - private Map _mimeMap; /* ------------------------------------------------------------ */ - /** Constructor. - */ - public MimeTypes() - { - } - - /* ------------------------------------------------------------ */ - public synchronized Map getMimeMap() - { - return _mimeMap; - } - - /* ------------------------------------------------------------ */ - /** - * @param mimeMap A Map of file extension to mime-type. - */ - public void setMimeMap(Map mimeMap) - { - if (mimeMap==null) - { - _mimeMap=null; - return; - } - - Map m=new HashMap(); - Iterator i=mimeMap.entrySet().iterator(); - while (i.hasNext()) - { - Map.Entry entry = (Map.Entry)i.next(); - m.put(entry.getKey(),normalizeMimeType(entry.getValue().toString())); - } - _mimeMap=m; - } - - /* ------------------------------------------------------------ */ /** Get the MIME type by filename extension. * @param filename A file name * @return MIME type matching the longest dot extension of the * file name. */ - public Buffer getMimeByExtension(String filename) + public String getMimeByExtension(String filename) { - Buffer type=null; + String type=null; if (filename!=null) { @@ -248,75 +229,51 @@ if (i<0 || i>=filename.length()) break; - String ext=StringUtil.asciiToLowerCase(filename.substring(i+1)); - if (_mimeMap!=null) - type = (Buffer)_mimeMap.get(ext); - if (type==null) - type=(Buffer)__dftMimeMap.get(ext); + String ext = filename.substring(i+1).toLowerCase(); + type = __dftMimeMap.get(ext); } } if (type==null) { - if (_mimeMap!=null) - type=(Buffer)_mimeMap.get("*"); - if (type==null) - type=(Buffer)__dftMimeMap.get("*"); + type = __dftMimeMap.get("*"); } return type; } - - /* ------------------------------------------------------------ */ - /** Set a mime mapping - * @param extension - * @param type - */ - public void addMimeMapping(String extension,String type) - { - if (_mimeMap==null) - _mimeMap=new HashMap(); - - _mimeMap.put(StringUtil.asciiToLowerCase(extension),normalizeMimeType(type)); - } - - /* ------------------------------------------------------------ */ +/* private static synchronized Buffer normalizeMimeType(String type) { - Buffer b =CACHE.get(type); + Buffer b = CACHE.get(type); if (b==null) - b=CACHE.add(type,__index++); + b = CACHE.add(type,__index++); return b; } - - /* ------------------------------------------------------------ */ - public static String getCharsetFromContentType(Buffer value) +*/ + public static String getCharsetFromContentType(String value) { - if (value instanceof CachedBuffer) + switch(CACHE.getOrdinal(value)) { - switch(((CachedBuffer)value).getOrdinal()) - { - case TEXT_HTML_8859_1_ORDINAL: - case TEXT_PLAIN_8859_1_ORDINAL: - case TEXT_XML_8859_1_ORDINAL: - return StringUtil.__ISO_8859_1; + case TEXT_HTML_8859_1_ORDINAL: + case TEXT_PLAIN_8859_1_ORDINAL: + case TEXT_XML_8859_1_ORDINAL: + return StringUtil.__ISO_8859_1; - case TEXT_HTML_UTF_8_ORDINAL: - case TEXT_PLAIN_UTF_8_ORDINAL: - case TEXT_XML_UTF_8_ORDINAL: - case TEXT_JSON_UTF_8_ORDINAL: - return StringUtil.__UTF8; - } + case TEXT_HTML_UTF_8_ORDINAL: + case TEXT_PLAIN_UTF_8_ORDINAL: + case TEXT_XML_UTF_8_ORDINAL: + case TEXT_JSON_UTF_8_ORDINAL: + return StringUtil.__UTF8; } - int i=value.getIndex(); - int end=value.putIndex(); + int i = 0; + int end = value.length(); int state=0; int start=0; boolean quote=false; for (;i<end;i++) { - byte b = value.peek(i); + char b = value.charAt(i); if (quote && state!=10) { @@ -364,13 +321,13 @@ case 10: if (!quote && (';'==b || ' '==b )|| (quote && '"'==b )) - return CACHE.lookup(value.peek(start,i-start)).toString(StringUtil.__UTF8); + return value.substring(start,i); } } if (state==10) - return CACHE.lookup(value.peek(start,i-start)).toString(StringUtil.__UTF8); + return value.substring(start,i); - return (String)__encodings.get(value); + return __encodings.get(value.toLowerCase()); } }
diff -r 3718afd99988 -r 27f3dc761452 src/org/eclipse/jetty/http/encoding.properties --- a/src/org/eclipse/jetty/http/encoding.properties Tue Nov 01 01:04:46 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,4 +0,0 @@ -text/html = ISO-8859-1 -text/plain = ISO-8859-1 -text/xml = UTF-8 -text/json = UTF-8
diff -r 3718afd99988 -r 27f3dc761452 src/org/eclipse/jetty/io/StringCache.java --- a/src/org/eclipse/jetty/io/StringCache.java Tue Nov 01 01:04:46 2016 -0600 +++ b/src/org/eclipse/jetty/io/StringCache.java Tue Nov 01 03:44:46 2016 -0600 @@ -30,6 +30,11 @@ return stringToOrdinal.containsKey(value.toLowerCase()); } + public String get(String value) + { + return stringMap.get(value.toLowerCase()); + } + public String getBest(String key) { key = key.toLowerCase(); String rtn = stringMap.get(key);
diff -r 3718afd99988 -r 27f3dc761452 src/org/eclipse/jetty/server/AbstractHttpConnection.java --- a/src/org/eclipse/jetty/server/AbstractHttpConnection.java Tue Nov 01 01:04:46 2016 -0600 +++ b/src/org/eclipse/jetty/server/AbstractHttpConnection.java Tue Nov 01 03:44:46 2016 -0600 @@ -552,7 +552,7 @@ case HttpHeaders.CONTENT_TYPE_ORDINAL: // value = MimeTypes.CACHE.lookup(value); - _charset=MimeTypes.getCharsetFromContentType(new ByteArrayBuffer(value)); + _charset = MimeTypes.getCharsetFromContentType(value); break; }
diff -r 3718afd99988 -r 27f3dc761452 src/org/eclipse/jetty/server/Response.java --- a/src/org/eclipse/jetty/server/Response.java Tue Nov 01 01:04:46 2016 -0600 +++ b/src/org/eclipse/jetty/server/Response.java Tue Nov 01 03:44:46 2016 -0600 @@ -42,7 +42,6 @@ import org.eclipse.jetty.http.HttpURI; import org.eclipse.jetty.http.HttpVersions; import org.eclipse.jetty.http.MimeTypes; -import org.eclipse.jetty.io.BufferCache.CachedBuffer; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.util.ByteArrayISO8859Writer; import org.eclipse.jetty.util.QuotedStringTokenizer; @@ -84,7 +83,7 @@ private String _reason; private Locale _locale; private String _mimeType; - private CachedBuffer _cachedMimeType; + private String _cachedMimeType; private String _characterEncoding; private boolean _explicitEncoding; private String _contentType; @@ -481,7 +480,7 @@ { _characterEncoding=null; if (_cachedMimeType!=null) - _contentType = _cachedMimeType.toString(); + _contentType = _cachedMimeType; else if (_mimeType!=null) _contentType = _mimeType; else @@ -505,10 +504,10 @@ _contentType=null; if(_cachedMimeType!=null) { - CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); + String content_type = MimeTypes.getAssociate(_cachedMimeType,_characterEncoding); if (content_type!=null) { - _contentType = content_type.toString(); + _contentType = content_type; _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); } } @@ -620,10 +619,10 @@ { if (_cachedMimeType!=null) { - CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); + String content_type = MimeTypes.getAssociate(_cachedMimeType,_characterEncoding); if (content_type!=null) { - _contentType=content_type.toString(); + _contentType=content_type; _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); } else @@ -657,10 +656,10 @@ if (_cachedMimeType!=null) { - CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); + String content_type = MimeTypes.getAssociate(_cachedMimeType,_characterEncoding); if (content_type!=null) { - _contentType=content_type.toString(); + _contentType=content_type; _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); } else @@ -704,10 +703,10 @@ { if (_cachedMimeType!=null) { - CachedBuffer content_type = _cachedMimeType.getAssociate(_characterEncoding); + String content_type = MimeTypes.getAssociate(_cachedMimeType,_characterEncoding); if (content_type!=null) { - _contentType=content_type.toString(); + _contentType=content_type; _connection._responseFields.put(HttpHeaders.CONTENT_TYPE,_contentType); } else
diff -r 3718afd99988 -r 27f3dc761452 src/org/eclipse/jetty/server/handler/ResourceHandler.java --- a/src/org/eclipse/jetty/server/handler/ResourceHandler.java Tue Nov 01 01:04:46 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/ResourceHandler.java Tue Nov 01 03:44:46 2016 -0600 @@ -385,12 +385,12 @@ } } - Buffer mime=_mimeTypes.getMimeByExtension(resource.toString()); + String mime = _mimeTypes.getMimeByExtension(resource.toString()); if (mime==null) mime=_mimeTypes.getMimeByExtension(request.getPathInfo()); // set the headers - doResponseHeaders(response,resource,mime!=null?mime.toString():null); + doResponseHeaders(response,resource,mime); response.setDateHeader(HttpHeaders.LAST_MODIFIED,last_modified); if(skipContentBody)