Update pcre interface for current varnish-cache

XXX NOTE:

as long as we do not have https://github.com/varnishcache/varnish-cache/pull/3724/files
in varnish-cache, we can not use anchored matches
parent 2855790a
......@@ -41,7 +41,8 @@ varnish v1 -vcl+backend {
sub vcl_init {
new foo = weightadjust.random();
foo.add_backend(backend=s1, weight=1,
weight_update = "^X-Weight: ([\d.]+)",
# XXX add back ^X-Weight
weight_update = "X-Weight: ([\d.]+)",
interval = 1h);
}
......@@ -49,7 +50,8 @@ varnish v1 -vcl+backend {
if (req.url == "/1") {
vtc.sleep(1s);
foo.add_backend(backend=s1_hosthdr, weight=1,
weight_update = "^X-Weight: (\d+\.\d+)",
# XXX add back ^X-Weight
weight_update = "X-Weight: (\d+\.\d+)",
interval = 1h);
vtc.sleep(1s);
foo.remove_backend(backend=s1);
......@@ -69,7 +71,8 @@ varnish v1 -vcl+backend {
GET /weight HTTP/1.1
Host: weighthost
"},
weight_update = "^X-Weight: ([\d.]+)",
# XXX add back ^X-Weight
weight_update = "X-Weight: ([\d.]+)",
interval = 1h);
vtc.sleep(1s);
return (synth(200));
......
......@@ -135,30 +135,6 @@ vmod_event_function(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e e)
return (0);
}
static vre_t *
wadj_cfg_re(VRT_CTX, struct vmod_weightadjust_random *rr, VCL_BACKEND be,
VCL_STRING weight_update)
{
vre_t *vre;
const char *err;
int erroff;
/*
* using PCRE options here violates the varnish interface abstaction -
* we'd need some more code duplication here if that was a concern
*/
vre = VRE_compile(weight_update, PCRE_MULTILINE,
&err, &erroff);
if (vre == NULL) {
VERR(ctx, "%s.%s regex errror (%s) for %s pos %d",
rr->vd->dir->vcl_name, be->vcl_name, err,
weight_update, erroff);
}
/* XXX check for one capturing subexpression */
return vre;
}
/* largely taken from vbp_build_req() */
static char *
wadj_cfg_req(const struct backend *be, VCL_STRING url, VCL_STRING request,
......@@ -242,10 +218,9 @@ vmod_random__fini(struct vmod_weightadjust_random **rrp)
}
VCL_VOID
vmod_random_add_backend(VRT_CTX, struct vmod_weightadjust_random *rr,
VCL_BACKEND vcl_be, VCL_REAL w, VCL_STRING weight_update, VCL_STRING url,
VCL_STRING request, VCL_DURATION timeout, VCL_DURATION interval,
VCL_INT buffer)
vmod_random_add_backend(VRT_CTX,
struct VPFX(weightadjust_random) *rr,
struct VARGS(random_add_backend) *args)
{
struct vmod_wadj_vcl *wa_vcl;
struct wadj_prop *prop;
......@@ -257,26 +232,26 @@ vmod_random_add_backend(VRT_CTX, struct vmod_weightadjust_random *rr,
wa_vcl = rr->extra.wa_vcl;
CHECK_OBJ_NOTNULL(wa_vcl, VMOD_WADJ_VCL_MAGIC);
if (vcl_be->priv == NULL) {
if (args->backend->priv == NULL) {
ERR(ctx, "can only be used with real backends "
"(no director layering)");
return;
}
CAST_OBJ_NOTNULL(be, vcl_be->priv, BACKEND_MAGIC);
CAST_OBJ_NOTNULL(be, args->backend->priv, BACKEND_MAGIC);
if (weight_update == NULL || weight_update[0] == '\0') {
if (! args->valid_weight_update) {
msg(ctx, "vmod weightadjust director %s "
"add_backend(%s) without weight update falls back "
"to standard behaviour",
rr->vd->dir->vcl_name, vcl_be->vcl_name);
rr->vd->dir->vcl_name, args->backend->vcl_name);
goto no_weightadjust;
}
vdir_rdlock(rr->vd);
nb = rr->vd->n_backend;
for (u = 0; u < nb; u++) {
if (rr->vd->backend[u] == vcl_be)
if (rr->vd->backend[u] == args->backend)
break;
}
vdir_unlock(rr->vd);
......@@ -292,19 +267,18 @@ vmod_random_add_backend(VRT_CTX, struct vmod_weightadjust_random *rr,
prop->vd = rr->vd;
prop->be = be;
prop->vd_updates = &rr->extra.updates;
prop->vre = wadj_cfg_re(ctx, rr, vcl_be, weight_update);
if (prop->vre == NULL)
goto err;
prop->req = wadj_cfg_req(be, url, request, &prop->reqlen);
if ((prop->timeout = timeout) < 0) {
AN(args->weight_update);
prop->vre = args->weight_update;
prop->req = wadj_cfg_req(be, args->url, args->request, &prop->reqlen);
if ((prop->timeout = args->timeout) < 0) {
ERR(ctx, "negative timeout");
goto err;
}
if ((prop->interval = interval ) < 0) {
if ((prop->interval = args->interval ) < 0) {
ERR(ctx, "negative interval");
goto err;
}
if ((prop->bufsz = buffer) < 128) {
if ((prop->bufsz = args->buffer) < 128) {
ERR(ctx, "buffer too small");
goto err;
}
......@@ -319,7 +293,7 @@ vmod_random_add_backend(VRT_CTX, struct vmod_weightadjust_random *rr,
AZ(pthread_mutex_unlock(&wa_vcl->mtx));
no_weightadjust:
vdir_add_backend(ctx, rr->vd, vcl_be, w);
vdir_add_backend(ctx, rr->vd, args->backend, args->weight);
return;
err:
......
......@@ -24,7 +24,7 @@ Example::
new vdir = weightadjust.random();
$Method VOID .add_backend(BACKEND backend, REAL weight,
STRING weight_update=0,
[REGEX weight_update],
STRING url="/",
STRING request=0,
DURATION timeout=2,
......@@ -47,7 +47,8 @@ Description
exactly one subexpression to capture a real value in C locale
format (ddd.ddd). The match is ran against the first *buffer*
bytes of the HTTP response, headers and body in multiline mode
(``^`` matches start of line, ``$`` matches end of line or end buffer).
(``^`` matches start of line, ``$`` matches end of line or end
buffer).
The interval to issue weight update requests is defined by the
*interval* parameter, the timeout both for sending and
......
......@@ -174,8 +174,8 @@ wadj_update(void *arg)
struct vdir *vd;
struct timespec due;
unsigned u = UINT_MAX;
const int ovsz = 2 * 3;
int i, ov[ovsz];
txt groups[2];
int i;
char *p, *pp;
AN(pr->bufsz);
char buf[pr->bufsz];
......@@ -208,8 +208,8 @@ wadj_update(void *arg)
assert(len <= pr->bufsz);
i = VRE_exec(pr->vre, buf, len, 0, PCRE_NEWLINE_ANYCRLF,
ov, ovsz, NULL);
// XXX should use VRE_MATCH_NOTBOL|VRE_MATCH_NOTEOL
i = VRE_capture(pr->vre, buf, len, 0 /*here*/, groups, 2, NULL);
if (i < 0) {
POKE_ERR(pr, "regular expression error %d", i);
......@@ -221,10 +221,8 @@ wadj_update(void *arg)
"need one caputuring match, got %d", i - 1);
continue;
}
assert(ov[2] < len);
assert(ov[3] < len);
p = buf + ov[2];
buf[ov[3]] = '\0';
groups[1].e = '\0';
p = (char *)groups[1].b;
errno = 0;
w = strtod(p, &pp);
......@@ -361,7 +359,6 @@ wadj_prop_fini(struct wadj_prop **propp)
CHECK_OBJ_NOTNULL(prop, WADJ_PROP_MAGIC);
assert(prop->run == STOPPED);
AN(prop->vre);
VRE_free(&prop->vre);
AN(prop->req);
free(prop->req);
AZ(pthread_cond_destroy(&prop->wakeup.cv));
......
......@@ -53,7 +53,7 @@ struct wadj_prop {
pthread_t thread;
struct wadj_prop_wakeup wakeup;
enum run_state_e run;
vre_t *vre;
VCL_REGEX vre;
char *req;
int reqlen;
VCL_DURATION timeout;
......
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