Commit ca49a982 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

First cut at gzip support in varnishtest.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@5676 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 55a60283
......@@ -9,7 +9,7 @@ check: varnishtest
DISTCLEANFILES = _.ok
INCLUDES = -I$(top_srcdir)/include
INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/lib/libvgz
bin_PROGRAMS = varnishtest
......@@ -30,6 +30,7 @@ varnishtest_LDADD = \
$(top_builddir)/lib/libvarnish/libvarnish.la \
$(top_builddir)/lib/libvarnishcompat/libvarnishcompat.la \
$(top_builddir)/lib/libvarnishapi/libvarnishapi.la \
$(top_builddir)/lib/libvgz/libvgz.la \
${LIBM} ${PTHREAD_LIBS}
varnishtest_CFLAGS = \
......
......@@ -5,6 +5,7 @@ flexelint \
-I/usr/include \
-I. \
-I../../include \
-I../../lib/libvgz \
-I../.. \
../flint.lnt \
flint.lnt \
......
# $Id$
test "test vtc gzip support"
server s1 {
rxreq
txresp -gzipbody FOO
} -start
client c1 -connect ${s1_sock} {
txreq
rxresp
expect resp.bodylen == "26"
gunzip
expect resp.bodylen == "3"
expect resp.http.content-encoding == "gzip"
} -run
......@@ -355,7 +355,7 @@ cmd_shell(CMD_ARGS)
return;
AN(av[1]);
AZ(av[2]);
vtc_dump(vl, 4, "shell", av[1]);
vtc_dump(vl, 4, "shell", av[1], -1);
r = system(av[1]);
assert(WEXITSTATUS(r) == 0);
}
......
......@@ -73,7 +73,7 @@ struct vtclog *vtc_logopen(const char *id);
void vtc_logclose(struct vtclog *vl);
void vtc_log(struct vtclog *vl, unsigned lvl, const char *fmt, ...);
void vtc_dump(struct vtclog *vl, unsigned lvl, const char *pfx,
const char *str);
const char *str, int len);
int exec_file(const char *fn, const char *script, const char *tmpdir,
char *logbuf, unsigned loglen);
......
......@@ -49,6 +49,8 @@ SVNID("$Id$")
#include "vtc.h"
#include "zlib.h"
#define MAX_HDR 50
struct http {
......@@ -65,6 +67,7 @@ struct http {
char *rxbuf;
int prxbuf;
char *body;
unsigned bodyl;
char bodylen[20];
char *req[MAX_HDR];
......@@ -135,7 +138,7 @@ http_write(const struct http *hp, int lvl, const char *pfx)
vsb_finish(hp->vsb);
AZ(vsb_overflowed(hp->vsb));
vtc_dump(hp->vl, lvl, pfx, vsb_data(hp->vsb));
vtc_dump(hp->vl, lvl, pfx, vsb_data(hp->vsb), vsb_len(hp->vsb));
l = write(hp->fd, vsb_data(hp->vsb), vsb_len(hp->vsb));
if (l != vsb_len(hp->vsb))
vtc_log(hp->vl, 0, "Write failed: %s", strerror(errno));
......@@ -315,7 +318,7 @@ http_splitheader(struct http *hp, int req)
for (n = 0; n < 3 || hh[n] != NULL; n++) {
sprintf(buf, "http[%2d] ", n);
vtc_dump(hp->vl, 4, buf, hh[n]);
vtc_dump(hp->vl, 4, buf, hh[n], -1);
}
}
......@@ -381,7 +384,8 @@ http_swallow_body(struct http *hp, char * const *hh, int body)
l = strtoul(p, NULL, 0);
hp->body = hp->rxbuf + hp->prxbuf;
http_rxchar(hp, l);
vtc_dump(hp->vl, 4, "body", hp->body);
vtc_dump(hp->vl, 4, "body", hp->body, l);
hp->bodyl = l;
sprintf(hp->bodylen, "%d", l);
return;
}
......@@ -393,7 +397,7 @@ http_swallow_body(struct http *hp, char * const *hh, int body)
do
http_rxchar(hp, 1);
while (hp->rxbuf[hp->prxbuf - 1] != '\n');
vtc_dump(hp->vl, 4, "len", hp->rxbuf + l);
vtc_dump(hp->vl, 4, "len", hp->rxbuf + l, -1);
i = strtoul(hp->rxbuf + l, &q, 16);
assert(q != hp->rxbuf + l);
assert(*q == '\0' || vct_islws(*q));
......@@ -401,7 +405,7 @@ http_swallow_body(struct http *hp, char * const *hh, int body)
if (i > 0) {
ll += i;
http_rxchar(hp, i);
vtc_dump(hp->vl, 4, "chunk", hp->rxbuf + l);
vtc_dump(hp->vl, 4, "chunk", hp->rxbuf + l, -1);
}
l = hp->prxbuf;
http_rxchar(hp, 2);
......@@ -412,7 +416,8 @@ http_swallow_body(struct http *hp, char * const *hh, int body)
if (i == 0)
break;
}
vtc_dump(hp->vl, 4, "body", hp->body);
vtc_dump(hp->vl, 4, "body", hp->body, ll);
hp->bodyl = ll;
sprintf(hp->bodylen, "%d", ll);
return;
}
......@@ -422,8 +427,9 @@ http_swallow_body(struct http *hp, char * const *hh, int body)
i = http_rxchar_eof(hp, 1);
ll += i;
} while (i > 0);
vtc_dump(hp->vl, 4, "rxeof", hp->body);
vtc_dump(hp->vl, 4, "rxeof", hp->body, ll);
}
hp->bodyl = ll;
sprintf(hp->bodylen, "%d", ll);
}
......@@ -454,7 +460,7 @@ http_rxhdr(struct http *hp)
if (i == 2)
break;
}
vtc_dump(hp->vl, 4, "rxhdr", hp->rxbuf);
vtc_dump(hp->vl, 4, "rxhdr", hp->rxbuf, -1);
}
......@@ -492,6 +498,82 @@ cmd_http_rxresp(CMD_ARGS)
vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen);
}
/**********************************************************************
* Ungzip rx'ed body
*/
#define TRUST_ME(ptr) ((void*)(uintptr_t)(ptr))
#define OVERHEAD 31
static void
cmd_http_gunzip_body(CMD_ARGS)
{
int i;
z_stream vz;
struct http *hp;
char *p;
unsigned l;
(void)cmd;
(void)vl;
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
ONLY_CLIENT(hp, av);
memset(&vz, 0, sizeof vz);
vz.next_in = TRUST_ME(hp->body);
vz.avail_in = hp->bodyl;
l = hp->bodyl;
p = calloc(l, 1);
AN(p);
vz.next_out = TRUST_ME(p);
vz.avail_out = l;
assert(Z_OK == inflateInit2(&vz, 31));
i = inflate(&vz, Z_FINISH);
if (i != Z_STREAM_END)
vtc_log(hp->vl, 0, "Gunzip error = %d", i);
hp->bodyl = vz.total_out;
memcpy(hp->body, p, hp->bodyl);
free(p);
vtc_dump(hp->vl, 4, "body", hp->body, hp->bodyl);
bprintf(hp->bodylen, "%u", hp->bodyl);
assert(Z_OK == inflateEnd(&vz));
}
/**********************************************************************
* Create a gzip'ed body
*/
static void
gzip_body(const char *txt, char **body, int *bodylen)
{
int l;
z_stream vz;
memset(&vz, 0, sizeof vz);
l = strlen(txt);
*body = calloc(l + OVERHEAD, 1);
AN(*body);
vz.next_in = TRUST_ME(txt);
vz.avail_in = l;
vz.next_out = TRUST_ME(*body);
vz.avail_out = l + OVERHEAD;
assert(Z_OK == deflateInit2(&vz,
Z_NO_COMPRESSION, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY));
assert(Z_STREAM_END == deflate(&vz, Z_FINISH));
*bodylen = vz.total_out;
assert(Z_OK == deflateEnd(&vz));
}
/**********************************************************************
* Transmit a response
*/
......@@ -571,6 +653,11 @@ cmd_http_txresp(CMD_ARGS)
body = synth_body(av[1]);
bodylen = strlen(body);
av++;
} else if (!strcmp(*av, "-gzipbody")) {
assert(body == nullbody);
gzip_body(av[1], &body, &bodylen);
vsb_printf(hp->vsb, "Content-Encoding: gzip%s", nl);
av++;
} else
break;
}
......@@ -726,7 +813,7 @@ cmd_http_send(CMD_ARGS)
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
AN(av[1]);
AZ(av[2]);
vtc_dump(hp->vl, 4, "send", av[1]);
vtc_dump(hp->vl, 4, "send", av[1], -1);
i = write(hp->fd, av[1], strlen(av[1]));
assert(i == strlen(av[1]));
......@@ -900,6 +987,7 @@ static const struct cmds http_cmds[] = {
{ "txresp", cmd_http_txresp },
{ "rxresp", cmd_http_rxresp },
{ "gunzip", cmd_http_gunzip_body },
{ "expect", cmd_http_expect },
{ "send", cmd_http_send },
{ "chunked", cmd_http_chunked },
......
......@@ -155,7 +155,7 @@ vtc_log(struct vtclog *vl, unsigned lvl, const char *fmt, ...)
//lint -e{818}
void
vtc_dump(struct vtclog *vl, unsigned lvl, const char *pfx, const char *str)
vtc_dump(struct vtclog *vl, unsigned lvl, const char *pfx, const char *str, int len)
{
int nl = 1;
unsigned l;
......@@ -170,9 +170,10 @@ vtc_dump(struct vtclog *vl, unsigned lvl, const char *pfx, const char *str)
vsb_printf(vl->vsb, "%s %-4s %s(null)\n",
lead[lvl], vl->id, pfx);
else {
l = 0;
for(; *str != '\0'; str++) {
if (++l > 512) {
if (len == -1)
len = strlen(str);
for (l = 0; l < len; l++, str++) {
if (l > 512) {
vsb_printf(vl->vsb, "...");
break;
}
......@@ -189,7 +190,7 @@ vtc_dump(struct vtclog *vl, unsigned lvl, const char *pfx, const char *str)
vsb_printf(vl->vsb, "\\n\n");
nl = 1;
} else if (*str < 0x20 || *str > 0x7e)
vsb_printf(vl->vsb, "\\x%02x", *str);
vsb_printf(vl->vsb, "\\x%02x", (*str) & 0xff);
else
vsb_printf(vl->vsb, "%c", *str);
}
......
......@@ -96,7 +96,7 @@ varnish_ask_cli(const struct varnish *v, const char *cmd, char **repl)
char *r;
if (cmd != NULL) {
vtc_dump(v->vl, 4, "CLI TX", cmd);
vtc_dump(v->vl, 4, "CLI TX", cmd, -1);
i = write(v->cli_fd, cmd, strlen(cmd));
assert(i == strlen(cmd));
i = write(v->cli_fd, "\n", 1);
......@@ -110,7 +110,7 @@ varnish_ask_cli(const struct varnish *v, const char *cmd, char **repl)
}
assert(i == 0);
vtc_log(v->vl, 3, "CLI RX %u", retval);
vtc_dump(v->vl, 4, "CLI RX", r);
vtc_dump(v->vl, 4, "CLI RX", r, -1);
if (repl != NULL)
*repl = r;
else
......@@ -221,7 +221,7 @@ varnish_thread(void *priv)
if (i <= 0)
break;
buf[i] = '\0';
vtc_dump(v->vl1, 3, "debug", buf);
vtc_dump(v->vl1, 3, "debug", buf, -1);
}
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