From a6c2e1107a120fdd47791a1fcf0f5f95433090f6 Mon Sep 17 00:00:00 2001 From: josesimoes Date: Mon, 2 Nov 2020 17:24:59 +0000 Subject: [PATCH 1/2] Add method to return MIME-type given a file name --- WebServer/WebServer.cs | 35 +++-------------------------------- 1 file changed, 3 insertions(+), 32 deletions(-) diff --git a/WebServer/WebServer.cs b/WebServer/WebServer.cs index f323899..375e371 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,7 @@ private void ListInterfaces() } } + /// Get the MIME-type for a file name. /// /// Dispose of any resources. /// From 299bc3aefc99000272586b846b2493c97d6276f2 Mon Sep 17 00:00:00 2001 From: josesimoes Date: Mon, 2 Nov 2020 17:31:31 +0000 Subject: [PATCH 2/2] Add method to send file content from byte array - Requires #6 to be merged. --- WebServer/WebServer.cs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/WebServer/WebServer.cs b/WebServer/WebServer.cs index 375e371..795329b 100644 --- a/WebServer/WebServer.cs +++ b/WebServer/WebServer.cs @@ -451,7 +451,44 @@ public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile s { throw e; } + } + + /// + /// Send file content over HTTP response. + /// + /// to send the content over. + /// Name of the file to send over . + /// Content of the file to send. + public static void SendFileOverHTTP(HttpListenerResponse response, string fileName, byte[] content) + { + string ContentType = GetContentTypeFromFileName(fileName.Substring(fileName.LastIndexOf('.'))); + + try + { + response.ContentType = ContentType; + response.ContentLength64 = content.Length; + + // Now loop to send all the data. + for (long bytesSent = 0; bytesSent < content.Length;) + { + // Determines amount of data left + long bytesToSend = content.Length - bytesSent; + bytesToSend = bytesToSend < MaxSizeBuffer ? bytesToSend : MaxSizeBuffer; + + // 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; + } + } + catch (Exception e) + { + throw e; + } } private void StartListener()