-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from jgiacomini/develop
Merge 1.6.0
- Loading branch information
Showing
24 changed files
with
734 additions
and
98 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
77 changes: 77 additions & 0 deletions
77
Tests/Tiny.RestClient.ForTest.Api/Middleware/ETagMiddleware.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,77 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.WebUtilities; | ||
using Microsoft.Net.Http.Headers; | ||
using System.IO; | ||
using System.Security.Cryptography; | ||
using System.Threading.Tasks; | ||
|
||
namespace Tiny.RestClient.ForTest.Api.Middleware | ||
{ | ||
public class ETagMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public ETagMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
var response = context.Response; | ||
using (var ms = new MemoryStream()) | ||
{ | ||
if (IsETagSupported(response)) | ||
{ | ||
var originalStream = response.Body; | ||
string checksum = CalculateChecksum(ms); | ||
|
||
response.Headers[HeaderNames.ETag] = checksum; | ||
|
||
if (context.Request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out var etag) && checksum == etag) | ||
{ | ||
response.StatusCode = StatusCodes.Status304NotModified; | ||
response.Body = ms; | ||
await _next(context); | ||
return; | ||
} | ||
|
||
await _next(context); | ||
} | ||
else | ||
{ | ||
await _next(context); | ||
} | ||
} | ||
} | ||
|
||
private static bool IsETagSupported(HttpResponse response) | ||
{ | ||
if (response.StatusCode != StatusCodes.Status200OK) | ||
{ | ||
return false; | ||
} | ||
|
||
if (response.Headers.ContainsKey(HeaderNames.ETag)) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static string CalculateChecksum(MemoryStream ms) | ||
{ | ||
string checksum = string.Empty; | ||
|
||
using (var algo = SHA1.Create()) | ||
{ | ||
ms.Position = 0; | ||
byte[] bytes = algo.ComputeHash(ms); | ||
checksum = $"\"{WebEncoders.Base64UrlEncode(bytes)}\""; | ||
} | ||
|
||
return checksum; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.