]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
OPTIM: http_ext: avoid useless copy in http_7239_extract_{ipv4,ipv6}
authorAurelien DARRAGON <adarragon@haproxy.com>
Mon, 18 Mar 2024 14:18:08 +0000 (15:18 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 25 Mar 2024 15:24:15 +0000 (16:24 +0100)
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")

reg-tests/http-rules/forwarded-header-7239.vtc
src/http_ext.c

index a894113e78bbb698fa28cdfbca3f976dfb370b48..dd2c6e3fd0fa2e404b81426521d925b0e478cd12 100644 (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} {
index 269a97f5e3b0be74af5db49163b6762772d62b30..3367e3884cd5301b992d6b03cdf62601fadd01e3 100644 (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;
 }