2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.http;
21 import java.nio.ByteBuffer;
22 import java.nio.charset.Charset;
23 import java.nio.charset.StandardCharsets;
24 import java.util.Enumeration;
25 import java.util.HashMap;
26 import java.util.HashSet;
28 import java.util.Map.Entry;
29 import java.util.MissingResourceException;
30 import java.util.ResourceBundle;
33 import org.eclipse.jetty.util.ArrayTrie;
34 import org.eclipse.jetty.util.BufferUtil;
35 import org.eclipse.jetty.util.StringUtil;
36 import org.eclipse.jetty.util.Trie;
37 import org.eclipse.jetty.util.log.Log;
38 import org.eclipse.jetty.util.log.Logger;
41 /* ------------------------------------------------------------ */
45 public class MimeTypes
49 FORM_ENCODED("application/x-www-form-urlencoded"),
50 MESSAGE_HTTP("message/http"),
51 MULTIPART_BYTERANGES("multipart/byteranges"),
53 TEXT_HTML("text/html"),
54 TEXT_PLAIN("text/plain"),
56 TEXT_JSON("text/json",StandardCharsets.UTF_8),
57 APPLICATION_JSON("application/json",StandardCharsets.UTF_8),
59 TEXT_HTML_8859_1("text/html; charset=ISO-8859-1",TEXT_HTML),
60 TEXT_HTML_UTF_8("text/html; charset=UTF-8",TEXT_HTML),
62 TEXT_PLAIN_8859_1("text/plain; charset=ISO-8859-1",TEXT_PLAIN),
63 TEXT_PLAIN_UTF_8("text/plain; charset=UTF-8",TEXT_PLAIN),
65 TEXT_XML_8859_1("text/xml; charset=ISO-8859-1",TEXT_XML),
66 TEXT_XML_UTF_8("text/xml; charset=UTF-8",TEXT_XML),
68 TEXT_JSON_8859_1("text/json; charset=ISO-8859-1",TEXT_JSON),
69 TEXT_JSON_UTF_8("text/json; charset=UTF-8",TEXT_JSON),
71 APPLICATION_JSON_8859_1("text/json; charset=ISO-8859-1",APPLICATION_JSON),
72 APPLICATION_JSON_UTF_8("text/json; charset=UTF-8",APPLICATION_JSON);
75 /* ------------------------------------------------------------ */
76 private final String _string;
77 private final Type _base;
78 private final ByteBuffer _buffer;
79 private final Charset _charset;
80 private final boolean _assumedCharset;
81 private final HttpField _field;
83 /* ------------------------------------------------------------ */
87 _buffer=BufferUtil.toBuffer(s);
90 _assumedCharset=false;
91 _field=new HttpGenerator.CachedHttpField(HttpHeader.CONTENT_TYPE,_string);
94 /* ------------------------------------------------------------ */
95 Type(String s,Type base)
98 _buffer=BufferUtil.toBuffer(s);
100 int i=s.indexOf("; charset=");
101 _charset=Charset.forName(s.substring(i+10));
102 _assumedCharset=false;
103 _field=new HttpGenerator.CachedHttpField(HttpHeader.CONTENT_TYPE,_string);
106 /* ------------------------------------------------------------ */
107 Type(String s,Charset cs)
111 _buffer=BufferUtil.toBuffer(s);
113 _assumedCharset=true;
114 _field=new HttpGenerator.CachedHttpField(HttpHeader.CONTENT_TYPE,_string);
117 /* ------------------------------------------------------------ */
118 public ByteBuffer asBuffer()
120 return _buffer.asReadOnlyBuffer();
123 /* ------------------------------------------------------------ */
124 public Charset getCharset()
129 /* ------------------------------------------------------------ */
130 public boolean is(String s)
132 return _string.equalsIgnoreCase(s);
135 /* ------------------------------------------------------------ */
136 public String asString()
141 /* ------------------------------------------------------------ */
143 public String toString()
148 /* ------------------------------------------------------------ */
149 public boolean isCharsetAssumed()
151 return _assumedCharset;
154 /* ------------------------------------------------------------ */
155 public HttpField getContentTypeField()
160 /* ------------------------------------------------------------ */
161 public Type getBaseType()
167 /* ------------------------------------------------------------ */
168 private static final Logger LOG = Log.getLogger(MimeTypes.class);
169 public final static Trie<MimeTypes.Type> CACHE= new ArrayTrie<>(512);
170 private final static Trie<ByteBuffer> TYPES= new ArrayTrie<ByteBuffer>(512);
171 private final static Map<String,String> __dftMimeMap = new HashMap<String,String>();
172 private final static Map<String,String> __encodings = new HashMap<String,String>();
176 for (MimeTypes.Type type : MimeTypes.Type.values())
178 CACHE.put(type.toString(),type);
179 TYPES.put(type.toString(),type.asBuffer());
181 int charset=type.toString().indexOf(";charset=");
184 CACHE.put(type.toString().replace(";charset=","; charset="),type);
185 TYPES.put(type.toString().replace(";charset=","; charset="),type.asBuffer());
191 ResourceBundle mime = ResourceBundle.getBundle("org/eclipse/jetty/http/mime");
192 Enumeration<String> i = mime.getKeys();
193 while(i.hasMoreElements())
195 String ext = i.nextElement();
196 String m = mime.getString(ext);
197 __dftMimeMap.put(StringUtil.asciiToLowerCase(ext),normalizeMimeType(m));
200 catch(MissingResourceException e)
202 LOG.warn(e.toString());
208 ResourceBundle encoding = ResourceBundle.getBundle("org/eclipse/jetty/http/encoding");
209 Enumeration<String> i = encoding.getKeys();
210 while(i.hasMoreElements())
212 String type = i.nextElement();
213 __encodings.put(type,encoding.getString(type));
216 catch(MissingResourceException e)
218 LOG.warn(e.toString());
224 /* ------------------------------------------------------------ */
225 private final Map<String,String> _mimeMap=new HashMap<String,String>();
227 /* ------------------------------------------------------------ */
234 /* ------------------------------------------------------------ */
235 public synchronized Map<String,String> getMimeMap()
240 /* ------------------------------------------------------------ */
242 * @param mimeMap A Map of file extension to mime-type.
244 public void setMimeMap(Map<String,String> mimeMap)
249 for (Entry<String, String> ext : mimeMap.entrySet())
250 _mimeMap.put(StringUtil.asciiToLowerCase(ext.getKey()),normalizeMimeType(ext.getValue()));
254 /* ------------------------------------------------------------ */
255 /** Get the MIME type by filename extension.
256 * @param filename A file name
257 * @return MIME type matching the longest dot extension of the
260 public String getMimeByExtension(String filename)
269 i=filename.indexOf(".",i+1);
271 if (i<0 || i>=filename.length())
274 String ext=StringUtil.asciiToLowerCase(filename.substring(i+1));
276 type=_mimeMap.get(ext);
278 type=__dftMimeMap.get(ext);
285 type=_mimeMap.get("*");
287 type=__dftMimeMap.get("*");
293 /* ------------------------------------------------------------ */
294 /** Set a mime mapping
298 public void addMimeMapping(String extension,String type)
300 _mimeMap.put(StringUtil.asciiToLowerCase(extension),normalizeMimeType(type));
303 /* ------------------------------------------------------------ */
304 public static Set<String> getKnownMimeTypes()
306 return new HashSet<>(__dftMimeMap.values());
309 /* ------------------------------------------------------------ */
310 private static String normalizeMimeType(String type)
312 MimeTypes.Type t =CACHE.get(type);
316 return StringUtil.asciiToLowerCase(type);
319 /* ------------------------------------------------------------ */
320 public static String getCharsetFromContentType(String value)
324 int end=value.length();
331 char b = value.charAt(i);
333 if (quote && state!=10)
352 case 1: if ('c'==b) state=2; else if (' '!=b) state=0; break;
353 case 2: if ('h'==b) state=3; else state=0;break;
354 case 3: if ('a'==b) state=4; else state=0;break;
355 case 4: if ('r'==b) state=5; else state=0;break;
356 case 5: if ('s'==b) state=6; else state=0;break;
357 case 6: if ('e'==b) state=7; else state=0;break;
358 case 7: if ('t'==b) state=8; else state=0;break;
360 case 8: if ('='==b) state=9; else if (' '!=b) state=0; break;
377 if (!quote && (';'==b || ' '==b )||
379 return StringUtil.normalizeCharset(value,start,i-start);
384 return StringUtil.normalizeCharset(value,start,i-start);
389 public static String inferCharsetFromContentType(String value)
391 return __encodings.get(value);
394 public static String getContentTypeWithoutCharset(String value)
396 int end=value.length();
401 StringBuilder builder=null;
404 char b = value.charAt(i);
420 builder.append(b);break;
424 builder=new StringBuilder();
425 builder.append(value,0,start+1);
437 if (builder!=null && state!=10)
451 case 1: if ('c'==b) state=2; else if (' '!=b) state=0; break;
452 case 2: if ('h'==b) state=3; else state=0;break;
453 case 3: if ('a'==b) state=4; else state=0;break;
454 case 4: if ('r'==b) state=5; else state=0;break;
455 case 5: if ('s'==b) state=6; else state=0;break;
456 case 6: if ('e'==b) state=7; else state=0;break;
457 case 7: if ('t'==b) state=8; else state=0;break;
458 case 8: if ('='==b) state=9; else if (' '!=b) state=0; break;
463 builder=new StringBuilder();
464 builder.append(value,0,start+1);
482 return builder.toString();