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
Showing
2 changed files
with
61 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace WalletWasabi.Backend.Middlewares | ||
{ | ||
/// <summary> | ||
/// https://www.tpeczek.com/2017/10/exploring-head-method-behavior-in.html | ||
/// https://github.com/tpeczek/Demo.AspNetCore.Mvc.CosmosDB/blob/master/Demo.AspNetCore.Mvc.CosmosDB/Middlewares/HeadMethodMiddleware.cs | ||
/// </summary> | ||
public class HeadMethodMiddleware | ||
{ | ||
#region Fields | ||
|
||
private readonly RequestDelegate _next; | ||
|
||
#endregion Fields | ||
|
||
#region Constructor | ||
|
||
public HeadMethodMiddleware(RequestDelegate next) | ||
{ | ||
_next = next ?? throw new ArgumentNullException(nameof(next)); | ||
} | ||
|
||
#endregion Constructor | ||
|
||
#region Methods | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
bool methodSwitched = false; | ||
|
||
if (HttpMethods.IsHead(context.Request.Method)) | ||
{ | ||
methodSwitched = true; | ||
|
||
context.Request.Method = HttpMethods.Get; | ||
context.Response.Body = Stream.Null; | ||
} | ||
|
||
await _next(context); | ||
|
||
if (methodSwitched) | ||
{ | ||
context.Request.Method = HttpMethods.Head; | ||
} | ||
} | ||
|
||
#endregion Methods | ||
} | ||
} |
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