Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HttpListenerResponse.cs #4

Merged
merged 3 commits into from
Apr 15, 2017
Merged
Changes from 1 commit
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
39 changes: 24 additions & 15 deletions src/NETStandard.HttpListener/HttpListenerResponse.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.IO;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using System.Diagnostics;

namespace System.Net.Http
{
Expand Down Expand Up @@ -55,22 +56,30 @@ internal void Initialize()

private async Task SendMessage()
{
var outputStream = OutputStream as MemoryStream;
outputStream.Seek(0, SeekOrigin.Begin);
var outputStream = OutputStream as MemoryStream;
outputStream.Seek(0, SeekOrigin.Begin);

var socketStream = _client.GetOutputStream();
var socketStream = _client.GetOutputStream();

string header = $"{Version} {StatusCode} {ReasonPhrase}\r\n" +
Headers +
$"Content-Length: {outputStream.Length}\r\n" +
"\r\n";

byte[] headerArray = Encoding.UTF8.GetBytes(header);
await socketStream.WriteAsync(headerArray, 0, headerArray.Length);
await outputStream.CopyToAsync(socketStream);

await socketStream.FlushAsync();
string header = $"{Version} {StatusCode} {ReasonPhrase}\r\n" +
Headers +
$"Content-Length: {outputStream.Length}\r\n" +
"\r\n";
byte[] headerArray = Encoding.UTF8.GetBytes(header);

await socketStream.WriteAsync(headerArray, 0, headerArray.Length);
try
{
await outputStream.CopyToAsync(socketStream);
}
catch (IOException ex)
{
Debug.WriteLine(ex.Message);
}
finally
{
await socketStream.FlushAsync();
}
}

/// <summary>
Expand Down Expand Up @@ -161,4 +170,4 @@ public void Dispose()
}
#endregion
}
}
}