-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e7788f
commit ca5c3d9
Showing
5 changed files
with
104 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
Source/Boxed.AspNetCore/Middleware/RequestCanceledMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
namespace Boxed.AspNetCore.Middleware; | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
|
||
/// <summary> | ||
/// A middleware which handles <see cref="OperationCanceledException"/> caused by the HTTP request being aborted, then | ||
/// shortcuts and returns an error status code. | ||
/// </summary> | ||
/// <seealso cref="IMiddleware" /> | ||
public class RequestCanceledMiddleware : IMiddleware | ||
{ | ||
private readonly ILogger<RequestCanceledMiddleware> logger; | ||
private readonly RequestCanceledMiddlewareOptions options; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RequestCanceledMiddleware"/> class. | ||
/// </summary> | ||
/// <param name="options">The middleware options.</param> | ||
/// <param name="logger">A logger.</param> | ||
public RequestCanceledMiddleware( | ||
RequestCanceledMiddlewareOptions options, | ||
ILogger<RequestCanceledMiddleware> logger) | ||
{ | ||
this.options = options; | ||
this.logger = logger; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task InvokeAsync(HttpContext context, RequestDelegate next) | ||
{ | ||
ArgumentNullException.ThrowIfNull(context); | ||
ArgumentNullException.ThrowIfNull(next); | ||
|
||
try | ||
{ | ||
await next(context).ConfigureAwait(false); | ||
} | ||
catch (OperationCanceledException operationCanceledException) | ||
when (operationCanceledException.CancellationToken == context.RequestAborted) | ||
{ | ||
this.logger.RequestCancelled(); | ||
context.Response.StatusCode = this.options.StatusCode; | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Source/Boxed.AspNetCore/Middleware/RequestCanceledMiddlewareOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Boxed.AspNetCore.Middleware; | ||
|
||
/// <summary> | ||
/// Options controlling <see cref="RequestCanceledMiddleware"/>. | ||
/// </summary> | ||
public class RequestCanceledMiddlewareOptions | ||
{ | ||
/// <summary> | ||
/// The non-standard 499 status code 'Client Closed Request' used by NGINX to signify an aborted/cancelled request. | ||
/// </summary> | ||
public const int ClientClosedRequest = 499; | ||
|
||
/// <summary> | ||
/// Gets or sets the status code to return for a cancelled request. The default is the non-standard 499 | ||
/// 'Client Closed Request' used by NGINX. | ||
/// See https://stackoverflow.com/questions/46234679/what-is-the-correct-http-status-code-for-a-cancelled-request. | ||
/// </summary> | ||
public int StatusCode { get; set; } = ClientClosedRequest; | ||
} |