diff --git a/WebServer/WebServer.cs b/WebServer/WebServer.cs index f323899..43055ed 100644 --- a/WebServer/WebServer.cs +++ b/WebServer/WebServer.cs @@ -418,38 +418,8 @@ public static void OutputHttpCode(HttpListenerResponse response, HttpStatusCode /// public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile strFilePath) { - string ContentType = "text/html"; - //determine the type of file for the http header - if (strFilePath.FileType.ToLower() == ".cs" || - strFilePath.FileType.ToLower() == ".txt" || - strFilePath.FileType.ToLower() == ".csproj" - ) - { - ContentType = "text/plain"; - } - else if (strFilePath.FileType.ToLower() == ".jpg" || - strFilePath.FileType.ToLower() == ".bmp" || - strFilePath.FileType.ToLower() == ".jpeg" || - strFilePath.FileType.ToLower() == ".png" - ) - { - ContentType = "image"; - } - else if (strFilePath.FileType.ToLower() == ".htm" || - strFilePath.FileType.ToLower() == ".html" - ) - { - ContentType = "text/html"; - } - else if (strFilePath.FileType.ToLower() == ".mp3") - { - ContentType = "audio/mpeg"; - } - else if (strFilePath.FileType.ToLower() == ".css") - { - ContentType = "text/css"; - } - + string ContentType = GetContentTypeFromFileName(strFilePath.FileType); + try { IBuffer readBuffer = FileIO.ReadBuffer(strFilePath); @@ -651,6 +621,56 @@ private void ListInterfaces() } } + /// + /// Get the MIME-type for a file name. + /// + /// File name to get content type for. + /// The MIME-type for the file name. + private static string GetContentTypeFromFileName(string fileName) + { + // normalize to lower case to speed comparison + fileName = fileName.ToLower(); + + string contentType = "text/html"; + + //determine the type of file for the http header + if (fileName == ".cs" || + fileName == ".txt" || + fileName == ".csproj" + ) + { + contentType = "text/plain"; + } + else if (fileName == ".jpg" || + fileName == ".bmp" || + fileName == ".jpeg" || + fileName == ".png" + ) + { + contentType = "image"; + } + else if (fileName == ".htm" || + fileName == ".html" + ) + { + contentType = "text/html"; + } + else if (fileName == ".mp3") + { + contentType = "audio/mpeg"; + } + else if (fileName == ".css") + { + contentType = "text/css"; + } + else if (fileName == ".ico") + { + contentType = "image/x-icon"; + } + + return contentType; + } + /// /// Dispose of any resources. ///