Skip to content

Commit 118e6ec

Browse files
committed
fix: wrong error handling in router invoker
1 parent a5ca3b0 commit 118e6ec

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

src/Http/HttpStatusInformation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private static void ValidateDescription ( string s ) {
100100
/// <param name="statusCode">The HTTP status code.</param>
101101
/// <returns>The description of the HTTP status code.</returns>
102102
public static string GetStatusCodeDescription ( int statusCode ) {
103-
return HttpStatusDescription.Get ( statusCode );
103+
return HttpStatusDescription.Get ( statusCode ) ?? "Unknown";
104104
}
105105

106106
/// <summary>

src/Http/Streams/HttpWebSocket.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,10 @@ private bool SendInternal ( ReadOnlyMemory<byte> buffer, WebSocketMessageType ms
299299

300300
ReadOnlyMemory<byte> chunk = buffer [ ca..cb ];
301301

302-
ctx.WebSocket.SendAsync ( chunk, msgType, i + 1 == chunks, CancellationToken.None )
303-
.AsTask ().Wait ();
302+
var sendVt = ctx.WebSocket.SendAsync ( chunk, msgType, i + 1 == chunks, asyncListenerToken?.Token ?? default );
303+
if (!sendVt.IsCompleted) {
304+
sendVt.AsTask ().GetAwaiter ().GetResult ();
305+
}
304306

305307
length += chunk.Length;
306308
}

src/Internal/HttpStatusDescription.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212
// Licensed to the .NET Foundation under one or more agreements.
1313
// The .NET Foundation licenses this file to you under the MIT license.
1414

15-
using System.Net;
16-
1715
namespace Sisk.Core.Internal;
1816

1917
static class HttpStatusDescription {
20-
internal static string? Get ( HttpStatusCode code ) =>
21-
Get ( (int) code );
2218

23-
internal static string Get ( int code ) =>
19+
internal static string? Get ( int code ) =>
2420
code switch {
2521
100 => "Continue",
2622
101 => "Switching Protocols",
@@ -88,6 +84,6 @@ internal static string Get ( int code ) =>
8884
510 => "Not Extended",
8985
511 => "Network Authentication Required",
9086

91-
_ => "Unknown"
87+
_ => null
9288
};
9389
}

src/Routing/Router.Invoker.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,25 @@ internal RouterExecutionResult Execute ( HttpContext context ) {
173173
}
174174
}
175175

176-
if (matchResult == RouteMatchResult.NotMatched && NotFoundErrorHandler is not null) {
177-
return new RouterExecutionResult ( NotFoundErrorHandler ( context ), null, matchResult, null );
176+
if (matchResult == RouteMatchResult.NotMatched) {
177+
if (NotFoundErrorHandler is not null) {
178+
return new RouterExecutionResult ( NotFoundErrorHandler ( context ), null, matchResult, null );
179+
}
180+
else {
181+
return new RouterExecutionResult ( new HttpResponse ( HttpStatusCode.NotFound ), null, matchResult, null );
182+
}
178183
}
179184
else if (matchResult == RouteMatchResult.OptionsMatched) {
180-
HttpResponse corsResponse = new HttpResponse ();
181-
182-
return new RouterExecutionResult ( corsResponse, null, matchResult, null );
185+
return new RouterExecutionResult ( new HttpResponse ( HttpStatusCode.OK ), null, matchResult, null );
183186
}
184-
else if (matchResult == RouteMatchResult.PathMatched && MethodNotAllowedErrorHandler is not null) {
187+
else if (matchResult == RouteMatchResult.PathMatched) {
185188
context.MatchedRoute = matchedRoute;
186-
return new RouterExecutionResult ( MethodNotAllowedErrorHandler ( context ), matchedRoute, matchResult, null );
189+
if (MethodNotAllowedErrorHandler is not null) {
190+
return new RouterExecutionResult ( MethodNotAllowedErrorHandler ( context ), matchedRoute, matchResult, null );
191+
}
192+
else {
193+
return new RouterExecutionResult ( new HttpResponse ( HttpStatusCode.MethodNotAllowed ), matchedRoute, matchResult, null );
194+
}
187195
}
188196
else if (matchResult == RouteMatchResult.FullyMatched && matchedRoute is not null) {
189197
context.MatchedRoute = matchedRoute;

src/Sisk.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<PropertyGroup>
5050
<AssemblyVersion>1.4.1</AssemblyVersion>
5151
<FileVersion>1.4.1</FileVersion>
52-
<Version>1.4.1</Version>
52+
<Version>1.4.1-beta1</Version>
5353
</PropertyGroup>
5454

5555
<!-- licensing, readme, signing -->

0 commit comments

Comments
 (0)