Skip to content

Commit

Permalink
Fixing basic MIME types (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellerbach authored Jan 24, 2023
1 parent 1569782 commit 27441ef
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 29 deletions.
15 changes: 13 additions & 2 deletions nanoFramework.WebServer/Authentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace nanoFramework.WebServer
{
/// <summary>
/// The authentication to be used by the server
/// The authentication to be used by the server.
/// </summary>
public class Authentication
{
Expand All @@ -20,7 +20,7 @@ public class Authentication
public AuthenticationType AuthenticationType { get; internal set; }

/// <summary>
/// The network credential user and password
/// The network credential user and password.
/// </summary>
public NetworkCredential Credentials { get; internal set; } = null;

Expand All @@ -29,18 +29,29 @@ public class Authentication
/// </summary>
public string ApiKey { get; internal set; } = null;

/// <summary>
/// Creates an autentication class from a credential.
/// </summary>
/// <param name="credential">The credentials.</param>
public Authentication(NetworkCredential credential)
{
AuthenticationType = AuthenticationType.Basic;
Credentials = credential;
}

/// <summary>
/// Creates an authentication from a key.
/// </summary>
/// <param name="apiKey">The key.</param>
public Authentication(string apiKey)
{
AuthenticationType = AuthenticationType.ApiKey;
ApiKey = apiKey;
}

/// <summary>
/// Creates an empty authenticate.
/// </summary>
public Authentication()
{
AuthenticationType = AuthenticationType.None;
Expand Down
9 changes: 8 additions & 1 deletion nanoFramework.WebServer/MethodAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
namespace nanoFramework.WebServer
{
/// <summary>
/// The HTTP Method
/// The HTTP Method.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class MethodAttribute : Attribute
{
/// <summary>
/// Gets or sets the method.
/// </summary>
public string Method { get; set; }

/// <summary>
/// Creates a method attribute.
/// </summary>
/// <param name="method">The method.</param>
public MethodAttribute(string method)
{
Method = method;
Expand Down
9 changes: 6 additions & 3 deletions nanoFramework.WebServer/RouteAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
namespace nanoFramework.WebServer
{
/// <summary>
/// Route custom attribute
/// Route custom attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class RouteAttribute : Attribute
{
/// <summary>
/// Gets or sets the route.
/// </summary>
public string Route { get; set; }

/// <summary>
/// A route attribute
/// A route attribute.
/// </summary>
/// <param name="route">The route like route/second/third</param>
/// <param name="route">The complete route like 'route/second/third'.</param>
public RouteAttribute(string route)
{
Route = route;
Expand Down
54 changes: 31 additions & 23 deletions nanoFramework.WebServer/WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,11 @@ public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile s
/// <param name="response"><see cref="HttpListenerResponse"/> to send the content over.</param>
/// <param name="fileName">Name of the file to send over <see cref="HttpListenerResponse"/>.</param>
/// <param name="content">Content of the file to send.</param>
/// /// <param name="contentType">The type of file, if empty string, then will use auto detection</param>
/// <param name="contentType">The type of file, if empty string, then will use auto detection.</param>
public static void SendFileOverHTTP(HttpListenerResponse response, string fileName, byte[] content, string contentType = "")
{
contentType = contentType == "" ? GetContentTypeFromFileName(fileName.Substring(fileName.LastIndexOf('.'))) : contentType;
// If no extension, we will get the full file name
contentType = contentType == "" ? GetContentTypeFromFileName(fileName.Substring(fileName.LastIndexOf('.') + 1)) : contentType;
response.ContentType = contentType;
response.ContentLength64 = content.Length;

Expand All @@ -483,8 +484,6 @@ public static void SendFileOverHTTP(HttpListenerResponse response, string fileNa
// Writes data to output stream
response.OutputStream.Write(content, (int)bytesSent, (int)bytesToSend);

// allow some time to physically send the bits. Can be reduce to 10 or even less if not too much other code running in parallel

// update bytes sent
bytesSent += bytesToSend;
}
Expand Down Expand Up @@ -676,49 +675,58 @@ private void ListInterfaces()
/// <summary>
/// Get the MIME-type for a file name.
/// </summary>
/// <param name="fileName">File name to get content type for.</param>
/// <param name="fileExt">File extension to get content type for.</param>
/// <returns>The MIME-type for the file name.</returns>
private static string GetContentTypeFromFileName(string fileName)
private static string GetContentTypeFromFileName(string fileExt)
{
// normalize to lower case to speed comparison
fileName = fileName.ToLower();
fileExt = fileExt.ToLower();

string contentType = "text/html";

//determine the type of file for the http header
if (fileName == ".cs" ||
fileName == ".txt" ||
fileName == ".csproj"
)
if (fileExt == "cs" ||
fileExt == "txt" ||
fileExt == "csproj")
{
contentType = "text/plain";
}
else if (fileName == ".jpg" ||
fileName == ".bmp" ||
fileName == ".jpeg" ||
fileName == ".png"
)
else if (fileExt == "jpg" ||
fileExt == "jpeg" ||
fileExt == "jpe")
{
contentType = "image/jpeg";
}
else if (fileExt == "bmp" ||
fileExt == "png" ||
fileExt == "gif" ||
fileExt == "ief")
{
contentType = "image";
contentType = $"image/{fileExt}";
}
else if (fileName == ".htm" ||
fileName == ".html"
)
else if (fileExt == "htm" ||
fileExt == "html")
{
contentType = "text/html";
}
else if (fileName == ".mp3")
else if (fileExt == "mp3")
{
contentType = "audio/mpeg";
}
else if (fileName == ".css")
else if (fileExt == "css")
{
contentType = "text/css";
}
else if (fileName == ".ico")
else if (fileExt == "ico")
{
contentType = "image/x-icon";
}
else if (fileExt == "zip" ||
fileExt == "json" ||
fileExt == "pdf")
{
contentType = $"application/{fileExt}";
}

return contentType;
}
Expand Down
3 changes: 3 additions & 0 deletions nanoFramework.WebServer/nanoFramework.WebServer.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.lock.json" />
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<ProjectExtensions>
<ProjectCapabilities>
Expand Down

0 comments on commit 27441ef

Please sign in to comment.