MEDIUM: http: make extract_cookie_value() iterate over cookie values

This will make the function usable for ACLs.
This commit is contained in:
Willy Tarreau 2012-04-06 18:20:06 +02:00
parent c89ccb6221
commit 4573af939c

View File

@ -8369,37 +8369,30 @@ pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
return 1; return 1;
} }
/* Try to find the last occurrence of a cookie name in a cookie header value. /* Try to find the next occurrence of a cookie name in a cookie header value.
* The pointer and size of the last occurrence of the cookie value is returned * The lookup begins at <hdr>. The pointer and size of the next occurrence of
* into *value and *value_l, and the function returns non-zero if the value was * the cookie value is returned into *value and *value_l, and the function
* found. Otherwise if the cookie was not found, zero is returned and neither * returns a pointer to the next pointer to search from if the value was found.
* value nor value_l are touched. The input hdr string should begin at the * Otherwise if the cookie was not found, NULL is returned and neither value
* header's value, and its size should be in hdr_l. <list> must be non-zero if * nor value_l are touched. The input <hdr> string should first point to the
* value may represent a list of values (cookie headers). * header's value, and the <hdr_end> pointer must point to the first character
* not part of the value. <list> must be non-zero if value may represent a list
* of values (cookie headers). This makes it faster to abort parsing when no
* list is expected.
*/ */
static int static char *
extract_cookie_value(char *hdr, size_t hdr_l, extract_cookie_value(char *hdr, const char *hdr_end,
char *cookie_name, size_t cookie_name_l, int list, char *cookie_name, size_t cookie_name_l, int list,
char **value, size_t *value_l) char **value, size_t *value_l)
{ {
int found = 0; char *equal, *att_end, *att_beg, *val_beg, *val_end;
char *equal, *att_end, *att_beg, *hdr_end, *val_beg, *val_end;
char *next; char *next;
/* Note that multiple cookies may be delimited with semi-colons, so we
* also have to loop on this.
*/
/* we search at least a cookie name followed by an equal, and more /* we search at least a cookie name followed by an equal, and more
* generally something like this : * generally something like this :
* Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
*/ */
if (hdr_l < cookie_name_l + 1) for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
return 0;
hdr_end = hdr + hdr_l;
for (att_beg = hdr; att_beg < hdr_end; att_beg = next + 1) {
/* Iterate through all cookies on this line */ /* Iterate through all cookies on this line */
while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
@ -8457,12 +8450,10 @@ extract_cookie_value(char *hdr, size_t hdr_l,
if (att_end - att_beg == cookie_name_l && if (att_end - att_beg == cookie_name_l &&
memcmp(att_beg, cookie_name, cookie_name_l) == 0) { memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
found = 1; /* let's return this value and indicate where to go on from */
*value = val_beg; *value = val_beg;
*value_l = val_end - val_beg; *value_l = val_end - val_beg;
/* right now we want to catch the last occurrence return next + 1;
* of the cookie, so let's go on searching.
*/
} }
/* Set-Cookie headers only have the name in the first attr=value part */ /* Set-Cookie headers only have the name in the first attr=value part */
@ -8470,7 +8461,7 @@ extract_cookie_value(char *hdr, size_t hdr_l,
break; break;
} }
return found; return NULL;
} }
/* Try to find in request or response message is in <msg> and whose transaction /* Try to find in request or response message is in <msg> and whose transaction
@ -8495,12 +8486,15 @@ find_cookie_value(struct http_msg *msg, struct http_txn *txn,
ctx.idx = 0; ctx.idx = 0;
while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) { while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
char *hdr, *end;
if (ctx.vlen < cookie_name_l + 1) if (ctx.vlen < cookie_name_l + 1)
continue; continue;
found |= extract_cookie_value(ctx.line + ctx.val, ctx.vlen, hdr = ctx.line + ctx.val;
cookie_name, cookie_name_l, 1, end = hdr + ctx.vlen;
value, value_l); while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l)))
found = 1;
} }
return found; return found;
} }