V2 matcher: OR in not

How can we achieve not (A or B)?

not {
  file
  path /exception/*
}

Seems to AND the file and the path

not {
  file
}
not {
  path /exception/*
}

The second not seems to overwrite the first one.

Is there anyway to OR not?

1 Like

That’s a good question.

Quick workaround, you should be able to make two matchers and use the same directive for both of the matchers.

Hi Francis,

Thanks for the reply. I’m a little confused. Would you care to elaborate?

For now, instead of trying to use one matcher with an OR, you could try to use two matchers on two directives. Think of it like:

if (not file)
    rewrite
if (not path)
    rewrite

(sorry, on mobile, best I can do for now)

I think it’s not equivalent.

!(A || B) == !A && !B
Your suggestion seems to be !A || !B

You’re right, sorry! I should not be thinking about boolean logic so late at night haha

Combining matchers in the Caddyfile is tricky because they get encoded as JSON, so to merge them we’d have to decode the JSON, switch on each matcher’s type, and then figure out the right logic for what merging looks like. (It’s hard. Maybe not? As far as I know it is.)

I’ve thought about making it possible to use matchers within other matchers. But haven’t gottena round to it yet…

@francislavoie thanks for taking a look anyway!

@matt did you mean it’s possible to do with json config?

Yeah, should be: JSON Config Structure - Caddy Documentation

Each matcher set is OR’ed, but matchers within a set are AND’ed together.

The Caddyfile adapter just happens to make 1 matcher set when you define a named matcher, hence they are AND’ed together. If you make two matcher sets in the JSON, each with a not matcher, they will be OR’ed.

Hi Matt,

If my brain is working correctly, I’m not sure if it is, not having a particular good day today (not because of caddy, caddy is great :), I think that would run into the same boolean algebra problem Francis and I discussed above :man_facepalming: :joy:

Two matchers with not are OR'ed but the end result is equivalent to one not with 2 conditions AND'ed.

If, say we could specify 2 not in the same matcher, and instead of the second one overwriting (assumed behavior judging by the result I saw) the first one, they get AND'ed, should make sense since they are in the same matcher set, that would achieve the same as not (A OR B).

@princemaple This should now be possible in Allow specifying multiple matcher sets in "not" matcher by mholt · Pull Request #3208 · caddyserver/caddy · GitHub – that PR should have build artifacts momentarily you can download and try. Please report back if it works for you! (It did for me.)

2 Likes

Seems to work! Thanks a lot for the quick addition :+1:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.