Skip to content
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

#360 Routing based on request header #1312

Conversation

jlukawska
Copy link
Contributor

@jlukawska jlukawska commented Aug 14, 2020

Closes #360

Proposed Changes

Additionally to routing by UpstreamPathTemplate, you can define UpstreamHeaderTemplates. Then to match a route, all the headers from this section must exist in the request headers.

The example:

    {
        "DownstreamPathTemplate": "/",
        "DownstreamScheme": "https",
        "DownstreamHostAndPorts": [
                { "Host": "10.0.10.1", "Port": 80 }
            ],
        "UpstreamPathTemplate": "/",
        "UpstreamHttpMethod": [ "Get" ],
        "UpstreamHeaderTemplates": {
            "country": "uk",
            "version": "v1"
        }
    }

In this case the route will be matched only if a request has both headers set to the configured values.

You can use placeholders in your UpstreamHeaderTemplates in the following way:

    {
        "DownstreamPathTemplate": "/{versionnumber}/api",
        "DownstreamScheme": "https",
        "DownstreamHostAndPorts": [
                { "Host": "10.0.10.1", "Port": 80 }
            ],
        "UpstreamPathTemplate": "/api",
        "UpstreamHttpMethod": [ "Get" ],
        "UpstreamHeaderTemplates": {
            "version": "{header:versionnumber}"
        }
    }

In this case the whole value for the request header version will be placed into the DownstreamPathTemplate. If you need, you can specify more complex upstream header template with placeholders like e.g. version-{header:version}_country-{header:country}.

Placeholders do not have to exist in DownstreamPathTemplate. This case may be used to require specific header no matter what the value it contains.

UpstreamHeaderTemplates section can be used also for Aggregates.

Copy link

@Thanos06660 Thanos06660 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good with one little comment ;)

@fadil-khodabuccus-cko
Copy link

Hi @Thanos06660 @jlukawska,

Thanks for this PR! This is something super useful :)

Do we know if this is going to be released soon?

Best,
Fadil

@CorporateActionMan
Copy link

CorporateActionMan commented Apr 1, 2022

@fadil-khodabuccus-cko commented on Apr 8, 2021

I agree this is super useful and should be released

@stodolos
Copy link

Vote up! Any word on when this will be released?

@madebykrol
Copy link

Hello.

Just wanted to check in to see if there is any progress to release this?

@akanmf
Copy link

akanmf commented Apr 13, 2023

Hi @jlukawska, Any news ?

@felipepiresdejesus
Copy link

felipepiresdejesus commented Apr 13, 2023

Hi, please accept it. We are really needing this feature and this is opened since 2020

@jlukawska
Copy link
Contributor Author

Hi @jlukawska, Any news ?

Unfortunately, I don't have rights to merge it. It seems that the Ocelot project has been abandoned by its owner :(

@raman-m raman-m changed the title Feature/360 routing based on request header #360 Routing based on request header Jul 17, 2023
@raman-m raman-m changed the base branch from master to develop July 17, 2023 19:36
@raman-m
Copy link
Member

raman-m commented Jul 17, 2023

@felipepiresdejesus commented on Apr 13, 2023:

Hi, please accept it. We are really needing this feature and this is opened since 2020

Don't worry, Felipe! 😊
Coming soon... 😎

Will you resolve all merge conflicts? 😉
It is a joke!

@raman-m raman-m added the conflicts Feature branch has merge conflicts label Jul 17, 2023
@raman-m
Copy link
Member

raman-m commented Jul 18, 2023

Well... @jlukawska

Before resolving merge conflicts for this PR, could we work on develop branch in your fork, please?
After merging PR 7 your repo has redundant merge commit. That was happen because you used "Merge commit" option while merging the PR. For canonic updates it is better to use fast-forwarding option (rebase).

Step 1

To fix that you need to remove current top-commit which is merge commit (it has 2 parents). Use this command:

git checkout develop
git reset --hard HEAD~1

Reset command will report: HEAD is now at 0af49617 Merge pull request #2 from ThreeMammals/develop

After that, try to Sync fork please?
I hope it should help you to make canonic update of develop branch. I hope Sync Fork operation will apply rebasing.

Step 2

Because you have other merge commits in develop branch which don't belong to head repository, I expect Sync Fork will fail because of some reason. In this case it is better just to remove develop branch, and provide new Sync Fork to pull develop branch entirely.

Step 3

If step 2 failed (no the same top commit in develop), just delete develop branch using GitHub UI and make git fetch --all, and remove develop locally.
After that use this script to pull develop from head (upstream) repository:

git remote add upstream https://github.com/ThreeMammals/Ocelot.git // add original repo as upstream remote
git fetch upstream                                                 // pull changes from original repo
git checkout -b develop upstream/develop                           // pull down develop branch locally
git push -u origin develop                                         // push develop branch to fork

Don't forget to make develop branch as default one.

Let me know upgrading results for develop branch, please!
Thank you!

@raman-m raman-m force-pushed the feature/360-routing-based-on-request-header branch from f5f437a to 8adb2e8 Compare August 2, 2023 17:20
@raman-m raman-m added feature A new feature needs feedback Issue is waiting on feedback before acceptance and removed conflicts Feature branch has merge conflicts labels Aug 2, 2023
@raman-m
Copy link
Member

raman-m commented Aug 2, 2023

The feature branch has been rebased onto ThreeMammals:develop! ✔️
All merge conflicts have been resolved successfully during rebase-operation! ✔️

@jlukawska
I thought you need a help in conflicts resolution.
We are now in the code review stage! 🎉

@raman-m
Copy link
Member

raman-m commented Aug 4, 2023

@BrettJG As the issue #360 author,
Could you share your opinion regarding this solution please?


@netren20000 @iderbyshev @pliolis @lalocarbone @Phiph @mrclayman @jrsanbornjr @sachinjagdale @SebastianMajewski @falcopr @brettwinters @RickJNash @PraveenValavan
After your hot discussions for #360 in the past, I am inviting you to review this solution, plz.
So, I will appreciate your feedbacks!

@raman-m
Copy link
Member

raman-m commented Aug 4, 2023

@Thanos06660 @fadil-khodabuccus-cko @CorporateActionMan @stodolos @madebykrol @akanmf @felipepiresdejesus
Welcome to code review session!
I know you've been waiting for this feature for years, but let's improve the quality of the solution. 😉

@raman-m raman-m force-pushed the feature/360-routing-based-on-request-header branch from a9ab2a3 to 6aeef96 Compare April 12, 2024 18:25
@raman-m
Copy link
Member

raman-m commented Apr 15, 2024

@RaynaldM approved these changes on Apr 15

Ray, all the issues you reviewed have been resolved in the commit c727b93.
I will continue working for a bit longer, though no substantial changes are expected.

Copy link
Member

@raman-m raman-m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Development Complete❕

Awaiting Final Code Reviews❗

@raman-m
Copy link
Member

raman-m commented Apr 16, 2024

@ggnaegi Could you review please?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ggnaegi Take a look at the new static class in the Ocelot.DependencyInjection namespace!

Comment on lines +147 to +148
// Features
Services.AddHeaderRouting();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ggnaegi So, this adds the feature. Additionally, you've restructured the code and introduced a new MessageInvokerPool feature at line 120: Services.AddOcelotMessageInvokerPool();.
I'm interested in expanding on this concept. It would be beneficial to have a feature-based design for Ocelot in the future.

Copy link
Member

@raman-m raman-m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready for delivery ✅

  • Code review ✔️✔️
  • Team approvals ✔️✔️✔️
  • Unit testing ✔️✔️✔️ →
    • UpstreamHeaderTemplatePatternCreatorTests
    • HeaderPlaceholderNameAndValueFinderTests
    • HeadersToHeaderTemplatesMatcherTests
    • extra FileConfigurationFluentValidatorTests, DownstreamRouteFinderTests
  • Acceptance testing ✔️ → RoutingBasedOnHeadersTests
  • Feature docs ✔️ → routing.rst

@raman-m raman-m merged commit 47a1379 into ThreeMammals:release/24.0 Apr 18, 2024
1 check passed
@raman-m
Copy link
Member

raman-m commented Apr 18, 2024

@jlukawska Dear Jolanta,
After spanning over three years on this journey of development and code reviews, I am announcing that your feature has finally merged! 🎉

Congrats on this significant milestone! 🥳

Good job! Your dedication and hard work are truly appreciated. I hope to see you return to our project soon! 😇

P.S.

The feature will be officially released once all PRs associated with the Annual 2023 milestone are successfully closed.

  • while the optimistic scenario anticipates a release by the end of April
  • a more realistic outlook suggests that the release may occur in May

ggnaegi pushed a commit that referenced this pull request May 3, 2024
* 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]>
@raman-m raman-m added Spring'24 Spring 2024 release and removed 2023 Annual 2023 release labels May 22, 2024
@raman-m raman-m modified the milestones: Annual 2023, Spring'24 May 22, 2024
raman-m added a commit that referenced this pull request May 23, 2024
* 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]>
@raman-m
Copy link
Member

raman-m commented May 23, 2024

Feature commit in develop → 6cef42e

@raman-m raman-m deleted the feature/360-routing-based-on-request-header branch May 23, 2024 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new feature Routing Ocelot feature: Routing Spring'24 Spring 2024 release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Routing based on Request Header