Skip to content

Commit 4318e0d

Browse files
authored
#2219 Remove preprocessor directives targeted at old .NET 6 and 7
1 parent 5c8938f commit 4318e0d

12 files changed

+7
-89
lines changed

src/Ocelot/Authorization/ClaimsAuthorizer.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ public ClaimsAuthorizer(IClaimsParser claimsParser)
1818
_claimsParser = claimsParser;
1919
}
2020

21-
#if NET7_0_OR_GREATER
2221
[GeneratedRegex(@"^{(?<variable>.+)}$", RegexOptions.None, RegexGlobal.DefaultMatchTimeoutMilliseconds)]
2322
private static partial Regex RegexAuthorize();
24-
#else
25-
private static readonly Regex _regexAuthorize = RegexGlobal.New(@"^{(?<variable>.+)}$");
26-
private static Regex RegexAuthorize() => _regexAuthorize;
27-
#endif
23+
2824
public Response<bool> Authorize(
2925
ClaimsPrincipal claimsPrincipal,
3026
Dictionary<string, string> routeClaimsRequirement,

src/Ocelot/Configuration/Creator/UpstreamHeaderTemplatePatternCreator.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,8 @@ namespace Ocelot.Configuration.Creator;
1010
/// <remarks>Ocelot feature: Routing based on request header.</remarks>
1111
public partial class UpstreamHeaderTemplatePatternCreator : IUpstreamHeaderTemplatePatternCreator
1212
{
13-
private const string PlaceHolderPattern = @"(\{header:.*?\})";
14-
#if NET7_0_OR_GREATER
15-
[GeneratedRegex(PlaceHolderPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline, RegexGlobal.DefaultMatchTimeoutMilliseconds, "en-US")]
13+
[GeneratedRegex(@"(\{header:.*?\})", RegexOptions.IgnoreCase | RegexOptions.Singleline, RegexGlobal.DefaultMatchTimeoutMilliseconds, "en-US")]
1614
private static partial Regex RegexPlaceholders();
17-
#else
18-
private static readonly Regex _regexPlaceholders = RegexGlobal.New(PlaceHolderPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
19-
private static Regex RegexPlaceholders() => _regexPlaceholders;
20-
#endif
2115

2216
public IDictionary<string, UpstreamHeaderTemplate> Create(IRoute route)
2317
{

src/Ocelot/Configuration/Parser/ClaimToThingConfigurationParser.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,12 @@ namespace Ocelot.Configuration.Parser;
99
public partial class ClaimToThingConfigurationParser : IClaimToThingConfigurationParser
1010
{
1111
private const char SplitToken = '>';
12-
#if NET7_0_OR_GREATER
12+
1313
[GeneratedRegex("Claims\\[.*\\]", RegexOptions.None, RegexGlobal.DefaultMatchTimeoutMilliseconds)]
1414
private static partial Regex ClaimRegex();
15+
1516
[GeneratedRegex("value\\[.*\\]", RegexOptions.None, RegexGlobal.DefaultMatchTimeoutMilliseconds)]
1617
private static partial Regex IndexRegex();
17-
#else
18-
private static readonly Regex _claimRegex = RegexGlobal.New("Claims\\[.*\\]");
19-
private static readonly Regex _indexRegex = RegexGlobal.New("value\\[.*\\]");
20-
private static Regex ClaimRegex() => _claimRegex;
21-
private static Regex IndexRegex() => _indexRegex;
22-
#endif
2318

2419
public Response<ClaimToThing> Extract(string existingKey, string value)
2520
{

src/Ocelot/Configuration/Validator/FileConfigurationFluentValidator.cs

-5
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,8 @@ private static bool AllRoutesForAggregateExist(FileAggregateRoute fileAggregateR
9999
return routesForAggregate.Count() == fileAggregateRoute.RouteKeys.Count;
100100
}
101101

102-
#if NET7_0_OR_GREATER
103102
[GeneratedRegex(@"\{\w+\}", RegexOptions.IgnoreCase | RegexOptions.Singleline, RegexGlobal.DefaultMatchTimeoutMilliseconds, "en-US")]
104103
private static partial Regex PlaceholderRegex();
105-
#else
106-
private static readonly Regex _placeholderRegex = RegexGlobal.New(@"\{\w+\}", RegexOptions.IgnoreCase | RegexOptions.Singleline);
107-
private static Regex PlaceholderRegex() => _placeholderRegex;
108-
#endif
109104

110105
private static bool IsPlaceholderNotDuplicatedIn(string pathTemplate)
111106
{

src/Ocelot/Configuration/Validator/RouteFluentValidator.cs

-11
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ private async Task<bool> IsSupportedAuthenticationProviders(FileAuthenticationOp
111111
|| (string.IsNullOrEmpty(primary) && options.AuthenticationProviderKeys.All(supportedSchemes.Contains));
112112
}
113113

114-
#if NET7_0_OR_GREATER
115114
[GeneratedRegex("^[0-9]+s", RegexOptions.None, RegexGlobal.DefaultMatchTimeoutMilliseconds)]
116115
private static partial Regex SecondsRegex();
117116
[GeneratedRegex("^[0-9]+m", RegexOptions.None, RegexGlobal.DefaultMatchTimeoutMilliseconds)]
@@ -120,16 +119,6 @@ private async Task<bool> IsSupportedAuthenticationProviders(FileAuthenticationOp
120119
private static partial Regex HoursRegex();
121120
[GeneratedRegex("^[0-9]+d", RegexOptions.None, RegexGlobal.DefaultMatchTimeoutMilliseconds)]
122121
private static partial Regex DaysRegex();
123-
#else
124-
private static readonly Regex _secondsRegex = RegexGlobal.New("^[0-9]+s");
125-
private static readonly Regex _minutesRegex = RegexGlobal.New("^[0-9]+m");
126-
private static readonly Regex _hoursRegex = RegexGlobal.New("^[0-9]+h");
127-
private static readonly Regex _daysRegex = RegexGlobal.New("^[0-9]+d");
128-
private static Regex SecondsRegex() => _secondsRegex;
129-
private static Regex MinutesRegex() => _minutesRegex;
130-
private static Regex HoursRegex() => _hoursRegex;
131-
private static Regex DaysRegex() => _daysRegex;
132-
#endif
133122

134123
private static bool IsValidPeriod(FileRateLimitRule rateLimitOptions)
135124
{

src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,9 @@ private static IConfigurationBuilder ApplyMergeOcelotJsonOption(IConfigurationBu
9696
AddOcelotJsonFile(builder, json, primaryConfigFile, optional, reloadOnChange);
9797
}
9898

99-
#if NET7_0_OR_GREATER
10099
[GeneratedRegex(@"^ocelot\.(.*?)\.json$", RegexOptions.IgnoreCase | RegexOptions.Singleline, RegexGlobal.DefaultMatchTimeoutMilliseconds, "en-US")]
101100
private static partial Regex SubConfigRegex();
102-
#else
103-
private static readonly Regex _subConfigRegex = RegexGlobal.New(@"^ocelot\.(.*?)\.json$", RegexOptions.IgnoreCase | RegexOptions.Singleline);
104-
private static Regex SubConfigRegex() => _subConfigRegex;
105-
#endif
101+
106102
private static string GetMergedOcelotJson(string folder, IWebHostEnvironment env,
107103
FileConfiguration fileConfiguration = null, string primaryFile = null, string globalFile = null, string environmentFile = null)
108104
{

src/Ocelot/DownstreamRouteFinder/UrlMatcher/UrlPathPlaceholderNameAndValueFinder.cs

-15
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,8 @@ public Response<List<PlaceholderNameAndValue>> Find(string path, string query, s
4343
}
4444

4545
private const int PlaceholdersMilliseconds = 1000;
46-
#if NET7_0_OR_GREATER
4746
[GeneratedRegex(@"\{(.*?)\}", RegexOptions.None, PlaceholdersMilliseconds)]
4847
private static partial Regex RegexPlaceholders();
49-
#else
50-
private static readonly Regex _regexPlaceholders = RegexGlobal.New(@"\{(.*?)\}", RegexOptions.None, TimeSpan.FromMilliseconds(PlaceholdersMilliseconds));
51-
private static Regex RegexPlaceholders() => _regexPlaceholders;
52-
#endif
5348

5449
/// <summary>Finds the placeholders in the request path and query.
5550
/// We use a <see cref="Regex"/> pattern to match the placeholders in the path template.
@@ -107,13 +102,8 @@ string MatchEvaluator(Match match)
107102
}
108103

109104
private const int CatchAllQueryMilliseconds = 300;
110-
#if NET7_0_OR_GREATER
111105
[GeneratedRegex(@"^[^{{}}]*\?\{(.*?)\}$", RegexOptions.None, CatchAllQueryMilliseconds)]
112106
private static partial Regex RegexCatchAllQuery();
113-
#else
114-
private static readonly Regex _regexCatchAllQuery = RegexGlobal.New(@"^[^{{}}]*\?\{(.*?)\}$", RegexOptions.None, TimeSpan.FromMilliseconds(CatchAllQueryMilliseconds));
115-
private static Regex RegexCatchAllQuery() => _regexCatchAllQuery;
116-
#endif
117107

118108
/// <summary>Checks if the path template contains a Catch-All query parameter.
119109
/// <para>It means that the path template ends with a question mark and a placeholder.
@@ -129,13 +119,8 @@ private static (bool IsMatch, string Placeholder) IsCatchAllQuery(string templat
129119
}
130120

131121
private const int CatchAllPathMilliseconds = 300;
132-
#if NET7_0_OR_GREATER
133122
[GeneratedRegex(@"^[^{{}}]*\{(.*?)\}/?$", RegexOptions.None, CatchAllPathMilliseconds)]
134123
private static partial Regex RegexCatchAllPath();
135-
#else
136-
private static readonly Regex _regexCatchAllPath = RegexGlobal.New(@"^[^{{}}]*\{(.*?)\}/?$", RegexOptions.None, TimeSpan.FromMilliseconds(CatchAllPathMilliseconds));
137-
private static Regex RegexCatchAllPath() => _regexCatchAllPath;
138-
#endif
139124

140125
/// <summary>Check if the path template contains a Catch-All path parameter.
141126
/// <para>It means that the path template ends with a placeholder and no other placeholders are present in the path template, without a question mark (query parameters).</para>

src/Ocelot/Infrastructure/ConfigAwarePlaceholders.cs

-5
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,8 @@ public Response Add(string key, Func<Response<string>> func)
4848
public Response Remove(string key)
4949
=> _placeholders.Remove(key);
5050

51-
#if NET7_0_OR_GREATER
5251
[GeneratedRegex(@"[{}]", RegexOptions.None, RegexGlobal.DefaultMatchTimeoutMilliseconds)]
5352
private static partial Regex Regex();
54-
#else
55-
private static readonly Regex _regex = RegexGlobal.New(@"[{}]");
56-
private static Regex Regex() => _regex;
57-
#endif
5853
private static string CleanKey(string key)
5954
=> Regex().Replace(key, string.Empty);
6055

src/Ocelot/Values/UpstreamPathTemplate.cs

-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@ namespace Ocelot.Values;
55
/// <summary>The model to keep data of upstream path.</summary>
66
public partial class UpstreamPathTemplate
77
{
8-
#if NET7_0_OR_GREATER
98
[GeneratedRegex("$^", RegexOptions.Singleline, RegexGlobal.DefaultMatchTimeoutMilliseconds)]
109
private static partial Regex RegexNoTemplate();
11-
#else
12-
private static readonly Regex _regexNoTemplate = RegexGlobal.New("$^", RegexOptions.Singleline);
13-
private static Regex RegexNoTemplate() => _regexNoTemplate;
14-
#endif
1510
private static readonly ConcurrentDictionary<string, Regex> _regex = new();
1611

1712
public UpstreamPathTemplate(string template, int priority, bool containsQueryString, string originalValue)

src/Ocelot/WebSockets/ClientWebSocketOptionsProxy.cs

+1-15
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,9 @@ public ClientWebSocketOptionsProxy(ClientWebSocketOptions options)
1313
_real = options;
1414
}
1515

16-
// .NET 6 and lower doesn't support the properties below.
17-
// Instead of throwing a NotSupportedException, we just implement the getter/setter.
18-
// This way, we can use the same code for .NET 6 and .NET 7+.
1916
// TODO The design should be reviewed since we are hiding the ClientWebSocketOptions properties.
20-
#if NET7_0_OR_GREATER
2117
public Version HttpVersion { get => _real.HttpVersion; set => _real.HttpVersion = value; }
2218
public HttpVersionPolicy HttpVersionPolicy { get => _real.HttpVersionPolicy; set => _real.HttpVersionPolicy = value; }
23-
#else
24-
public Version HttpVersion { get; set; }
25-
public HttpVersionPolicy HttpVersionPolicy { get; set; }
26-
#endif
2719
public bool UseDefaultCredentials { get => _real.UseDefaultCredentials; set => _real.UseDefaultCredentials = value; }
2820
public ICredentials Credentials { get => _real.Credentials; set => _real.Credentials = value; }
2921
public IWebProxy Proxy { get => _real.Proxy; set => _real.Proxy = value; }
@@ -32,16 +24,10 @@ public ClientWebSocketOptionsProxy(ClientWebSocketOptions options)
3224
public CookieContainer Cookies { get => _real.Cookies; set => _real.Cookies = value; }
3325
public TimeSpan KeepAliveInterval { get => _real.KeepAliveInterval; set => _real.KeepAliveInterval = value; }
3426
public WebSocketDeflateOptions DangerousDeflateOptions { get => _real.DangerousDeflateOptions; set => _real.DangerousDeflateOptions = value; }
35-
#if NET7_0_OR_GREATER
3627
public bool CollectHttpResponseDetails { get => _real.CollectHttpResponseDetails; set => _real.CollectHttpResponseDetails = value; }
37-
#else
38-
public bool CollectHttpResponseDetails { get; set; }
39-
#endif
40-
public void AddSubProtocol(string subProtocol) => _real.AddSubProtocol(subProtocol);
4128

29+
public void AddSubProtocol(string subProtocol) => _real.AddSubProtocol(subProtocol);
4230
public void SetBuffer(int receiveBufferSize, int sendBufferSize) => _real.SetBuffer(receiveBufferSize, sendBufferSize);
43-
4431
public void SetBuffer(int receiveBufferSize, int sendBufferSize, ArraySegment<byte> buffer) => _real.SetBuffer(receiveBufferSize, sendBufferSize, buffer);
45-
4632
public void SetRequestHeader(string headerName, string headerValue) => _real.SetRequestHeader(headerName, headerValue);
4733
}

test/Ocelot.AcceptanceTests/CancelRequestTests.cs

-4
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ public async Task ShouldAbortServiceWork_WhenCancellingTheRequest()
4848
// Assert
4949
started.NotificationSent.ShouldBeTrue();
5050
stopped.NotificationSent.ShouldBeFalse();
51-
#if NET8_0_OR_GREATER
5251
ex.ShouldNotBeNull().ShouldBeOfType<TaskCanceledException>();
53-
#else
54-
ex.ShouldNotBeNull().ShouldBeOfType<OperationCanceledException>();
55-
#endif
5652
}
5753

5854
private Task Cancel(Task t) => Task.Run(_ocelotClient.CancelPendingRequests);

test/Ocelot.AcceptanceTests/ServiceDiscovery/ConsulServiceDiscoveryTests.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -594,13 +594,9 @@ private void GivenIResetCounters()
594594
private void GivenTheServicesAreRegisteredWithConsul(params ServiceEntry[] serviceEntries) => _consulServices.AddRange(serviceEntries);
595595
private void GivenTheServiceNodesAreRegisteredWithConsul(params Node[] nodes) => _consulNodes.AddRange(nodes);
596596

597-
#if NET7_0_OR_GREATER
598597
[GeneratedRegex("/v1/health/service/(?<serviceName>[^/]+)", RegexOptions.Singleline, RegexGlobal.DefaultMatchTimeoutMilliseconds)]
599598
private static partial Regex ServiceNameRegex();
600-
#else
601-
private static readonly Regex ServiceNameRegexVar = RegexGlobal.New("/v1/health/service/(?<serviceName>[^/]+)", RegexOptions.Singleline);
602-
private static Regex ServiceNameRegex() => ServiceNameRegexVar;
603-
#endif
599+
604600
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url)
605601
{
606602
_consulHandler.GivenThereIsAServiceRunningOn(url, async context =>

0 commit comments

Comments
 (0)