added HttpHeaders::in(...) method for checking lists and fixed a bug when searching header names

feature/UrlUtils
Kenneth Barbour 2018-10-18 11:55:08 -04:00 committed by Kenneth Barbour
parent 9c1e30ac37
commit a00fcae67e
3 changed files with 68 additions and 8 deletions

View File

@ -74,13 +74,44 @@ int HttpHeaders::indexof( const char * name)
bool HttpHeaders::names_match(const char * a, const char * b)
{
char ac,bc;
for (int i = 0; ac=a[i]; i++) {
for (int i = 0; ; i++) {
ac = a[i];
bc = b[i];
if (!ac) break;
if (ac == bc) continue;
if (ac > 96 && ac < 123) ac -= 32;
if (bc > 96 && bc < 123) bc -= 32;
if (ac != bc) return false;
}
return true;
return (ac == bc);
}
bool HttpHeaders::in(const char * name, const char * search)
{
char* val = get(name);
if (val == nullptr) return false;
int i = 0;
int j = 0;
while (true) {
if (val[i] == '\0')
return (search[j] == '\0');
// stop searching
if (search[j] == '\0') {
if (val[i] == '\0' || val[i] == ',' || val[i] == ';')
return true;
else j = 0;
}
if (val[i] == search[j]) {
j++;
} else {
j = 0;
}
i++;
}
return false;
}

View File

@ -29,6 +29,8 @@ class HttpHeaders: public Printable {
void append(const char *, const char *);
char* get(const char*); //TODO make const
bool has(const char*);
bool in(const char*, const char*);
unsigned int count();
unsigned int length();
virtual size_t printTo(Print&) const;
@ -39,4 +41,3 @@ class HttpHeaders: public Printable {
int indexof( const char *);
bool names_match(const char *, const char *);
};

View File

@ -6,8 +6,8 @@
using Catch::Matchers::Equals;
TEST_CASE("Empty HttpHeaders","[HttpHeaders]") {
HttpHeaders headers;
HttpHeaders headers;
CHECK(headers.count() == 0);
}
@ -23,6 +23,13 @@ TEST_CASE("Get and set headers","[HttpHeaders]"){
CHECK(headers.count() == 1);
}
TEST_CASE("Name contains another name", "[HttpHeaders]")
{
HttpHeaders h;
h.set("Accept-Encoding","foo, bar");
CHECK(h.has("Accept") == false);
}
TEST_CASE("Set headers again","[HttpHeaders]"){
HttpHeaders headers;
headers.set("foo","sushi");
@ -46,7 +53,7 @@ TEST_CASE("Append empty header behaves like set","[HttpHeaders]")
{
HttpHeaders headers;
headers.append("foo","bar");
CHECK_THAT(headers.get("foo"), Equals("bar"));
CHECK(headers.count() == 1);
}
@ -55,13 +62,13 @@ TEST_CASE("Fill headers","[HttpHeaders]") {
HttpHeaders headers;
char name[HTTPHEADERS_NAME_SZ] = "";
strcpy(name, "header");
for (int i = 0; i < HTTPHEADERS_NUM; i++) {
name[6] = 0x41 + i;
headers.set(name,"Foo");
REQUIRE_THAT(headers.get(name), Equals("Foo"));
CHECK(headers.count() == (i+1) );
}
}
}
TEST_CASE("Header name is case insensitive","[HttpHeaders]") {
@ -106,3 +113,24 @@ TEST_CASE("HttpHeaders printTo", "[HttpHeaders]")
buffer.print(h);
CHECK_THAT((char *) buff, Equals("Foo: Bar\r\nBar: baz\r\n"));
}
TEST_CASE("HttpHeaders find in list", "[HttpHeaders][HttpHeaders::in]")
{
HttpHeaders h;
h.set("Accept-Encoding","gzip, deflate");
CHECK(h.in("Accept-Encoding","gzip") == true);
CHECK(h.in("Accept-Encoding","deflate") == true);
CHECK(h.in("Accept-Encoding","identity") == false);
CHECK(h.in("Accept","gzip") == false); // Accept is not in the headers
}
TEST_CASE("Find matches when a q-factor is provided", "[HttpHeaders]")
{
HttpHeaders h;
h.set("Accept","text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8");
CHECK(h.in("Accept","text/html"));
CHECK(h.in("Accept","application/xhtml+xml"));
CHECK(h.in("Accept","application/xml"));
CHECK(h.in("Accept","*/*"));
}