src: Use str_size

This commit is contained in:
Tatsuhiro Tsujikawa 2016-03-03 23:23:51 +09:00
parent 9afc017532
commit aaf0177318
2 changed files with 111 additions and 110 deletions

View File

@ -926,10 +926,10 @@ parse_next_link_header_once(const char *first, const char *last) {
if (!ign) { if (!ign) {
// rel can take several relations using quoted form. // rel can take several relations using quoted form.
static constexpr char PLP[] = "rel=\""; static constexpr char PLP[] = "rel=\"";
static constexpr size_t PLPLEN = sizeof(PLP) - 1; static constexpr size_t PLPLEN = str_size(PLP);
static constexpr char PLT[] = "preload"; static constexpr char PLT[] = "preload";
static constexpr size_t PLTLEN = sizeof(PLT) - 1; static constexpr size_t PLTLEN = str_size(PLT);
if (first + PLPLEN < last && *(first + PLPLEN - 1) == '"' && if (first + PLPLEN < last && *(first + PLPLEN - 1) == '"' &&
std::equal(PLP, PLP + PLPLEN, first, util::CaseCmp())) { std::equal(PLP, PLP + PLPLEN, first, util::CaseCmp())) {
// we have to search preload in whitespace separated list: // we have to search preload in whitespace separated list:
@ -975,7 +975,7 @@ 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 constexpr char PL[] = "rel=preload"; static constexpr char PL[] = "rel=preload";
static constexpr size_t PLLEN = sizeof(PL) - 1; static constexpr size_t PLLEN = str_size(PL);
if (first + PLLEN == last) { if (first + PLLEN == last) {
if (std::equal(PL, PL + PLLEN, first, util::CaseCmp())) { if (std::equal(PL, PL + PLLEN, first, util::CaseCmp())) {
// ok = true; // ok = true;
@ -1005,7 +1005,7 @@ parse_next_link_header_once(const char *first, const char *last) {
} }
// we have to reject URI if we have nonempty anchor parameter. // we have to reject URI if we have nonempty anchor parameter.
static constexpr char ANCHOR[] = "anchor="; static constexpr char ANCHOR[] = "anchor=";
static constexpr size_t ANCHORLEN = sizeof(ANCHOR) - 1; static constexpr size_t ANCHORLEN = str_size(ANCHOR);
if (!ign && !check_link_param_empty(first, last, ANCHOR, ANCHORLEN)) { if (!ign && !check_link_param_empty(first, last, ANCHOR, ANCHORLEN)) {
ign = true; ign = true;
} }
@ -1013,7 +1013,7 @@ parse_next_link_header_once(const char *first, const char *last) {
// reject URI if we have non-empty loadpolicy. This could be // reject URI if we have non-empty loadpolicy. This could be
// tightened up to just pick up "next" or "insert". // tightened up to just pick up "next" or "insert".
static constexpr char LOADPOLICY[] = "loadpolicy="; static constexpr char LOADPOLICY[] = "loadpolicy=";
static constexpr size_t LOADPOLICYLEN = sizeof(LOADPOLICY) - 1; static constexpr size_t LOADPOLICYLEN = str_size(LOADPOLICY);
if (!ign && if (!ign &&
!check_link_param_empty(first, last, LOADPOLICY, LOADPOLICYLEN)) { !check_link_param_empty(first, last, LOADPOLICY, LOADPOLICYLEN)) {
ign = true; ign = true;

View File

@ -274,353 +274,354 @@ void test_http2_lookup_token(void) {
void test_http2_parse_link_header(void) { void test_http2_parse_link_header(void) {
{ {
// only URI appears; we don't extract URI unless it bears rel=preload // only URI appears; we don't extract URI unless it bears rel=preload
const char s[] = "<url>"; constexpr char s[] = "<url>";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// URI url should be extracted // URI url should be extracted
const char s[] = "<url>; rel=preload"; constexpr char s[] = "<url>; rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// With extra link-param. URI url should be extracted // With extra link-param. URI url should be extracted
const char s[] = "<url>; rel=preload; as=file"; constexpr char s[] = "<url>; rel=preload; as=file";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// With extra link-param. URI url should be extracted // With extra link-param. URI url should be extracted
const char s[] = "<url>; as=file; rel=preload"; constexpr char s[] = "<url>; as=file; rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// With extra link-param and quote-string. URI url should be // With extra link-param and quote-string. URI url should be
// extracted // extracted
const char s[] = R"(<url>; rel=preload; title="foo,bar")"; constexpr char s[] = R"(<url>; rel=preload; title="foo,bar")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// With extra link-param and quote-string. URI url should be // With extra link-param and quote-string. URI url should be
// extracted // extracted
const char s[] = R"(<url>; title="foo,bar"; rel=preload)"; constexpr char s[] = R"(<url>; title="foo,bar"; rel=preload)";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// ',' after quote-string // ',' after quote-string
const char s[] = R"(<url>; title="foo,bar", <url>; rel=preload)"; constexpr char s[] = R"(<url>; title="foo,bar", <url>; rel=preload)";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[25], &s[28]) == res[0].uri); CU_ASSERT(std::make_pair(&s[25], &s[28]) == res[0].uri);
} }
{ {
// Only first URI should be extracted. // Only first URI should be extracted.
const char s[] = "<url>; rel=preload, <url>"; constexpr char s[] = "<url>; rel=preload, <url>";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// Both have rel=preload, so both urls should be extracted // Both have rel=preload, so both urls should be extracted
const char s[] = "<url>; rel=preload, <url>; rel=preload"; constexpr char s[] = "<url>; rel=preload, <url>; rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(2 == res.size()); CU_ASSERT(2 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
CU_ASSERT(std::make_pair(&s[21], &s[24]) == res[1].uri); CU_ASSERT(std::make_pair(&s[21], &s[24]) == res[1].uri);
} }
{ {
// Second URI uri should be extracted. // Second URI uri should be extracted.
const char s[] = "<url>, <url>;rel=preload"; constexpr char s[] = "<url>, <url>;rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[8], &s[11]) == res[0].uri); CU_ASSERT(std::make_pair(&s[8], &s[11]) == res[0].uri);
} }
{ {
// Error if input ends with ';' // Error if input ends with ';'
const char s[] = "<url>;rel=preload;"; constexpr char s[] = "<url>;rel=preload;";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// Error if link header ends with ';' // Error if link header ends with ';'
const char s[] = "<url>;rel=preload;, <url>"; constexpr char s[] = "<url>;rel=preload;, <url>";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// OK if input ends with ',' // OK if input ends with ','
const char s[] = "<url>;rel=preload,"; constexpr char s[] = "<url>;rel=preload,";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// Multiple repeated ','s between fields is OK // Multiple repeated ','s between fields is OK
const char s[] = "<url>,,,<url>;rel=preload"; constexpr char s[] = "<url>,,,<url>;rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[9], &s[12]) == res[0].uri); CU_ASSERT(std::make_pair(&s[9], &s[12]) == res[0].uri);
} }
{ {
// Error if url is not enclosed by <> // Error if url is not enclosed by <>
const char s[] = "url>;rel=preload"; constexpr char s[] = "url>;rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// Error if url is not enclosed by <> // Error if url is not enclosed by <>
const char s[] = "<url;rel=preload"; constexpr char s[] = "<url;rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// Empty parameter value is not allowed // Empty parameter value is not allowed
const char s[] = "<url>;rel=preload; as="; constexpr char s[] = "<url>;rel=preload; as=";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// Empty parameter value is not allowed // Empty parameter value is not allowed
const char s[] = "<url>;as=;rel=preload"; constexpr char s[] = "<url>;as=;rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// Empty parameter value is not allowed // Empty parameter value is not allowed
const char s[] = "<url>;as=, <url>;rel=preload"; constexpr char s[] = "<url>;as=, <url>;rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// Empty parameter name is not allowed // Empty parameter name is not allowed
const char s[] = "<url>; =file; rel=preload"; constexpr char s[] = "<url>; =file; rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// Without whitespaces // Without whitespaces
const char s[] = "<url>;as=file;rel=preload,<url>;rel=preload"; constexpr char s[] = "<url>;as=file;rel=preload,<url>;rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(2 == res.size()); CU_ASSERT(2 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
CU_ASSERT(std::make_pair(&s[27], &s[30]) == res[1].uri); CU_ASSERT(std::make_pair(&s[27], &s[30]) == res[1].uri);
} }
{ {
// link-extension may have no value // link-extension may have no value
const char s[] = "<url>; as; rel=preload"; constexpr char s[] = "<url>; as; rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// ext-name-star // ext-name-star
const char s[] = "<url>; foo*=bar; rel=preload"; constexpr char s[] = "<url>; foo*=bar; rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// '*' is not allowed expect for trailing one // '*' is not allowed expect for trailing one
const char s[] = "<url>; *=bar; rel=preload"; constexpr char s[] = "<url>; *=bar; rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// '*' is not allowed expect for trailing one // '*' is not allowed expect for trailing one
const char s[] = "<url>; foo*bar=buzz; rel=preload"; constexpr char s[] = "<url>; foo*bar=buzz; rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// ext-name-star must be followed by '=' // ext-name-star must be followed by '='
const char s[] = "<url>; foo*; rel=preload"; constexpr char s[] = "<url>; foo*; rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// '>' is not followed by ';' // '>' is not followed by ';'
const char s[] = "<url> rel=preload"; constexpr char s[] = "<url> rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// Starting with whitespace is no problem. // Starting with whitespace is no problem.
const char s[] = " <url>; rel=preload"; constexpr char s[] = " <url>; rel=preload";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
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 // preload is a prefix of bogus rel parameter value
const char s[] = "<url>; rel=preloadx"; constexpr char s[] = "<url>; rel=preloadx";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// preload in relation-types list // preload in relation-types list
const char s[] = R"(<url>; rel="preload")"; constexpr char s[] = R"(<url>; rel="preload")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// preload in relation-types list followed by another parameter // preload in relation-types list followed by another parameter
const char s[] = R"(<url>; rel="preload foo")"; constexpr char s[] = R"(<url>; rel="preload foo")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// preload in relation-types list following another parameter // preload in relation-types list following another parameter
const char s[] = R"(<url>; rel="foo preload")"; constexpr char s[] = R"(<url>; rel="foo preload")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// preload in relation-types list between other parameters // preload in relation-types list between other parameters
const char s[] = R"(<url>; rel="foo preload bar")"; constexpr char s[] = R"(<url>; rel="foo preload bar")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// preload in relation-types list between other parameters // preload in relation-types list between other parameters
const char s[] = R"(<url>; rel="foo preload bar")"; constexpr char s[] = R"(<url>; rel="foo preload bar")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// no preload in relation-types list // no preload in relation-types list
const char s[] = R"(<url>; rel="foo")"; constexpr char s[] = R"(<url>; rel="foo")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// no preload in relation-types list, multiple unrelated elements. // no preload in relation-types list, multiple unrelated elements.
const char s[] = R"(<url>; rel="foo bar")"; constexpr char s[] = R"(<url>; rel="foo bar")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// preload in relation-types list, followed by another link-value. // preload in relation-types list, followed by another link-value.
const char s[] = R"(<url>; rel="preload", <url>)"; constexpr char s[] = R"(<url>; rel="preload", <url>)";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// preload in relation-types list, following another link-value. // preload in relation-types list, following another link-value.
const char s[] = R"(<url>, <url>; rel="preload")"; constexpr char s[] = R"(<url>, <url>; rel="preload")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[8], &s[11]) == res[0].uri); CU_ASSERT(std::make_pair(&s[8], &s[11]) == res[0].uri);
} }
{ {
// preload in relation-types list, followed by another link-param. // preload in relation-types list, followed by another link-param.
const char s[] = R"(<url>; rel="preload"; as="font")"; constexpr char s[] = R"(<url>; rel="preload"; as="font")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// preload in relation-types list, followed by character other // preload in relation-types list, followed by character other
// than ';' or ',' // than ';' or ','
const char s[] = R"(<url>; rel="preload".)"; constexpr char s[] = R"(<url>; rel="preload".)";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// preload in relation-types list, followed by ';' but it // preload in relation-types list, followed by ';' but it
// terminates input // terminates input
const char s[] = R"(<url>; rel="preload";)"; constexpr char s[] = R"(<url>; rel="preload";)";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// preload in relation-types list, followed by ',' but it // preload in relation-types list, followed by ',' but it
// terminates input // terminates input
const char s[] = R"(<url>; rel="preload",)"; constexpr char s[] = R"(<url>; rel="preload",)";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// preload in relation-types list but there is preceding white // preload in relation-types list but there is preceding white
// space. // space.
const char s[] = R"(<url>; rel=" preload")"; constexpr char s[] = R"(<url>; rel=" preload")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// preload in relation-types list but there is trailing white // preload in relation-types list but there is trailing white
// space. // space.
const char s[] = R"(<url>; rel="preload ")"; constexpr char s[] = R"(<url>; rel="preload ")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// backslash escaped characters in quoted-string // backslash escaped characters in quoted-string
const char s[] = R"(<url>; rel=preload; title="foo\"baz\"bar")"; constexpr char s[] = R"(<url>; rel=preload; title="foo\"baz\"bar")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// anchor="" is acceptable // anchor="" is acceptable
const char s[] = R"(<url>; rel=preload; anchor="")"; constexpr char s[] = R"(<url>; rel=preload; anchor="")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// With anchor="#foo", url should be ignored // With anchor="#foo", url should be ignored
const char s[] = R"(<url>; rel=preload; anchor="#foo")"; constexpr char s[] = R"(<url>; rel=preload; anchor="#foo")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// With anchor=f, url should be ignored // With anchor=f, url should be ignored
const char s[] = "<url>; rel=preload; anchor=f"; constexpr char s[] = "<url>; rel=preload; anchor=f";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// First url is ignored With anchor="#foo", but url should be // First url is ignored With anchor="#foo", but url should be
// accepted. // accepted.
const char s[] = R"(<url>; rel=preload; anchor="#foo", <url>; rel=preload)"; constexpr char s[] =
auto res = http2::parse_link_header(s, sizeof(s) - 1); R"(<url>; rel=preload; anchor="#foo", <url>; rel=preload)";
auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[36], &s[39]) == res[0].uri); CU_ASSERT(std::make_pair(&s[36], &s[39]) == res[0].uri);
} }
{ {
// With loadpolicy="next", url should be ignored // With loadpolicy="next", url should be ignored
const char s[] = R"(<url>; rel=preload; loadpolicy="next")"; constexpr char s[] = R"(<url>; rel=preload; loadpolicy="next")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size()); CU_ASSERT(0 == res.size());
} }
{ {
// url should be picked up if empty loadpolicy is specified // url should be picked up if empty loadpolicy is specified
const char s[] = R"(<url>; rel=preload; loadpolicy="")"; constexpr char s[] = R"(<url>; rel=preload; loadpolicy="")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(1 == res.size()); CU_ASSERT(1 == res.size());
CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri);
} }
{ {
// case-insensitive match // case-insensitive match
const char s[] = R"(<url>; rel=preload; ANCHOR="#foo", <url>; )" constexpr char s[] = R"(<url>; rel=preload; ANCHOR="#foo", <url>; )"
R"(REL=PRELOAD, <url>; REL="foo PRELOAD bar")"; R"(REL=PRELOAD, <url>; REL="foo PRELOAD bar")";
auto res = http2::parse_link_header(s, sizeof(s) - 1); auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(2 == res.size()); CU_ASSERT(2 == res.size());
CU_ASSERT(std::make_pair(&s[36], &s[39]) == res[0].uri); CU_ASSERT(std::make_pair(&s[36], &s[39]) == res[0].uri);
CU_ASSERT(std::make_pair(&s[42 + 14], &s[42 + 17]) == res[1].uri); CU_ASSERT(std::make_pair(&s[42 + 14], &s[42 + 17]) == res[1].uri);