Commit 8859a614 authored by Nils Goroll's avatar Nils Goroll

first bits working

parent 807eb5c5
# looks like -*- perl -*-
varnishtest "Test convert vmod"
server s1 {
......@@ -9,14 +11,17 @@ varnish v1 -vcl+backend {
import convert from "${vmod_topbuild}/src/.libs/libvmod_convert.so";
sub vcl_deliver {
set resp.http.hello = convert.hello("World");
set resp.http.hex =
convert.transcode(IDENTITY, HEXLC, req.url +
"Hel" + "lo " + "" + "world" +
req.url);
}
} -start
client c1 {
txreq -url "/"
rxresp
expect resp.http.hello == "Hello, World"
expect resp.http.hex == "2f48656c6c6f20776f726c642f"
}
client c1 -run
......@@ -23,6 +23,101 @@ static const struct vmod_priv const null_blob[1] =
}
};
static inline char
hexnibblelc(char c)
{
return ((c > 9) ? ('a' + (c - 0xa)) : ('0' + c));
}
/*
* en/decoders need to work for the string_list vargs case (implicit length) as
* well as for the blob case (explicit length). Fortunately, the blob case is
* not varadic, so we can assume just one length argument _or_ varags
*
* rblob: alloc additional whitespace so any result can also be (safely)
* used as a string.
*/
static VCL_BLOB
hex_encode_va(struct ws *ws, struct vmod_priv *rblob, const ssize_t len,
const char *in, va_list va)
{
char *p = ws->f;
char *out = p; // should be const, but (struct vmod_priv).priv isnt
const char *e = out + WS_Reserve(ws, 0);
ssize_t insz;
int i;
AN(rblob);
if (in == vrt_magic_string_end)
goto nodata;
if (len != -1)
insz = len;
else if (in == NULL)
insz = 0;
else
insz = strlen(in);
fprintf(stderr, "magic_string_end %p\n", vrt_magic_string_end);
do {
if (insz == 0)
goto next;
fprintf(stderr, "debug in %p %s\n", in, in);
// p + 2 includes terminating null byte
for (i = 0; i < insz && p + 2 < e; i++) {
*p++ = hexnibblelc((in[i] & 0xf0) >> 4);
*p++ = hexnibblelc( in[i] & 0x0f);
}
if (i != insz)
goto err;
next:
in = va_arg(va, const char *);
if (in == vrt_magic_string_end)
break;
insz = (in != NULL) ? strlen(in) : 0;
} while(1);
if (p == out)
goto nodata;
*p++ = '\0';
assert(p < e);
rblob->priv = out;
rblob->len = p - out - 1;
rblob->free = NULL;
WS_ReleaseP(ws, p);
return rblob;
nodata:
WS_Release(ws, 0);
return null_blob;
err:
WS_Release(ws, 0);
return NULL;
}
static VCL_BLOB
hex_encode(struct ws *ws, struct vmod_priv *rblob, const ssize_t len,
const char *in, ...)
{
va_list ap;
VCL_BLOB r;
va_start(ap, in);
r = hex_encode_va(ws, rblob, len, in, ap);
va_end(ap);
return r;
}
/* Functions */
VCL_BLOB
......@@ -36,18 +131,41 @@ vmod_decode(VRT_CTX, VCL_ENUM decs, const char *p, ...) {
VCL_STRING
vmod_encode(VRT_CTX, VCL_ENUM encs, VCL_BLOB b) {
enum encoding enc = parse_encoding(encs);
struct vmod_priv tmp[1];
const struct vmod_priv *r;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
assert(enc != _INVALID);
return "XXX";
assert(enc == HEXLC); // XXX
r = hex_encode(ctx->ws, tmp, b->len, b->priv, vrt_magic_string_end);
if (r == NULL)
return NULL;
return r->priv;
}
VCL_STRING
VCL_STRING __match_proto__(td_convert_transcode)
vmod_transcode(VRT_CTX, VCL_ENUM decs, VCL_ENUM encs, const char *p, ...) {
enum encoding dec = parse_encoding(decs);
enum encoding enc = parse_encoding(encs);
struct vmod_priv tmp[1];
va_list ap;
VCL_BLOB r;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
assert(dec != _INVALID);
assert(enc != _INVALID);
return p;
// XXX DO THE DECODE BIT
assert(enc == HEXLC); // XXX
va_start(ap, p);
r = hex_encode_va(ctx->ws, tmp, -1, p, ap);
va_end(ap);
if (r == NULL)
return NULL;
return r->priv;
}
VCL_BLOB
vmod_transcode_blob(VRT_CTX, VCL_ENUM decs, VCL_ENUM encs, VCL_BLOB b) {
......
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