src: Refactor parse_next_link_header_once

This commit is contained in:
Tatsuhiro Tsujikawa 2015-02-08 17:43:34 +09:00
parent 9e723b6b1d
commit 4d47c31ebe
2 changed files with 30 additions and 18 deletions

View File

@ -759,26 +759,32 @@ parse_next_link_header_once(const char *first, const char *last) {
// we are only interested in rel=preload parameter. Others are // we are only interested in rel=preload parameter. Others are
// simply skipped. // simply skipped.
static const char PL[] = "rel=preload"; static const char PL[] = "rel=preload";
if (last - first >= sizeof(PL) - 1) { static const size_t PLLEN = sizeof(PL) - 1;
if (memcmp(PL, first, sizeof(PL) - 1) == 0) { if (first + PLLEN == last) {
if (first + sizeof(PL) - 1 == last) { if (std::equal(PL, PL + PLLEN, first)) {
ok = true; ok = true;
// this is the end of sequence // this is the end of sequence
return {{{url_first, url_last}}, last}; return {{{url_first, url_last}}, last};
}
} else if (first + PLLEN + 1 <= last) {
switch (*(first + PLLEN)) {
case ',':
if (!std::equal(PL, PL + PLLEN, first)) {
break;
} }
switch (*(first + sizeof(PL) - 1)) { ok = true;
case ',': // skip including ','
ok = true; first += PLLEN + 1;
// skip including ',' return {{{url_first, url_last}}, first};
first += sizeof(PL); case ';':
return {{{url_first, url_last}}, first}; if (!std::equal(PL, PL + PLLEN, first)) {
case ';': break;
ok = true;
// skip including ';'
first += sizeof(PL);
// continue parse next link-param
continue;
} }
ok = true;
// skip including ';'
first += PLLEN + 1;
// continue parse next link-param
continue;
} }
} }
auto param_first = first; auto param_first = first;

View File

@ -479,6 +479,12 @@ void test_http2_parse_link_header(void) {
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[3], &s[6]) == res[0].uri); CU_ASSERT(std::make_pair(&s[3], &s[6]) == res[0].uri);
} }
{
// preload is a prefix of bogus rel parameter value
const char s[] = "<url>; rel=preloadx";
auto res = http2::parse_link_header(s, sizeof(s) - 1);
CU_ASSERT(0 == res.size());
}
} }
void test_http2_path_join(void) { void test_http2_path_join(void) {