From 7b4fefb2cdd6ecee0d62bc530361ff694df893ad Mon Sep 17 00:00:00 2001 From: Laurent Ellerbach Date: Sun, 22 Jan 2023 18:07:18 +0100 Subject: [PATCH 1/3] Fixing basic MIME types --- nanoFramework.WebServer/Authentication.cs | 15 +++++- nanoFramework.WebServer/MethodAttribute.cs | 9 +++- nanoFramework.WebServer/RouteAttribute.cs | 9 ++-- nanoFramework.WebServer/WebServer.cs | 48 +++++++++++-------- .../nanoFramework.WebServer.nfproj | 3 ++ 5 files changed, 58 insertions(+), 26 deletions(-) diff --git a/nanoFramework.WebServer/Authentication.cs b/nanoFramework.WebServer/Authentication.cs index 952bd93..c237fb8 100644 --- a/nanoFramework.WebServer/Authentication.cs +++ b/nanoFramework.WebServer/Authentication.cs @@ -10,7 +10,7 @@ namespace nanoFramework.WebServer { /// - /// The authentication to be used by the server + /// The authentication to be used by the server. /// public class Authentication { @@ -20,7 +20,7 @@ public class Authentication public AuthenticationType AuthenticationType { get; internal set; } /// - /// The network credential user and password + /// The network credential user and password. /// public NetworkCredential Credentials { get; internal set; } = null; @@ -29,18 +29,29 @@ public class Authentication /// public string ApiKey { get; internal set; } = null; + /// + /// Creates an autentication class from a credential. + /// + /// The credentials. public Authentication(NetworkCredential credential) { AuthenticationType = AuthenticationType.Basic; Credentials = credential; } + /// + /// Creates an authentication from a key. + /// + /// The key. public Authentication(string apiKey) { AuthenticationType = AuthenticationType.ApiKey; ApiKey = apiKey; } + /// + /// Creates an empty authenticate. + /// public Authentication() { AuthenticationType = AuthenticationType.None; diff --git a/nanoFramework.WebServer/MethodAttribute.cs b/nanoFramework.WebServer/MethodAttribute.cs index 5f489d4..7b3e8fe 100644 --- a/nanoFramework.WebServer/MethodAttribute.cs +++ b/nanoFramework.WebServer/MethodAttribute.cs @@ -8,13 +8,20 @@ namespace nanoFramework.WebServer { /// - /// The HTTP Method + /// The HTTP Method. /// [AttributeUsage(AttributeTargets.Method)] public class MethodAttribute : Attribute { + /// + /// Gets or sets the method. + /// public string Method { get; set; } + /// + /// Creates a method attribute. + /// + /// The method. public MethodAttribute(string method) { Method = method; diff --git a/nanoFramework.WebServer/RouteAttribute.cs b/nanoFramework.WebServer/RouteAttribute.cs index d1bdf6e..bc6f979 100644 --- a/nanoFramework.WebServer/RouteAttribute.cs +++ b/nanoFramework.WebServer/RouteAttribute.cs @@ -8,17 +8,20 @@ namespace nanoFramework.WebServer { /// - /// Route custom attribute + /// Route custom attribute. /// [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public class RouteAttribute : Attribute { + /// + /// Gets or sets the route. + /// public string Route { get; set; } /// - /// A route attribute + /// A route attribute. /// - /// The route like route/second/third + /// The complete route like 'route/second/third'. public RouteAttribute(string route) { Route = route; diff --git a/nanoFramework.WebServer/WebServer.cs b/nanoFramework.WebServer/WebServer.cs index 924450e..a667740 100644 --- a/nanoFramework.WebServer/WebServer.cs +++ b/nanoFramework.WebServer/WebServer.cs @@ -371,7 +371,7 @@ public bool Start() /// /// Restart the server. /// - private bool Restart() + public bool Restart() { Stop(); return Start(); @@ -468,7 +468,8 @@ public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile s /// /// The type of file, if empty string, then will use auto detection 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; @@ -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; } @@ -686,39 +685,48 @@ private static string GetContentTypeFromFileName(string fileName) string contentType = "text/html"; //determine the type of file for the http header - if (fileName == ".cs" || - fileName == ".txt" || - fileName == ".csproj" - ) + if (fileName == "cs" || + fileName == "txt" || + fileName == "csproj") { contentType = "text/plain"; } - else if (fileName == ".jpg" || - fileName == ".bmp" || - fileName == ".jpeg" || - fileName == ".png" - ) + else if (fileName == "jpg" || + fileName == "jpeg" || + fileName == "jpe") + { + contentType = "image/jpeg"; + } + else if (fileName == "bmp" || + fileName == "png" || + fileName == "gif" || + fileName == "ief") { - contentType = "image"; + contentType = $"image/{fileName}"; } - else if (fileName == ".htm" || - fileName == ".html" - ) + else if (fileName == "htm" || + fileName == "html") { contentType = "text/html"; } - else if (fileName == ".mp3") + else if (fileName == "mp3") { contentType = "audio/mpeg"; } - else if (fileName == ".css") + else if (fileName == "css") { contentType = "text/css"; } - else if (fileName == ".ico") + else if (fileName == "ico") { contentType = "image/x-icon"; } + else if (fileName == "zip" || + fileName == "json" || + fileName == "pdf") + { + contentType = $"application/{fileName}"; + } return contentType; } diff --git a/nanoFramework.WebServer/nanoFramework.WebServer.nfproj b/nanoFramework.WebServer/nanoFramework.WebServer.nfproj index 44aee1e..ade4377 100644 --- a/nanoFramework.WebServer/nanoFramework.WebServer.nfproj +++ b/nanoFramework.WebServer/nanoFramework.WebServer.nfproj @@ -91,6 +91,9 @@ + + + From 95c5622263b652f8a66bbba87befbe7c90481401 Mon Sep 17 00:00:00 2001 From: Laurent Ellerbach Date: Mon, 23 Jan 2023 08:33:19 +0100 Subject: [PATCH 2/3] renaming filename to fileext --- nanoFramework.WebServer/WebServer.cs | 46 ++++++++++++++-------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/nanoFramework.WebServer/WebServer.cs b/nanoFramework.WebServer/WebServer.cs index a667740..cd26666 100644 --- a/nanoFramework.WebServer/WebServer.cs +++ b/nanoFramework.WebServer/WebServer.cs @@ -675,57 +675,57 @@ private void ListInterfaces() /// /// Get the MIME-type for a file name. /// - /// File name to get content type for. + /// File extension to get content type for. /// The MIME-type for the file name. - 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 == "jpeg" || - fileName == "jpe") + else if (fileExt == "jpg" || + fileExt == "jpeg" || + fileExt == "jpe") { contentType = "image/jpeg"; } - else if (fileName == "bmp" || - fileName == "png" || - fileName == "gif" || - fileName == "ief") + else if (fileExt == "bmp" || + fileExt == "png" || + fileExt == "gif" || + fileExt == "ief") { - contentType = $"image/{fileName}"; + 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 (fileName == "zip" || - fileName == "json" || - fileName == "pdf") + else if (fileExt == "zip" || + fileExt == "json" || + fileExt == "pdf") { - contentType = $"application/{fileName}"; + contentType = $"application/{fileExt}"; } return contentType; From 0cef2f6eb82d29d023894a2da717f14662c56add Mon Sep 17 00:00:00 2001 From: Laurent Ellerbach Date: Mon, 23 Jan 2023 11:27:00 +0100 Subject: [PATCH 3/3] adjusting --- nanoFramework.WebServer/WebServer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nanoFramework.WebServer/WebServer.cs b/nanoFramework.WebServer/WebServer.cs index cd26666..1cba312 100644 --- a/nanoFramework.WebServer/WebServer.cs +++ b/nanoFramework.WebServer/WebServer.cs @@ -371,7 +371,7 @@ public bool Start() /// /// Restart the server. /// - public bool Restart() + private bool Restart() { Stop(); return Start(); @@ -465,7 +465,7 @@ public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile s /// to send the content over. /// Name of the file to send over . /// Content of the file to send. - /// /// The type of file, if empty string, then will use auto detection + /// The type of file, if empty string, then will use auto detection. public static void SendFileOverHTTP(HttpListenerResponse response, string fileName, byte[] content, string contentType = "") { // If no extension, we will get the full file name