Add a fallback argument to j.unquote()

parent 4d11868f
...@@ -504,8 +504,15 @@ vmod_object(VRT_CTX, VCL_STRANDS s) ...@@ -504,8 +504,15 @@ vmod_object(VRT_CTX, VCL_STRANDS s)
return (p + 1); 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 VCL_STRING
vmod_unquote(VRT_CTX, VCL_STRING p) vmod_unquote(VRT_CTX, VCL_STRING p, VCL_STRING fallback)
{ {
struct vsb vsb[1]; struct vsb vsb[1];
const char *err = NULL; const char *err = NULL;
...@@ -515,15 +522,15 @@ vmod_unquote(VRT_CTX, VCL_STRING p) ...@@ -515,15 +522,15 @@ vmod_unquote(VRT_CTX, VCL_STRING p)
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (*p != '"' || p[strlen(p) - 1] != '"') { if (*p != '"' || p[strlen(p) - 1] != '"') {
VRT_fail(ctx, "j.unquote() argument missing quotes"); fail(ctx, fallback, "j.unquote() argument missing quotes");
return (NULL); return (fallback);
} }
p++; p++;
WS_VSB_new(vsb, ctx->ws); WS_VSB_new(vsb, ctx->ws);
if (vsbjunquot(vsb, p, &err) && err != NULL) { if (vsbjunquot(vsb, p, &err) && err != NULL) {
VRT_fail(ctx, "j.unquote() error at: ...%.10s", err); fail(ctx, fallback, "j.unquote() error at: ...%.10s", err);
return (NULL); return (fallback);
} }
r = WS_VSB_finish(vsb, ctx->ws, NULL); r = WS_VSB_finish(vsb, ctx->ws, NULL);
if (r == NULL) { if (r == NULL) {
......
...@@ -39,7 +39,7 @@ SYNOPSIS ...@@ -39,7 +39,7 @@ SYNOPSIS
STRING object(STRING) STRING object(STRING)
STRING unquote(STRING) STRING unquote(STRING, STRING fallback)
INTRODUCTION INTRODUCTION
...@@ -404,10 +404,13 @@ Deprecated alias for ``object()``. ...@@ -404,10 +404,13 @@ Deprecated alias for ``object()``.
.. _j.unquote(): .. _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 WARNING
......
...@@ -339,9 +339,12 @@ concatenation argument (strands) as key/value pairs. ...@@ -339,9 +339,12 @@ concatenation argument (strands) as key/value pairs.
$Alias obj object $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 WARNING
......
...@@ -108,6 +108,10 @@ varnish v1 -vcl { ...@@ -108,6 +108,10 @@ varnish v1 -vcl {
"C" + j.array("A" + 2 + j.object(j.nil())) "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.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 = ""; set resp.body = "";
return (deliver); return (deliver);
} }
...@@ -156,6 +160,9 @@ client c1 { ...@@ -156,6 +160,9 @@ client c1 {
expect resp.http.o3 == \ expect resp.http.o3 == \
"{\"A\":null,\"B\":{\"BB\":42.42e42,\"CC\":false,\"DD\":true},\"C\":[\"A\",2,{}]}" "{\"A\":null,\"B\":{\"BB\":42.42e42,\"CC\":false,\"DD\":true},\"C\":[\"A\",2,{}]}"
expect resp.http.o4 == "{\"A\":{}}" expect resp.http.o4 == "{\"A\":{}}"
expect resp.http.unok == "abc"
expect resp.http.unno == "null"
} -start } -start
logexpect l2 -v v1 -q "ReqURL ~ \"^/e-badutf\"" { 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