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