Commit fe7ffa86 authored by Geoff Simmons's avatar Geoff Simmons

implement and test formatters for %u

parent 8eff7732
......@@ -475,23 +475,20 @@ format_U_##dir(tx_t *tx, char *name, enum VSL_tag_e tag, \
FORMAT_U(client, ReqURL)
FORMAT_U(backend, BereqURL)
#if 0
#define FORMAT_u(dir, hx) \
static void \
format_u_##dir(logline_t *ll, char *name, enum VSL_tag_e tag, \
char **s, size_t *len) \
void \
format_u_##dir(tx_t *tx, char *name, enum VSL_tag_e tag, \
char **s, size_t *len) \
{ \
(void) name; \
(void) tag; \
record_t *rec; \
char *hdr; \
\
if ((rec = GET_HDR(ll, hx, "Authorization")) != NULL \
&& strncasecmp(rec->data + strlen("Authorization: "), "Basic", \
strlen("Basic")) == 0) { \
char *c, *auth = get_fld(rec, 3); \
if ((hdr = get_hdr(tx, SLT_##hx, auth_re)) != NULL \
&& strcasecmp(get_fld(hdr, 0), "Basic") == 0) { \
char *c, *auth = get_fld(hdr, 1); \
VB64_init(); \
VB64_decode(scratch, config.max_reclen, auth); \
VB64_decode(scratch, config.max_reclen, auth, auth + strlen(auth)); \
c = strchr(scratch, ':'); \
if (c != NULL) \
*c = '\0'; \
......@@ -505,8 +502,10 @@ format_u_##dir(logline_t *ll, char *name, enum VSL_tag_e tag, \
} \
}
FORMAT_u(client, rx)
FORMAT_u(backend, tx)
FORMAT_u(client, ReqHeader)
FORMAT_u(backend, BereqHeader)
#if 0
#define FORMAT_Xio(dir, io, hx) \
static void \
......
......@@ -47,6 +47,9 @@ vre_t *time_beresp_body_re;
#define HOST_REGEX "^\\s*Host\\s*:\\s*(.+)$"
vre_t *host_re;
#define AUTH_REGEX "^\\s*Authorization\\s*:\\s*(.+)$"
vre_t *auth_re;
typedef void formatter_f(tx_t *tx, char *name, enum VSL_tag_e tag,
char **s, size_t *len);
......@@ -94,3 +97,6 @@ formatter_f format_T_backend;
formatter_f format_U_client;
formatter_f format_U_backend;
formatter_f format_u_client;
formatter_f format_u_backend;
......@@ -22,6 +22,7 @@ test_format_SOURCES = \
test_format_LDADD = \
../config.$(OBJEXT) \
../strfTIM.$(OBJEXT) \
../base64.$(OBJEXT) \
../format.$(OBJEXT) \
@VARNISH_LIBS@ @VARNISH_LIBVARNISH_LIB@ -lm -lvarnish
......
......@@ -108,6 +108,10 @@ static const char
VMASSERT(host_re != NULL, "Error compiling " HOST_REGEX ": %s (offset %d)",
error, erroroffset);
auth_re = VRE_compile(AUTH_REGEX, VRE_CASELESS, &error, &erroroffset);
VMASSERT(auth_re != NULL, "Error compiling " AUTH_REGEX ": %s (offset %d)",
error, erroroffset);
return NULL;
}
......@@ -893,6 +897,71 @@ static const char
return NULL;
}
static const char
*test_format_u(void)
{
tx_t tx;
logline_t rec;
chunk_t chunk;
char *str;
size_t len;
printf("... testing format_u_*()\n");
init_tx_rec_chunk(&tx, &rec, &chunk);
MAN(chunk.data);
#define BASIC_AUTH_PAYLOAD "Authorization: Basic dmFybmlzaDo0ZXZlcg=="
set_record_data(&rec, &chunk, BASIC_AUTH_PAYLOAD, SLT_ReqHeader);
format_u_client(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "varnish") == 0);
MASSERT(len == 7);
rec.tag = SLT_BereqHeader;
format_u_backend(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "varnish") == 0);
MASSERT(len == 7);
/* No header record */
rec.tag = SLT__Bogus;
format_u_client(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "-") == 0);
MASSERT(len == 1);
format_u_backend(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "-") == 0);
MASSERT(len == 1);
/* No auth header */
rec.tag = SLT_ReqHeader;
rec.len = 0;
format_u_client(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "-") == 0);
MASSERT(len == 1);
rec.tag = SLT_BereqHeader;
format_u_backend(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "-") == 0);
MASSERT(len == 1);
/* No basic auth header
* Not a real example of a digest auth header, but kept short, so
* that we can test with only one chunk.
*/
#define DIGEST_AUTH_PAYLOAD "Authorization: Digest username=\"Mufasa\", realm=\"realm@host.com\""
set_record_data(&rec, &chunk, DIGEST_AUTH_PAYLOAD, SLT_ReqHeader);
format_u_client(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "-") == 0);
MASSERT(len == 1);
rec.tag = SLT_BereqHeader;
format_u_backend(&tx, NULL, SLT__Bogus, &str, &len);
MASSERT(strcmp(str, "-") == 0);
MASSERT(len == 1);
return NULL;
}
static const char
*all_tests(void)
{
......@@ -916,6 +985,7 @@ static const char
mu_run_test(test_format_t);
mu_run_test(test_format_T);
mu_run_test(test_format_U);
mu_run_test(test_format_u);
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