<div dir="ltr">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.<div><br></div><div>I would suggest one of the following:</div><div><br></div><div>- strstr</div><div>- instr</div><div>- contains</div><div><br></div><div>Also, just out of curiosity, exactly how much faster is this than a regex match? </div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Sep 20, 2014 at 11:12 AM, Per Buer <span dir="ltr"><<a href="mailto:perbu@varnish-software.com" target="_blank">perbu@varnish-software.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div> bin/varnishtest/tests/m00018.vtc | 34 ++++++++++++++++++++++++++++++++++<br></div><div> lib/libvmod_std/vmod.vcc | 12 +++++++++++-</div><div> lib/libvmod_std/vmod_std.c | 14 ++++++++++++++</div><div> 3 files changed, 59 insertions(+), 1 deletion(-)</div><div> create mode 100644 bin/varnishtest/tests/m00018.vtc</div><div><br></div><div>diff --git a/bin/varnishtest/tests/m00018.vtc b/bin/varnishtest/tests/m00018.vtc</div><div>new file mode 100644</div><div>index 0000000..af00716</div><div>--- /dev/null</div><div>+++ b/bin/varnishtest/tests/m00018.vtc</div><div>@@ -0,0 +1,34 @@</div><div>+varnishtest "Test substring matching in std"</div><div>+</div><div>+server s1 {</div><div>+ rxreq</div><div>+ txresp</div><div>+</div><div>+ rxreq</div><div>+ txresp</div><div>+} -start</div><div>+</div><div>+varnish v1 -vcl+backend {</div><div>+<span style="white-space:pre-wrap"> </span>import ${vmod_std};</div><div>+</div><div>+<span style="white-space:pre-wrap"> </span>sub vcl_deliver {</div><div>+<span style="white-space:pre-wrap"> </span> if (std.substr(req.url, "foo")) {</div><div>+<span style="white-space:pre-wrap"> </span>set resp.http.sub = "found";<span style="white-space:pre-wrap"> </span></div><div>+<span style="white-space:pre-wrap"> </span> } else {</div><div>+<span style="white-space:pre-wrap"> </span>set resp.http.sub = "not found";<span style="white-space:pre-wrap"> </span></div><div>+<span style="white-space:pre-wrap"> </span> }</div><div>+<span style="white-space:pre-wrap"> </span></div><div>+<span style="white-space:pre-wrap"> </span>}</div><div>+} -start</div><div>+</div><div>+client c1 {</div><div>+<span style="white-space:pre-wrap"> </span>txreq -url "/foobar"</div><div>+<span style="white-space:pre-wrap"> </span>rxresp</div><div>+<span style="white-space:pre-wrap"> </span>expect resp.http.sub == "found"</div><div>+</div><div>+</div><div>+<span style="white-space:pre-wrap"> </span>txreq -url "/quux"</div><div>+<span style="white-space:pre-wrap"> </span>rxresp</div><div>+<span style="white-space:pre-wrap"> </span>expect resp.http.sub == "not found"</div><div>+} -run</div><div>+</div><div>diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc</div><div>index fbc082b..e59c7a4 100644</div><div>--- a/lib/libvmod_std/vmod.vcc</div><div>+++ b/lib/libvmod_std/vmod.vcc</div><div>@@ -195,7 +195,6 @@ $Function STRING querysort(STRING)</div><div> </div><div> Description</div><div> Sorts the querystring for cache normalization purposes.</div><div>-</div><div> Example</div><div> set req.url = std.querysort(req.url);</div><div> </div><div>@@ -208,6 +207,17 @@ Example</div><div> </div><div> This will cache the req.body if its size is smaller than 1KB.</div><div> </div><div>+$Function BOOL substr(STRING, STRING)</div><div>+</div><div>+Description</div><div>+<span style="white-space:pre-wrap"> </span>Returns true if the second string is a substring of the first</div><div>+<span style="white-space:pre-wrap"> </span>string. Note that the comparison is case sensitive. You can</div><div>+<span style="white-space:pre-wrap"> </span>use the tolower function on both strings if you want case</div><div>+<span style="white-space:pre-wrap"> </span>insensitivity.</div><div>+Example</div><div>+<span style="white-space:pre-wrap"> </span>if (std.substr(req.url, req.http.x-restrict)) </div><div>+</div><div>+</div><div> </div><div> SEE ALSO</div><div> ========</div><div>diff --git a/lib/libvmod_std/vmod_std.c b/lib/libvmod_std/vmod_std.c</div><div>index 8872a1c..8a89bb8 100644</div><div>--- a/lib/libvmod_std/vmod_std.c</div><div>+++ b/lib/libvmod_std/vmod_std.c</div><div>@@ -239,3 +239,17 @@ vmod_cache_req_body(const struct vrt_ctx *ctx, VCL_BYTES size)</div><div> <span style="white-space:pre-wrap"> </span>result = VRT_CacheReqBody(ctx, size);</div><div> <span style="white-space:pre-wrap"> </span>VSLb(ctx->vsl, SLT_Debug,"VRT_CacheReqBody(%zu): %d", (size_t)size, result);</div><div> }</div><div>+</div><div>+VCL_BOOL __match_proto__(td_std_substr)</div><div>+vmod_substr(const struct vrt_ctx *ctx, VCL_STRING mstr, VCL_STRING msubstr)</div><div>+{</div><div>+ </div><div>+ CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);</div><div>+ char *match = strstr(mstr, msubstr);</div><div>+</div><div>+<span style="white-space:pre-wrap"> </span>if (match) </div><div>+<span style="white-space:pre-wrap"> </span> return(1);</div><div>+<span style="white-space:pre-wrap"> </span>else </div><div>+<span style="white-space:pre-wrap"> </span> return(0);</div><div>+}</div><div>+</div><span class="HOEnZb"><font color="#888888"><div>-- </div><div>1.9.3 (Apple Git-50)+GitX</div><div><br></div>-- <br><div dir="ltr"><table border="0" cellpadding="0" cellspacing="0" style="border-bottom-width:1px;padding-top:5px;border-top-style:solid;width:550px;padding-bottom:5px;border-bottom-color:rgb(238,238,238);border-top-width:1px;border-bottom-style:solid;line-height:1.5em;border-top-color:rgb(238,238,238);color:rgb(102,102,102);font-size:12px;font-family:'Helvetica Neue',Arial,sans-serif;margin-top:20px"><tbody><tr><td width="100"><img src="http://www.varnish-software.com/static/media/logo-email.png"></td><td><font color="#222222"><span style="font-size:14px"><b>Per Buer</b></span></font><br>CTO | Varnish Software AS<br>Cell: <a value="+4790181750" style="color:rgb(17,85,204)">+47 95839117</a><br><span style="font-weight:bold">We Make Websites Fly!<br><a href="https://www.varnish-software.com/" style="color:rgb(17,85,204)" target="_blank">www.varnish-software.com</a></span></td></tr></tbody></table><br><div style="color:rgb(136,136,136)"><a href="http://info.varnish-software.com/varnish-summits-autumn-2014-registration" style="color:rgb(17,85,204)" target="_blank"><img src="https://www.varnish-software.com/sites/default/files/u388/masters_s_0.png" alt=" Register now"></a></div></div>
</font></span></div>
<br>_______________________________________________<br>
varnish-dev mailing list<br>
<a href="mailto:varnish-dev@varnish-cache.org">varnish-dev@varnish-cache.org</a><br>
<a href="https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev" target="_blank">https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev</a><br></blockquote></div><br></div>