I am attempting to correct a URL parameter issue by forcing a URL encode on a node of a POST path that is somewhat a frequent occurrence. It seems best, at this time, to fix this at the proxy layer until a better solution has be developed.. but Haproxy is giving me problems with this, I should also mention that I am stuck with Haproxy v1.5 at the moment (which, from what I can tell, also leaves using Lua out of the list of options ..introduced in v1.6?).
An example of this is goes like this..
I get a POST request quite typically in the form like this..
So, it may look more like this in practice..
Needing the following out the other end..
The problem is that abc/123 is a single material that needs to look more like "abc%2F123", where the '/' slash is changing the actual path.
I am attempting to catch this in the proxy, I can get regex to capture what I need but it seems that whenever I try to have a "slash" '/' in a capture group and/or attempt to put a slash back into the replace section it breaks the rewrite.
Here are examples of what I have tried, also keep in mind that I intend to expand the capturing to grab the entire url, but was simplifying to try to work these out, also I am attempting to tell some of the story from memory at this point, so please forgive if the below is not dead on.. I have tried many, many combinations trying to come up with a strategy that would work.
In this way..
reqrep (\w+\s?)\/(material)\/(\w+\s?)\/(.*) \1\2\3%2f\4
I can get the capture groups to put the url back together again, but without the path delimeters ("/") between the path nodes.
Like this, it does not replace, it will just send the original path.
reqrep (\w+\s?)\/(material)\/(\w+\s?)\/(.*) \1\/\2\/\3%2f\4
Taking a strategy like this...
reqrep (\w+\s?)(\/)(material)(\/)(\w+\s?)\/(.*) \1\2\3\4\5%2f\6
One other strategy that I tried is keeping the "/"'s in the capturing groups so they may come out in the replace, leaving the un-desired "slash" not in a capture group, similar to below..
reqrep (\w+\s?)(\/material\/)(\w+\s?)\/(.*) \1\2\3%2f\4
I have also read about, and seen examples where some of the regex has spaces and the replace has some spacing.. I can get close by using some spacing in the replace, but that leaves undesired spaces in the end result.
If I escape a space, then add a slash it seems to work closer..
\1\ /\2 but then I would get something like (for instance)
location /material.. adding the space like mentioned above.
The pattern that I am noticing is that when I attempt to add the slashes to a capture group in the regex it messes up the replace making me guess wildly about things like.. are the slashes not getting escaped because they are in the capture group? And, why can't I just put them back in the replace as literals? This is the point where I imagine I may have stumbled upon a bug.. but also am aware that I can be screwing this up. A solution has been developed using Nginx, but standing an instance of that in front of what we need is also not the most practical if I can get Haproxy to do this, mostly because we are already using Haproxy to do quite a bit of other stuff already.
I honestly prefer to address this issue in another way, but for now using the proxy seems to be one of my best choices. I also don't have the luxury of forcing the originator to give be better paths.