Add a fallback argument to j.unquote()

parent 4d11868f
......@@ -504,8 +504,15 @@ vmod_object(VRT_CTX, VCL_STRANDS s)
return (p + 1);
}
#define fail(ctx, fallback, ...) do { \
if (fallback != NULL) \
VSLb(ctx->vsl, SLT_Error, __VA_ARGS__); \
else \
VRT_fail(ctx, __VA_ARGS__); \
} while(0)
VCL_STRING
vmod_unquote(VRT_CTX, VCL_STRING p)
vmod_unquote(VRT_CTX, VCL_STRING p, VCL_STRING fallback)
{
struct vsb vsb[1];
const char *err = NULL;
......@@ -515,15 +522,15 @@ vmod_unquote(VRT_CTX, VCL_STRING p)
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (*p != '"' || p[strlen(p) - 1] != '"') {
VRT_fail(ctx, "j.unquote() argument missing quotes");
return (NULL);
fail(ctx, fallback, "j.unquote() argument missing quotes");
return (fallback);
}
p++;
WS_VSB_new(vsb, ctx->ws);
if (vsbjunquot(vsb, p, &err) && err != NULL) {
VRT_fail(ctx, "j.unquote() error at: ...%.10s", err);
return (NULL);
fail(ctx, fallback, "j.unquote() error at: ...%.10s", err);
return (fallback);
}
r = WS_VSB_finish(vsb, ctx->ws, NULL);
if (r == NULL) {
......
......@@ -39,7 +39,7 @@ SYNOPSIS
STRING object(STRING)
STRING unquote(STRING)
STRING unquote(STRING, STRING fallback)
INTRODUCTION
......@@ -404,10 +404,13 @@ Deprecated alias for ``object()``.
.. _j.unquote():
STRING unquote(STRING)
----------------------
STRING unquote(STRING, STRING fallback=0)
-----------------------------------------
Utility function to decode JSON strings into UTF-8.
Utility function to decode JSON strings into UTF-8
If decoding fails and *fallback* is provided, the error is logged with
tag ``Error`` and *fallback* is returned. Otherwise, the VCL is failed.
WARNING
......
......@@ -339,9 +339,12 @@ concatenation argument (strands) as key/value pairs.
$Alias obj object
$Function STRING unquote(STRING)
$Function STRING unquote(STRING, STRING fallback=0)
Utility function to decode JSON strings into UTF-8
Utility function to decode JSON strings into UTF-8.
If decoding fails and *fallback* is provided, the error is logged with
tag ``Error`` and *fallback* is returned. Otherwise, the VCL is failed.
WARNING
......
......@@ -108,6 +108,10 @@ varnish v1 -vcl {
"C" + j.array("A" + 2 + j.object(j.nil()))
);
set resp.http.o4 = j.object(j.string("A") + j.object(j.nil()));
set resp.http.unok = j.unquote({""abc""});
set resp.http.unno = j.unquote("", j.null());
set resp.body = "";
return (deliver);
}
......@@ -156,6 +160,9 @@ client c1 {
expect resp.http.o3 == \
"{\"A\":null,\"B\":{\"BB\":42.42e42,\"CC\":false,\"DD\":true},\"C\":[\"A\",2,{}]}"
expect resp.http.o4 == "{\"A\":{}}"
expect resp.http.unok == "abc"
expect resp.http.unno == "null"
} -start
logexpect l2 -v v1 -q "ReqURL ~ \"^/e-badutf\"" {
......
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