Repository navigation
Expand file tree
/
Copy pathRequestExtensions.cs
More file actions
114 lines (98 loc) · 3.68 KB
/
Copy pathRequestExtensions.cs
File metadata and controls
114 lines (98 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Datastore;
namespace Sonarr.Http.Extensions
{
public static class RequestExtensions
{
// See src/Readarr.Api.V1/Queue/QueueModule.cs
private static readonly HashSet<string> VALID_SORT_KEYS = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"series.sortname", // Workaround authors table properties not being added on isValidSortKey call
"episode.title", // Deprecated
"episode.airDateUtc", // Deprecated
"episode.language", // Deprecated
"timeleft",
"estimatedCompletionTime",
"protocol",
"episode",
"indexer",
"downloadClient",
"quality",
"status",
"title",
"progress"
};
private static readonly HashSet<string> EXCLUDED_KEYS = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
{
"page",
"pageSize",
"sortKey",
"sortDirection",
"filterKey",
"filterValue",
};
public static bool IsApiRequest(this HttpRequest request)
{
return request.Path.StartsWithSegments("/api", StringComparison.InvariantCultureIgnoreCase);
}
public static bool IsFavIconRequest(this HttpRequest request)
{
return request.Path.Equals("/favicon.ico", StringComparison.InvariantCultureIgnoreCase);
}
public static bool GetBooleanQueryParameter(this HttpRequest request, string parameter, bool defaultValue = false)
{
var parameterValue = request.Query[parameter];
if (parameterValue.Any())
{
return bool.Parse(parameterValue.ToString());
}
return defaultValue;
}
public static PagingResource<TResource> ApplyToPage<TResource, TModel>(this PagingSpec<TModel> pagingSpec, Func<PagingSpec<TModel>, PagingSpec<TModel>> function, Converter<TModel, TResource> mapper)
{
pagingSpec = function(pagingSpec);
return new PagingResource<TResource>
{
Page = pagingSpec.Page,
PageSize = pagingSpec.PageSize,
SortDirection = pagingSpec.SortDirection,
SortKey = pagingSpec.SortKey,
TotalRecords = pagingSpec.TotalRecords,
Records = pagingSpec.Records.ConvertAll(mapper)
};
}
public static string GetRemoteIP(this HttpContext context)
{
return context?.Request?.GetRemoteIP() ?? "Unknown";
}
public static string GetRemoteIP(this HttpRequest request)
{
if (request == null)
{
return "Unknown";
}
var remoteIP = request.HttpContext.Connection.RemoteIpAddress;
if (remoteIP.IsIPv4MappedToIPv6)
{
remoteIP = remoteIP.MapToIPv4();
}
return remoteIP.ToString();
}
public static void DisableCache(this IHeaderDictionary headers)
{
headers.Remove("Last-Modified");
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");
}
}
}