Commit 4d9878cb authored by Geoff Simmons's avatar Geoff Simmons

Add the info_str() method.

parent 645789a2
Pipeline #266 skipped
......@@ -112,6 +112,15 @@ regex.info_int
INT regex.info_int(ENUM {BACKREFMAX,CAPTURECOUNT,JITSIZE,MATCHLIMIT,MAXLOOKBEHIND,MINLENGTH,RECURSIONLIMIT,SIZE})
.. _func_regex.info_str:
regex.info_str
--------------
::
STRING regex.info_str(ENUM {BSR,FIRSTCODEUNIT,FIRSTCODEUNITS,LASTCODEUNIT,NEWLINE}, STRING sep=" ")
.. _func_match:
match
......
......@@ -25,6 +25,8 @@
*
*/
#include <stdio.h>
#include "vmod_pcre2.h"
#include "vcc_if.h"
......@@ -217,3 +219,111 @@ vmod_regex_info_int(VRT_CTX, struct vmod_pcre2_regex *regex, VCL_ENUM ints)
AZ(ret);
return where;
}
VCL_STRING
vmod_regex_info_str(VRT_CTX, struct vmod_pcre2_regex *regex, VCL_ENUM strs,
VCL_STRING sep)
{
uint32_t what, where;
int ret;
char *str;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(regex, VMOD_PCRE2_REGEX_MAGIC);
if (sep == NULL) {
VERR(ctx, "sep is undefined in %s.info_str()", regex->vcl_name);
return NULL;
}
if (strcmp(strs, "BSR") == 0)
what = PCRE2_INFO_BSR;
else if (strcmp(strs, "FIRSTCODEUNIT") == 0)
what = PCRE2_INFO_FIRSTCODEUNIT;
else if (strcmp(strs, "FIRSTCODEUNITS") == 0)
what = PCRE2_INFO_FIRSTBITMAP;
else if (strcmp(strs, "LASTCODEUNIT") == 0)
what = PCRE2_INFO_LASTCODEUNIT;
else if (strcmp(strs, "NEWLINE") == 0)
what = PCRE2_INFO_NEWLINE;
else
WRONG("illegal enum in info_str");
if (what == PCRE2_INFO_FIRSTBITMAP) {
unsigned bytes;
int ctr, len;
const uint8_t *map;
char *p;
ret = pcre2_pattern_info(regex->code, what, &map);
AZ(ret);
if (map == NULL)
return "";
str = WS_Front(ctx->ws);
bytes = WS_Reserve(ctx->ws, 0);
if (bytes == 0)
goto nomem;
ctr = bytes;
len = strlen(sep);
p = str;
for (int i = 1; i < 256; i++)
if (map[i >> 3] & (1 << (i & 7))) {
if (ctr - len - 2 < 0)
goto nomem;
sprintf(p, "%c%s", (uint8_t)i, sep);
p += len + 1;
ctr -= len + 1;
}
assert(ctr >= 0);
assert((unsigned)ctr < bytes);
*(p - len) = '\0';
WS_Release(ctx->ws, strlen(str) + 1);
return (VCL_STRING)str;
nomem:
VERRNOMEM(ctx, "allocating space for first code units in "
"%s.info_str()", regex->vcl_name);
WS_Release(ctx->ws, 0);
return NULL;
}
ret = pcre2_pattern_info(regex->code, what, &where);
AZ(ret);
if (what == PCRE2_INFO_FIRSTCODEUNIT
|| what == PCRE2_INFO_LASTCODEUNIT) {
if (where == 0)
return "";
if ((str = WS_Printf(ctx->ws, "%c", where)) == NULL) {
VERRNOMEM(ctx, "allocating space for return string in "
"%s.info_str()", regex->vcl_name);
return NULL;
}
return (VCL_STRING)str;
}
if (what == PCRE2_INFO_BSR)
switch(where) {
case PCRE2_BSR_UNICODE:
return("UNICODE");
case PCRE2_BSR_ANYCRLF:
return ("ANYCRLF");
default:
WRONG("unexpected value returned for BSR");
}
assert(what == PCRE2_INFO_NEWLINE);
switch(where) {
case PCRE2_NEWLINE_CR:
return("CR");
case PCRE2_NEWLINE_LF:
return ("LF");
case PCRE2_NEWLINE_CRLF:
return ("CRLF");
case PCRE2_NEWLINE_ANY:
return ("ANY");
case PCRE2_NEWLINE_ANYCRLF:
return ("ANYCRLF");
default:
WRONG("unexpected value returned for BSR");
}
}
......@@ -312,19 +312,18 @@ client c1 {
expect resp.http.multiline_arg == "true"
} -run
# Testing UTF for true is done in the UTF8-dependent tests
# Testing UTF and UCP for true is done in the UTF8-dependent tests
varnish v1 -vcl {
import pcre2 from "${vmod_topbuild}/src/.libs/libvmod_pcre2.so";
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new r1
new r
= pcre2.regex("abc", never_backslash_c=true, never_ucp=true,
never_utf=true, no_auto_capture=true,
no_auto_possess=true, no_dotstar_anchor=true,
no_start_optimize=true, no_utf_check=true,
ungreedy=true, use_offset_limit=true);
new r2 = pcre2.regex("abc", ucp=true);
}
sub vcl_recv {
......@@ -333,40 +332,38 @@ varnish v1 -vcl {
sub vcl_synth {
set resp.http.never_backslash_c
= r1.info_bool(NEVER_BACKSLASH_C);
= r.info_bool(NEVER_BACKSLASH_C);
set resp.http.never_backslash_c_arg
= r1.info_bool(NEVER_BACKSLASH_C, compiled=false);
set resp.http.never_ucp = r1.info_bool(NEVER_UCP);
= r.info_bool(NEVER_BACKSLASH_C, compiled=false);
set resp.http.never_ucp = r.info_bool(NEVER_UCP);
set resp.http.never_ucp_arg
= r1.info_bool(NEVER_UCP, compiled=false);
set resp.http.never_utf = r1.info_bool(NEVER_UTF);
= r.info_bool(NEVER_UCP, compiled=false);
set resp.http.never_utf = r.info_bool(NEVER_UTF);
set resp.http.never_utf_arg
= r1.info_bool(NEVER_UTF, compiled=false);
set resp.http.no_auto_capture = r1.info_bool(NO_AUTO_CAPTURE);
= r.info_bool(NEVER_UTF, compiled=false);
set resp.http.no_auto_capture = r.info_bool(NO_AUTO_CAPTURE);
set resp.http.no_auto_capture_arg
= r1.info_bool(NO_AUTO_CAPTURE, compiled=false);
set resp.http.no_auto_possess = r1.info_bool(NO_AUTO_POSSESS);
= r.info_bool(NO_AUTO_CAPTURE, compiled=false);
set resp.http.no_auto_possess = r.info_bool(NO_AUTO_POSSESS);
set resp.http.no_auto_possess_arg
= r1.info_bool(NO_AUTO_POSSESS, compiled=false);
= r.info_bool(NO_AUTO_POSSESS, compiled=false);
set resp.http.no_dotstar_anchor
= r1.info_bool(NO_DOTSTAR_ANCHOR);
= r.info_bool(NO_DOTSTAR_ANCHOR);
set resp.http.no_dotstar_anchor_arg
= r1.info_bool(NO_DOTSTAR_ANCHOR, compiled=false);
= r.info_bool(NO_DOTSTAR_ANCHOR, compiled=false);
set resp.http.no_start_optimize
= r1.info_bool(NO_START_OPTIMIZE);
= r.info_bool(NO_START_OPTIMIZE);
set resp.http.no_start_optimize_arg
= r1.info_bool(NO_START_OPTIMIZE, compiled=false);
set resp.http.no_utf_check = r1.info_bool(NO_UTF_CHECK);
= r.info_bool(NO_START_OPTIMIZE, compiled=false);
set resp.http.no_utf_check = r.info_bool(NO_UTF_CHECK);
set resp.http.no_utf_check_arg
= r1.info_bool(NO_UTF_CHECK, compiled=false);
set resp.http.ucp = r2.info_bool(UCP);
set resp.http.ucp_arg = r2.info_bool(UCP, compiled=false);
set resp.http.ungreedy = r1.info_bool(UNGREEDY);
= r.info_bool(NO_UTF_CHECK, compiled=false);
set resp.http.ungreedy = r.info_bool(UNGREEDY);
set resp.http.ungreedy_arg
= r1.info_bool(UNGREEDY, compiled=false);
set resp.http.use_offset_limit = r1.info_bool(USE_OFFSET_LIMIT);
= r.info_bool(UNGREEDY, compiled=false);
set resp.http.use_offset_limit = r.info_bool(USE_OFFSET_LIMIT);
set resp.http.use_offset_limit_arg
= r1.info_bool(USE_OFFSET_LIMIT, compiled=false);
= r.info_bool(USE_OFFSET_LIMIT, compiled=false);
return(deliver);
}
......@@ -392,8 +389,6 @@ client c1 {
expect resp.http.no_start_optimize_arg == "true"
expect resp.http.no_utf_check == "true"
expect resp.http.no_utf_check_arg == "true"
expect resp.http.ucp == "true"
expect resp.http.ucp_arg == "true"
expect resp.http.ungreedy == "true"
expect resp.http.ungreedy_arg == "true"
expect resp.http.use_offset_limit == "true"
......@@ -407,10 +402,9 @@ varnish v1 -vcl {
sub vcl_init {
new r1 = pcre2.regex("^abc");
new r2 = pcre2.regex("^abc", multiline=true);
new r3 = pcre2.regex("(*UCP)abc");
new r4 = pcre2.regex("(*NO_AUTO_POSSESS)abc");
new r5 = pcre2.regex("(*NO_START_OPT)abc");
new r6 = pcre2.regex("(*NO_DOTSTAR_ANCHOR).*abc");
new r3 = pcre2.regex("(*NO_AUTO_POSSESS)abc");
new r4 = pcre2.regex("(*NO_START_OPT)abc");
new r5 = pcre2.regex("(*NO_DOTSTAR_ANCHOR).*abc");
}
sub vcl_recv {
......@@ -424,19 +418,17 @@ varnish v1 -vcl {
set resp.http.anchored-2 = r2.info_bool(ANCHORED);
set resp.http.anchored_arg-2
= r2.info_bool(ANCHORED, compiled=false);
set resp.http.ucp = r3.info_bool(UCP);
set resp.http.ucp_arg = r3.info_bool(UCP, compiled=false);
set resp.http.no_auto_possess = r4.info_bool(NO_AUTO_POSSESS);
set resp.http.no_auto_possess = r3.info_bool(NO_AUTO_POSSESS);
set resp.http.no_auto_possess_arg
= r4.info_bool(NO_AUTO_POSSESS, compiled=false);
= r3.info_bool(NO_AUTO_POSSESS, compiled=false);
set resp.http.no_start_optimize
= r5.info_bool(NO_START_OPTIMIZE);
= r4.info_bool(NO_START_OPTIMIZE);
set resp.http.no_start_optimize_arg
= r5.info_bool(NO_START_OPTIMIZE, compiled=false);
= r4.info_bool(NO_START_OPTIMIZE, compiled=false);
set resp.http.no_dotstar_anchor
= r6.info_bool(NO_DOTSTAR_ANCHOR);
= r5.info_bool(NO_DOTSTAR_ANCHOR);
set resp.http.no_dotstar_anchor_arg
= r6.info_bool(NO_DOTSTAR_ANCHOR, compiled=false);
= r5.info_bool(NO_DOTSTAR_ANCHOR, compiled=false);
return(deliver);
}
......@@ -450,8 +442,6 @@ client c1 {
expect resp.http.anchored_arg-1 == "false"
expect resp.http.anchored-2 == "false"
expect resp.http.anchored_arg-2 == "false"
expect resp.http.ucp == "true"
expect resp.http.ucp_arg == "false"
expect resp.http.no_auto_possess == "true"
expect resp.http.no_auto_possess_arg == "false"
expect resp.http.no_start_optimize == "true"
......@@ -599,3 +589,138 @@ client c1 {
expect resp.http.jchanged-3 == "false"
expect resp.http.empty-3 == "true"
} -run
varnish v1 -vcl {
import pcre2 from "${vmod_topbuild}/src/.libs/libvmod_pcre2.so";
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new r1 = pcre2.regex("abc");
new r2 = pcre2.regex("a*bc");
new r3 = pcre2.regex("^abc");
new r4 = pcre2.regex("abc$");
new r5 = pcre2.regex("abc", anchored=true);
new r6 = pcre2.regex("(a|[^\d\n\rZ])");
new r7 = pcre2.regex("a|ba|\w");
}
sub vcl_recv {
return(synth(200));
}
sub vcl_synth {
set resp.http.first-1 = r1.info_str(FIRSTCODEUNIT);
set resp.http.firsts-1 = r1.info_str(FIRSTCODEUNITS);
set resp.http.last-1 = r1.info_str(LASTCODEUNIT);
set resp.http.first-2 = r2.info_str(FIRSTCODEUNIT);
set resp.http.firsts-2 = r2.info_str(FIRSTCODEUNITS);
set resp.http.last-2 = r2.info_str(LASTCODEUNIT);
set resp.http.first-3 = r3.info_str(FIRSTCODEUNIT);
set resp.http.firsts-3 = r3.info_str(FIRSTCODEUNITS);
set resp.http.last-3 = r3.info_str(LASTCODEUNIT);
set resp.http.first-4 = r4.info_str(FIRSTCODEUNIT);
set resp.http.firsts-4 = r4.info_str(FIRSTCODEUNITS);
set resp.http.last-4 = r4.info_str(LASTCODEUNIT);
set resp.http.first-5 = r5.info_str(FIRSTCODEUNIT);
set resp.http.firsts-5 = r5.info_str(FIRSTCODEUNITS);
set resp.http.last-5 = r5.info_str(LASTCODEUNIT);
set resp.http.first-6 = r6.info_str(FIRSTCODEUNIT);
set resp.http.firsts-6 = r6.info_str(FIRSTCODEUNITS);
set resp.http.last-6 = r6.info_str(LASTCODEUNIT);
set resp.http.first-7 = r7.info_str(FIRSTCODEUNIT);
set resp.http.firsts-7 = r7.info_str(FIRSTCODEUNITS);
set resp.http.last-7 = r7.info_str(LASTCODEUNIT);
set resp.http.firsts-stars-7
= r7.info_str(FIRSTCODEUNITS, sep="***");
return(deliver);
}
}
client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.first-1 == "a"
expect resp.http.firsts-1 == ""
expect resp.http.last-1 == "c"
expect resp.http.first-2 == ""
expect resp.http.firsts-2 == "a b"
expect resp.http.last-2 == "c"
expect resp.http.first-3 == ""
expect resp.http.firsts-3 == ""
expect resp.http.last-3 == ""
expect resp.http.first-4 == "a"
expect resp.http.firsts-4 == ""
expect resp.http.last-4 == "c"
expect resp.http.first-5 == ""
expect resp.http.firsts-5 == ""
expect resp.http.last-5 == ""
expect resp.http.first-6 == ""
expect resp.http.firsts-6 == {                          ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~  }
# "
expect resp.http.last-6 == ""
expect resp.http.first-7 == ""
expect resp.http.firsts-7 == "0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z _ a b c d e f g h i j k l m n o p q r s t u v w x y z"
expect resp.http.last-7 == ""
expect resp.http.firsts-stars-7 == "0***1***2***3***4***5***6***7***8***9***A***B***C***D***E***F***G***H***I***J***K***L***M***N***O***P***Q***R***S***T***U***V***W***X***Y***Z***_***a***b***c***d***e***f***g***h***i***j***k***l***m***n***o***p***q***r***s***t***u***v***w***x***y***z"
} -run
varnish v1 -vcl {
import pcre2 from "${vmod_topbuild}/src/.libs/libvmod_pcre2.so";
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new r1 = pcre2.regex("a\Rb");
new r2 = pcre2.regex("a\Rb", bsr=ANYCRLF);
new r3 = pcre2.regex("a\Rb", bsr=UNICODE);
new r4 = pcre2.regex("a\nb");
new r5 = pcre2.regex("a\nb", newline=CR);
new r6 = pcre2.regex("a\nb", newline=LF);
new r7 = pcre2.regex("a\nb", newline=CRLF);
new r8 = pcre2.regex("a\nb", newline=ANYCRLF);
new r9 = pcre2.regex("a\nb", newline=ANY);
}
sub vcl_recv {
return(synth(200));
}
sub vcl_synth {
set resp.http.default-bsr = pcre2.config_str(BSR);
set resp.http.bsr-1 = r1.info_str(BSR);
set resp.http.bsr-2 = r2.info_str(BSR);
set resp.http.bsr-3 = r3.info_str(BSR);
set resp.http.default-newline = pcre2.config_str(NEWLINE);
set resp.http.newline-4 = r4.info_str(NEWLINE);
set resp.http.newline-5 = r5.info_str(NEWLINE);
set resp.http.newline-6 = r6.info_str(NEWLINE);
set resp.http.newline-7 = r7.info_str(NEWLINE);
set resp.http.newline-8 = r8.info_str(NEWLINE);
set resp.http.newline-9 = r9.info_str(NEWLINE);
return(deliver);
}
}
client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.bsr-1 == resp.http.default-bsr
expect resp.http.bsr-2 == "ANYCRLF"
expect resp.http.bsr-3 == "UNICODE"
expect resp.http.newline-4 == resp.http.default-newline
expect resp.http.newline-5 == "CR"
expect resp.http.newline-6 == "LF"
expect resp.http.newline-7 == "CRLF"
expect resp.http.newline-8 == "ANYCRLF"
expect resp.http.newline-9 == "ANY"
} -run
......@@ -66,6 +66,9 @@ $Method BOOL .info_bool(ENUM {ALLOW_EMPTY_CLASS, ANCHORED, ALT_BSUX,
$Method INT .info_int(ENUM {BACKREFMAX, CAPTURECOUNT, JITSIZE, MATCHLIMIT,
MAXLOOKBEHIND, MINLENGTH, RECURSIONLIMIT, SIZE})
$Method STRING .info_str(ENUM {BSR, FIRSTCODEUNIT, FIRSTCODEUNITS, LASTCODEUNIT,
NEWLINE}, STRING sep=" ")
$Function BOOL match(PRIV_CALL, PRIV_TASK, STRING pattern, STRING subject,
BOOL allow_empty_class=0, BOOL anchored=0,
ENUM {ANYCRLF, UNICODE} bsr=0, BOOL alt_bsux=0,
......
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