Commit ef22cf37 authored by Geoff Simmons's avatar Geoff Simmons

The optimization in trancode() to return the concatenated STRING_LIST

when the encoding and decoding are the same is only allowed when the
decoding was legal. Also, use a VRT function to concatenate the
STRING_LIST into workspace (rather than rolling our own).
parent db66dd1a
......@@ -56,6 +56,14 @@ varnish v1 -vcl+backend {
"+/+/" + req.http.foo);
set resp.http.badpad = convert.transcode(BASE64URLNOPAD, IDENTITY,
"TWFu" + req.http.foo);
set resp.http.badhex2hex = convert.transcode(HEX, HEX, "abcdefg");
set resp.http.bad64264 = convert.transcode(BASE64, BASE64,
"_-_-" + req.http.foo);
set resp.http.badurl2url = convert.transcode(BASE64URL, BASE64URL,
"/+/+" + req.http.foo);
set resp.http.badpad2pad
= convert.transcode(BASE64URLNOPAD, BASE64URLNOPAD,
"Zm9v" + req.http.foo);
}
} -start
......@@ -73,9 +81,13 @@ client c1 {
expect resp.http.b64url2b64url == "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
expect resp.http.b64urlnopad2b64urlnopad == "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
expect resp.http.badhex == ""
expect resp.http.badhex2hex == ""
expect resp.http.bad64 == ""
expect resp.http.bad64264 == ""
expect resp.http.badurl == ""
expect resp.http.badurl2url == ""
expect resp.http.badpad == ""
expect resp.http.badpad2pad == ""
}
client c1 -run
......@@ -89,6 +101,14 @@ logexpect l1 -v v1 -d 1 -g vxid -q "VCL_Error" {
expect * = VCL_Error "^vmod convert error: cannot decode, illegal encoding beginning with \"\+/\+/\"$"
#"
expect * = VCL_Error "^vmod convert error: cannot decode, illegal encoding beginning with \"TWFu\"$"
#"
expect * = VCL_Error "^vmod convert error: cannot decode, illegal encoding beginning with \"abcdefg\"$"
#"
expect * = VCL_Error "^vmod convert error: cannot decode, illegal encoding beginning with \"_-_-\"$"
#"
expect * = VCL_Error "^vmod convert error: cannot decode, illegal encoding beginning with \"/\+/\+\"$"
#"
expect * = VCL_Error "^vmod convert error: cannot decode, illegal encoding beginning with \"Zm9v\"$"
#"
expect * = End
} -start
......
......@@ -71,43 +71,6 @@ static const struct vmod_priv const null_blob[1] =
}
};
static inline VCL_STRING
concat_va(struct ws *ws, const char *p, va_list ap)
{
const char *next;
struct wb_s wb;
CHECK_OBJ_NOTNULL(ws, WS_MAGIC);
if (p == vrt_magic_string_end)
return "";
SKIP_EMPTY(next, ap);
if (next == vrt_magic_string_end) {
if ( p == NULL || *p == '\0')
return "";
else
return p;
}
if (!wb_create(ws, &wb) || !wb_append(&wb, p, -1))
return NULL;
do {
if (!wb_append(&wb, next, -1)) {
wb_reset(&wb);
return NULL;
}
SKIP_EMPTY(next, ap);
} while (next != vrt_magic_string_end);
if (wb_len(&wb) == 0) {
wb_reset(&wb);
return "";
}
return wb_finish(&wb, NULL);
}
static inline size_t
decode_l_va(enum encoding dec, const char * const p, va_list ap)
{
......@@ -318,6 +281,7 @@ vmod_decode(VRT_CTX, VCL_ENUM decs, const char *p, ...) {
ILLEGAL(ctx, "decode()");
return NULL;
}
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
snap = WS_Snapshot(ctx->ws);
if ((b = WS_Alloc(ctx->ws, sizeof(struct vmod_priv))) == NULL) {
......@@ -368,6 +332,7 @@ vmod_encode(VRT_CTX, VCL_ENUM encs, VCL_BLOB b) {
if (b == NULL)
return NULL;
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
if (wb_create(ctx->ws, &wb) == NULL) {
ERRNOMEM(ctx, "cannot encode");
return NULL;
......@@ -401,18 +366,7 @@ vmod_transcode(VRT_CTX, VCL_ENUM decs, VCL_ENUM encs, const char *p, ...) {
ILLEGAL(ctx, "transcode()");
return NULL;
}
/*
* If the encoding and decoding are the same, just return the
* concatenated string.
* XXX: Doesn't reject illegal decodings.
*/
if (enc == dec) {
va_start(ap, p);
VCL_STRING s = concat_va(ctx->ws, p, ap);
va_end(ap);
return s;
}
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
/* Allocate space for the decoded blob on the stack */
va_start(ap, p);
......@@ -432,6 +386,17 @@ vmod_transcode(VRT_CTX, VCL_ENUM decs, VCL_ENUM encs, const char *p, ...) {
return NULL;
}
/*
* If the encoding and decoding are the same, and the decoding was
* legal, just return the concatenated string.
*/
if (enc == dec) {
va_start(ap, p);
VCL_STRING s = VRT_String(ctx->ws, NULL, p, ap);
va_end(ap);
return s;
}
if (wb_create(ctx->ws, &wb) == NULL) {
ERRNOMEM(ctx, "cannot encode after decode");
return NULL;
......
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