On 10/15/2014 09:37 PM, Tejas Dinkar wrote:
I've seen a few places online where people are trying to use a substring
in the path / hostname as the stick-key, and I was trying to do the same
today. [1] The current solution appears to be to statically declare
every path / hostname possible, but I was wondering if it was possible
to make it dynamic.
I've attached a patch that allows you to extract a string from another
as a conversion filter. This can be used as follows:
stick on path extract_field(/, 3) # this returns baz from /foo/bar/baz
(the 0th element is an empty string).
Similarly,
stick on hostname extract_field(., 1) would ensure that you correctly
stick a.subdomain.domain.com and b.subdomain.domain.com.
Assumptions in the code (please tell me if these are incorrect):
* Returning 0 from a conversion filter correctly aborts the stick.
* I do not need to garbage collect the sample, after I run smp_dup (I
used 'lower' as the base I started from)
PS: I hope I got the email subject right. Patch also available here:
https://github.com/gja/haproxy.git, just in case this list does not
allow attachments (and I am not sure if people still use git send-email)
[1] http://marc.info/?l=haproxy&m=136212840626459
There is issues here:
// This chunk must be correctly terminated for strsep
+ separator = args[0].data.str.str;
+ separator[args[0].data.str.len] = '\0';
Useless because arg's chunk always ended with a null char
+ // This chunk must be correctly terminated for strsep
+ ptr = smp->data.str.str;
+ ptr[smp->data.str.len] = '\0';
Sample's chunk of type string is not forced to be null terminated, and
you could have a buffer overflow if args[0].data.str.len ==
args[0].data.str.size
In addition
+ if (!smp_dup(smp))
+ return 0;
Is here to warranty that you could modify data (it makes a copy of the
input chunk if it is a CONST).
In your case, you don't need to modify data so i think the most
elegant(zerocopy) way to do this converter is to not use smp_dup:
You only need to do a manual parsing to reach your token based on string
length and not char '0'. You only need to make smp->data.str.str point
on the start of the wanted field, and to set smp->data.str.len to the
appropriate value.
R,
Emeric