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

#1252 Fix HttpContext in DelegatingHandler #1268

Conversation

jlukawska
Copy link
Contributor

@jlukawska jlukawska commented Jun 23, 2020

Fixes #1252

The problem

After changes from fe3e8bd there is a new instance of HttpContext created for each request (or even more instances for aggregated routes). If HttpContextAccessor is used to obtain a HttpContext instance in a delegating handler class, it will return the original HttpContext without e.g. authentication data that is added to the new instance.

Proposed Changes

  • To obtain a HttpContext, a delegating handler class should implement IDelegatingHandlerWithHttpContext interface
  • DelegatingHandlerHandlerFactory will add the valid HttpContext as a property to the delegating handler class

@jlukawska jlukawska marked this pull request as draft April 28, 2021 11:24
@jlukawska jlukawska marked this pull request as ready for review May 7, 2021 10:39
stefancruz added a commit to stefancruz/Ocelot that referenced this pull request Dec 22, 2022
Conflicts:
	src/Ocelot/Requester/HttpClientBuilder.cs
	src/Ocelot/Requester/HttpClientWrapper.cs

Cherry picked from ThreeMammals#1268
@raman-m raman-m changed the base branch from master to develop June 26, 2023 10:02
@raman-m raman-m changed the title Fix/1252 HttpContext in DelegatingHandler #1252 Fix HttpContext in DelegatingHandler Jun 26, 2023
@raman-m
Copy link
Member

raman-m commented Jun 26, 2023

Hi J!
Thanks for a great PR!

I've merged latest top-commits from develop, resolved merge conflicts, fixed a few compile errors.
But all acceptance ClaimsInDelegatingHandlerTests are failing because of this error 😞

Message: 
System.Net.Http.HttpRequestException : Response status code does not indicate success: 400 (Bad Request).

  Stack Trace: 
HttpResponseMessage.EnsureSuccessStatusCode()
Steps.GivenIHaveAToken(String url, String username) line 918
Steps.GivenIHaveAToken(String url) line 897
<>c__DisplayClass1_0`1.<GetStepAction>b__0(Object o)
StepExecutor.Execute(Step step, Object testObject)
<>c__DisplayClass3_0.<ExecuteStep>b__0()
AsyncTestRunner.Run(Func`1 performStep)
ScenarioExecutor.ExecuteStep(Step step)
ExceptionProcessor.Process(Story story)
Engine.Run()
BDDfyExtensions.BDDfy(Object testObject, String scenarioTitle, String caller)
ClaimsInDelegatingHandlerTests.should_expose_claims_in_route_specific_delegating_handler() line 144
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

I believe a small fix in test setup section should fix the problem of 400 Bad Request.

Could you have a look to it and fix the issue please?

@raman-m raman-m self-requested a review July 3, 2023 12:50
@raman-m raman-m added bug Identified as a potential bug needs feedback Issue is waiting on feedback before acceptance labels Jul 19, 2023
@raman-m raman-m force-pushed the fix/1252-user-context-in-delegating-handler branch from 3222fe3 to 55d2738 Compare August 1, 2023 15:20
@raman-m
Copy link
Member

raman-m commented Aug 1, 2023

@jlukawska
The feature branch has been rebased onto ThreeMammals:develop successfully!
We are going to code review stage...

@raman-m
Copy link
Member

raman-m commented Aug 1, 2023

@davipsr @bjartekh @AgentShark
Could you review the code please?
Your opinion is prioritized for sure because of past discussions of the #1252 issue.


@amweiss
Because you're the #1252 reporter, I believe this PR will be interesting to look into.

public HttpClientWrapper(HttpClient client)
public DelegatingHandler ClientMainHandler { get; }

public HttpClientWrapper(HttpClient client, DelegatingHandler clientMainHandler)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not making clientMainHandler default null?

Copy link
Member

Choose a reason for hiding this comment

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

Or define new overloaded method version. 😉

@@ -34,7 +30,7 @@ public class DelegatingHandlerHandlerFactory : IDelegatingHandlerHandlerFactory
_qoSFactory = qoSFactory;
}

public Response<List<Func<DelegatingHandler>>> Get(DownstreamRoute downstreamRoute)
public Response<List<Func<DelegatingHandler>>> Get(DownstreamRoute downstreamRoute, HttpContext httpContext)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
public Response<List<Func<DelegatingHandler>>> Get(DownstreamRoute downstreamRoute, HttpContext httpContext)
public Response<IList<Func<DelegatingHandler>>> Get(DownstreamRoute downstreamRoute, HttpContext httpContext)

}

return delegatingHandler;
}).ToList();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
}).ToList();
}).ToArray();

Copy link
Member

Choose a reason for hiding this comment

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

Do you hate lists?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Array is more efficient than list on ram usage

@raman-m
Copy link
Member

raman-m commented Aug 3, 2023

@RaynaldM Hey Ray!
Thanks for code review!

When you've approved the PR, does it mean that all issues from your code review are minor?
I suggest not giving an approval if issues are major, and they should be fixed.
OK? 😉

@raman-m raman-m force-pushed the fix/1252-user-context-in-delegating-handler branch from b67123a to 6a8822a Compare October 13, 2023 19:00
@raman-m
Copy link
Member

raman-m commented Oct 13, 2023

Build 2170 has been failed after last changes in Polly provider.

@RaynaldM @jlukawska
Could you look into build logs and understand the problem please?

It seems we have to fix these tests:

All these tests belong to the ClaimsInDelegatingHandlerTests class.
I guess this is setup test problem, or test server cannot start.

@jlukawska
Copy link
Contributor Author

This PR was created 4 years ago and almost all modified files no longer exist. As I saw in #1252 there is another idea to solve this issue. I'm closing this PR for now.

@jlukawska jlukawska closed this May 13, 2024
@raman-m
Copy link
Member

raman-m commented May 16, 2024

I understand your decision to close this PR. However, you are welcome to open a new PR if you wish to contribute. It is crucial to use the new code base from the development branch. The internal Ocelot system kernel underwent a redesign last year, and we need to devise a new solution or re-investigate the reported issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Identified as a potential bug needs feedback Issue is waiting on feedback before acceptance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HttpContext lost between HttpClientWrapper and DelegatingHandler
4 participants