-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integration of additional PR feedback
- Loading branch information
Showing
12 changed files
with
308 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 4 additions & 5 deletions
9
src/Ocelot/Configuration/Creator/IUpstreamHeaderRoutingOptionsCreator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
using Ocelot.Configuration.File; | ||
|
||
namespace Ocelot.Configuration.Creator | ||
namespace Ocelot.Configuration.Creator; | ||
|
||
public interface IUpstreamHeaderRoutingOptionsCreator | ||
{ | ||
public interface IUpstreamHeaderRoutingOptionsCreator | ||
{ | ||
UpstreamHeaderRoutingOptions Create(FileUpstreamHeaderRoutingOptions options); | ||
} | ||
UpstreamHeaderRoutingOptions Create(FileUpstreamHeaderRoutingOptions options); | ||
} |
30 changes: 16 additions & 14 deletions
30
src/Ocelot/Configuration/Creator/UpstreamHeaderRoutingOptionsCreator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using Ocelot.Configuration.File; | ||
|
||
namespace Ocelot.Configuration.Creator | ||
namespace Ocelot.Configuration.Creator; | ||
|
||
public class UpstreamHeaderRoutingOptionsCreator : IUpstreamHeaderRoutingOptionsCreator | ||
{ | ||
public class UpstreamHeaderRoutingOptionsCreator : IUpstreamHeaderRoutingOptionsCreator | ||
public UpstreamHeaderRoutingOptions Create(FileUpstreamHeaderRoutingOptions options) | ||
{ | ||
public UpstreamHeaderRoutingOptions Create(FileUpstreamHeaderRoutingOptions options) | ||
var mode = UpstreamHeaderRoutingTriggerMode.Any; | ||
if (options.TriggerOn.Length > 0) | ||
{ | ||
UpstreamHeaderRoutingTriggerMode mode = UpstreamHeaderRoutingTriggerMode.Any; | ||
if (options.TriggerOn.Length > 0) | ||
{ | ||
mode = Enum.Parse<UpstreamHeaderRoutingTriggerMode>(options.TriggerOn, true); | ||
} | ||
mode = Enum.Parse<UpstreamHeaderRoutingTriggerMode>(options.TriggerOn, true); | ||
} | ||
|
||
Dictionary<string, HashSet<string>> headers = options.Headers.ToDictionary( | ||
kv => kv.Key.ToLowerInvariant(), | ||
kv => new HashSet<string>(kv.Value.Select(v => v.ToLowerInvariant()))); | ||
// Keys are converted to uppercase as apparently that is the preferred | ||
// approach according to https://learn.microsoft.com/en-us/dotnet/standard/base-types/best-practices-strings | ||
// Values are left untouched but value comparison at runtime is done in | ||
// a case-insensitive manner by using the appropriate StringComparer. | ||
var headers = options.Headers.ToDictionary( | ||
kv => kv.Key.ToUpperInvariant(), | ||
kv => kv.Value); | ||
|
||
return new UpstreamHeaderRoutingOptions(headers, mode); | ||
} | ||
return new UpstreamHeaderRoutingOptions(headers, mode); | ||
} | ||
} |
17 changes: 5 additions & 12 deletions
17
src/Ocelot/Configuration/File/FileUpstreamHeaderRoutingOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,10 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Ocelot.Configuration.File | ||
{ | ||
public class FileUpstreamHeaderRoutingOptions | ||
{ | ||
public FileUpstreamHeaderRoutingOptions() | ||
{ | ||
Headers = new Dictionary<string, IList<string>>(); | ||
TriggerOn = string.Empty; | ||
} | ||
namespace Ocelot.Configuration.File; | ||
|
||
public IDictionary<string, IList<string>> Headers { get; set; } | ||
public class FileUpstreamHeaderRoutingOptions | ||
{ | ||
public IDictionary<string, ICollection<string>> Headers { get; set; } = new Dictionary<string, ICollection<string>>(); | ||
|
||
public string TriggerOn { get; set; } | ||
} | ||
public string TriggerOn { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Ocelot.Configuration | ||
namespace Ocelot.Configuration; | ||
|
||
public class UpstreamHeaderRoutingOptions | ||
{ | ||
public class UpstreamHeaderRoutingOptions | ||
public UpstreamHeaderRoutingOptions(IReadOnlyDictionary<string, ICollection<string>> headers, UpstreamHeaderRoutingTriggerMode mode) | ||
{ | ||
public UpstreamHeaderRoutingOptions(IReadOnlyDictionary<string, HashSet<string>> headers, UpstreamHeaderRoutingTriggerMode mode) | ||
{ | ||
Headers = new UpstreamRoutingHeaders(headers); | ||
Mode = mode; | ||
} | ||
Headers = new UpstreamRoutingHeaders(headers); | ||
Mode = mode; | ||
} | ||
|
||
public bool Enabled() => Headers.Any(); | ||
public bool Enabled() => Headers.Any(); | ||
|
||
public UpstreamRoutingHeaders Headers { get; } | ||
public UpstreamRoutingHeaders Headers { get; } | ||
|
||
public UpstreamHeaderRoutingTriggerMode Mode { get; } | ||
} | ||
public UpstreamHeaderRoutingTriggerMode Mode { get; } | ||
} |
11 changes: 5 additions & 6 deletions
11
src/Ocelot/Configuration/UpstreamHeaderRoutingTriggerMode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
namespace Ocelot.Configuration | ||
namespace Ocelot.Configuration; | ||
|
||
public enum UpstreamHeaderRoutingTriggerMode : byte | ||
{ | ||
public enum UpstreamHeaderRoutingTriggerMode | ||
{ | ||
Any = 0, | ||
All = 1, | ||
} | ||
Any, | ||
All, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Ocelot.Configuration | ||
namespace Ocelot.Configuration; | ||
|
||
public class UpstreamRoutingHeaders | ||
{ | ||
public class UpstreamRoutingHeaders | ||
{ | ||
public IReadOnlyDictionary<string, HashSet<string>> Headers { get; } | ||
public IReadOnlyDictionary<string, ICollection<string>> Headers { get; } | ||
|
||
public UpstreamRoutingHeaders(IReadOnlyDictionary<string, HashSet<string>> headers) | ||
{ | ||
Headers = headers; | ||
} | ||
public UpstreamRoutingHeaders(IReadOnlyDictionary<string, ICollection<string>> headers) | ||
{ | ||
Headers = headers; | ||
} | ||
|
||
public bool Any() => Headers.Any(); | ||
public bool Any() => Headers.Any(); | ||
|
||
public bool HasAnyOf(IHeaderDictionary requestHeaders) | ||
public bool HasAnyOf(IHeaderDictionary requestHeaders) | ||
{ | ||
IHeaderDictionary normalizedHeaders = NormalizeHeaderNames(requestHeaders); | ||
foreach (var h in Headers) | ||
{ | ||
IHeaderDictionary lowerCaseHeaders = GetLowerCaseHeaders(requestHeaders); | ||
foreach (KeyValuePair<string, HashSet<string>> h in Headers) | ||
if (normalizedHeaders.TryGetValue(h.Key, out var values) && | ||
h.Value.Intersect(values, StringComparer.OrdinalIgnoreCase).Any()) | ||
{ | ||
if (lowerCaseHeaders.TryGetValue(h.Key, out var values)) | ||
{ | ||
HashSet<string> requestHeaderValues = new(values); | ||
if (h.Value.Overlaps(requestHeaderValues)) | ||
{ | ||
return true; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public bool HasAllOf(IHeaderDictionary requestHeaders) | ||
return false; | ||
} | ||
|
||
public bool HasAllOf(IHeaderDictionary requestHeaders) | ||
{ | ||
IHeaderDictionary normalizedHeaders = NormalizeHeaderNames(requestHeaders); | ||
foreach (var h in Headers) | ||
{ | ||
IHeaderDictionary lowerCaseHeaders = GetLowerCaseHeaders(requestHeaders); | ||
foreach (KeyValuePair<string, HashSet<string>> h in Headers) | ||
if (!normalizedHeaders.TryGetValue(h.Key, out var values)) | ||
{ | ||
if (!lowerCaseHeaders.TryGetValue(h.Key, out var values)) | ||
{ | ||
return false; | ||
} | ||
|
||
HashSet<string> requestHeaderValues = new(values); | ||
if (!h.Value.Overlaps(requestHeaderValues)) | ||
{ | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static IHeaderDictionary GetLowerCaseHeaders(IHeaderDictionary headers) | ||
{ | ||
IHeaderDictionary lowerCaseHeaders = new HeaderDictionary(); | ||
foreach (KeyValuePair<string, StringValues> kv in headers) | ||
if (!h.Value.Intersect(values, StringComparer.OrdinalIgnoreCase).Any()) | ||
{ | ||
string key = kv.Key.ToLowerInvariant(); | ||
StringValues values = new(kv.Value.Select(v => v.ToLowerInvariant()).ToArray()); | ||
lowerCaseHeaders.Add(key, values); | ||
return false; | ||
} | ||
} | ||
|
||
return lowerCaseHeaders; | ||
return true; | ||
} | ||
|
||
private static IHeaderDictionary NormalizeHeaderNames(IHeaderDictionary headers) | ||
{ | ||
var upperCaseHeaders = new HeaderDictionary(); | ||
foreach (KeyValuePair<string, StringValues> kv in headers) | ||
{ | ||
var key = kv.Key.ToUpperInvariant(); | ||
upperCaseHeaders.Add(key, kv.Value); | ||
} | ||
|
||
return upperCaseHeaders; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.