int vslq_runquery(const struct vslq_query *query, struct VSL_transaction * const ptrans[]) { struct VSL_transaction *t; struct VSL_cursor *c; int i, len; const char *data; CHECK_OBJ_NOTNULL(query, VSLQ_QUERY_MAGIC); AN(query->regex); t = ptrans[0]; while (t) { c = t->c; while (1) { i = VSL_Next(c); if (i == 0) break; assert(i == 1); AN(c->rec.ptr); len = VSL_LEN(c->rec.ptr); data = VSL_CDATA(c->rec.ptr); i = VRE_exec(query->regex, data, len, 0, 0, NULL, 0, NULL); if (i != VRE_ERROR_NOMATCH) { AZ(VSL_ResetCursor(c)); return (1); } } AZ(VSL_ResetCursor(c)); t = *++ptrans; } return (0); }
static void cmd_http_expect(CMD_ARGS) { struct http *hp; const char *lhs; char *cmp; const char *rhs; vre_t *vre; const char *error; int erroroffset; int i; (void)cmd; (void)vl; CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); assert(!strcmp(av[0], "expect")); av++; AN(av[0]); AN(av[1]); AN(av[2]); AZ(av[3]); lhs = cmd_var_resolve(hp, av[0]); cmp = av[1]; rhs = cmd_var_resolve(hp, av[2]); if (!strcmp(cmp, "==")) { if (strcmp(lhs, rhs)) vtc_log(hp->vl, 0, "EXPECT %s (%s) %s %s (%s) failed", av[0], lhs, av[1], av[2], rhs); else vtc_log(hp->vl, 4, "EXPECT %s (%s) %s %s (%s) match", av[0], lhs, av[1], av[2], rhs); } else if (!strcmp(cmp, "!=")) { if (!strcmp(lhs, rhs)) vtc_log(hp->vl, 0, "EXPECT %s (%s) %s %s (%s) failed", av[0], lhs, av[1], av[2], rhs); else vtc_log(hp->vl, 4, "EXPECT %s (%s) %s %s (%s) match", av[0], lhs, av[1], av[2], rhs); } else if (!strcmp(cmp, "~") || !strcmp(cmp, "!~")) { vre = VRE_compile(rhs, 0, &error, &erroroffset); if (vre == NULL) vtc_log(hp->vl, 0, "REGEXP error: %s (@%d) (%s)", error, erroroffset, rhs); i = VRE_exec(vre, lhs, strlen(lhs), 0, 0, NULL, 0, 0); if ((i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!')) vtc_log(hp->vl, 4, "EXPECT %s (%s) %s \"%s\" match", av[0], lhs, cmp, rhs); else vtc_log(hp->vl, 0, "EXPECT %s (%s) %s \"%s\" failed", av[0], lhs, cmp, rhs); VRE_free(&vre); } else { vtc_log(hp->vl, 0, "EXPECT %s (%s) %s %s (%s) test not implemented", av[0], lhs, av[1], av[2], rhs); } }
void vtc_expect(struct vtclog *vl, const char *olhs, const char *lhs, const char *cmp, const char *orhs, const char *rhs) { vre_t *vre; const char *error; int erroroffset; int i, j, retval = -1; double fl, fr; j = lhs == NULL || rhs == NULL; if (lhs == NULL) lhs = "<undef>"; if (rhs == NULL) rhs = "<undef>"; if (!strcmp(cmp, "~") || !strcmp(cmp, "!~")) { vre = VRE_compile(rhs, 0, &error, &erroroffset); if (vre == NULL) vtc_fatal(vl, "REGEXP error: %s (@%d) (%s)", error, erroroffset, rhs); i = VRE_exec(vre, lhs, strlen(lhs), 0, 0, NULL, 0, 0); retval = (i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!'); VRE_free(&vre); } else if (!strcmp(cmp, "==")) { retval = strcmp(lhs, rhs) == 0; } else if (!strcmp(cmp, "!=")) { retval = strcmp(lhs, rhs) != 0; } else if (j) { // fail inequality comparisons if either side is undef'ed retval = 0; } else { fl = VNUM(lhs); fr = VNUM(rhs); if (!strcmp(cmp, "<")) retval = isless(fl, fr); else if (!strcmp(cmp, ">")) retval = isgreater(fl, fr); else if (!strcmp(cmp, "<=")) retval = islessequal(fl, fr); else if (!strcmp(cmp, ">=")) retval = isgreaterequal(fl, fr); } if (retval == -1) vtc_fatal(vl, "EXPECT %s (%s) %s %s (%s) test not implemented", olhs, lhs, cmp, orhs, rhs); else if (retval == 0) vtc_fatal(vl, "EXPECT %s (%s) %s \"%s\" failed", olhs, lhs, cmp, rhs); else vtc_log(vl, 4, "EXPECT %s (%s) %s \"%s\" match", olhs, lhs, cmp, rhs); }
int VRT_re_match(const struct sess *sp, const char *s, void *re) { vre_t *t; int i; if (s == NULL) s = ""; AN(re); t = re; i = VRE_exec(t, s, strlen(s), 0, 0, NULL, 0, ¶ms->vre_limits); if (i >= 0) return (1); if (i < VRE_ERROR_NOMATCH ) WSP(sp, SLT_VCL_Error, "Regexp matching returned %d", i); return (0); }
int VRT_re_match(struct req *req, const char *s, void *re) { vre_t *t; int i; CHECK_OBJ_NOTNULL(req, REQ_MAGIC); if (s == NULL) s = ""; AN(re); t = re; i = VRE_exec(t, s, strlen(s), 0, 0, NULL, 0, &cache_param->vre_limits); if (i >= 0) return (1); if (i < VRE_ERROR_NOMATCH ) VSLb(req->vsl, SLT_VCL_Error, "Regexp matching returned %d", i); return (0); }
static int vsl_match_IX(struct VSL_data *vsl, const vslf_list *list, const struct VSL_cursor *c) { enum VSL_tag_e tag; const char *cdata; int len; const struct vslf *vslf; (void)vsl; tag = VSL_TAG(c->rec.ptr); cdata = VSL_CDATA(c->rec.ptr); len = VSL_LEN(c->rec.ptr); VTAILQ_FOREACH(vslf, list, list) { CHECK_OBJ_NOTNULL(vslf, VSLF_MAGIC); if (vslf->tag >= 0 && vslf->tag != tag) continue; if (VRE_exec(vslf->vre, cdata, len, 0, 0, NULL, 0, NULL) >= 0) return (1); }
const char * VRT_regsub(const struct sess *sp, int all, const char *str, void *re, const char *sub) { int ovector[30]; vre_t *t; int i, l; txt res; char *b0; const char *s; unsigned u, x; AN(re); if (str == NULL) str = ""; t = re; memset(ovector, 0, sizeof(ovector)); i = VRE_exec(t, str, strlen(str), 0, 0, ovector, 30, ¶ms->vre_limits); /* If it didn't match, we can return the original string */ if (i == VRE_ERROR_NOMATCH) return(str); if (i < VRE_ERROR_NOMATCH ) { WSP(sp, SLT_VCL_Error, "Regexp matching returned %d", i); return(str); } u = WS_Reserve(sp->http->ws, 0); res.e = res.b = b0 = sp->http->ws->f; res.e += u; do { /* Copy prefix to match */ Tadd(&res, str, ovector[0]); for (s = sub ; *s != '\0'; s++ ) { if (*s != '\\' || s[1] == '\0') { if (res.b < res.e) *res.b++ = *s; continue; } s++; if (isdigit(*s)) { x = *s - '0'; l = ovector[2*x+1] - ovector[2*x]; Tadd(&res, str + ovector[2*x], l); continue; } else { if (res.b < res.e) *res.b++ = *s; } } str += ovector[1]; if (!all) break; memset(&ovector, 0, sizeof(ovector)); i = VRE_exec(t, str, strlen(str), 0, 0, ovector, 30, ¶ms->vre_limits); if (i < VRE_ERROR_NOMATCH ) { WSP(sp, SLT_VCL_Error, "Regexp matching returned %d", i); return(str); } } while (i != VRE_ERROR_NOMATCH); /* Copy suffix to match */ l = strlen(str) + 1; Tadd(&res, str, l); if (res.b >= res.e) { WS_Release(sp->http->ws, 0); return (str); } Tcheck(res); WS_ReleaseP(sp->http->ws, res.b); return (b0); }
static void cmd_http_expect(CMD_ARGS) { struct http *hp; const char *lhs, *clhs; char *cmp; const char *rhs, *crhs; vre_t *vre; const char *error; int erroroffset; int i, retval = -1; (void)cmd; (void)vl; CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); AZ(strcmp(av[0], "expect")); av++; AN(av[0]); AN(av[1]); AN(av[2]); AZ(av[3]); lhs = cmd_var_resolve(hp, av[0]); cmp = av[1]; rhs = cmd_var_resolve(hp, av[2]); clhs = lhs ? lhs : "<undef>"; crhs = rhs ? rhs : "<undef>"; if (!strcmp(cmp, "~") || !strcmp(cmp, "!~")) { vre = VRE_compile(crhs, 0, &error, &erroroffset); if (vre == NULL) vtc_log(hp->vl, 0, "REGEXP error: %s (@%d) (%s)", error, erroroffset, crhs); i = VRE_exec(vre, clhs, strlen(clhs), 0, 0, NULL, 0, 0); retval = (i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!'); VRE_free(&vre); } else if (!strcmp(cmp, "==")) { retval = strcmp(clhs, crhs) == 0; } else if (!strcmp(cmp, "!=")) { retval = strcmp(clhs, crhs) != 0; } else if (lhs == NULL || rhs == NULL) { // fail inequality comparisons if either side is undef'ed retval = 0; } else if (!strcmp(cmp, "<")) { retval = isless(VNUM(lhs), VNUM(rhs)); } else if (!strcmp(cmp, ">")) { retval = isgreater(VNUM(lhs), VNUM(rhs)); } else if (!strcmp(cmp, "<=")) { retval = islessequal(VNUM(lhs), VNUM(rhs)); } else if (!strcmp(cmp, ">=")) { retval = isgreaterequal(VNUM(lhs), VNUM(rhs)); } if (retval == -1) vtc_log(hp->vl, 0, "EXPECT %s (%s) %s %s (%s) test not implemented", av[0], clhs, av[1], av[2], crhs); else vtc_log(hp->vl, retval ? 4 : 0, "EXPECT %s (%s) %s \"%s\" %s", av[0], clhs, cmp, crhs, retval ? "match" : "failed"); }
const char * VRT_regsub(struct req *req, int all, const char *str, void *re, const char *sub) { int ovector[30]; vre_t *t; int i, l; txt res; char *b0; const char *s; unsigned u, x; int options = 0; size_t len; CHECK_OBJ_NOTNULL(req, REQ_MAGIC); AN(re); if (str == NULL) str = ""; if (sub == NULL) sub = ""; t = re; memset(ovector, 0, sizeof(ovector)); len = strlen(str); i = VRE_exec(t, str, len, 0, options, ovector, 30, &cache_param->vre_limits); /* If it didn't match, we can return the original string */ if (i == VRE_ERROR_NOMATCH) return(str); if (i < VRE_ERROR_NOMATCH ) { VSLb(req->vsl, SLT_VCL_Error, "Regexp matching returned %d", i); return(str); } u = WS_Reserve(req->http->ws, 0); res.e = res.b = b0 = req->http->ws->f; res.e += u; do { /* Copy prefix to match */ Tadd(&res, str, ovector[0]); for (s = sub ; *s != '\0'; s++ ) { if (*s != '\\' || s[1] == '\0') { if (res.b < res.e) *res.b++ = *s; continue; } s++; if (isdigit(*s)) { x = *s - '0'; l = ovector[2*x+1] - ovector[2*x]; Tadd(&res, str + ovector[2*x], l); continue; } else { if (res.b < res.e) *res.b++ = *s; } } str += ovector[1]; len -= ovector[1]; if (!all) break; memset(&ovector, 0, sizeof(ovector)); options |= VRE_NOTEMPTY; i = VRE_exec(t, str, len, 0, options, ovector, 30, &cache_param->vre_limits); if (i < VRE_ERROR_NOMATCH ) { WS_Release(req->http->ws, 0); VSLb(req->vsl, SLT_VCL_Error, "Regexp matching returned %d", i); return(str); } } while (i != VRE_ERROR_NOMATCH); /* Copy suffix to match */ Tadd(&res, str, len+1); if (res.b >= res.e) { WS_Release(req->http->ws, 0); return (str); } Tcheck(res); WS_ReleaseP(req->http->ws, res.b); return (b0); }
int VSL_NextLog(struct VSL_data *vd, unsigned char **pp) { unsigned char *p, t; unsigned u, l; int i; CHECK_OBJ_NOTNULL(vd, VSL_MAGIC); while (1) { i = vsl_nextlog(vd, &p); if (i != 1) return (i); u = SHMLOG_ID(p); l = SHMLOG_LEN(p); (void)l; switch(p[SHMLOG_TAG]) { case SLT_SessionOpen: case SLT_ReqStart: vbit_set(vd->vbm_client, u); vbit_clr(vd->vbm_backend, u); break; case SLT_BackendOpen: case SLT_BackendXID: vbit_clr(vd->vbm_client, u); vbit_set(vd->vbm_backend, u); break; default: break; } if (vd->skip) { --vd->skip; continue; } else if (vd->keep) { if (--vd->keep == 0) return (-1); } t = p[SHMLOG_TAG]; if (vbit_test(vd->vbm_select, t)) { *pp = p; return (1); } if (vbit_test(vd->vbm_supress, t)) continue; if (vd->b_opt && !vbit_test(vd->vbm_backend, u)) continue; if (vd->c_opt && !vbit_test(vd->vbm_client, u)) continue; if (vd->regincl != NULL) { i = VRE_exec(vd->regincl, (char *)p + SHMLOG_DATA, SHMLOG_LEN(p), /* Length */ 0, 0, NULL, 0); if (i == VRE_ERROR_NOMATCH) continue; } if (vd->regexcl != NULL) { i = VRE_exec(vd->regexcl, (char *)p + SHMLOG_DATA, SHMLOG_LEN(p), /* Length */ 0, 0, NULL, 0); if (i != VRE_ERROR_NOMATCH) continue; } *pp = p; return (1); } }