-
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.
Browse files
Browse the repository at this point in the history
* routing based on headers (all specified headers must match) * routing based on headers for aggregated routes * unit tests and small modifications * find placeholders in header templates * match upstream headers to header templates * find placeholders name and values, fix regex for finding placeholders values * fix unit tests * change header placeholder pattern * unit tests * unit tests * unit tests * unit tests * extend validation with checking upstreamheadertemplates, acceptance tests for cases from the issue * update docs and minor changes * SA1649 File name should match first type name * Fix compilation errors by code review after resolving conflicts * Fix warnings * File-scoped namespaces * File-scoped namespace * Target-typed 'new' expressions (C# 9). https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/target-typed-new * IDE1006 Naming rule violation: These words must begin with upper case characters: should_* * Target-typed 'new' expressions (C# 9). https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/target-typed-new * Fix build errors * DownstreamRouteBuilder * AggregatesCreator * IUpstreamHeaderTemplatePatternCreator, RoutesCreator * UpstreamHeaderTemplatePatternCreator * FileAggregateRoute * FileAggregateRoute * FileRoute * Route, IRoute * FileConfigurationFluentValidator * OcelotBuilder * DownstreamRouteCreator * DownstreamRouteFinder * HeaderMatcher * DownstreamRouteFinderMiddleware * UpstreamHeaderTemplate * Routing folder * RoutingBasedOnHeadersTests * Refactor acceptance tests * AAA pattern in unit tests * CS8936: Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater. * Code review by @RaynaldM * Convert facts to one `Theory` * AAA pattern * Add traits * Update routing.rst Check grammar and style * Update docs --------- Co-authored-by: raman-m <[email protected]>
- Loading branch information
Showing
39 changed files
with
2,101 additions
and
492 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
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
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
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
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
17 changes: 17 additions & 0 deletions
17
src/Ocelot/Configuration/Creator/IUpstreamHeaderTemplatePatternCreator.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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Ocelot.Configuration.File; | ||
using Ocelot.Values; | ||
|
||
namespace Ocelot.Configuration.Creator; | ||
|
||
/// <summary> | ||
/// Ocelot feature: <see href="https://github.com/ThreeMammals/Ocelot/blob/develop/docs/features/routing.rst#upstream-headers">Routing based on request header</see>. | ||
/// </summary> | ||
public interface IUpstreamHeaderTemplatePatternCreator | ||
{ | ||
/// <summary> | ||
/// Creates upstream templates based on route headers. | ||
/// </summary> | ||
/// <param name="route">The route info.</param> | ||
/// <returns>An <see cref="IDictionary{TKey, TValue}"/> object where TKey is <see langword="string"/>, TValue is <see cref="UpstreamHeaderTemplate"/>.</returns> | ||
IDictionary<string, UpstreamHeaderTemplate> Create(IRoute route); | ||
} |
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
50 changes: 50 additions & 0 deletions
50
src/Ocelot/Configuration/Creator/UpstreamHeaderTemplatePatternCreator.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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Ocelot.Configuration.File; | ||
using Ocelot.Values; | ||
|
||
namespace Ocelot.Configuration.Creator; | ||
|
||
/// <summary> | ||
/// Default creator of upstream templates based on route headers. | ||
/// </summary> | ||
/// <remarks>Ocelot feature: Routing based on request header.</remarks> | ||
public partial class UpstreamHeaderTemplatePatternCreator : IUpstreamHeaderTemplatePatternCreator | ||
{ | ||
private const string PlaceHolderPattern = @"(\{header:.*?\})"; | ||
#if NET7_0_OR_GREATER | ||
[GeneratedRegex(PlaceHolderPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline, "en-US")] | ||
private static partial Regex RegExPlaceholders(); | ||
#else | ||
private static readonly Regex RegExPlaceholdersVar = new(PlaceHolderPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromMilliseconds(1000)); | ||
private static Regex RegExPlaceholders() => RegExPlaceholdersVar; | ||
#endif | ||
|
||
public IDictionary<string, UpstreamHeaderTemplate> Create(IRoute route) | ||
{ | ||
var result = new Dictionary<string, UpstreamHeaderTemplate>(); | ||
|
||
foreach (var headerTemplate in route.UpstreamHeaderTemplates) | ||
{ | ||
var headerTemplateValue = headerTemplate.Value; | ||
var matches = RegExPlaceholders().Matches(headerTemplateValue); | ||
|
||
if (matches.Count > 0) | ||
{ | ||
var placeholders = matches.Select(m => m.Groups[1].Value).ToArray(); | ||
for (int i = 0; i < placeholders.Length; i++) | ||
{ | ||
var indexOfPlaceholder = headerTemplateValue.IndexOf(placeholders[i]); | ||
var placeholderName = placeholders[i][8..^1]; // remove "{header:" and "}" | ||
headerTemplateValue = headerTemplateValue.Replace(placeholders[i], $"(?<{placeholderName}>.+)"); | ||
} | ||
} | ||
|
||
var template = route.RouteIsCaseSensitive | ||
? $"^{headerTemplateValue}$" | ||
: $"^(?i){headerTemplateValue}$"; // ignore case | ||
|
||
result.Add(headerTemplate.Key, new(template, headerTemplate.Value)); | ||
} | ||
|
||
return result; | ||
} | ||
} |
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.