You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/src/Lidarr.Http/Middleware/IfModifiedMiddleware.cs

44 lines
1.3 KiB

using System.Linq;
using System.Threading.Tasks;
using Lidarr.Http.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
namespace Lidarr.Http.Middleware
{
public class IfModifiedMiddleware
{
private readonly RequestDelegate _next;
private readonly ICacheableSpecification _cacheableSpecification;
private readonly IContentTypeProvider _mimeTypeProvider;
public IfModifiedMiddleware(RequestDelegate next, ICacheableSpecification cacheableSpecification)
{
_next = next;
_cacheableSpecification = cacheableSpecification;
_mimeTypeProvider = new FileExtensionContentTypeProvider();
}
public async Task InvokeAsync(HttpContext context)
{
if (_cacheableSpecification.IsCacheable(context.Request) && context.Request.Headers["IfModifiedSince"].Any())
{
context.Response.StatusCode = 304;
context.Response.Headers.EnableCache();
if (!_mimeTypeProvider.TryGetContentType(context.Request.Path.ToString(), out var mimeType))
{
mimeType = "application/octet-stream";
}
context.Response.ContentType = mimeType;
return;
}
await _next(context);
}
}
}