Skip to content

Commit 9d4c378

Browse files
committed
vmod_blob: Add convencience functions for urldecode and urlencode
For the common cases, the generic and versatile syntax is really cumbersome. Why exactly did we not add these earlier?
1 parent c412418 commit 9d4c378

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

vmod/tests/blob_b00012.vtc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ varnish v1 -arg "-p workspace_client=256k" -vcl {
4747
set resp.http.id2url
4848
= blob.transcode(IDENTITY, URL, encoded=req.http.foobar);
4949

50+
set resp.http.urlencode = blob.urlencode(req.http.foobar);
51+
5052
set resp.http.id2urluc
5153
= blob.transcode(IDENTITY, URL, UPPER, encoded=req.http.foobar);
5254

@@ -108,6 +110,7 @@ client c1 {
108110
expect resp.http.id2hexuc == "4D616E2069732064697374696E677569736865642C206E6F74206F6E6C792062792068697320726561736F6E2C2062757420627920746869732073696E67756C61722070617373696F6E2066726F6D206F7468657220616E696D616C732C2077686963682069732061206C757374206F6620746865206D696E642C20746861742062792061207065727365766572616E6365206F662064656C6967687420696E2074686520636F6E74696E75656420616E6420696E6465666174696761626C652067656E65726174696F6E206F66206B6E6F776C656467652C2065786365656473207468652073686F727420766568656D656E6365206F6620616E79206361726E616C20706C6561737572652E"
109111
expect resp.http.id2hexlc == resp.http.id2hex
110112
expect resp.http.id2url == "foo%3abar%3abaz%3aquux"
113+
expect resp.http.urlencode == resp.http.id2url
111114
expect resp.http.id2urluc == "foo%3Abar%3Abaz%3Aquux"
112115
expect resp.http.id2urllc == resp.http.id2url
113116
expect resp.http.b642id == "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."
@@ -294,6 +297,8 @@ varnish v1 -vcl {
294297
set resp.http.urllc2id
295298
= blob.transcode(URL, IDENTITY, encoded=req.http.urlhobbeslc);
296299

300+
set resp.http.urldecode = blob.urldecode(req.http.urlhobbeslc);
301+
297302
set resp.http.urlalldownuc2b64
298303
= blob.transcode(URL, BASE64, encoded=req.http.urlalldownuc);
299304

@@ -376,6 +381,7 @@ client c1 {
376381
rxresp
377382
expect resp.http.urluc2id == "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."
378383
expect resp.http.urllc2id == "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."
384+
expect resp.http.urldecode == resp.http.urllc2id
379385
expect resp.http.urlalldownuc2b64 == "//79/Pv6+fj39vX08/Lx8O/u7ezr6uno5+bl5OPi4eDf3t3c29rZ2NfW1dTT0tHQz87NzMvKycjHxsXEw8LBwL++vby7urm4t7a1tLOysbCvrq2sq6qpqKempaSjoqGgn56dnJuamZiXlpWUk5KRkI+OjYyLiomIh4aFhIOCgYB/fn18e3p5eHd2dXRzcnFwb25tbGtqaWhnZmVkY2JhYF9eXVxbWllYV1ZVVFNSUVBPTk1MS0pJSEdGRURDQkFAPz49PDs6OTg3NjU0MzIxMC8uLSwrKikoJyYlJCMiISAfHh0cGxoZGBcWFRQTEhEQDw4NDAsKCQgHBgUEAwIBAA=="
380386
expect resp.http.urlalldownuc2b64url == "__79_Pv6-fj39vX08_Lx8O_u7ezr6uno5-bl5OPi4eDf3t3c29rZ2NfW1dTT0tHQz87NzMvKycjHxsXEw8LBwL--vby7urm4t7a1tLOysbCvrq2sq6qpqKempaSjoqGgn56dnJuamZiXlpWUk5KRkI-OjYyLiomIh4aFhIOCgYB_fn18e3p5eHd2dXRzcnFwb25tbGtqaWhnZmVkY2JhYF9eXVxbWllYV1ZVVFNSUVBPTk1MS0pJSEdGRURDQkFAPz49PDs6OTg3NjU0MzIxMC8uLSwrKikoJyYlJCMiISAfHh0cGxoZGBcWFRQTEhEQDw4NDAsKCQgHBgUEAwIBAA=="
381387
expect resp.http.urlalldownuc2b64nopad == "__79_Pv6-fj39vX08_Lx8O_u7ezr6uno5-bl5OPi4eDf3t3c29rZ2NfW1dTT0tHQz87NzMvKycjHxsXEw8LBwL--vby7urm4t7a1tLOysbCvrq2sq6qpqKempaSjoqGgn56dnJuamZiXlpWUk5KRkI-OjYyLiomIh4aFhIOCgYB_fn18e3p5eHd2dXRzcnFwb25tbGtqaWhnZmVkY2JhYF9eXVxbWllYV1ZVVFNSUVBPTk1MS0pJSEdGRURDQkFAPz49PDs6OTg3NjU0MzIxMC8uLSwrKikoJyYlJCMiISAfHh0cGxoZGBcWFRQTEhEQDw4NDAsKCQgHBgUEAwIBAA"

vmod/vmod_blob.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,20 @@ vmod_transcode(VRT_CTX, VCL_ENUM decs, VCL_ENUM encs, VCL_ENUM case_s,
476476
return (r);
477477
}
478478

479+
VCL_STRING v_matchproto_(td_blob_urldecode)
480+
vmod_urldecode(VRT_CTX, VCL_STRANDS s)
481+
{
482+
return (vmod_transcode(ctx, VENUM(URL), VENUM(IDENTITY), VENUM(DEFAULT),
483+
0, s));
484+
}
485+
486+
VCL_STRING v_matchproto_(td_blob_urlencode)
487+
vmod_urlencode(VRT_CTX, VCL_STRANDS s)
488+
{
489+
return (vmod_transcode(ctx, VENUM(IDENTITY), VENUM(URL), VENUM(DEFAULT),
490+
0, s));
491+
}
492+
479493
VCL_BOOL v_matchproto_(td_blob_same)
480494
vmod_same(VRT_CTX, VCL_BLOB b1, VCL_BLOB b2)
481495
{

vmod/vmod_blob.vcc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@ Example::
277277
set resp.http.urlencoded
278278
= blob.transcode(encoded="foo bar", encoding=URL);
279279

280+
$Function STRING urldecode(STRANDS s)
281+
282+
Shorthand for ``blob.transcode(encoded = *s*, decoding=URL);``
283+
284+
$Function STRING urlencode(STRANDS s)
285+
286+
Shorthand for ``blob.transcode(encoded = *s*, encoding=URL);``
287+
280288
$Function BOOL same(BLOB, BLOB)
281289

282290
Returns ``true`` if and only if the two BLOB arguments are the same

0 commit comments

Comments
 (0)