You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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]>
Copy file name to clipboardExpand all lines: docs/features/configuration.rst
+6-3
Original file line number
Diff line number
Diff line change
@@ -19,9 +19,11 @@ Here is an example Route configuration. You don't need to set all of these thing
19
19
.. code-block:: json
20
20
21
21
{
22
-
"DownstreamPathTemplate": "/",
23
22
"UpstreamPathTemplate": "/",
23
+
"UpstreamHeaderTemplates": {}, // dictionary
24
+
"UpstreamHost": "",
24
25
"UpstreamHttpMethod": [ "Get" ],
26
+
"DownstreamPathTemplate": "/",
25
27
"DownstreamHttpMethod": "",
26
28
"DownstreamHttpVersion": "",
27
29
"AddHeadersToRequest": {},
@@ -37,7 +39,7 @@ Here is an example Route configuration. You don't need to set all of these thing
37
39
"ServiceName": "",
38
40
"DownstreamScheme": "http",
39
41
"DownstreamHostAndPorts": [
40
-
{ "Host": "localhost", "Port": 51876 }
42
+
{ "Host": "localhost", "Port": 12345 }
41
43
],
42
44
"QoSOptions": {
43
45
"ExceptionsAllowedBeforeBreaking": 0,
@@ -70,7 +72,8 @@ Here is an example Route configuration. You don't need to set all of these thing
70
72
}
71
73
}
72
74
73
-
More information on how to use these options is below.
75
+
The actual Route schema for properties can be found in the C# `FileRoute <https://github.com/ThreeMammals/Ocelot/blob/main/src/Ocelot/Configuration/File/FileRoute.cs>`_ class.
76
+
If you're interested in learning more about how to utilize these options, read below!
Copy file name to clipboardExpand all lines: docs/features/routing.rst
+57-4
Original file line number
Diff line number
Diff line change
@@ -154,6 +154,58 @@ The Route above will only be matched when the ``Host`` header value is ``somedom
154
154
If you do not set **UpstreamHost** on a Route then any ``Host`` header will match it.
155
155
This means that if you have two Routes that are the same, apart from the **UpstreamHost**, where one is null and the other set Ocelot will favour the one that has been set.
156
156
157
+
.. _routing-upstream-headers:
158
+
159
+
Upstream Headers [#f3]_
160
+
-----------------------
161
+
162
+
In addition to routing by ``UpstreamPathTemplate``, you can also define ``UpstreamHeaderTemplates``.
163
+
For a route to match, all headers specified in this dictionary object must be present in the request headers.
164
+
165
+
.. code-block:: json
166
+
167
+
{
168
+
// ...
169
+
"UpstreamPathTemplate": "/",
170
+
"UpstreamHttpMethod": [ "Get" ],
171
+
"UpstreamHeaderTemplates": { // dictionary
172
+
"country": "uk", // 1st header
173
+
"version": "v1"// 2nd header
174
+
}
175
+
}
176
+
177
+
In this scenario, the route will only match if a request includes both headers with the specified values.
178
+
179
+
Header placeholders
180
+
^^^^^^^^^^^^^^^^^^^
181
+
182
+
Let's explore a more intriguing scenario where placeholders can be effectively utilized within your ``UpstreamHeaderTemplates``.
183
+
184
+
Consider the following approach using the special placeholder format ``{header:placeholdername}``:
185
+
186
+
.. code-block:: json
187
+
188
+
{
189
+
"DownstreamPathTemplate": "/{versionnumber}/api", // with placeholder
190
+
"DownstreamScheme": "https",
191
+
"DownstreamHostAndPorts": [
192
+
{ "Host": "10.0.10.1", "Port": 80 }
193
+
],
194
+
"UpstreamPathTemplate": "/api",
195
+
"UpstreamHttpMethod": [ "Get" ],
196
+
"UpstreamHeaderTemplates": {
197
+
"version": "{header:versionnumber}"// 'header:' prefix vs placeholder
198
+
}
199
+
}
200
+
201
+
In this scenario, the entire value of the request header "**version**" is inserted into the ``DownstreamPathTemplate``.
202
+
If necessary, a more intricate upstream header template can be specified, using placeholders such as ``version-{header:version}_country-{header:country}``.
203
+
204
+
**Note 1**: Placeholders are not required in ``DownstreamPathTemplate``.
205
+
This scenario can be utilized to mandate a specific header regardless of its value.
206
+
207
+
**Note 2**: Additionally, the ``UpstreamHeaderTemplates`` dictionary options are applicable for :doc:`../features/requestaggregation` as well.
208
+
157
209
Priority
158
210
--------
159
211
@@ -294,7 +346,7 @@ Here are two user scenarios.
294
346
295
347
.. _routing-security-options:
296
348
297
-
Security Options [#f3]_
349
+
Security Options [#f4]_
298
350
-----------------------
299
351
300
352
Ocelot allows you to manage multiple patterns for allowed/blocked IPs using the `IPAddressRange <https://github.com/jsakamoto/ipaddressrange>`_ package
@@ -326,7 +378,7 @@ The current patterns managed are the following:
326
378
327
379
.. _routing-dynamic:
328
380
329
-
Dynamic Routing [#f4]_
381
+
Dynamic Routing [#f5]_
330
382
----------------------
331
383
332
384
The idea is to enable dynamic routing when using a :doc:`../features/servicediscovery` provider so you don't have to provide the Route config.
@@ -336,5 +388,6 @@ See the :ref:`sd-dynamic-routing` docs if this sounds interesting to you.
336
388
337
389
.. [#f1] ":ref:`routing-empty-placeholders`" feature is available starting in version `23.0 <https://github.com/ThreeMammals/Ocelot/releases/tag/23.0.0>`_, see issue `748 <https://github.com/ThreeMammals/Ocelot/issues/748>`_ and the `23.0 <https://github.com/ThreeMammals/Ocelot/releases/tag/23.0.0>`__ release notes for details.
338
390
.. [#f2] ":ref:`routing-upstream-host`" feature was requested as part of `issue 216 <https://github.com/ThreeMammals/Ocelot/pull/216>`_.
339
-
.. [#f3] ":ref:`routing-security-options`" feature was requested as part of `issue 628 <https://github.com/ThreeMammals/Ocelot/issues/628>`_ (of `12.0.1 <https://github.com/ThreeMammals/Ocelot/releases/tag/12.0.1>`_ version), then redesigned and improved by `issue 1400 <https://github.com/ThreeMammals/Ocelot/issues/1400>`_, and published in version `20.0 <https://github.com/ThreeMammals/Ocelot/releases/tag/20.0.0>`_ docs.
340
-
.. [#f4] ":ref:`routing-dynamic`" feature was requested as part of `issue 340 <https://github.com/ThreeMammals/Ocelot/issues/340>`_. Complete reference: :ref:`sd-dynamic-routing`.
391
+
.. [#f3] ":ref:`routing-upstream-headers`" feature was proposed in `issue 360 <https://github.com/ThreeMammals/Ocelot/issues/360>`_, and released in version `24.0 <https://github.com/ThreeMammals/Ocelot/releases/tag/24.0.0>`_.
392
+
.. [#f4] ":ref:`routing-security-options`" feature was requested as part of `issue 628 <https://github.com/ThreeMammals/Ocelot/issues/628>`_ (of `12.0.1 <https://github.com/ThreeMammals/Ocelot/releases/tag/12.0.1>`_ version), then redesigned and improved by `issue 1400 <https://github.com/ThreeMammals/Ocelot/issues/1400>`_, and published in version `20.0 <https://github.com/ThreeMammals/Ocelot/releases/tag/20.0.0>`_ docs.
393
+
.. [#f5] ":ref:`routing-dynamic`" feature was requested as part of `issue 340 <https://github.com/ThreeMammals/Ocelot/issues/340>`_. Complete reference: :ref:`sd-dynamic-routing`.
/// Ocelot feature: <see href="https://github.com/ThreeMammals/Ocelot/blob/develop/docs/features/routing.rst#upstream-headers">Routing based on request header</see>.
/// Creates upstream templates based on route headers.
13
+
/// </summary>
14
+
/// <param name="route">The route info.</param>
15
+
/// <returns>An <see cref="IDictionary{TKey, TValue}"/> object where TKey is <see langword="string"/>, TValue is <see cref="UpstreamHeaderTemplate"/>.</returns>
0 commit comments