Commit a42d97a0 authored by Nils Goroll's avatar Nils Goroll

allow calling .policy multiple times on the cached result

parent 676e2d9d
Pipeline #183 skipped
...@@ -449,7 +449,7 @@ hosts.policy ...@@ -449,7 +449,7 @@ hosts.policy
:: ::
INT hosts.policy(PRIV_TASK, STRING host, STRING path) INT hosts.policy(PRIV_TASK, STRING host=0, STRING path=0)
Determine the policy type that holds for ``host`` and ``path``. The Determine the policy type that holds for ``host`` and ``path``. The
return values are: return values are:
...@@ -460,9 +460,8 @@ return values are: ...@@ -460,9 +460,8 @@ return values are:
* -1 if no matching policy can be found * -1 if no matching policy can be found
* -2 if there was an internal error * -2 if there was an internal error
The ``host`` and ``path`` parameters are required, and must be This method MAY NOT be called in ``vcl_init``. If it is, then the VCL
non-empty. This method MAY NOT be called in ``vcl_init``. If it is, load fails.
then the VCL load fails.
The method searches for host names added by the ``.add()`` method that The method searches for host names added by the ``.add()`` method that
match ``host``, possibly matching the suffix if the host name in match ``host``, possibly matching the suffix if the host name in
...@@ -502,6 +501,13 @@ Subsequent calls to the ``.token()``, ``.secret()`` or ``.explain()`` ...@@ -502,6 +501,13 @@ Subsequent calls to the ``.token()``, ``.secret()`` or ``.explain()``
methods refer to the most recent invocation of ``.policy()`` in the methods refer to the most recent invocation of ``.policy()`` in the
same task scope, that is in the same client or backend transaction. same task scope, that is in the same client or backend transaction.
Likewise, if both the ``host`` and ``path`` parameters are empty,
``.policy()`` returns again the result of the most recent invocation
with parameters.
Calling ``.policy()`` with only one of the ``host`` and ``path``
parameters empty is an error.
.. _func_hosts.token: .. _func_hosts.token:
hosts.token hosts.token
......
...@@ -21,9 +21,13 @@ varnish v1 -vcl { ...@@ -21,9 +21,13 @@ varnish v1 -vcl {
} }
sub vcl_synth { sub vcl_synth {
set resp.http.p1 = h.policy("example.com", "/foo/bar"); set resp.http.p0 = h.policy();
set resp.http.p2 = h.policy("example.org", "/quux/4711"); set resp.http.p1 = h.policy("example.com", "/foo/bar");
set resp.http.p3 = h.policy("example.com", "/bar/foo"); set resp.http.p1c = h.policy();
set resp.http.p2 = h.policy("example.org", "/quux/4711");
set resp.http.p2c = h.policy();
set resp.http.p3 = h.policy("example.com", "/bar/foo");
set resp.http.p3c = h.policy();
} }
} -start } -start
...@@ -31,9 +35,13 @@ client c1 { ...@@ -31,9 +35,13 @@ client c1 {
txreq txreq
rxresp rxresp
expect resp.status == 200 expect resp.status == 200
expect resp.http.p1 == "2" expect resp.http.p0 == "-2"
expect resp.http.p2 == "1" expect resp.http.p1 == "2"
expect resp.http.p3 == "0" expect resp.http.p1c == "2"
expect resp.http.p2 == "1"
expect resp.http.p2c == "1"
expect resp.http.p3 == "0"
expect resp.http.p3c == "0"
} -run } -run
# Examples from Akamai docs # Examples from Akamai docs
......
...@@ -94,6 +94,11 @@ WS_Contains(struct ws * const restrict ws, const void * const restrict ptr, ...@@ -94,6 +94,11 @@ WS_Contains(struct ws * const restrict ws, const void * const restrict ptr,
assert((char *)ptr >= ws->s && (char *)(ptr + len) <= ws->e); assert((char *)ptr >= ws->s && (char *)(ptr + len) <= ws->e);
} }
static struct vmod_hoailona_policy *
get_policy(VRT_CTX, const struct vmod_priv * restrict const,
const char * restrict const,
const char * restrict const);
void void
errmsg(VRT_CTX, const char *fmt, ...) errmsg(VRT_CTX, const char *fmt, ...)
{ {
...@@ -490,10 +495,24 @@ vmod_hosts_policy(VRT_CTX, struct vmod_hoailona_hosts *hosts, ...@@ -490,10 +495,24 @@ vmod_hosts_policy(VRT_CTX, struct vmod_hoailona_hosts *hosts,
return -2; return -2;
} }
if (hostname == NULL || hostname[0] == '\0') { if (hostname == NULL || hostname[0] == '\0') {
VERR(ctx, "host is empty in %s.policy()", hosts->vcl_name); if (! (pathname == NULL || pathname[0] == '\0')) {
return -2; VERR(ctx, "host is empty in %s.policy()",
} hosts->vcl_name);
if (pathname == NULL || pathname[0] == '\0') { return -2;
}
if (priv_task->priv == NULL) {
VERR(ctx, "%s.policy() no cached result",
hosts->vcl_name);
return -2;
}
policy = get_policy(ctx, priv_task, hosts->vcl_name, "policy");
if (policy == NULL)
return -1;
return policy->type;
} else if (pathname == NULL || pathname[0] == '\0') {
VERR(ctx, "path is empty in %s.policy()", hosts->vcl_name); VERR(ctx, "path is empty in %s.policy()", hosts->vcl_name);
return -2; return -2;
} }
......
...@@ -399,7 +399,7 @@ Examples:: ...@@ -399,7 +399,7 @@ Examples::
h.add("evil.org", "deny", description="no access to evil.org"); h.add("evil.org", "deny", description="no access to evil.org");
} }
$Method INT .policy(PRIV_TASK, STRING host, STRING path) $Method INT .policy(PRIV_TASK, STRING host=0, STRING path=0)
Determine the policy type that holds for ``host`` and ``path``. The Determine the policy type that holds for ``host`` and ``path``. The
return values are: return values are:
...@@ -410,9 +410,8 @@ return values are: ...@@ -410,9 +410,8 @@ return values are:
* -1 if no matching policy can be found * -1 if no matching policy can be found
* -2 if there was an internal error * -2 if there was an internal error
The ``host`` and ``path`` parameters are required, and must be This method MAY NOT be called in ``vcl_init``. If it is, then the VCL
non-empty. This method MAY NOT be called in ``vcl_init``. If it is, load fails.
then the VCL load fails.
The method searches for host names added by the ``.add()`` method that The method searches for host names added by the ``.add()`` method that
match ``host``, possibly matching the suffix if the host name in match ``host``, possibly matching the suffix if the host name in
...@@ -452,6 +451,13 @@ Subsequent calls to the ``.token()``, ``.secret()`` or ``.explain()`` ...@@ -452,6 +451,13 @@ Subsequent calls to the ``.token()``, ``.secret()`` or ``.explain()``
methods refer to the most recent invocation of ``.policy()`` in the methods refer to the most recent invocation of ``.policy()`` in the
same task scope, that is in the same client or backend transaction. same task scope, that is in the same client or backend transaction.
Likewise, if both the ``host`` and ``path`` parameters are empty,
``.policy()`` returns again the result of the most recent invocation
with parameters.
Calling ``.policy()`` with only one of the ``host`` and ``path``
parameters empty is an error.
$Method STRING .token(PRIV_TASK, STRING acl=0, DURATION ttl=0, STRING data=0) $Method STRING .token(PRIV_TASK, STRING acl=0, DURATION ttl=0, STRING data=0)
If the previous invocation of ``.policy()`` determined policy type If the previous invocation of ``.policy()`` determined policy type
......
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