Commit c8235335 authored by Geoff Simmons's avatar Geoff Simmons

refactor format.c/get_hdr() to use a string rather than a regex

(temporarily #if'fing out everything that uses it)
parent fe7ffa86
......@@ -32,6 +32,7 @@
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include "vas.h"
#include "miniobj.h"
......@@ -55,11 +56,11 @@ typedef struct compiled_fmt_t {
char tags[MAX_VSL_TAG];
} compiled_fmt_t;
#if 0
/* XXX: When FMT_Init is implemented, malloc to config.max_reclen */
static char scratch[DEFAULT_MAX_RECLEN];
#if 0
static compiled_fmt_t cformat, bformat, zformat;
static char i_arg[BUFSIZ] = "";
......@@ -121,21 +122,33 @@ get_tag(tx_t *tx, enum VSL_tag_e tag)
* in tx that matches the tag and the regex.
*/
char *
get_hdr(tx_t *tx, enum VSL_tag_e tag, vre_t *hdr_re)
get_hdr(tx_t *tx, enum VSL_tag_e tag, const char *hdr)
{
logline_t *rec;
#define OV_SIZE (2 * 3)
int ov[OV_SIZE];
char *hdr_payload = NULL;
CHECK_OBJ_NOTNULL(tx, TX_MAGIC);
VSTAILQ_FOREACH(rec, &tx->lines, linelist) {
int s;
char *c;
CHECK_OBJ_NOTNULL(rec, LOGLINE_MAGIC);
if (rec->tag != tag)
continue;
get_payload(rec);
c = VSB_data(payload);
while (isspace(*c))
c++;
if (strncasecmp(c, hdr, strlen(hdr)) != 0)
continue;
c += strlen(hdr);
while (isspace(*c))
c++;
if (*c++ != ':')
continue;
while (isspace(*c))
c++;
hdr_payload = c;
#if 0
s = VRE_exec(hdr_re, VSB_data(payload), rec->len, 0, 0, ov, OV_SIZE,
NULL);
assert(s >= VRE_ERROR_NOMATCH && s != 0);
......@@ -144,6 +157,7 @@ get_hdr(tx_t *tx, enum VSL_tag_e tag, vre_t *hdr_re)
assert(ov[2] >= 0 && ov[3] >= ov[2]);
hdr_payload = VSB_data(payload) + ov[2];
hdr_payload[ov[3]] = '\0';
#endif
}
return hdr_payload;
......@@ -175,7 +189,7 @@ get_rec_fld(logline_t *rec, int n)
get_payload(rec);
return get_fld(VSB_data(payload), n);
}
#if 0
double
get_tm(tx_t *tx)
{
......@@ -504,7 +518,7 @@ format_u_##dir(tx_t *tx, char *name, enum VSL_tag_e tag, \
FORMAT_u(client, ReqHeader)
FORMAT_u(backend, BereqHeader)
#endif
#if 0
#define FORMAT_Xio(dir, io, hx) \
......
......@@ -34,6 +34,7 @@
/* XXX: init as fixed size with length max_reclen + 1 */
struct vsb *payload;
#if 0
/* XXX: init time_start_re as VRE_compile(TS_START_REGEX) */
#define TS_START_REGEX "^\\s*Start\\s*:\\s*(.+)$"
vre_t *time_start_re;
......@@ -49,15 +50,17 @@ vre_t *host_re;
#define AUTH_REGEX "^\\s*Authorization\\s*:\\s*(.+)$"
vre_t *auth_re;
#endif
typedef void formatter_f(tx_t *tx, char *name, enum VSL_tag_e tag,
char **s, size_t *len);
void get_payload(logline_t *rec);
logline_t *get_tag(tx_t *tx, enum VSL_tag_e tag);
char *get_hdr(tx_t *tx, enum VSL_tag_e tag, vre_t *hdr_re);
char *get_hdr(tx_t *tx, enum VSL_tag_e tag, const char *hdr);
char *get_fld(const char *str, int n);
char *get_rec_fld(logline_t *rec, int n);
#if 0
double get_tm(tx_t *tx);
formatter_f format_b_client;
......@@ -100,3 +103,4 @@ formatter_f format_U_backend;
formatter_f format_u_client;
formatter_f format_u_backend;
#endif
......@@ -43,6 +43,7 @@
int tests_run = 0;
#if 0
static void
add_rec_chunk(tx_t *tx, logline_t *rec, chunk_t *chunk)
{
......@@ -71,14 +72,12 @@ set_record_data(logline_t *rec, chunk_t *chunk, const char *data,
if (tag != SLT__Bogus)
rec->tag = tag;
}
#endif
/* N.B.: Always run the tests in this order */
static const char
*test_format_init(void)
{
const char *error;
int erroroffset;
printf("... initializing format tests\n");
CONF_Init();
......@@ -86,6 +85,7 @@ static const char
payload = VSB_new(NULL, NULL, DEFAULT_MAX_RECLEN + 1, VSB_FIXEDLEN);
MAN(payload);
#if 0
time_start_re = VRE_compile(TS_START_REGEX, VRE_CASELESS, &error,
&erroroffset);
VMASSERT(time_start_re != NULL,
......@@ -111,6 +111,7 @@ static const char
auth_re = VRE_compile(AUTH_REGEX, VRE_CASELESS, &error, &erroroffset);
VMASSERT(auth_re != NULL, "Error compiling " AUTH_REGEX ": %s (offset %d)",
error, erroroffset);
#endif
return NULL;
}
......@@ -206,21 +207,12 @@ static const char
*test_format_get_hdr(void)
{
tx_t tx;
#define HDR_REGEX "^\\s*Foo\\s*:\\s*(.+)$"
logline_t recs[NRECORDS];
chunk_t c[NRECORDS];
vre_t *hdr_re;
const char *error;
char *hdr;
int erroroffset;
printf("... testing get_hdr()\n");
hdr_re = VRE_compile(HDR_REGEX, VRE_CASELESS, &error, &erroroffset);
VMASSERT(hdr_re != NULL,
"Error compiling \"" HDR_REGEX "\": %s (offset %d)",
error, erroroffset);
tx.magic = TX_MAGIC;
VSTAILQ_INIT(&tx.lines);
for (int i = 0; i < NRECORDS; i++) {
......@@ -238,19 +230,31 @@ static const char
strcpy(c[NRECORDS / 2].data, "Foo: quux");
recs[NRECORDS - 1].len = strlen("Foo: wilco");
strcpy(c[NRECORDS - 1].data, "Foo: wilco");
hdr = get_hdr(&tx, SLT_ReqHeader, hdr_re);
hdr = get_hdr(&tx, SLT_ReqHeader, "Foo");
MAN(hdr);
MASSERT(strcmp(hdr, "wilco") == 0);
/* Case-insensitive match */
hdr = get_hdr(&tx, SLT_ReqHeader, "fOO");
MAN(hdr);
MASSERT(strcmp(hdr, "wilco") == 0);
/* Ignore whitespace */
recs[NRECORDS - 1].len = strlen(" Foo : wilco");
strcpy(c[NRECORDS - 1].data, " Foo : wilco");
hdr = get_hdr(&tx, SLT_ReqHeader, "Foo");
MAN(hdr);
MASSERT(strcmp(hdr, "wilco") == 0);
/* Record not found */
recs[NRECORDS / 2].tag = SLT_RespHeader;
recs[NRECORDS - 1].tag = SLT_RespHeader;
hdr = get_hdr(&tx, SLT_ReqHeader, hdr_re);
hdr = get_hdr(&tx, SLT_ReqHeader, "Foo");
MAZ(hdr);
/* Empty line list */
VSTAILQ_INIT(&tx.lines);
hdr = get_hdr(&tx, SLT_ReqHeader, hdr_re);
hdr = get_hdr(&tx, SLT_ReqHeader, "Foo");
MAZ(hdr);
return NULL;
......@@ -344,6 +348,7 @@ static const char
return NULL;
}
#if 0
static const char
*test_format_get_tm(void)
{
......@@ -961,6 +966,7 @@ static const char
return NULL;
}
#endif
static const char
*all_tests(void)
......@@ -971,6 +977,7 @@ static const char
mu_run_test(test_format_get_hdr);
mu_run_test(test_format_get_fld);
mu_run_test(test_format_get_rec_fld);
#if 0
mu_run_test(test_format_get_tm);
mu_run_test(test_format_b);
mu_run_test(test_format_D);
......@@ -986,6 +993,7 @@ static const char
mu_run_test(test_format_T);
mu_run_test(test_format_U);
mu_run_test(test_format_u);
#endif
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