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

View File

@ -479,6 +479,12 @@ void test_http2_parse_link_header(void) {
CU_ASSERT(1 == res.size());
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) {