[4.0] 8c7e938 Prefer exact matches in varnishadm backend.set_health.
Lasse Karstensen
lkarsten at varnish-software.com
Thu Jan 15 16:35:40 CET 2015
commit 8c7e93833db8aa898db74878bb076c1ca093bf33
Author: Dag Haavi Finstad <daghf at varnish-software.com>
Date: Tue Oct 28 13:34:06 2014 +0100
Prefer exact matches in varnishadm backend.set_health.
Fixes: #1349
diff --git a/bin/varnishd/cache/cache_backend_cfg.c b/bin/varnishd/cache/cache_backend_cfg.c
index dbe07c1..5cf077e 100644
--- a/bin/varnishd/cache/cache_backend_cfg.c
+++ b/bin/varnishd/cache/cache_backend_cfg.c
@@ -288,7 +288,7 @@ backend_find(struct cli *cli, const char *matcher, bf_func *func, void *priv)
const char *port_b = NULL;
ssize_t port_l = 0;
int found = 0;
- int i;
+ int i, j;
name_b = matcher;
if (matcher != NULL) {
@@ -342,22 +342,33 @@ backend_find(struct cli *cli, const char *matcher, bf_func *func, void *priv)
}
}
}
- VTAILQ_FOREACH(b, &backends, list) {
- CHECK_OBJ_NOTNULL(b, BACKEND_MAGIC);
- if (port_b != NULL && strncmp(b->port, port_b, port_l) != 0)
- continue;
- if (name_b != NULL && strncmp(b->vcl_name, name_b, name_l) != 0)
- continue;
- if (ip_b != NULL &&
- (b->ipv4_addr == NULL ||
- strncmp(b->ipv4_addr, ip_b, ip_l)) &&
- (b->ipv6_addr == NULL ||
- strncmp(b->ipv6_addr, ip_b, ip_l)))
+
+ for (j = 0; j < 2; ++j) {
+ if (j == 0 && name_b == NULL)
continue;
- found++;
- i = func(cli, b, priv);
- if (i)
- return(i);
+ VTAILQ_FOREACH(b, &backends, list) {
+ CHECK_OBJ_NOTNULL(b, BACKEND_MAGIC);
+ if (port_b != NULL &&
+ strncmp(b->port, port_b, port_l) != 0)
+ continue;
+ if (name_b != NULL &&
+ strncmp(b->vcl_name, name_b, name_l) != 0)
+ continue;
+ if (j == 0 && b->vcl_name[name_l] != '\0')
+ continue;
+ if (ip_b != NULL &&
+ (b->ipv4_addr == NULL ||
+ strncmp(b->ipv4_addr, ip_b, ip_l)) &&
+ (b->ipv6_addr == NULL ||
+ strncmp(b->ipv6_addr, ip_b, ip_l)))
+ continue;
+ found++;
+ i = func(cli, b, priv);
+ if (i)
+ return(i);
+ if (j == 0)
+ return (1);
+ }
}
return (found);
}
diff --git a/bin/varnishtest/tests/r01349.vtc b/bin/varnishtest/tests/r01349.vtc
new file mode 100644
index 0000000..78c64ba
--- /dev/null
+++ b/bin/varnishtest/tests/r01349.vtc
@@ -0,0 +1,66 @@
+varnishtest "Exact matching for varnishadm backend.set_health"
+
+server s1 -repeat 2 {
+ rxreq
+ txresp -hdr "Backend: b1"
+} -start
+
+server s2 -repeat 2 {
+ rxreq
+ txresp -hdr "Backend: b"
+} -start
+
+varnish v1 -vcl {
+ backend b1 {
+ .host = "${s1_addr}";
+ .port = "${s1_port}";
+ }
+
+ backend b {
+ .host = "${s2_addr}";
+ .port = "${s2_port}";
+ }
+
+ sub vcl_recv {
+ return(pass);
+ }
+
+ sub vcl_backend_fetch {
+ if (bereq.http.backend == "b1") {
+ set bereq.backend = b1;
+ }
+ else {
+ set bereq.backend = b;
+ }
+ }
+
+} -start
+
+varnish v1 -cliok "backend.list b"
+
+client c1 {
+ txreq -hdr "Backend: b1"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.backend == "b1"
+
+ txreq -hdr "Backend: b"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.backend == "b"
+} -run
+
+varnish v1 -cliok "backend.set_health b sick"
+
+client c1 {
+ txreq -hdr "Backend: b1"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.backend == "b1"
+
+ txreq -hdr "Backend: b"
+ rxresp
+ expect resp.status == 503
+} -run
+
+varnish v1 -clierr 106 "backend.set_health b(1.2.3.4:) healthy"
More information about the varnish-commit
mailing list