diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-20 23:41:36 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-08-20 23:41:36 +0800 |
commit | 03549a181521009baf6d353a98f4cb8804602cdc (patch) | |
tree | 4554d77dcf4e10cdfa9955e496c0ede22788448a /Timeline/Controllers | |
parent | 7814ed7059f74cfecfa8a0c9aa4f9946d1015364 (diff) | |
download | timeline-03549a181521009baf6d353a98f4cb8804602cdc.tar.gz timeline-03549a181521009baf6d353a98f4cb8804602cdc.tar.bz2 timeline-03549a181521009baf6d353a98f4cb8804602cdc.zip |
Use etag for cache.
Diffstat (limited to 'Timeline/Controllers')
-rw-r--r-- | Timeline/Controllers/UserAvatarController.cs | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs index ffadcb86..964c9b98 100644 --- a/Timeline/Controllers/UserAvatarController.cs +++ b/Timeline/Controllers/UserAvatarController.cs @@ -2,7 +2,9 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
+using Microsoft.Net.Http.Headers;
using System;
+using System.Linq;
using System.Threading.Tasks;
using Timeline.Authenticate;
using Timeline.Filters;
@@ -61,22 +63,23 @@ namespace Timeline.Controllers [Authorize]
public async Task<IActionResult> Get([FromRoute] string username)
{
- const string IfModifiedSinceHeaderKey = "If-Modified-Since";
+ const string IfNonMatchHeaderKey = "If-None-Match";
try
{
- var avatarInfo = await _service.GetAvatar(username);
- var avatar = avatarInfo.Avatar;
- if (Request.Headers.TryGetValue(IfModifiedSinceHeaderKey, out var value))
+ var eTag = new EntityTagHeaderValue($"\"{await _service.GetAvatarETag(username)}\"");
+
+ if (Request.Headers.TryGetValue(IfNonMatchHeaderKey, out var value))
{
- var t = DateTime.Parse(value);
- if (t > avatarInfo.LastModified)
- {
- Response.Headers.Add(IfModifiedSinceHeaderKey, avatarInfo.LastModified.ToString("r"));
+ if (!EntityTagHeaderValue.TryParseStrictList(value, out var eTagList))
+ return BadRequest(CommonResponse.BadIfNonMatch());
+
+ if (eTagList.First(e => e.Equals(eTag)) != null)
return StatusCode(StatusCodes.Status304NotModified);
- }
}
- return File(avatar.Data, avatar.Type, new DateTimeOffset(avatarInfo.LastModified), null);
+ var avatarInfo = await _service.GetAvatar(username);
+ var avatar = avatarInfo.Avatar;
+ return File(avatar.Data, avatar.Type, new DateTimeOffset(avatarInfo.LastModified), eTag);
}
catch (UserNotExistException e)
{
|