Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 52 additions & 32 deletions WebServer/WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,38 +418,8 @@ public static void OutputHttpCode(HttpListenerResponse response, HttpStatusCode
/// </summary>
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);
Expand Down Expand Up @@ -651,6 +621,56 @@ private void ListInterfaces()
}
}

/// <summary>
/// Get the MIME-type for a file name.
/// </summary>
/// <param name="fileName">File name to get content type for.</param>
/// <returns>The MIME-type for the file name.</returns>
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;
}

/// <summary>
/// Dispose of any resources.
/// </summary>
Expand Down