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

Add HeaderMatchMode.NotExists, enable matchers that only match if the… #1806

Merged
merged 2 commits into from
Jul 25, 2022

Conversation

kevbry
Copy link
Contributor

@kevbry kevbry commented Jul 15, 2022

Addresses #1697 by adding HeaderMatchMode.NotExists. Inverse of HeaderMatchMode.Exists, being that it only matches if the header is not present at all.

Have added a few tests, but had trouble deciphering the intent behind some of the tests in HeaderMatcherPolicyTests.cs and so may have missed some cases. Particularly the sortorder and "_AppliesScenarios" tests were not added to as I couldn't figure out what the intent of them was.

One failing test Yarp.ReverseProxy.FunctionalTests.HttpProxyCookieTests_Http2::ProxyAsync_RequestWithCookieHeaders is also failing in main, so I don't think I broke it.

Thanks for a great piece of software.

@ghost
Copy link

ghost commented Jul 15, 2022

CLA assistant check
All CLA requirements met.

Copy link
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution!

Particularly the sortorder and "_AppliesScenarios" tests were not added to as I couldn't figure out what the intent of them was.

No worries, your change does not impact these tests.
They only care about the number of matchers, not their type.

src/ReverseProxy/Routing/HeaderMatcherPolicy.cs Outdated Show resolved Hide resolved
src/ReverseProxy/Routing/HeaderMatcherPolicy.cs Outdated Show resolved Hide resolved
@@ -70,21 +70,31 @@ public Task ApplyAsync(HttpContext httpContext, CandidateSet candidates)

foreach (var matcher in matchers)
{
if (headers.TryGetValue(matcher.Name, out var requestHeaderValues) &&
!StringValues.IsNullOrEmpty(requestHeaderValues))
var headerExistsInRequest = headers.TryGetValue(matcher.Name, out var requestHeaderValues);
Copy link
Member

@Tratcher Tratcher Jul 22, 2022

Choose a reason for hiding this comment

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

headerExistsInRequest should include !StringValues.IsNullOrEmpty. An empty header has no semantic meaning in almost all cases. I don't know if any servers would actually return an empty header, but you already have that check on the next line so you might as well incorporate it to be consistent.

Copy link
Member

Choose a reason for hiding this comment

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

I would be surprised if I set NotExists and it still matched on a request that does have the header (albeit empty).

Copy link
Member

Choose a reason for hiding this comment

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

I guess that the difference between literally empty vs semantically empty. I doubt it will make a difference in practice. Nevermind.

Copy link
Contributor Author

@kevbry kevbry Jul 22, 2022

Choose a reason for hiding this comment

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

I would be surprised if I set NotExists and it still matched on a request that does have the header (albeit empty).

That was my thinking in breaking the two portions apart. Also the hope of using this to help glue together weird legacy software that does things like send no Accept header to a service with an incompatible default.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thinking about it a little further, I don't see a way to express "match only if the specified header is present, but empty". My use-case didn't need that, and I assumed that if I did at some point I could just use ExactHeader or PrefixHeader to an empty string. Looks like that existing IsNullOrEmpty check would prevent those from matching in that scenario.

I doubt it matters, just a note.

@MihaZupan MihaZupan added this to the YARP 2.0.0 milestone Jul 25, 2022
@MihaZupan MihaZupan merged commit b0a2452 into microsoft:main Jul 25, 2022
@Kahbazi
Copy link
Collaborator

Kahbazi commented Jul 26, 2022

@MihaZupan @Tratcher The docs needs to be updated.

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

Successfully merging this pull request may close these issues.

4 participants