Lidarr/src/Lidarr.Http/Extensions/RequestExtensions.cs

215 lines
7.1 KiB
C#
Raw Normal View History

using System;
2021-08-04 20:42:40 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Net;
2021-08-04 20:42:40 +00:00
using Microsoft.AspNetCore.Http;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
2021-08-04 20:42:40 +00:00
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Exceptions;
2013-09-20 22:19:48 +00:00
2017-09-04 02:20:56 +00:00
namespace Lidarr.Http.Extensions
2013-09-20 22:19:48 +00:00
{
public static class RequestExtensions
{
2021-08-04 20:42:40 +00:00
// See src/Lidarr.Api.V1/Queue/QueueModule.cs
private static readonly HashSet<string> VALID_SORT_KEYS = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
2013-09-20 22:19:48 +00:00
{
"artists.sortname", // Workaround authors table properties not being added on isValidSortKey call
2021-08-04 20:42:40 +00:00
"timeleft",
"estimatedCompletionTime",
"protocol",
"indexer",
"downloadClient",
"quality",
"status",
"title",
"progress"
};
private static readonly HashSet<string> EXCLUDED_KEYS = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
2013-09-20 22:19:48 +00:00
{
2021-08-04 20:42:40 +00:00
"page",
"pageSize",
"sortKey",
"sortDirection",
"filterKey",
"filterValue",
};
public static bool IsApiRequest(this HttpRequest request)
2015-01-26 02:03:21 +00:00
{
2021-08-04 20:42:40 +00:00
return request.Path.StartsWithSegments("/api", StringComparison.InvariantCultureIgnoreCase);
2015-01-26 02:03:21 +00:00
}
2021-08-04 20:42:40 +00:00
public static bool GetBooleanQueryParameter(this HttpRequest request, string parameter, bool defaultValue = false)
2017-09-04 02:20:56 +00:00
{
var parameterValue = request.Query[parameter];
2021-08-04 20:42:40 +00:00
if (parameterValue.Any())
2017-09-04 02:20:56 +00:00
{
2021-08-04 20:42:40 +00:00
return bool.Parse(parameterValue.ToString());
2017-09-04 02:20:56 +00:00
}
return defaultValue;
}
2021-08-04 20:42:40 +00:00
public static PagingResource<TResource> ReadPagingResourceFromRequest<TResource>(this HttpRequest request)
{
2021-08-04 20:42:40 +00:00
if (!int.TryParse(request.Query["PageSize"].ToString(), out var pageSize))
{
pageSize = 10;
}
2021-08-04 20:42:40 +00:00
if (!int.TryParse(request.Query["Page"].ToString(), out var page))
{
page = 1;
}
2021-08-04 20:42:40 +00:00
var pagingResource = new PagingResource<TResource>
{
PageSize = pageSize,
Page = page,
Filters = new List<PagingResourceFilter>()
};
2021-08-04 20:42:40 +00:00
if (request.Query["SortKey"].Any())
{
2021-08-04 20:42:40 +00:00
var sortKey = request.Query["SortKey"].ToString();
if (!VALID_SORT_KEYS.Contains(sortKey) &&
!TableMapping.Mapper.IsValidSortKey(sortKey))
{
throw new BadRequestException($"Invalid sort key {sortKey}");
}
pagingResource.SortKey = sortKey;
if (request.Query["SortDirection"].Any())
{
pagingResource.SortDirection = request.Query["SortDirection"].ToString()
.Equals("ascending", StringComparison.InvariantCultureIgnoreCase)
? SortDirection.Ascending
: SortDirection.Descending;
}
}
2021-08-04 20:42:40 +00:00
// For backwards compatibility with v2
if (request.Query["FilterKey"].Any())
{
var filter = new PagingResourceFilter
{
Key = request.Query["FilterKey"].ToString()
};
2021-08-04 20:42:40 +00:00
if (request.Query["FilterValue"].Any())
{
filter.Value = request.Query["FilterValue"].ToString();
}
2021-08-04 20:42:40 +00:00
pagingResource.Filters.Add(filter);
}
// v3 uses filters in key=value format
foreach (var pair in request.Query)
{
2021-08-04 20:42:40 +00:00
if (EXCLUDED_KEYS.Contains(pair.Key))
{
continue;
}
pagingResource.Filters.Add(new PagingResourceFilter
{
Key = pair.Key,
Value = pair.Value.ToString()
});
}
2021-08-04 20:42:40 +00:00
return pagingResource;
}
2020-09-07 02:51:25 +00:00
2021-08-04 20:42:40 +00:00
public static PagingResource<TResource> ApplyToPage<TResource, TModel>(this PagingSpec<TModel> pagingSpec, Func<PagingSpec<TModel>, PagingSpec<TModel>> function, Converter<TModel, TResource> mapper)
2020-09-07 02:51:25 +00:00
{
2021-08-04 20:42:40 +00:00
pagingSpec = function(pagingSpec);
2020-09-07 02:51:25 +00:00
2021-08-04 20:42:40 +00:00
return new PagingResource<TResource>
2020-09-07 02:51:25 +00:00
{
2021-08-04 20:42:40 +00:00
Page = pagingSpec.Page,
PageSize = pagingSpec.PageSize,
SortDirection = pagingSpec.SortDirection,
SortKey = pagingSpec.SortKey,
TotalRecords = pagingSpec.TotalRecords,
Records = pagingSpec.Records.ConvertAll(mapper)
};
}
2020-09-07 02:51:25 +00:00
2021-08-04 20:42:40 +00:00
public static string GetRemoteIP(this HttpContext context)
{
return context?.Request?.GetRemoteIP() ?? "Unknown";
2020-09-07 02:51:25 +00:00
}
2021-08-04 20:42:40 +00:00
public static string GetRemoteIP(this HttpRequest request)
{
2021-08-04 20:42:40 +00:00
if (request == null)
{
return "Unknown";
}
2021-08-04 20:42:40 +00:00
var remoteIP = request.HttpContext.Connection.RemoteIpAddress;
if (remoteIP.IsIPv4MappedToIPv6)
{
remoteIP = remoteIP.MapToIPv4();
}
2021-08-04 20:42:40 +00:00
var remoteAddress = remoteIP.ToString();
// Only check if forwarded by a local network reverse proxy
2021-08-04 20:42:40 +00:00
if (remoteIP.IsLocalAddress())
{
2021-08-04 20:42:40 +00:00
var realIPHeader = request.Headers["X-Real-IP"];
if (realIPHeader.Any())
{
return realIPHeader.First().ToString();
}
2021-08-04 20:42:40 +00:00
var forwardedForHeader = request.Headers["X-Forwarded-For"];
if (forwardedForHeader.Any())
{
// Get the first address that was forwarded by a local IP to prevent remote clients faking another proxy
foreach (var forwardedForAddress in forwardedForHeader.SelectMany(v => v.Split(',')).Select(v => v.Trim()).Reverse())
{
if (!IPAddress.TryParse(forwardedForAddress, out remoteIP))
{
return remoteAddress;
}
if (!remoteIP.IsLocalAddress())
{
return forwardedForAddress;
}
remoteAddress = forwardedForAddress;
}
}
}
return remoteAddress;
}
2021-08-04 20:42:40 +00:00
public static void DisableCache(this IHeaderDictionary headers)
{
headers.Remove("Last-Modified");
2021-08-04 20:42:40 +00:00
headers["Cache-Control"] = "no-cache, no-store";
headers["Expires"] = "-1";
headers["Pragma"] = "no-cache";
}
public static void EnableCache(this IHeaderDictionary headers)
{
headers["Cache-Control"] = "max-age=31536000, public";
headers["Last-Modified"] = BuildInfo.BuildDateTime.ToString("r");
}
2013-09-20 22:19:48 +00:00
}
}