mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2026-04-20 22:14:34 -04:00
f48c9f9f88
(cherry picked from commit f30207c3d130c1a37f29e214101c8ec9613d18ee)
85 lines
2.4 KiB
C#
85 lines
2.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using NLog;
|
|
using Prowlarr.Http.Extensions;
|
|
using Prowlarr.Http.Frontend.Mappers;
|
|
|
|
namespace Prowlarr.Http.Frontend
|
|
{
|
|
[Authorize(Policy="UI")]
|
|
[ApiController]
|
|
public class StaticResourceController : Controller
|
|
{
|
|
private readonly IEnumerable<IMapHttpRequestsToDisk> _requestMappers;
|
|
private readonly Logger _logger;
|
|
private static readonly Regex InvalidPathRegex = new(@"([\/\\]|%2f|%5c)\.\.|\.\.([\/\\]|%2f|%5c)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
|
|
|
public StaticResourceController(IEnumerable<IMapHttpRequestsToDisk> requestMappers,
|
|
Logger logger)
|
|
{
|
|
_requestMappers = requestMappers;
|
|
_logger = logger;
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet("login")]
|
|
public async Task<IActionResult> LoginPage()
|
|
{
|
|
return await MapResource("login");
|
|
}
|
|
|
|
[EnableCors("AllowGet")]
|
|
[AllowAnonymous]
|
|
[HttpGet("/content/{**path:regex(^(?!api/).*)}")]
|
|
public async Task<IActionResult> IndexContent([FromRoute] string path)
|
|
{
|
|
return await MapResource("Content/" + path);
|
|
}
|
|
|
|
[HttpGet("")]
|
|
[HttpGet("/{**path:regex(^(?!api/).*)}")]
|
|
public async Task<IActionResult> Index([FromRoute] string path)
|
|
{
|
|
return await MapResource(path);
|
|
}
|
|
|
|
private async Task<IActionResult> MapResource(string path)
|
|
{
|
|
path = "/" + (path ?? "");
|
|
|
|
if (InvalidPathRegex.IsMatch(path))
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var mapper = _requestMappers.SingleOrDefault(m => m.CanHandle(path));
|
|
|
|
if (mapper != null)
|
|
{
|
|
var result = await mapper.GetResponse(path);
|
|
|
|
if (result != null)
|
|
{
|
|
if ((result as FileResult)?.ContentType == "text/html")
|
|
{
|
|
Response.Headers.DisableCache();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
return NotFound();
|
|
}
|
|
|
|
_logger.Warn("Couldn't find handler for {0}", path);
|
|
|
|
return NotFound();
|
|
}
|
|
}
|
|
}
|