OPTIM: http_ext: avoid useless copy in http_7239_extract_{ipv4,ipv6}

In http_7239_extract_{ipv4,ipv6}, we declare a local buffer in order to
use inet_pton() since it requires a valid destination argument (cannot be
NULL). Then, if the caller provided <ip> argument, we copy inet_pton()
result (from local buffer to <ip>).

In fact when the caller provides <ip>, we may directly use <ip> as
inet_pton() dst argument to avoid an useless copy. Thus the local buffer
is only relevant when the user doesn't provide <ip>.

While at it, let's add a missing testcase for the rfc7239_n2nn converter
(to check that http_7239_extract_ipv4() with <ip> provided works properly)

This could be backported in 2.8 with b2bb925 ("MINOR: proxy/http_ext:
introduce proxy forwarded option")
This commit is contained in:
Aurelien DARRAGON 2024-03-18 15:18:08 +01:00 committed by Willy Tarreau
parent 3de1acfb23
commit db1cd8f881
2 changed files with 16 additions and 6 deletions

View File

@ -151,6 +151,12 @@ client c1 -connect ${h1_fe1_sock} {
expect resp.status == 200
expect resp.http.nodename == "::1"
expect resp.http.nodeport == "_id"
txreq -req GET -url /req4 \
-hdr "forwarded: for=127.9.0.1"
rxresp
expect resp.status == 200
expect resp.http.nodename == "127.9.0.1"
} -run
client c2 -connect ${h2_fe1h2_sock} {

View File

@ -114,8 +114,12 @@ static inline int http_7239_extract_ipv4(struct ist *input, struct in_addr *ip)
{
char ip4[INET_ADDRSTRLEN];
unsigned char buf[sizeof(struct in_addr)];
void *dst = buf;
int it = 0;
if (ip)
dst = ip;
/* extract ipv4 addr */
while (it < istlen(*input) && it < (sizeof(ip4) - 1)) {
if (!isdigit((unsigned char)istptr(*input)[it]) &&
@ -125,11 +129,9 @@ static inline int http_7239_extract_ipv4(struct ist *input, struct in_addr *ip)
it += 1;
}
ip4[it] = 0;
if (inet_pton(AF_INET, ip4, buf) != 1)
if (inet_pton(AF_INET, ip4, dst) != 1)
return 0; /* invalid ip4 addr */
/* ok */
if (ip)
memcpy(ip, buf, sizeof(buf));
*input = istadv(*input, it);
return 1;
}
@ -146,8 +148,12 @@ static inline int http_7239_extract_ipv6(struct ist *input, struct in6_addr *ip)
{
char ip6[INET6_ADDRSTRLEN];
unsigned char buf[sizeof(struct in6_addr)];
void *dst = buf;
int it = 0;
if (ip)
dst = ip;
*input = istnext(*input); /* skip '[' leading char */
/* extract ipv6 addr */
while (it < istlen(*input) &&
@ -162,11 +168,9 @@ static inline int http_7239_extract_ipv6(struct ist *input, struct in6_addr *ip)
if ((istlen(*input)-it) < 1 || istptr(*input)[it] != ']')
return 0; /* missing ending "]" char */
it += 1;
if (inet_pton(AF_INET6, ip6, buf) != 1)
if (inet_pton(AF_INET6, ip6, dst) != 1)
return 0; /* invalid ip6 addr */
/* ok */
if (ip)
memcpy(ip, buf, sizeof(buf));
*input = istadv(*input, it);
return 1;
}