Commit ebbc84ee authored by Geoff Simmons's avatar Geoff Simmons

added identity encode and decode, also fixed wb_append() and wb_printf

parent 7f59f107
......@@ -15,6 +15,17 @@ varnish v1 -vcl+backend {
convert.transcode(IDENTITY, HEXLC, req.url +
"Hel" + "lo " + "" + "world" +
req.url);
set resp.http.id =
convert.encode(IDENTITY, convert.decode(IDENTITY,
"The quick brown fox jumps over the lazy dog"));
set resp.http.idlist =
convert.encode(IDENTITY, convert.decode(IDENTITY, "" + req.url +
"The quick " + "brown fox jumps over " + "" +
"the lazy dog" + "" + req.url + ""));
set resp.http.idempty =
convert.encode(IDENTITY, convert.decode(IDENTITY, ""));
set resp.http.idemptylist =
convert.encode(IDENTITY, convert.decode(IDENTITY, "" + "" + ""));
}
} -start
......@@ -22,6 +33,10 @@ client c1 {
txreq -url "/"
rxresp
expect resp.http.hex == "2f48656c6c6f20776f726c642f"
expect resp.http.id == "The quick brown fox jumps over the lazy dog"
expect resp.http.idlist == "/The quick brown fox jumps over the lazy dog/"
expect resp.http.idempty == ""
expect resp.http.idemptylist == ""
}
client c1 -run
......@@ -79,6 +79,62 @@ event(VRT_CTX, struct vmod_priv *priv, enum vcl_event_e event)
return (0);
}
static VCL_BLOB
id_encode(struct ws *ws, struct vmod_priv *rblob, const char *in, size_t inlen)
{
struct wb_s wb;
CHECK_OBJ_NOTNULL(ws, WS_MAGIC);
AN(rblob);
if (in == NULL || inlen == 0)
return null_blob;
if (!wb_create(ws, &wb) || !wb_append(&wb, in, inlen))
return NULL;
(void) wb_finish_blob(&wb, rblob);
assert(rblob->len > 0);
return rblob;
}
static VCL_BLOB
id_decode(struct ws *ws, struct vmod_priv *rblob, const char *p, va_list ap)
{
const char *next;
struct wb_s wb;
CHECK_OBJ_NOTNULL(ws, WS_MAGIC);
AN(rblob);
if (p == vrt_magic_string_end)
return null_blob;
do {
next = va_arg(ap, const char *);
} while (*next == '\0' || next == NULL);
if (next == vrt_magic_string_end && ( p == NULL || *p == '\0'))
return null_blob;
if (!wb_create(ws, &wb) || !wb_append(&wb, p, -1))
return NULL;
while (next != vrt_magic_string_end) {
if (!wb_append(&wb, next, -1)) {
wb_reset(&wb);
return NULL;
}
do {
next = va_arg(ap, const char *);
} while (next == NULL || *next == '\0');
}
(void) wb_finish_blob(&wb, rblob);
if (rblob->len == 0)
return null_blob;
return rblob;
}
/*
* Base64-encode *in (size: inlen) into the blob supplied as rblob. If
* there is insufficient space, it will bail out and return
......@@ -262,10 +318,17 @@ hex_encode(struct ws *ws, struct vmod_priv *rblob, const ssize_t len,
VCL_BLOB
vmod_decode(VRT_CTX, VCL_ENUM decs, const char *p, ...) {
enum encoding dec = parse_encoding(decs);
va_list ap;
struct vmod_priv tmp[1];
VCL_BLOB r;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
assert(dec != _INVALID);
return null_blob;
assert(dec == IDENTITY); // XXX
va_start(ap, p);
r = id_decode(ctx->ws, tmp, p, ap);
va_end(ap);
return r;
}
VCL_STRING
......@@ -279,7 +342,7 @@ vmod_encode(VRT_CTX, VCL_ENUM encs, VCL_BLOB b) {
switch(enc) {
case IDENTITY:
// XXX
r = id_encode(ctx->ws, tmp, b->priv, b->len);
break;
case BASE64:
case BASE64URL:
......
......@@ -57,7 +57,7 @@ wb_reset(struct wb_s *wb)
}
bool
wb_append(struct wb_s *wb, const char *format, ...)
wb_printf(struct wb_s *wb, const char *format, ...)
{
int len;
va_list ap;
......@@ -77,6 +77,18 @@ wb_append(struct wb_s *wb, const char *format, ...)
return true;
}
bool
wb_append(struct wb_s *wb, const char *p, int len)
{
if (len == -1)
len = strlen(p);
if (len >= wb_space(wb))
return false;
memcpy(wb->w, p, len);
wb_advance(wb, len);
return true;
}
/*
* release varnish workspace
*
......
......@@ -47,5 +47,6 @@ wb_advanceP(struct wb_s *wb, char *w) {
char *wb_create(struct ws *ws, struct wb_s *wb);
void wb_reset(struct wb_s *wb);
bool wb_printf(struct wb_s *wb, const char *format, ...);
bool wb_append(struct wb_s *wb, const char *p, int len);
char *wb_finish(struct wb_s *wb, ssize_t *l);
struct vmod_priv *wb_finish_blob(struct wb_s *wb, struct vmod_priv *blob);
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