What is the best way to delete a single cookie?
I tried
http-request replace-header Cookie mycookie=[^;]*(;\s*|$) ""
but then I get an error that http-request replace-header
requires three parameters. Apparently it doesn’t recognise the empty string as a parameter . (at least on 1.7).
I was kind of surprised there doesn’t seem to be any built-in ways to manipulate cookies since haproxy already has to know how to parse cookies for the cook
fetch.
What is the best way to delete an individual cookie?
You are right, there is no build-in feature for this. Multiple regexes are required to delete a cookie, I assume the following untested configuration would do the job:
# delete mycookie when
http-request replace-header Cookie mycookie=[^;]*;\s*(.*) \1
# removing mycookie when at the end of the header
http-request replace-header Cookie (.*);\s*mycookie=[^=]$ \1
# deleting entire Cookie header (single cookie)
reqidel ^Cookie: mycookie=[^=]$
2 Likes
I think that the code posted by lukastribus on Nov’18 has a typo on the first line, it said:
# delete mycookie when
instead of:
# delete mycookie when it is at the beginning of the header
In addition to the above, a new case:
# replace mycookie in any place inside the header
http-request replace-header Cookie (.*)mycookie=[^;]*(.*) \1mycookie=ha_drop\2
When possible, I encourage replace it with a fixed reference instead the deletion because it allows an easy identification and debug downstream. But anyway, this is the deletion code:
# delete mycookie in any place inside the header
http-request replace-header Cookie (.*)mycookie=[^;]*;?(.*) \1\2
But take note that this anyplace match usually needs more steps (cpu time) to finish than the fixed match at the beginning or at the end.
The test regex of replace is available here on regex101.com
1 Like