src: Don't push if Link header field includes nopush

This commit is contained in:
Tatsuhiro Tsujikawa 2016-03-03 23:20:32 +09:00
parent 5da38b22c0
commit 9afc017532
2 changed files with 148 additions and 83 deletions

View File

@ -864,6 +864,29 @@ bool check_link_param_empty(const char *first, const char *last,
} }
} // namespace } // namespace
namespace {
// Returns true if link-param consists of only parmname, and it
// matches string [pat, pat + patlen).
bool check_link_param_without_value(const char *first, const char *last,
const char *pat, size_t patlen) {
if (first + patlen > last) {
return false;
}
if (first + patlen == last) {
return std::equal(pat, pat + patlen, first, util::CaseCmp());
}
switch (*(first + patlen)) {
case ';':
case ',':
return std::equal(pat, pat + patlen, first, util::CaseCmp());
}
return false;
}
} // namespace
namespace { namespace {
std::pair<LinkHeader, const char *> std::pair<LinkHeader, const char *>
parse_next_link_header_once(const char *first, const char *last) { parse_next_link_header_once(const char *first, const char *last) {
@ -900,6 +923,7 @@ parse_next_link_header_once(const char *first, const char *last) {
} }
// we expect link-param // we expect link-param
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 = sizeof(PLP) - 1;
@ -995,6 +1019,15 @@ parse_next_link_header_once(const char *first, const char *last) {
ign = true; ign = true;
} }
// reject URI if we have nopush attribute.
static constexpr char NOPUSH[] = "nopush";
static constexpr size_t NOPUSHLEN = str_size(NOPUSH);
if (!ign &&
check_link_param_without_value(first, last, NOPUSH, NOPUSHLEN)) {
ign = true;
}
}
auto param_first = first; auto param_first = first;
for (; first != last;) { for (; first != last;) {
if (util::in_attr_char(*first)) { if (util::in_attr_char(*first)) {

View File

@ -625,6 +625,38 @@ void test_http2_parse_link_header(void) {
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);
} }
{
// nopush at the end of input
constexpr char s[] = "<url>; rel=preload; nopush";
auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size());
}
{
// nopush followed by ';'
constexpr char s[] = "<url>; rel=preload; nopush; foo";
auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size());
}
{
// nopush followed by ','
constexpr char s[] = "<url>; nopush; rel=preload";
auto res = http2::parse_link_header(s, str_size(s));
CU_ASSERT(0 == res.size());
}
{
// string whose prefix is nopush
constexpr char s[] = "<url>; nopushyes; 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);
}
{
// rel=preload twice
constexpr char s[] = "<url>; rel=preload; 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);
}
} }
void test_http2_path_join(void) { void test_http2_path_join(void) {