mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-11-28 22:31:06 +01:00
MEDIUM: sample: change the behavior of the bin2str cast
The bin2str cast gives the hexadecimal representation of the binary content when it is used as string. This was inherited from the stick-table casts without realizing that it was a mistake. Indeed, it breaks string processing on binary contents, preventing any _reg, _beg, etc from working. For example, with an HTTP GET request, the fetch "req.payload(0,3)" returns the 3 bytes "G", "E", and "T" in binary. If this fetch is used with regex, it is automatically converted to "474554" and the regex is applied on this string, so it never matches. This commit changes the cast so that bin2str does not convert the contents anymore, and returns a string type. The contents can thus be matched as is, and the NULL character continues to mark the end of the string to avoid any issue with some string-based functions. This commit could almost have been marked as a bug fix since it does what the doc says. Note that in case someone would rely on the hex encoding, then the same behaviour could be achieved by appending ",hex" after the sample fetch function (brought by previous patch).
This commit is contained in:
parent
2f49d6d17b
commit
e87cac16cc
16
src/sample.c
16
src/sample.c
@ -477,18 +477,24 @@ static int c_str2ipv6(struct sample *smp)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* The sample is always copied into a new one so that smp->size is always
|
||||
* valid. The NULL char always enforces the end of string if it is met.
|
||||
*/
|
||||
static int c_bin2str(struct sample *smp)
|
||||
{
|
||||
struct chunk *trash = get_trash_chunk();
|
||||
unsigned char c;
|
||||
int ptr = 0;
|
||||
|
||||
trash->len = 0;
|
||||
while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
|
||||
c = smp->data.str.str[ptr++];
|
||||
trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
|
||||
trash->str[trash->len++] = hextab[c & 0xF];
|
||||
while (ptr < smp->data.str.len) {
|
||||
c = smp->data.str.str[ptr];
|
||||
if (!c)
|
||||
break;
|
||||
trash->str[ptr] = c;
|
||||
ptr++;
|
||||
}
|
||||
trash->len = ptr;
|
||||
trash->str[ptr] = 0;
|
||||
smp->data.str = *trash;
|
||||
smp->type = SMP_T_STR;
|
||||
return 1;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user