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

Manipulate headers to downstream hosts #1267

Closed
jeroenvheel opened this issue Jun 21, 2020 · 3 comments
Closed

Manipulate headers to downstream hosts #1267

jeroenvheel opened this issue Jun 21, 2020 · 3 comments

Comments

@jeroenvheel
Copy link

jeroenvheel commented Jun 21, 2020

Hi is was playing with Ocelot and was wondering if it was possible to add information in the headers for the downstream hosts

I'm trying to add the subdomain to the headers so my back end logic can switch things based on a tenant domain.

I found this in the closed issues #1011

        var configuration = new OcelotPipelineConfiguration()
        {
            PreQueryStringBuilderMiddleware = async (context, next) =>
            {
                var host = $"{context.HttpContext.Request.Host.Value}";
                context.DownstreamRequest.Headers.Add("OriginalHost", host);
                await next.Invoke();
            }
        };

but the solution gives a error on the HttpContext and the DownstreamRequest
when i try to fix the code i can't find the DownstreamRequest

Ocelot version: 16.0.1

@jeroenvheel
Copy link
Author

I reverted back to 15.0.6 and found out the above code is working.
Sinds version 15.0.7 the solution is broken.

@rh101
Copy link

rh101 commented Jun 25, 2020

@jeroenvheel This may be an alternative solution for you:

public class HostInjectorDelegatingHandler : DelegatingHandler
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public HostInjectorDelegatingHandler(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var host = $"{_httpContextAccessor.HttpContext.Request.Host.Value}";
        request.Headers.Add("OriginalHost",  host);
        return base.SendAsync(request, cancellationToken);
    }
}

In Startup.cs, add the handler like this:

services.AddHttpContextAccessor(); // This is required to enable access to the HttpContext

services.AddOcelot()
    .AddDelegatingHandler<HostInjectorDelegatingHandler>();

Then in your ocelot config, you set the delegating handler as follows:

{
    "DownstreamPathTemplate": ...
    "UpstreamPathTemplate": ...
    "DelegatingHandlers": [
        "HostInjectorDelegatingHandler"
    ]
}

Note: There may be an issue with the above implementation for version 15.0.7+, as described in #1252

@jeroenvheel
Copy link
Author

@rh101 Thanks ! this is working perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants