From 9d38bf9b0d5028dea5e2b25e7e610ccf50386f82 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Sat, 19 Jun 2010 19:20:50 +0000 Subject: [PATCH] Mime code by extension retrieval optimized in StaticHandler --- IISMainHandler/handlers/StaticHandler.cs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/IISMainHandler/handlers/StaticHandler.cs b/IISMainHandler/handlers/StaticHandler.cs index 4df06a5..c0ce399 100644 --- a/IISMainHandler/handlers/StaticHandler.cs +++ b/IISMainHandler/handlers/StaticHandler.cs @@ -16,6 +16,23 @@ namespace FLocal.IISHandler.handlers { this.requestParts = requestParts; } + private static Dictionary extension2mime = new Dictionary(); + private static string getMimeByExtension(string extension) { + if(!extension2mime.ContainsKey(extension)) { + lock(extension2mime) { + if(!extension2mime.ContainsKey(extension)) { + RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(extension); + if (regKey != null && regKey.GetValue("Content Type") != null) { + extension2mime[extension] = regKey.GetValue("Content Type").ToString(); + } else { + return null; + } + } + } + } + return extension2mime[extension]; + } + public void Handle(WebContext context) { if(this.requestParts.Length < 2) { throw new HttpException(403, "listing not allowed"); @@ -39,9 +56,9 @@ namespace FLocal.IISHandler.handlers { throw new HttpException(403, "forbidden"); } - RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(fileinfo.Extension); - if (regKey != null && regKey.GetValue("Content Type") != null) { - context.httpresponse.ContentType = regKey.GetValue("Content Type").ToString(); + string mime = getMimeByExtension(fileinfo.Extension); + if(mime != null) { + context.httpresponse.ContentType = mime; } else { throw new HttpException(403, "wrong file type"); }