Can you match on header name?

Is it possible to match a request if any of the request header names contains a string?

So not the header value, but if the key/name contains the string.

e.g. for string test, I’d like the request to match if there’s a header like x-test-something: random_value or header like test: random_value, you get what I mean.

Thank you!

I went through the matchers’ docs up and down trying to figure out a trick, but I was empty handed. It should be extremely trivial to implement a module to match with that criteria.

Usually I wouldn’t ask you if it’s an XY problem, but is it? :grin: What’s the scenario you need this in?

2 Likes

I tried header_regexp and failed miserably :laughing:

Hey @Mohammed90, it’s fine, I figured out another way to implement what I needed.

Basically, JS script that loads on the website sends a lot of requests to my server. And I had to tweak certain headers before I forward those requests to the upstream. But, I only needed to tweak headers for requests that contained this particular request header. I hope this explanation makes sense to you :smiley:

In the end, I just manually matched 10 or so requests by their path and handled it that way. Matcher looks uglier because there are 10 different paths I match, but what can you do.

Maybe I’ll give it a try and implement my first module as a fun weekend project!

2 Likes

Use the header matcher:

If the list [of values] is empty, the field must simply exist, regardless of its value.

1 Like

My understanding is he wants to search for a string within the header name string. The header matcher needs to know the header name in full. The asterisk wildcard only works on values, so he cannot do something like this:

@matcher {
    header *test*
}
4 Likes

Yep, exactly! I tried using wildcard with header matcher for the key/name, but it understands the wildcard as a literal string. So matching on *test* will only match if there’s a header with exact name *test*, and I’d need it to match on test-1, sth-test, bla-test-blah etc.

Ah… yeah, I don’t think the header module does that currently. We can add it to our list. (Open an issue to request it?) Or someone can contribute the functionality :smiley:

2 Likes

I may give it a shot myself! But in a few weeks when I catch some time. It should be a fun project!

4 Likes