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
40 changes: 38 additions & 2 deletions WebServer/WebServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright (c) 2020 Laurent Ellerbach and the project contributors
// See LICENSE file in the project root for full license information.
//
Expand Down Expand Up @@ -451,7 +451,44 @@ public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile s
{
throw e;
}
}

/// <summary>
/// Send file content over HTTP response.
/// </summary>
/// <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>
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()
Expand Down Expand Up @@ -621,7 +658,6 @@ private void ListInterfaces()
}
}

/// Get the MIME-type for a file name.
/// <summary>
/// Get the MIME-type for a file name.
/// </summary>
Expand Down