diff options
Diffstat (limited to 'core/src/main')
3 files changed, 89 insertions, 0 deletions
diff --git a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java index 3b3d019..0d30d9f 100644 --- a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java +++ b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java @@ -58,6 +58,7 @@ import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.net.SocketTimeoutException; +import java.net.URL; import java.net.URLDecoder; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; @@ -68,11 +69,13 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; +import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Properties; import java.util.StringTokenizer; import java.util.TimeZone; import java.util.logging.Level; @@ -1574,6 +1577,45 @@ public abstract class NanoHTTPD { private static final Logger LOG = Logger.getLogger(NanoHTTPD.class.getName()); /** + * Hashtable mapping (String)FILENAME_EXTENSION -> (String)MIME_TYPE + */ + protected static Map<String, String> MIME_TYPES; + + public static Map<String, String> mimeTypes() { + if (MIME_TYPES == null) { + MIME_TYPES = new HashMap<String, String>(); + loadMimeTypes(MIME_TYPES, "META-INF/nanohttpd/default-mimetypes.properties"); + loadMimeTypes(MIME_TYPES, "META-INF/nanohttpd/mimetypes.properties"); + if (MIME_TYPES.isEmpty()) { + LOG.log(Level.WARNING, "no mime types found in the classpath! please provide mimetypes.properties"); + } + } + return MIME_TYPES; + } + + private static void loadMimeTypes(Map<String, String> result, String resourceName) { + try { + Enumeration<URL> resources = NanoHTTPD.class.getClassLoader().getResources(resourceName); + while (resources.hasMoreElements()) { + URL url = (URL) resources.nextElement(); + Properties properties = new Properties(); + InputStream stream = null; + try { + stream = url.openStream(); + properties.load(url.openStream()); + } catch (IOException e) { + LOG.log(Level.SEVERE, "could not load mimetypes from " + url, e); + } finally { + safeClose(stream); + } + result.putAll((Map) properties); + } + } catch (IOException e) { + LOG.log(Level.INFO, "no mime types available at " + resourceName); + } + }; + + /** * Creates an SSLSocketFactory for HTTPS. Pass a loaded KeyStore and an * array of loaded KeyManagers. These objects must properly * loaded/initialized by the caller. @@ -1622,6 +1664,22 @@ public abstract class NanoHTTPD { } } + /** + * Get MIME type from file name extension, if possible + * + * @param uri + * the string representing a file + * @return the connected mime/type + */ + public static String getMimeTypeForFile(String uri) { + int dot = uri.lastIndexOf('.'); + String mime = null; + if (dot >= 0) { + mime = mimeTypes().get(uri.substring(dot + 1).toLowerCase()); + } + return mime == null ? "application/octet-stream" : mime; + } + private static final void safeClose(Object closeable) { try { if (closeable != null) { diff --git a/core/src/main/resources/META-INF/nanohttpd/default-mimetypes.properties b/core/src/main/resources/META-INF/nanohttpd/default-mimetypes.properties new file mode 100644 index 0000000..3fb242f --- /dev/null +++ b/core/src/main/resources/META-INF/nanohttpd/default-mimetypes.properties @@ -0,0 +1,30 @@ +#default mime types for nanohttpd, use META-INF/mimetypes.properties for user defined mimetypes +css=text/css +htm=text/html +html=text/html +xml=text/xml +java=text/x-java-source, text/java +md=text/plain +txt=text/plain +asc=text/plain +gif=image/gif +jpg=image/jpeg +jpeg=image/jpeg +png=image/png +svg=image/svg+xml +mp3=audio/mpeg +m3u=audio/mpeg-url +mp4=video/mp4 +ogv=video/ogg +flv=video/x-flv +mov=video/quicktime +swf=application/x-shockwave-flash +js=application/javascript +pdf=application/pdf +doc=application/msword +ogg=application/x-ogg +zip=application/octet-stream +exe=application/octet-stream +class=application/octet-stream +m3u8=application/vnd.apple.mpegurl +ts=video/mp2t
\ No newline at end of file diff --git a/core/src/main/resources/META-INF/nanohttpd/mimetypes.properties b/core/src/main/resources/META-INF/nanohttpd/mimetypes.properties new file mode 100644 index 0000000..7166a88 --- /dev/null +++ b/core/src/main/resources/META-INF/nanohttpd/mimetypes.properties @@ -0,0 +1 @@ +#mime types for nanohttpd, use a file like this for user defined mimetypes
\ No newline at end of file |