forked from ThreeMammals/Ocelot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/transform headers (ThreeMammals#204)
* New feature that lets a user do find and replace on an upstream header * can transform downstream and upstream headers, not sure if interface is good * can replace location header with placeholder * added some syntax
- Loading branch information
1 parent
9c048ba
commit d0eee70
Showing
28 changed files
with
1,028 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
Headers Transformation | ||
===================== | ||
|
||
Ocelot allows the user to transform headers pre and post downstream request. At the moment Ocelot only supports find and replace. This feature was requested `GitHub #190 <https://github.com/TomPallister/Ocelot/issues/190>`_ and I decided that it was going to be useful in various ways. | ||
|
||
Syntax | ||
^^^^^^ | ||
|
||
In order to transform a header first we specify the header key and then the type of transform we want e.g. | ||
|
||
.. code-block:: json | ||
"Test": "http://www.bbc.co.uk/, http://ocelot.com/" | ||
The key is "Test" and the value is "http://www.bbc.co.uk/, http://ocelot.com/". The value is saying replace http://www.bbc.co.uk/ with http://ocelot.com/. The syntax is {find}, {replace}. Hopefully pretty simple. There are examples below that explain more. | ||
|
||
Pre Downstream Request | ||
^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Add the following to a ReRoute in configuration.json in order to replace http://www.bbc.co.uk/ with http://ocelot.com/. This header will be changed before the request downstream and will be sent to the downstream server. | ||
|
||
.. code-block:: json | ||
"UpstreamHeaderTransform": { | ||
"Test": "http://www.bbc.co.uk/, http://ocelot.com/" | ||
}, | ||
Post Downstream Request | ||
^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Add the following to a ReRoute in configuration.json in order to replace http://www.bbc.co.uk/ with http://ocelot.com/. This transformation will take place after Ocelot has received the response from the downstream service. | ||
|
||
.. code-block:: json | ||
"DownstreamHeaderTransform": { | ||
"Test": "http://www.bbc.co.uk/, http://ocelot.com/" | ||
}, | ||
Placeholders | ||
^^^^^^^^^^^^ | ||
|
||
Ocelot allows placeholders that can be used in header transformation. At the moment there is only one placeholder. | ||
|
||
{BaseUrl} - This will use Ocelot's base url e.g. http://localhost:5000 as its value. | ||
|
||
Handling 302 Redirects | ||
^^^^^^^^^^^^^^^^^^^^^^ | ||
Ocelot will by default automatically follow redirects however if you want to return the location header to the client you might want to change the location to be Ocelot not the downstream service. Ocelot allows this with the following configuration. | ||
|
||
.. code-block:: json | ||
"DownstreamHeaderTransform": { | ||
"Location": "http://www.bbc.co.uk/, http://ocelot.com/" | ||
}, | ||
"HttpHandlerOptions": { | ||
"AllowAutoRedirect": false, | ||
}, | ||
or you could use the BaseUrl placeholder. | ||
|
||
.. code-block:: json | ||
"DownstreamHeaderTransform": { | ||
"Location": "http://localhost:6773, {BaseUrl}" | ||
}, | ||
"HttpHandlerOptions": { | ||
"AllowAutoRedirect": false, | ||
}, | ||
Ocelot will not try and replace the location header returned by the downstream service with its own URL. |
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
68 changes: 68 additions & 0 deletions
68
src/Ocelot/Configuration/Creator/HeaderFindAndReplaceCreator.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,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Ocelot.Configuration.File; | ||
using Ocelot.Middleware; | ||
|
||
namespace Ocelot.Configuration.Creator | ||
{ | ||
public class HeaderFindAndReplaceCreator : IHeaderFindAndReplaceCreator | ||
{ | ||
private IBaseUrlFinder _finder; | ||
private Dictionary<string, Func<string>> _placeholders; | ||
|
||
public HeaderFindAndReplaceCreator(IBaseUrlFinder finder) | ||
{ | ||
_finder = finder; | ||
_placeholders = new Dictionary<string, Func<string>>(); | ||
_placeholders.Add("{BaseUrl}", () => { | ||
return _finder.Find(); | ||
}); | ||
} | ||
|
||
public HeaderTransformations Create(FileReRoute fileReRoute) | ||
{ | ||
var upstream = new List<HeaderFindAndReplace>(); | ||
|
||
foreach(var input in fileReRoute.UpstreamHeaderTransform) | ||
{ | ||
var hAndr = Map(input); | ||
upstream.Add(hAndr); | ||
} | ||
|
||
var downstream = new List<HeaderFindAndReplace>(); | ||
|
||
foreach(var input in fileReRoute.DownstreamHeaderTransform) | ||
{ | ||
var hAndr = Map(input); | ||
downstream.Add(hAndr); | ||
} | ||
|
||
return new HeaderTransformations(upstream, downstream); | ||
} | ||
|
||
private HeaderFindAndReplace Map(KeyValuePair<string,string> input) | ||
{ | ||
var findAndReplace = input.Value.Split(","); | ||
|
||
var replace = findAndReplace[1].TrimStart(); | ||
|
||
var startOfPlaceholder = replace.IndexOf("{"); | ||
if(startOfPlaceholder > -1) | ||
{ | ||
var endOfPlaceholder = replace.IndexOf("}", startOfPlaceholder); | ||
|
||
var placeholder = replace.Substring(startOfPlaceholder, startOfPlaceholder + (endOfPlaceholder + 1)); | ||
|
||
if(_placeholders.ContainsKey(placeholder)) | ||
{ | ||
var value = _placeholders[placeholder].Invoke(); | ||
replace = replace.Replace(placeholder, value); | ||
} | ||
} | ||
|
||
var hAndr = new HeaderFindAndReplace(input.Key, findAndReplace[0], replace, 0); | ||
|
||
return hAndr; | ||
} | ||
} | ||
} |
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 System.Collections.Generic; | ||
|
||
namespace Ocelot.Configuration.Creator | ||
{ | ||
public class HeaderTransformations | ||
{ | ||
public HeaderTransformations(List<HeaderFindAndReplace> upstream, List<HeaderFindAndReplace> downstream) | ||
{ | ||
Upstream = upstream; | ||
Downstream = downstream; | ||
} | ||
|
||
public List<HeaderFindAndReplace> Upstream {get;private set;} | ||
|
||
public List<HeaderFindAndReplace> Downstream {get;private set;} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Ocelot/Configuration/Creator/IHeaderFindAndReplaceCreator.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,10 @@ | ||
using System.Collections.Generic; | ||
using Ocelot.Configuration.File; | ||
|
||
namespace Ocelot.Configuration.Creator | ||
{ | ||
public interface IHeaderFindAndReplaceCreator | ||
{ | ||
HeaderTransformations Create(FileReRoute fileReRoute); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Ocelot.Configuration | ||
{ | ||
public class HeaderFindAndReplace | ||
{ | ||
public HeaderFindAndReplace(string key, string find, string replace, int index) | ||
{ | ||
Key = key; | ||
Find = find; | ||
Replace = replace; | ||
Index = index; | ||
} | ||
|
||
public string Key {get;} | ||
public string Find {get;} | ||
public string Replace {get;} | ||
|
||
// only index 0 for now.. | ||
public int Index {get;} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Http; | ||
using Ocelot.Configuration; | ||
using Ocelot.Responses; | ||
|
||
namespace Ocelot.Headers | ||
{ | ||
public class HttpContextRequestHeaderReplacer : IHttpContextRequestHeaderReplacer | ||
{ | ||
public Response Replace(HttpContext context, List<HeaderFindAndReplace> fAndRs) | ||
{ | ||
foreach (var f in fAndRs) | ||
{ | ||
if(context.Request.Headers.TryGetValue(f.Key, out var values)) | ||
{ | ||
var replaced = values[f.Index].Replace(f.Find, f.Replace); | ||
context.Request.Headers.Remove(f.Key); | ||
context.Request.Headers.Add(f.Key, replaced); | ||
} | ||
} | ||
|
||
return new OkResponse(); | ||
} | ||
} | ||
} |
Oops, something went wrong.