HAProxy community

How to use "]" in a regsub converter inside a log format

I want to have something like this:

http-request set-path %[path,regsub(/prefix/([a-zA-Z0-9]+), /newprefix/\1)] if { path -m beg /prefix }

However, I get an errror, because haproxy interprets the first “]” as the end of the fetch for “%[” instead of part of the regular expression.

I’ve tried to quote the whole regex, and to escape the ] with a backslash, but neither seem to work.

Using haproxy 1.8.20

I found this in the docs:

It is important to note that due to the current limitations of the
configuration parser, some characters such as closing parenthesis, closing
square brackets or comma are not possible to use in the arguments. The first
use of this converter is to replace certain characters or sequence of
characters with other ones.

This is a pretty big limitation, since it means you can’t use capture groups, character classes, or commas (not to mention matching those specific characters). The documentation doesn’t mention any workarounds. Are there any?

In 2.0 you have http-request replace-uri:

# suffix /foo : turn /bar?q=1 into /bar/foo?q=1 :
http-request replace-uri ([^?]*)(\?(.*))? \1/foo\2

In releases older than 2.0 use the deprecated reqrep.

Although replace-uri (or reqrep) would work in this case. It isn’t a solution in general. For example, what if I want to use regsub in an actual log format, or in the expression stored in a variable with http-request set-var.

So this is not about that actual use-case, but a feature request.

Please file it on github.

Well, not just that particular use case. So, If I understand correctly, there isn’t any way to escape those characters then?

No, there is not.

Ok. Well, I filed a feature request, and I guess I’ll have to stick with the much messier reqrep for now.

FYI I posted a workaround via LUA, which also works for log formats here: