Discussion:
[PATCH] substring matching in std
Per Buer
2014-09-20 09:12:39 UTC
Permalink
bin/varnishtest/tests/m00018.vtc | 34 ++++++++++++++++++++++++++++++++++
lib/libvmod_std/vmod.vcc | 12 +++++++++++-
lib/libvmod_std/vmod_std.c | 14 ++++++++++++++
3 files changed, 59 insertions(+), 1 deletion(-)
create mode 100644 bin/varnishtest/tests/m00018.vtc

diff --git a/bin/varnishtest/tests/m00018.vtc
b/bin/varnishtest/tests/m00018.vtc
new file mode 100644
index 0000000..af00716
--- /dev/null
+++ b/bin/varnishtest/tests/m00018.vtc
@@ -0,0 +1,34 @@
+varnishtest "Test substring matching in std"
+
+server s1 {
+ rxreq
+ txresp
+
+ rxreq
+ txresp
+} -start
+
+varnish v1 -vcl+backend {
+ import ${vmod_std};
+
+ sub vcl_deliver {
+ if (std.substr(req.url, "foo")) {
+ set resp.http.sub = "found";
+ } else {
+ set resp.http.sub = "not found";
+ }
+
+ }
+} -start
+
+client c1 {
+ txreq -url "/foobar"
+ rxresp
+ expect resp.http.sub == "found"
+
+
+ txreq -url "/quux"
+ rxresp
+ expect resp.http.sub == "not found"
+} -run
+
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index fbc082b..e59c7a4 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -195,7 +195,6 @@ $Function STRING querysort(STRING)

Description
Sorts the querystring for cache normalization purposes.
-
Example
set req.url = std.querysort(req.url);

@@ -208,6 +207,17 @@ Example

This will cache the req.body if its size is smaller than 1KB.

+$Function BOOL substr(STRING, STRING)
+
+Description
+ Returns true if the second string is a substring of the first
+ string. Note that the comparison is case sensitive. You can
+ use the tolower function on both strings if you want case
+ insensitivity.
+Example
+ if (std.substr(req.url, req.http.x-restrict))
+
+

SEE ALSO
========
diff --git a/lib/libvmod_std/vmod_std.c b/lib/libvmod_std/vmod_std.c
index 8872a1c..8a89bb8 100644
--- a/lib/libvmod_std/vmod_std.c
+++ b/lib/libvmod_std/vmod_std.c
@@ -239,3 +239,17 @@ vmod_cache_req_body(const struct vrt_ctx *ctx,
VCL_BYTES size)
result = VRT_CacheReqBody(ctx, size);
VSLb(ctx->vsl, SLT_Debug,"VRT_CacheReqBody(%zu): %d", (size_t)size,
result);
}
+
+VCL_BOOL __match_proto__(td_std_substr)
+vmod_substr(const struct vrt_ctx *ctx, VCL_STRING mstr, VCL_STRING msubstr)
+{
+
+ CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+ char *match = strstr(mstr, msubstr);
+
+ if (match)
+ return(1);
+ else
+ return(0);
+}
+
--
1.9.3 (Apple Git-50)+GitX
--
*Per Buer*
CTO | Varnish Software AS
Cell: +47 95839117
We Make Websites Fly!
www.varnish-software.com
[image: Register now]
<http://info.varnish-software.com/varnish-summits-autumn-2014-registration>
Rogier 'DocWilco' Mulhuijzen
2014-09-20 09:30:34 UTC
Permalink
I'm not fond of using "substr" as the function name, as other languages use
this function with start/end or start/length parameters to create a new
string from the existing string.

I would suggest one of the following:

- strstr
- instr
- contains

Also, just out of curiosity, exactly how much faster is this than a regex
match?
Post by Per Buer
bin/varnishtest/tests/m00018.vtc | 34 ++++++++++++++++++++++++++++++++++
lib/libvmod_std/vmod.vcc | 12 +++++++++++-
lib/libvmod_std/vmod_std.c | 14 ++++++++++++++
3 files changed, 59 insertions(+), 1 deletion(-)
create mode 100644 bin/varnishtest/tests/m00018.vtc
diff --git a/bin/varnishtest/tests/m00018.vtc
b/bin/varnishtest/tests/m00018.vtc
new file mode 100644
index 0000000..af00716
--- /dev/null
+++ b/bin/varnishtest/tests/m00018.vtc
@@ -0,0 +1,34 @@
+varnishtest "Test substring matching in std"
+
+server s1 {
+ rxreq
+ txresp
+
+ rxreq
+ txresp
+} -start
+
+varnish v1 -vcl+backend {
+ import ${vmod_std};
+
+ sub vcl_deliver {
+ if (std.substr(req.url, "foo")) {
+ set resp.http.sub = "found";
+ } else {
+ set resp.http.sub = "not found";
+ }
+
+ }
+} -start
+
+client c1 {
+ txreq -url "/foobar"
+ rxresp
+ expect resp.http.sub == "found"
+
+
+ txreq -url "/quux"
+ rxresp
+ expect resp.http.sub == "not found"
+} -run
+
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index fbc082b..e59c7a4 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -195,7 +195,6 @@ $Function STRING querysort(STRING)
Description
Sorts the querystring for cache normalization purposes.
-
Example
set req.url = std.querysort(req.url);
@@ -208,6 +207,17 @@ Example
This will cache the req.body if its size is smaller than 1KB.
+$Function BOOL substr(STRING, STRING)
+
+Description
+ Returns true if the second string is a substring of the first
+ string. Note that the comparison is case sensitive. You can
+ use the tolower function on both strings if you want case
+ insensitivity.
+Example
+ if (std.substr(req.url, req.http.x-restrict))
+
+
SEE ALSO
========
diff --git a/lib/libvmod_std/vmod_std.c b/lib/libvmod_std/vmod_std.c
index 8872a1c..8a89bb8 100644
--- a/lib/libvmod_std/vmod_std.c
+++ b/lib/libvmod_std/vmod_std.c
@@ -239,3 +239,17 @@ vmod_cache_req_body(const struct vrt_ctx *ctx,
VCL_BYTES size)
result = VRT_CacheReqBody(ctx, size);
VSLb(ctx->vsl, SLT_Debug,"VRT_CacheReqBody(%zu): %d", (size_t)size,
result);
}
+
+VCL_BOOL __match_proto__(td_std_substr)
+vmod_substr(const struct vrt_ctx *ctx, VCL_STRING mstr, VCL_STRING msubstr)
+{
+
+ CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+ char *match = strstr(mstr, msubstr);
+
+ if (match)
+ return(1);
+ else
+ return(0);
+}
+
--
1.9.3 (Apple Git-50)+GitX
--
*Per Buer*
CTO | Varnish Software AS
Cell: +47 95839117
We Make Websites Fly!
www.varnish-software.com
[image: Register now]
<http://info.varnish-software.com/varnish-summits-autumn-2014-registration>
_______________________________________________
varnish-dev mailing list
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev
Federico Schwindt
2014-09-20 11:00:00 UTC
Permalink
I'd prefer to keep substr (or rename it to strstr) but returning the
substring instead so you can also use the string.

The reason for this is that regex match doesn't work on dynamic strings.
Post by Rogier 'DocWilco' Mulhuijzen
I'm not fond of using "substr" as the function name, as other languages
use this function with start/end or start/length parameters to create a new
string from the existing string.
- strstr
- instr
- contains
Also, just out of curiosity, exactly how much faster is this than a regex
match?
Post by Per Buer
bin/varnishtest/tests/m00018.vtc | 34 ++++++++++++++++++++++++++++++++++
lib/libvmod_std/vmod.vcc | 12 +++++++++++-
lib/libvmod_std/vmod_std.c | 14 ++++++++++++++
3 files changed, 59 insertions(+), 1 deletion(-)
create mode 100644 bin/varnishtest/tests/m00018.vtc
diff --git a/bin/varnishtest/tests/m00018.vtc
b/bin/varnishtest/tests/m00018.vtc
new file mode 100644
index 0000000..af00716
--- /dev/null
+++ b/bin/varnishtest/tests/m00018.vtc
@@ -0,0 +1,34 @@
+varnishtest "Test substring matching in std"
+
+server s1 {
+ rxreq
+ txresp
+
+ rxreq
+ txresp
+} -start
+
+varnish v1 -vcl+backend {
+ import ${vmod_std};
+
+ sub vcl_deliver {
+ if (std.substr(req.url, "foo")) {
+ set resp.http.sub = "found";
+ } else {
+ set resp.http.sub = "not found";
+ }
+
+ }
+} -start
+
+client c1 {
+ txreq -url "/foobar"
+ rxresp
+ expect resp.http.sub == "found"
+
+
+ txreq -url "/quux"
+ rxresp
+ expect resp.http.sub == "not found"
+} -run
+
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index fbc082b..e59c7a4 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -195,7 +195,6 @@ $Function STRING querysort(STRING)
Description
Sorts the querystring for cache normalization purposes.
-
Example
set req.url = std.querysort(req.url);
@@ -208,6 +207,17 @@ Example
This will cache the req.body if its size is smaller than 1KB.
+$Function BOOL substr(STRING, STRING)
+
+Description
+ Returns true if the second string is a substring of the first
+ string. Note that the comparison is case sensitive. You can
+ use the tolower function on both strings if you want case
+ insensitivity.
+Example
+ if (std.substr(req.url, req.http.x-restrict))
+
+
SEE ALSO
========
diff --git a/lib/libvmod_std/vmod_std.c b/lib/libvmod_std/vmod_std.c
index 8872a1c..8a89bb8 100644
--- a/lib/libvmod_std/vmod_std.c
+++ b/lib/libvmod_std/vmod_std.c
@@ -239,3 +239,17 @@ vmod_cache_req_body(const struct vrt_ctx *ctx,
VCL_BYTES size)
result = VRT_CacheReqBody(ctx, size);
VSLb(ctx->vsl, SLT_Debug,"VRT_CacheReqBody(%zu): %d", (size_t)size,
result);
}
+
+VCL_BOOL __match_proto__(td_std_substr)
+vmod_substr(const struct vrt_ctx *ctx, VCL_STRING mstr, VCL_STRING msubstr)
+{
+
+ CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+ char *match = strstr(mstr, msubstr);
+
+ if (match)
+ return(1);
+ else
+ return(0);
+}
+
--
1.9.3 (Apple Git-50)+GitX
--
*Per Buer*
CTO | Varnish Software AS
Cell: +47 95839117
We Make Websites Fly!
www.varnish-software.com
[image: Register now]
<http://info.varnish-software.com/varnish-summits-autumn-2014-registration>
_______________________________________________
varnish-dev mailing list
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev
_______________________________________________
varnish-dev mailing list
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev
Nils Goroll
2014-09-20 11:55:23 UTC
Permalink
Post by Federico Schwindt
The reason for this is that regex match doesn't work on dynamic strings.
https://code.uplex.de/uplex-varnish/libvmod-re
Per Buer
2014-09-20 15:58:41 UTC
Permalink
Right. Renamed to strstr and returns the substring. The use is the more or
less the same.
From 41498f59900079f8ec653ba517d867b35aea77cf Mon Sep 17 00:00:00 2001
Subject: [PATCH] add std.strstr

---
bin/varnishtest/tests/m00018.vtc | 34 ++++++++++++++++++++++++++++++++++
lib/libvmod_std/vmod.vcc | 12 +++++++++++-
lib/libvmod_std/vmod_std.c | 11 +++++++++++
3 files changed, 56 insertions(+), 1 deletion(-)
create mode 100644 bin/varnishtest/tests/m00018.vtc

diff --git a/bin/varnishtest/tests/m00018.vtc
b/bin/varnishtest/tests/m00018.vtc
new file mode 100644
index 0000000..b51ed18
--- /dev/null
+++ b/bin/varnishtest/tests/m00018.vtc
@@ -0,0 +1,34 @@
+varnishtest "Test substring matching in std"
+
+server s1 {
+ rxreq
+ txresp
+
+ rxreq
+ txresp
+} -start
+
+varnish v1 -vcl+backend {
+ import ${vmod_std};
+
+ sub vcl_deliver {
+ if (std.strstr(req.url, "foo")) {
+ set resp.http.sub = "found";
+ } else {
+ set resp.http.sub = "not found";
+ }
+
+ }
+} -start
+
+client c1 {
+ txreq -url "/foobar"
+ rxresp
+ expect resp.http.sub == "found"
+
+
+ txreq -url "/quux"
+ rxresp
+ expect resp.http.sub == "not found"
+} -run
+
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index fbc082b..455485a 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -195,7 +195,6 @@ $Function STRING querysort(STRING)

Description
Sorts the querystring for cache normalization purposes.
-
Example
set req.url = std.querysort(req.url);

@@ -208,6 +207,17 @@ Example

This will cache the req.body if its size is smaller than 1KB.

+$Function STRING strstr(STRING, STRING)
+
+Description
+ Returns true if the second string is a substring of the first
+ string. Note that the comparison is case sensitive. You can
+ use the tolower function on both strings if you want case
+ insensitivity.
+Example
+ if (std.strstr(req.url, req.http.x-restrict))
+
+

SEE ALSO
========
diff --git a/lib/libvmod_std/vmod_std.c b/lib/libvmod_std/vmod_std.c
index 8872a1c..898e8dd 100644
--- a/lib/libvmod_std/vmod_std.c
+++ b/lib/libvmod_std/vmod_std.c
@@ -239,3 +239,14 @@ vmod_cache_req_body(const struct vrt_ctx *ctx,
VCL_BYTES size)
result = VRT_CacheReqBody(ctx, size);
VSLb(ctx->vsl, SLT_Debug,"VRT_CacheReqBody(%zu): %d", (size_t)size,
result);
}
+
+VCL_STRING __match_proto__(td_std_strstr)
+vmod_strstr(const struct vrt_ctx *ctx, VCL_STRING mstr, VCL_STRING msubstr)
+{
+
+ CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+ char *match = strstr(mstr, msubstr);
+
+ return(match);
+}
+
--
1.9.3 (Apple Git-50)+GitX
I'd prefer to keep substr (or rename it to strstr) but returning the
substring instead so you can also use the string.
The reason for this is that regex match doesn't work on dynamic strings.
On 20 Sep 2014 10:31, "Rogier 'DocWilco' Mulhuijzen" <
Post by Rogier 'DocWilco' Mulhuijzen
I'm not fond of using "substr" as the function name, as other languages
use this function with start/end or start/length parameters to create a new
string from the existing string.
- strstr
- instr
- contains
Also, just out of curiosity, exactly how much faster is this than a regex
match?
Post by Per Buer
bin/varnishtest/tests/m00018.vtc | 34 ++++++++++++++++++++++++++++++++++
lib/libvmod_std/vmod.vcc | 12 +++++++++++-
lib/libvmod_std/vmod_std.c | 14 ++++++++++++++
3 files changed, 59 insertions(+), 1 deletion(-)
create mode 100644 bin/varnishtest/tests/m00018.vtc
diff --git a/bin/varnishtest/tests/m00018.vtc
b/bin/varnishtest/tests/m00018.vtc
new file mode 100644
index 0000000..af00716
--- /dev/null
+++ b/bin/varnishtest/tests/m00018.vtc
@@ -0,0 +1,34 @@
+varnishtest "Test substring matching in std"
+
+server s1 {
+ rxreq
+ txresp
+
+ rxreq
+ txresp
+} -start
+
+varnish v1 -vcl+backend {
+ import ${vmod_std};
+
+ sub vcl_deliver {
+ if (std.substr(req.url, "foo")) {
+ set resp.http.sub = "found";
+ } else {
+ set resp.http.sub = "not found";
+ }
+
+ }
+} -start
+
+client c1 {
+ txreq -url "/foobar"
+ rxresp
+ expect resp.http.sub == "found"
+
+
+ txreq -url "/quux"
+ rxresp
+ expect resp.http.sub == "not found"
+} -run
+
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index fbc082b..e59c7a4 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -195,7 +195,6 @@ $Function STRING querysort(STRING)
Description
Sorts the querystring for cache normalization purposes.
-
Example
set req.url = std.querysort(req.url);
@@ -208,6 +207,17 @@ Example
This will cache the req.body if its size is smaller than 1KB.
+$Function BOOL substr(STRING, STRING)
+
+Description
+ Returns true if the second string is a substring of the first
+ string. Note that the comparison is case sensitive. You can
+ use the tolower function on both strings if you want case
+ insensitivity.
+Example
+ if (std.substr(req.url, req.http.x-restrict))
+
+
SEE ALSO
========
diff --git a/lib/libvmod_std/vmod_std.c b/lib/libvmod_std/vmod_std.c
index 8872a1c..8a89bb8 100644
--- a/lib/libvmod_std/vmod_std.c
+++ b/lib/libvmod_std/vmod_std.c
@@ -239,3 +239,17 @@ vmod_cache_req_body(const struct vrt_ctx *ctx,
VCL_BYTES size)
result = VRT_CacheReqBody(ctx, size);
VSLb(ctx->vsl, SLT_Debug,"VRT_CacheReqBody(%zu): %d", (size_t)size,
result);
}
+
+VCL_BOOL __match_proto__(td_std_substr)
+vmod_substr(const struct vrt_ctx *ctx, VCL_STRING mstr, VCL_STRING msubstr)
+{
+
+ CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+ char *match = strstr(mstr, msubstr);
+
+ if (match)
+ return(1);
+ else
+ return(0);
+}
+
--
1.9.3 (Apple Git-50)+GitX
--
*Per Buer*
CTO | Varnish Software AS
Cell: +47 95839117
We Make Websites Fly!
www.varnish-software.com
[image: Register now]
<http://info.varnish-software.com/varnish-summits-autumn-2014-registration>
_______________________________________________
varnish-dev mailing list
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev
_______________________________________________
varnish-dev mailing list
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev
--
*Per Buer*
CTO | Varnish Software AS
Cell: +47 95839117
We Make Websites Fly!
www.varnish-software.com
[image: Register now]
<http://info.varnish-software.com/varnish-summits-autumn-2014-registration>
Poul-Henning Kamp
2014-09-22 07:09:59 UTC
Permalink
--------
Post by Per Buer
Right. Renamed to strstr and returns the substring. The use is the more or
less the same.
Looks good, but two questions:

A) Should it use strcasestr() ?

or

B) should we have both std.strstr() an std.strcasestr() ?

Commit when you agree on the answer and have tested the resulting patch...
--
Poul-Henning Kamp | UNIX since Zilog Zeus 3.20
phk-***@public.gmane.org | TCP/IP since RFC 956
FreeBSD committer | BSD since 4.3-tahoe
Never attribute to malice what can adequately be explained by incompetence.
Poul-Henning Kamp
2014-09-22 07:46:30 UTC
Permalink
--------
Post by Poul-Henning Kamp
--------
Post by Per Buer
Right. Renamed to strstr and returns the substring. The use is the more or
less the same.
A) Should it use strcasestr() ?
or
B) should we have both std.strstr() an std.strcasestr() ?
Commit when you agree on the answer and have tested the resulting patch...
Ohh, and: It needs to test for NULL inputs...
--
Poul-Henning Kamp | UNIX since Zilog Zeus 3.20
phk-***@public.gmane.org | TCP/IP since RFC 956
FreeBSD committer | BSD since 4.3-tahoe
Never attribute to malice what can adequately be explained by incompetence.
Loading...