Commit 1f694f5f authored by Joshua Bussdieker's avatar Joshua Bussdieker

Word matching working and a few tests

parent 3b589fda
......@@ -260,6 +260,24 @@ KEY_Validate(const uint8_t *key)
}
}
int word_match(char *param, char *string) {
char *p = strstr(string, param);
if (p == 0)
return 0;
if (p != string)
if (p[-1] != ' ' && p[-1] != ',')
return 0;
char *r = p + strlen(param);
if (r < string + strlen(string))
if (r[0] != ' ' && r[0] != ',')
return 0;
return 1;
}
int
KEY_Match(struct req *req, const uint8_t *key)
{
......@@ -329,12 +347,40 @@ KEY_Match(struct req *req, const uint8_t *key)
// Matcher match
} else if (key[2] == 1) {
DEBUG && printf(" - Matcher: %s\n", key+4);
DEBUG && printf(" Header (Matcher): %s\n", key + 4);
char *e;
unsigned l = vbe16dec(key);
i = http_GetHdr(req->http, (const char*)(key+3), &h);
// TODO: Perhaps not matcher should allow this
if (i == 0)
return 0;
const char *matcher = key + 4 + key[3] + 1;
while (*matcher != 0 && *matcher != -1) {
if (*matcher == M_WORD) {
DEBUG && printf(" - Word: %s ", matcher + 1);
if (word_match(matcher + 1, h) != 1) {
printf("NG\n");
return 0;
}
printf("OK\n");
matcher += strlen(matcher) + 1;
} else if (*matcher == M_CASE) {
DEBUG && printf(" - Case\n");
matcher++;
} else {
DEBUG && printf("UNKNOWN MATCHER (%d)\n", *matcher);
return 0;
}
}
}
key += key_len(key);
}
DEBUG && printf("KEY_Match(req: %p, key: %p) = 0\n", req, key);
DEBUG && printf("KEY_Match(req: %p, key: %p) = 1\n", req, key);
return 1;
}
varnishtest "Test Key matcher functionality (WORD)"
server s1 {
rxreq
expect req.http.foobar == "Foo"
txresp -hdr "Key: Foobar;w=\"Foo\"" -hdr "Snafu: 1" -body "1111\n"
rxreq
expect req.http.foobar == "Bar"
txresp -hdr "Key: Foobar;w=\"Bar\"" -hdr "Snafu: 2" -body "2222\n"
rxreq
expect req.http.foobar == "Foo"
txresp -hdr "Key: Foobar;w=\"Foo\"" -hdr "Snafu: 3" -body "3333\n"
} -start
varnish v1 -vcl+backend {} -start
client c1 {
txreq -hdr "Foobar: Foo"
rxresp
expect resp.status == 200
expect resp.http.X-Varnish == "1001"
expect resp.http.snafu == "1"
txreq -hdr "Foobar: Bar"
rxresp
expect resp.status == 200
expect resp.http.X-Varnish == "1003"
expect resp.http.snafu == "2"
txreq -hdr "Foobar: Foo"
rxresp
expect resp.status == 200
expect resp.http.X-Varnish == "1005 1002"
expect resp.http.snafu == "1"
} -run
varnishtest "Test Key matcher functionality (WORD)"
server s1 {
rxreq
expect req.http.foobar == "A B C"
txresp -hdr "Key: Foobar;w=\"A\"" -hdr "Snafu: 1" -body "1111\n"
rxreq
expect req.http.foobar == "CAB"
txresp -hdr "Key: Foobar;w=\"CAB\"" -hdr "Snafu: 2" -body "2222\n"
} -start
varnish v1 -vcl+backend {} -start
client c1 {
txreq -hdr "Foobar: A B C"
rxresp
expect resp.status == 200
expect resp.http.X-Varnish == "1001"
expect resp.http.snafu == "1"
txreq -hdr "Foobar: B C A"
rxresp
expect resp.status == 200
expect resp.http.X-Varnish == "1003 1002"
expect resp.http.snafu == "1"
txreq -hdr "Foobar: C A B"
rxresp
expect resp.status == 200
expect resp.http.X-Varnish == "1004 1002"
expect resp.http.snafu == "1"
txreq -hdr "Foobar: CAB"
rxresp
expect resp.status == 200
expect resp.http.X-Varnish == "1005"
expect resp.http.snafu == "2"
txreq -hdr "Foobar: A CAB RIDE"
rxresp
expect resp.status == 200
expect resp.http.X-Varnish == "1007 1006"
expect resp.http.snafu == "2"
} -run
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