Skip to content

Commit

Permalink
Merge pull request #143 from GitHubProUser67/master
Browse files Browse the repository at this point in the history
CRITICAL: Fixes a Watson/Lite crashing issue with Concurrent Streams.
  • Loading branch information
jchristn authored Apr 30, 2024
2 parents d5048c1 + 768716a commit a33a28a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/WatsonWebserver.Lite/Webserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,10 @@ private async void ClientConnected(object sender, ClientConnectedEventArgs args)
{
ctx.Response.StatusCode = 404;
ctx.Response.ContentType = DefaultPages.Pages[404].ContentType;
await ctx.Response.Send(DefaultPages.Pages[404].Content, _Token).ConfigureAwait(false);
if (ctx.Response.ChunkedTransfer)
await ctx.Response.SendFinalChunk(Encoding.UTF8.GetBytes(DefaultPages.Pages[404].Content), _Token).ConfigureAwait(false);
else
await ctx.Response.Send(DefaultPages.Pages[404].Content, _Token).ConfigureAwait(false);
return;
}

Expand All @@ -633,7 +636,10 @@ private async void ClientConnected(object sender, ClientConnectedEventArgs args)

try
{
await ctx.Response.Send(DefaultPages.Pages[500].Content, _Token).ConfigureAwait(false);
if (ctx.Response.ChunkedTransfer)
await ctx.Response.SendFinalChunk(Encoding.UTF8.GetBytes(DefaultPages.Pages[500].Content), _Token).ConfigureAwait(false);
else
await ctx.Response.Send(DefaultPages.Pages[500].Content, _Token).ConfigureAwait(false);
}
catch
{
Expand All @@ -657,7 +663,10 @@ private async void ClientConnected(object sender, ClientConnectedEventArgs args)
{
ctx.Response.StatusCode = 500;
ctx.Response.ContentType = DefaultPages.Pages[500].ContentType;
await ctx.Response.Send(DefaultPages.Pages[500].Content).ConfigureAwait(false);
if (ctx.Response.ChunkedTransfer)
await ctx.Response.SendFinalChunk(Encoding.UTF8.GetBytes(DefaultPages.Pages[500].Content)).ConfigureAwait(false);
else
await ctx.Response.Send(DefaultPages.Pages[500].Content).ConfigureAwait(false);
}

ctx.Timestamp.End = DateTime.UtcNow;
Expand Down
16 changes: 13 additions & 3 deletions src/WatsonWebserver/Webserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text.Json.Serialization;
using WatsonWebserver.Core;
using System.Runtime.InteropServices;
using System.Text;

namespace WatsonWebserver
{
Expand Down Expand Up @@ -554,7 +555,10 @@ private async Task AcceptConnections(CancellationToken token)
{
ctx.Response.StatusCode = 404;
ctx.Response.ContentType = DefaultPages.Pages[404].ContentType;
await ctx.Response.Send(DefaultPages.Pages[404].Content).ConfigureAwait(false);
if (ctx.Response.ChunkedTransfer)
await ctx.Response.SendFinalChunk(Encoding.UTF8.GetBytes(DefaultPages.Pages[404].Content)).ConfigureAwait(false);
else
await ctx.Response.Send(DefaultPages.Pages[404].Content).ConfigureAwait(false);
return;
}

Expand All @@ -564,7 +568,10 @@ private async Task AcceptConnections(CancellationToken token)
{
ctx.Response.StatusCode = 500;
ctx.Response.ContentType = DefaultPages.Pages[500].ContentType;
await ctx.Response.Send(DefaultPages.Pages[500].Content).ConfigureAwait(false);
if (ctx.Response.ChunkedTransfer)
await ctx.Response.SendFinalChunk(Encoding.UTF8.GetBytes(DefaultPages.Pages[500].Content)).ConfigureAwait(false);
else
await ctx.Response.Send(DefaultPages.Pages[500].Content).ConfigureAwait(false);
Events.HandleExceptionEncountered(this, new ExceptionEventArgs(ctx, eInner));
}
finally
Expand All @@ -577,7 +584,10 @@ private async Task AcceptConnections(CancellationToken token)
{
ctx.Response.StatusCode = 500;
ctx.Response.ContentType = DefaultPages.Pages[500].ContentType;
await ctx.Response.Send(DefaultPages.Pages[500].Content).ConfigureAwait(false);
if (ctx.Response.ChunkedTransfer)
await ctx.Response.SendFinalChunk(Encoding.UTF8.GetBytes(DefaultPages.Pages[500].Content)).ConfigureAwait(false);
else
await ctx.Response.Send(DefaultPages.Pages[500].Content).ConfigureAwait(false);
}

ctx.Timestamp.End = DateTime.UtcNow;
Expand Down

0 comments on commit a33a28a

Please sign in to comment.