Commit 0c54b6bf authored by Nils Goroll's avatar Nils Goroll

Match hosts in the order in which policies are added

parent 4ddbfe66
......@@ -12,12 +12,23 @@ varnish v1 -vcl {
new h = hoailona.hosts();
h.add("example.com", "p1");
h.add("example.org", "p2");
// specific entry after wildcard - never matched
h.add("*.example.com", "p1", "/foo/...");
h.add("*.example.org", "p2", "/bar/...");
h.add("foo.example.org", "p2", "...");
h.add("example.net", "p1", description="net");
h.add("example.edu", "p2", description="edu");
h.add("*.example.net", "p1", "/baz/...", description="sub net");
h.add("*.example.edu", "p2", "/baz/...", description="sub edu");
// specific entry before wildcard has precendence,
// but lookup stops at the first host match
h.add("foo.example.net", "p1", "/bar/...",
description="sub net");
h.add("*.example.net", "p1", "/baz/...",
description="wild sub net");
h.add("*.example.edu", "p2", "/baz/...",
description="wild sub edu");
}
sub vcl_recv {
......@@ -37,12 +48,16 @@ varnish v1 -vcl {
set resp.http.e5 = h.explain();
set resp.http.p6 = h.policy("example.edu", "/foo/bar");
set resp.http.e6 = h.explain();
set resp.http.p7 = h.policy("foo.example.net", "/baz/quux");
set resp.http.p7 = h.policy("foo.example.net", "/bar/quux");
set resp.http.e7 = h.explain();
set resp.http.p8 = h.policy("foo.example.edu", "/baz/quux");
set resp.http.p8 = h.policy("foo.example.net", "/baz/quux");
set resp.http.e8 = h.explain();
set resp.http.p9 = h.policy("foo.example.com", "/baz/quux");
set resp.http.p9 = h.policy("bar.example.net", "/baz/quux");
set resp.http.e9 = h.explain();
set resp.http.pA = h.policy("foo.example.edu", "/baz/quux");
set resp.http.eA = h.explain();
set resp.http.pB = h.policy("foo.example.com", "/baz/quux");
set resp.http.eB = h.explain();
}
} -start
......@@ -63,11 +78,15 @@ client c1 {
expect resp.http.p6 == "1"
expect resp.http.e6 == "Matched host example.edu (edu) for global policy p2 (open)"
expect resp.http.p7 == "2"
expect resp.http.e7 == "Matched host *.example.net and pattern /baz/... (sub net) for policy p1"
expect resp.http.p8 == "1"
expect resp.http.e8 == "Matched host *.example.edu and pattern /baz/... (sub edu) for policy p2 (open)"
expect resp.http.p9 == "-1"
expect resp.http.e9 == "No policy was matched"
expect resp.http.e7 == "Matched host foo.example.net and pattern /bar/... (sub net) for policy p1"
expect resp.http.p8 == "-1"
expect resp.http.e8 == "No policy was matched"
expect resp.http.p9 == "2"
expect resp.http.e9 == "Matched host *.example.net and pattern /baz/... (wild sub net) for policy p1"
expect resp.http.pA == "1"
expect resp.http.eA == "Matched host *.example.edu and pattern /baz/... (wild sub edu) for policy p2 (open)"
expect resp.http.pB == "-1"
expect resp.http.eB == "No policy was matched"
} -run
varnish v1 -errvcl {h.explain() may not be called in vcl_init} {
......
......@@ -56,14 +56,14 @@ struct host {
unsigned magic;
#define VMOD_HOAILONA_HOST_MAGIC 0x731af58f
struct assign_tree assignments;
VSLIST_ENTRY(host) list;
VSTAILQ_ENTRY(host) list;
char *name;
char *description;
struct vmod_hoailona_policy *policy;
size_t len;
};
typedef VSLIST_HEAD(hosthead, host) hosthead_t;
typedef VSTAILQ_HEAD(hosthead, host) hosthead_t;
struct vmod_hoailona_hosts {
unsigned magic;
......@@ -246,7 +246,7 @@ vmod_hosts__init(VRT_CTX, struct vmod_hoailona_hosts **hostsp,
hosts->vcl_name = strdup(vcl_name);
AN(hosts->vcl_name);
VSLIST_INIT(&hosts->hosthead);
VSTAILQ_INIT(&hosts->hosthead);
}
/*
......@@ -265,7 +265,7 @@ vmod_hosts__fini(struct vmod_hoailona_hosts **hostsp)
CHECK_OBJ_NOTNULL(hosts, VMOD_HOAILONA_HOSTS_MAGIC);
if (hosts->vcl_name != NULL)
free(hosts->vcl_name);
host = VSLIST_FIRST(&hosts->hosthead);
host = VSTAILQ_FIRST(&hosts->hosthead);
while (host != NULL) {
struct assignment *a;
struct host *next_host = NULL;
......@@ -295,7 +295,7 @@ vmod_hosts__fini(struct vmod_hoailona_hosts **hostsp)
FREE_OBJ(a);
a = next_ass;
}
next_host = VSLIST_NEXT(host, list);
next_host = VSTAILQ_NEXT(host, list);
FREE_OBJ(host);
host = next_host;
}
......@@ -396,7 +396,7 @@ vmod_hosts_add(VRT_CTX, struct vmod_hoailona_hosts *hosts,
return;
}
VSLIST_FOREACH(host, &hosts->hosthead, list) {
VSTAILQ_FOREACH(host, &hosts->hosthead, list) {
CHECK_OBJ(host, VMOD_HOAILONA_HOST_MAGIC);
if (strcmp(hostname, host->name) == 0)
break;
......@@ -443,7 +443,7 @@ vmod_hosts_add(VRT_CTX, struct vmod_hoailona_hosts *hosts,
VRB_INIT(&host->assignments);
AZ(host->description);
AZ(host->policy);
VSLIST_INSERT_HEAD(&hosts->hosthead, host, list);
VSTAILQ_INSERT_TAIL(&hosts->hosthead, host, list);
}
if (path == NULL) {
......@@ -519,7 +519,7 @@ vmod_hosts_policy(VRT_CTX, struct vmod_hoailona_hosts *hosts,
/* XXX optimize */
hostlen = strlen(hostname);
VSLIST_FOREACH(h, &hosts->hosthead, list) {
VSTAILQ_FOREACH(h, &hosts->hosthead, list) {
const char *q, *hs;
CHECK_OBJ(h, VMOD_HOAILONA_HOST_MAGIC);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment