]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: listener: add so_name sample fetch
authorJerome Magnin <jmagnin@haproxy.com>
Fri, 27 Mar 2020 21:08:40 +0000 (22:08 +0100)
committerWilly Tarreau <w@1wt.eu>
Sun, 29 Mar 2020 03:47:29 +0000 (05:47 +0200)
Add a sample fetch for the name of a bind. This can be useful to
take decisions when PROXY protocol is used and we can't rely on dst,
such as the sample config below.

  defaults
    mode http
  listen bar
    bind 127.0.0.1:1111
    server s1 127.0.1.1:1234 send-proxy

  listen foo
    bind 127.0.1.1:1234 name foo accept-proxy
    http-request return status 200 hdr dst %[dst] if { dst 127.0.1.1 }

doc/configuration.txt
reg-tests/sample_fetches/so_name.vtc [new file with mode: 0644]
src/listener.c

index 8be19e36942e5f73c6983e0f30faa80a59f350e4..8eb3f8e84ae3c35c1821567b258407d6504497e2 100644 (file)
@@ -15449,6 +15449,11 @@ so_id : integer
   in frontends involving many "bind" lines, or to stick all users coming via a
   same socket to the same server.
 
+so_name : string
+  Returns a string containing the current listening socket's name, as defined
+  with name on a "bind" line. It can serve the same purposes as so_id but with
+  strings instead of integers.
+
 src : ip
   This is the source IPv4 address of the client of the session. It is of type
   IP and works on both IPv4 and IPv6 tables. On IPv6 tables, IPv4 addresses are
diff --git a/reg-tests/sample_fetches/so_name.vtc b/reg-tests/sample_fetches/so_name.vtc
new file mode 100644 (file)
index 0000000..c6211fa
--- /dev/null
@@ -0,0 +1,22 @@
+varnishtest "so_name sample fetche Test"
+
+#REQUIRE_VERSION=2.2
+
+feature ignore_unknown_macro
+
+haproxy h1 -conf {
+    defaults
+        mode http
+
+    frontend fe
+        bind "fd@${fe}" name foo
+        http-request return status 200 hdr so-name %[so_name]
+
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+    txreq -url "/"
+    rxresp
+    expect resp.status == 200
+    expect resp.http.so-name == "foo"
+} -run
index ceb52af0d562ee5770708d77ad21a4f00a1a9f70..fdd972a4216502319f45dee4c5c9b293dec1efbc 100644 (file)
@@ -1290,6 +1290,18 @@ smp_fetch_so_id(const struct arg *args, struct sample *smp, const char *kw, void
        smp->data.u.sint = smp->sess->listener->luid;
        return 1;
 }
+static int
+smp_fetch_so_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
+{
+       smp->data.u.str.area = smp->sess->listener->name;
+       if (!smp->data.u.str.area)
+               return 0;
+
+       smp->data.type = SMP_T_STR;
+       smp->flags = SMP_F_CONST;
+       smp->data.u.str.data = strlen(smp->data.u.str.area);
+       return 1;
+}
 
 /* parse the "accept-proxy" bind keyword */
 static int bind_parse_accept_proxy(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
@@ -1526,6 +1538,7 @@ static int cfg_parse_tune_listener_mq(char **args, int section_type, struct prox
 static struct sample_fetch_kw_list smp_kws = {ILH, {
        { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
        { "so_id",    smp_fetch_so_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
+       { "so_name",  smp_fetch_so_name, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
        { /* END */ },
 }};