diff --git a/src/http2.cc b/src/http2.cc index 68587216..8fcbd43c 100644 --- a/src/http2.cc +++ b/src/http2.cc @@ -926,10 +926,10 @@ parse_next_link_header_once(const char *first, const char *last) { if (!ign) { // rel can take several relations using quoted form. 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 size_t PLTLEN = sizeof(PLT) - 1; + static constexpr size_t PLTLEN = str_size(PLT); if (first + PLPLEN < last && *(first + PLPLEN - 1) == '"' && std::equal(PLP, PLP + PLPLEN, first, util::CaseCmp())) { // 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 // simply skipped. 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 (std::equal(PL, PL + PLLEN, first, util::CaseCmp())) { // 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. 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)) { 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 // tightened up to just pick up "next" or "insert". static constexpr char LOADPOLICY[] = "loadpolicy="; - static constexpr size_t LOADPOLICYLEN = sizeof(LOADPOLICY) - 1; + static constexpr size_t LOADPOLICYLEN = str_size(LOADPOLICY); if (!ign && !check_link_param_empty(first, last, LOADPOLICY, LOADPOLICYLEN)) { ign = true; diff --git a/src/http2_test.cc b/src/http2_test.cc index 46653686..88bf89e3 100644 --- a/src/http2_test.cc +++ b/src/http2_test.cc @@ -274,353 +274,354 @@ void test_http2_lookup_token(void) { void test_http2_parse_link_header(void) { { // only URI appears; we don't extract URI unless it bears rel=preload - const char s[] = ""; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = ""; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // URI url should be extracted - const char s[] = "; rel=preload"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = "; rel=preload"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // With extra link-param. URI url should be extracted - const char s[] = "; rel=preload; as=file"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = "; rel=preload; as=file"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // With extra link-param. URI url should be extracted - const char s[] = "; as=file; rel=preload"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = "; as=file; rel=preload"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // With extra link-param and quote-string. URI url should be // extracted - const char s[] = R"(; rel=preload; title="foo,bar")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel=preload; title="foo,bar")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // With extra link-param and quote-string. URI url should be // extracted - const char s[] = R"(; title="foo,bar"; rel=preload)"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; title="foo,bar"; rel=preload)"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // ',' after quote-string - const char s[] = R"(; title="foo,bar", ; rel=preload)"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; title="foo,bar", ; rel=preload)"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[25], &s[28]) == res[0].uri); } { // Only first URI should be extracted. - const char s[] = "; rel=preload, "; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = "; rel=preload, "; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // Both have rel=preload, so both urls should be extracted - const char s[] = "; rel=preload, ; rel=preload"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = "; rel=preload, ; rel=preload"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(2 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); CU_ASSERT(std::make_pair(&s[21], &s[24]) == res[1].uri); } { // Second URI uri should be extracted. - const char s[] = ", ;rel=preload"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = ", ;rel=preload"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[8], &s[11]) == res[0].uri); } { // Error if input ends with ';' - const char s[] = ";rel=preload;"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = ";rel=preload;"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // Error if link header ends with ';' - const char s[] = ";rel=preload;, "; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = ";rel=preload;, "; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // OK if input ends with ',' - const char s[] = ";rel=preload,"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = ";rel=preload,"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // Multiple repeated ','s between fields is OK - const char s[] = ",,,;rel=preload"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = ",,,;rel=preload"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[9], &s[12]) == res[0].uri); } { // Error if url is not enclosed by <> - const char s[] = "url>;rel=preload"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = "url>;rel=preload"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // Error if url is not enclosed by <> - const char s[] = "' is not followed by ';' - const char s[] = " rel=preload"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = " rel=preload"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // Starting with whitespace is no problem. - const char s[] = " ; rel=preload"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = " ; rel=preload"; + auto res = http2::parse_link_header(s, str_size(s)); 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[] = "; rel=preloadx"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = "; rel=preloadx"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // preload in relation-types list - const char s[] = R"(; rel="preload")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="preload")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // preload in relation-types list followed by another parameter - const char s[] = R"(; rel="preload foo")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="preload foo")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // preload in relation-types list following another parameter - const char s[] = R"(; rel="foo preload")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="foo preload")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // preload in relation-types list between other parameters - const char s[] = R"(; rel="foo preload bar")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="foo preload bar")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // preload in relation-types list between other parameters - const char s[] = R"(; rel="foo preload bar")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="foo preload bar")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // no preload in relation-types list - const char s[] = R"(; rel="foo")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="foo")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // no preload in relation-types list, multiple unrelated elements. - const char s[] = R"(; rel="foo bar")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="foo bar")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // preload in relation-types list, followed by another link-value. - const char s[] = R"(; rel="preload", )"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="preload", )"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // preload in relation-types list, following another link-value. - const char s[] = R"(, ; rel="preload")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(, ; rel="preload")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[8], &s[11]) == res[0].uri); } { // preload in relation-types list, followed by another link-param. - const char s[] = R"(; rel="preload"; as="font")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="preload"; as="font")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // preload in relation-types list, followed by character other // than ';' or ',' - const char s[] = R"(; rel="preload".)"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="preload".)"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // preload in relation-types list, followed by ';' but it // terminates input - const char s[] = R"(; rel="preload";)"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="preload";)"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // preload in relation-types list, followed by ',' but it // terminates input - const char s[] = R"(; rel="preload",)"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="preload",)"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // preload in relation-types list but there is preceding white // space. - const char s[] = R"(; rel=" preload")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel=" preload")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // preload in relation-types list but there is trailing white // space. - const char s[] = R"(; rel="preload ")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel="preload ")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // backslash escaped characters in quoted-string - const char s[] = R"(; rel=preload; title="foo\"baz\"bar")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel=preload; title="foo\"baz\"bar")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // anchor="" is acceptable - const char s[] = R"(; rel=preload; anchor="")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel=preload; anchor="")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // With anchor="#foo", url should be ignored - const char s[] = R"(; rel=preload; anchor="#foo")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel=preload; anchor="#foo")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // With anchor=f, url should be ignored - const char s[] = "; rel=preload; anchor=f"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = "; rel=preload; anchor=f"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // First url is ignored With anchor="#foo", but url should be // accepted. - const char s[] = R"(; rel=preload; anchor="#foo", ; rel=preload)"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = + R"(; rel=preload; anchor="#foo", ; rel=preload)"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[36], &s[39]) == res[0].uri); } { // With loadpolicy="next", url should be ignored - const char s[] = R"(; rel=preload; loadpolicy="next")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel=preload; loadpolicy="next")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(0 == res.size()); } { // url should be picked up if empty loadpolicy is specified - const char s[] = R"(; rel=preload; loadpolicy="")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel=preload; loadpolicy="")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(1 == res.size()); CU_ASSERT(std::make_pair(&s[1], &s[4]) == res[0].uri); } { // case-insensitive match - const char s[] = R"(; rel=preload; ANCHOR="#foo", ; )" - R"(REL=PRELOAD, ; REL="foo PRELOAD bar")"; - auto res = http2::parse_link_header(s, sizeof(s) - 1); + constexpr char s[] = R"(; rel=preload; ANCHOR="#foo", ; )" + R"(REL=PRELOAD, ; REL="foo PRELOAD bar")"; + auto res = http2::parse_link_header(s, str_size(s)); CU_ASSERT(2 == res.size()); 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);