-
Notifications
You must be signed in to change notification settings - Fork 1.7k
#360 Routing based on request header #1312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
raman-m
merged 49 commits into
ThreeMammals:release/24.0
from
jlukawska:feature/360-routing-based-on-request-header
Apr 18, 2024
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
36a1e28
routing based on headers (all specified headers must match)
jlukawska 3630d50
routing based on headers for aggregated routes
jlukawska c392809
unit tests and small modifications
jlukawska 1b74d53
find placeholders in header templates
jlukawska e121c8a
match upstream headers to header templates
jlukawska 073c6fe
find placeholders name and values, fix regex for finding placeholders…
jlukawska 800a025
fix unit tests
jlukawska a72a3d9
change header placeholder pattern
jlukawska c2829e7
unit tests
jlukawska e14ce8e
unit tests
jlukawska 4ad836a
unit tests
jlukawska 565e1c2
unit tests
jlukawska d797679
extend validation with checking upstreamheadertemplates, acceptance t…
jlukawska 8c9cd40
update docs and minor changes
jlukawska cac4c3b
SA1649 File name should match first type name
raman-m d2b9cd7
Fix compilation errors by code review after resolving conflicts
raman-m 30f5b3d
Fix warnings
raman-m 2a32d1f
File-scoped namespaces
raman-m dec3145
File-scoped namespace
raman-m e13f19a
Target-typed 'new' expressions (C# 9).
raman-m 39edcaa
IDE1006 Naming rule violation: These words must begin with upper case…
raman-m 2dcb983
Target-typed 'new' expressions (C# 9).
raman-m d6c8659
Fix build errors
raman-m 29d58b7
DownstreamRouteBuilder
raman-m 8a64955
AggregatesCreator
raman-m 864b63a
IUpstreamHeaderTemplatePatternCreator, RoutesCreator
raman-m 79adba6
UpstreamHeaderTemplatePatternCreator
raman-m 9a1d117
FileAggregateRoute
raman-m 7b2dd43
FileAggregateRoute
raman-m 0bf166e
FileRoute
raman-m a9f3f0e
Route, IRoute
raman-m 790d590
FileConfigurationFluentValidator
raman-m 38cd00c
OcelotBuilder
raman-m 0667a93
DownstreamRouteCreator
raman-m 079f9d2
DownstreamRouteFinder
raman-m 32d8310
HeaderMatcher
raman-m 36cf99f
DownstreamRouteFinderMiddleware
raman-m 98fe814
UpstreamHeaderTemplate
raman-m ea55272
Routing folder
raman-m 16f6f88
RoutingBasedOnHeadersTests
raman-m 7f5bb67
Refactor acceptance tests
raman-m 6e7ab00
AAA pattern in unit tests
raman-m 6aeef96
CS8936: Feature 'collection expressions' is not available in C# 10.0.
raman-m c727b93
Code review by @RaynaldM
raman-m 7e98824
Convert facts to one `Theory`
raman-m 6a6a417
AAA pattern
raman-m 75c9d85
Add traits
raman-m 57e2fdb
Update routing.rst
raman-m dc5a671
Update docs
raman-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) | ||
|
raman-m marked this conversation as resolved.
|
||
| { | ||
| 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.