Commit ec0c8a2c authored by Geoff Simmons's avatar Geoff Simmons

implement and test %r formatters

parent 0a92f869
......@@ -356,52 +356,57 @@ format_q_##dir(tx_t *tx, char *name, enum VSL_tag_e tag, \
FORMAT_q(client, ReqURL)
FORMAT_q(backend, BereqURL)
#if 0
#define FORMAT_r(dir, dx, hx) \
static void \
format_r_##dir(logline_t *ll, char *name, enum VSL_tag_e tag, \
char **s, size_t *len) \
{ \
(void) name; \
(void) tag; \
\
record_t *rec; \
\
rec = &TAG(ll, SLT_##dx##Request); \
if (rec->len) \
snprintf(scratch, rec->len+1, "%s", rec->data); \
else \
strcpy(scratch, "-"); \
strcat(scratch, " "); \
\
if ((rec = GET_HDR(ll, hx, "Host")) != NULL) { \
if (strncmp(rec->data, "http://", 7) != 0) \
strcat(scratch, "http://"); \
strncat(scratch, rec->data+6, rec->len-6); \
} \
else \
strcat(scratch, "http://localhost"); \
\
rec = &TAG(ll, SLT_##dx##URL); \
if (rec->len) \
strncat(scratch, rec->data, rec->len); \
else \
strcat(scratch, "-"); \
\
strcat(scratch, " "); \
rec = &TAG(ll, SLT_##dx##Protocol); \
if (rec->len) \
strncat(scratch, rec->data, rec->len); \
else \
strcat(scratch, "HTTP/1.0"); \
\
*s = scratch; \
*len = strlen(scratch); \
#define FORMAT_r(dir, dx) \
void \
format_r_##dir(tx_t *tx, char *name, enum VSL_tag_e tag, \
char **s, size_t *len) \
{ \
char *str; \
(void) name; \
(void) tag; \
\
logline_t *rec = get_tag(tx, SLT_##dx##Method); \
if (rec != NULL) { \
get_payload(rec); \
sprintf(scratch, VSB_data(payload)); \
} \
else \
strcpy(scratch, "-"); \
strcat(scratch, " "); \
\
if ((str = get_hdr(tx, SLT_##dx##Header, host_re)) != NULL) { \
if (strncmp(str, "http://", 7) != 0) \
strcat(scratch, "http://"); \
strcat(scratch, str); \
} \
else \
strcat(scratch, "http://localhost"); \
\
rec = get_tag(tx, SLT_##dx##URL); \
if (rec->len) { \
get_payload(rec); \
strcat(scratch, VSB_data(payload)); \
} \
else \
strcat(scratch, "-"); \
\
strcat(scratch, " "); \
rec = get_tag(tx, SLT_##dx##Protocol); \
if (rec->len) { \
get_payload(rec); \
strcat(scratch, VSB_data(payload)); \
} \
else \
strcat(scratch, "HTTP/1.0"); \
\
*s = scratch; \
*len = strlen(scratch); \
}
FORMAT_r(client, Rx, rx)
FORMAT_r(backend, Tx, tx)
FORMAT_r(client, Req)
FORMAT_r(backend, Bereq)
#if 0
FORMAT(client, s, TxStatus)
FORMAT(backend, s, RxStatus)
......
......@@ -44,6 +44,9 @@ vre_t *time_resp_re;
#define TS_BERESP_BODY_REGEX "^\\s*BerespBody\\s*:\\s*(.+)$"
vre_t *time_beresp_body_re;
#define HOST_REGEX "^\\s*Host\\s*:\\s*(.+)$"
vre_t *host_re;
typedef void formatter_f(tx_t *tx, char *name, enum VSL_tag_e tag,
char **s, size_t *len);
......@@ -77,3 +80,6 @@ formatter_f format_O_backend;
formatter_f format_q_client;
formatter_f format_q_backend;
formatter_f format_r_client;
formatter_f format_r_backend;
......@@ -44,10 +44,8 @@
int tests_run = 0;
static void
init_tx_rec_chunk(tx_t *tx, logline_t *rec, chunk_t *chunk)
add_rec_chunk(tx_t *tx, logline_t *rec, chunk_t *chunk)
{
tx->magic = TX_MAGIC;
VSTAILQ_INIT(&tx->lines);
VSTAILQ_INSERT_TAIL(&tx->lines, rec, linelist);
rec->magic = LOGLINE_MAGIC;
VSTAILQ_INIT(&rec->chunks);
......@@ -56,6 +54,14 @@ init_tx_rec_chunk(tx_t *tx, logline_t *rec, chunk_t *chunk)
chunk->data = (char *) calloc(1, config.chunk_size);
}
static void
init_tx_rec_chunk(tx_t *tx, logline_t *rec, chunk_t *chunk)
{
tx->magic = TX_MAGIC;
VSTAILQ_INIT(&tx->lines);
add_rec_chunk(tx, rec, chunk);
}
static void
set_record_data(logline_t *rec, chunk_t *chunk, const char *data,
enum VSL_tag_e tag)
......@@ -98,6 +104,10 @@ static const char
"Error compiling " TS_BERESP_BODY_REGEX ": %s (offset %d)",
error, erroroffset);
host_re = VRE_compile(HOST_REGEX, VRE_CASELESS, &error, &erroroffset);
VMASSERT(host_re != NULL, "Error compiling " HOST_REGEX ": %s (offset %d)",
error, erroroffset);
return NULL;
}
......@@ -628,6 +638,134 @@ static const char
return NULL;
}
static const char
*test_format_r(void)
{
tx_t tx;
logline_t rec_method, rec_host, rec_url, rec_proto;
chunk_t chunk_method, chunk_host, chunk_url, chunk_proto;
char *str;
size_t len;
printf("... testing format_r_*()\n");
init_tx_rec_chunk(&tx, &rec_method, &chunk_method);
MAN(chunk_method.data);
add_rec_chunk(&tx, &rec_host, &chunk_host);
MAN(chunk_host.data);
add_rec_chunk(&tx, &rec_url, &chunk_url);
MAN(chunk_url.data);
add_rec_chunk(&tx, &rec_proto, &chunk_proto);
MAN(chunk_proto.data);
set_record_data(&rec_method, &chunk_method, "GET", SLT_ReqMethod);
set_record_data(&rec_host, &chunk_host, "Host: www.foobar.com",
SLT_ReqHeader);
set_record_data(&rec_url, &chunk_url, URL_PAYLOAD, SLT_ReqURL);
set_record_data(&rec_proto, &chunk_proto, PROTOCOL_PAYLOAD,
SLT_ReqProtocol);
format_r_client(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "GET http://www.foobar.com/foo HTTP/1.1") == 0);
MASSERT(len == 38);
rec_method.tag = SLT_BereqMethod;
rec_host.tag = SLT_BereqHeader;
rec_url.tag = SLT_BereqURL;
rec_proto.tag = SLT_BereqProtocol;
format_r_backend(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "GET http://www.foobar.com/foo HTTP/1.1") == 0);
MASSERT(len == 38);
/* No method record */
rec_method.tag = SLT__Bogus;
rec_host.tag = SLT_ReqHeader;
rec_url.tag = SLT_ReqURL;
rec_proto.tag = SLT_ReqProtocol;
format_r_client(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "- http://www.foobar.com/foo HTTP/1.1") == 0);
MASSERT(len == 36);
rec_host.tag = SLT_BereqHeader;
rec_url.tag = SLT_BereqURL;
rec_proto.tag = SLT_BereqProtocol;
format_r_backend(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "- http://www.foobar.com/foo HTTP/1.1") == 0);
MASSERT(len == 36);
/* No host header */
rec_method.tag = SLT_ReqMethod;
set_record_data(&rec_host, &chunk_host, "Foo: bar", SLT_ReqHeader);
rec_url.tag = SLT_ReqURL;
rec_proto.tag = SLT_ReqProtocol;
format_r_client(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "GET http://localhost/foo HTTP/1.1") == 0);
MASSERT(len == 33);
rec_method.tag = SLT_BereqMethod;
rec_host.tag = SLT_BereqHeader;
rec_url.tag = SLT_BereqURL;
rec_proto.tag = SLT_BereqProtocol;
format_r_backend(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "GET http://localhost/foo HTTP/1.1") == 0);
MASSERT(len == 33);
/* No header record */
rec_method.tag = SLT_ReqMethod;
rec_host.tag = SLT__Bogus;
rec_url.tag = SLT_ReqURL;
rec_proto.tag = SLT_ReqProtocol;
format_r_client(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "GET http://localhost/foo HTTP/1.1") == 0);
MASSERT(len == 33);
rec_method.tag = SLT_BereqMethod;
rec_url.tag = SLT_BereqURL;
rec_proto.tag = SLT_BereqProtocol;
format_r_backend(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "GET http://localhost/foo HTTP/1.1") == 0);
MASSERT(len == 33);
/* URL record empty */
set_record_data(&rec_host, &chunk_host, "Host: www.foobar.com",
SLT_ReqHeader);
rec_method.tag = SLT_ReqMethod;
rec_url.tag = SLT_ReqURL;
rec_url.len = 0;
rec_proto.tag = SLT_ReqProtocol;
format_r_client(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "GET http://www.foobar.com- HTTP/1.1") == 0);
MASSERT(len == 35);
rec_method.tag = SLT_BereqMethod;
rec_host.tag = SLT_BereqHeader;
rec_url.tag = SLT_BereqURL;
rec_proto.tag = SLT_BereqProtocol;
format_r_backend(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "GET http://www.foobar.com- HTTP/1.1") == 0);
MASSERT(len == 35);
/* Proto record empty */
rec_method.tag = SLT_ReqMethod;
rec_host.tag = SLT_ReqHeader;
rec_url.tag = SLT_ReqURL;
rec_url.len = strlen(URL_PAYLOAD);
rec_proto.tag = SLT_ReqProtocol;
rec_proto.len = 0;
format_r_client(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "GET http://www.foobar.com/foo HTTP/1.0") == 0);
MASSERT(len == 38);
rec_method.tag = SLT_BereqMethod;
rec_host.tag = SLT_BereqHeader;
rec_url.tag = SLT_BereqURL;
rec_proto.tag = SLT_BereqProtocol;
format_r_backend(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "GET http://www.foobar.com/foo HTTP/1.0") == 0);
MASSERT(len == 38);
return NULL;
}
static const char
*all_tests(void)
{
......@@ -646,6 +784,7 @@ static const char
mu_run_test(test_format_m);
mu_run_test(test_format_O);
mu_run_test(test_format_q);
mu_run_test(test_format_r);
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