From d3561a63b11be8b1ad598cef52c37ff03525ffd6 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 17 Apr 2015 21:25:31 +0900 Subject: [PATCH] nghttp: Depend on "leader" anchor if js is linked inside head element --- src/HtmlParser.cc | 24 ++++++++++++++++++++---- src/HtmlParser.h | 3 +++ src/nghttp.cc | 1 + 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/HtmlParser.cc b/src/HtmlParser.cc index 26559cce..51f6a12b 100644 --- a/src/HtmlParser.cc +++ b/src/HtmlParser.cc @@ -30,7 +30,8 @@ namespace nghttp2 { -ParserData::ParserData(const std::string &base_uri) : base_uri(base_uri) {} +ParserData::ParserData(const std::string &base_uri) + : base_uri(base_uri), inside_head(0) {} HtmlParser::HtmlParser(const std::string &base_uri) : base_uri_(base_uri), parser_ctx_(nullptr), parser_data_(base_uri) {} @@ -68,6 +69,9 @@ namespace { void start_element_func(void *user_data, const xmlChar *name, const xmlChar **attrs) { auto parser_data = static_cast(user_data); + if (util::strieq(reinterpret_cast(name), "head")) { + ++parser_data->inside_head; + } if (util::strieq(reinterpret_cast(name), "link")) { auto rel_attr = get_attr(attrs, "rel"); auto href_attr = get_attr(attrs, "href"); @@ -90,8 +94,20 @@ void start_element_func(void *user_data, const xmlChar *name, if (!src_attr) { return; } - // TODO if script is inside in head, this should be REQ_LEADERS. - add_link(parser_data, src_attr, REQ_UNBLOCK_JS); + if (parser_data->inside_head) { + add_link(parser_data, src_attr, REQ_JS); + } else { + add_link(parser_data, src_attr, REQ_UNBLOCK_JS); + } + } +} +} // namespace + +namespace { +void end_element_func(void *user_data, const xmlChar *name) { + auto parser_data = static_cast(user_data); + if (util::strieq(reinterpret_cast(name), "head")) { + --parser_data->inside_head; } } } // namespace @@ -113,7 +129,7 @@ xmlSAXHandler saxHandler = { nullptr, // startDocumentSAXFunc nullptr, // endDocumentSAXFunc &start_element_func, // startElementSAXFunc - nullptr, // endElementSAXFunc + &end_element_func, // endElementSAXFunc nullptr, // referenceSAXFunc nullptr, // charactersSAXFunc nullptr, // ignorableWhitespaceSAXFunc diff --git a/src/HtmlParser.h b/src/HtmlParser.h index 6d438212..694b6c9b 100644 --- a/src/HtmlParser.h +++ b/src/HtmlParser.h @@ -41,6 +41,7 @@ namespace nghttp2 { // Lower value has higher priority enum RequestPriority { REQ_CSS = 1, + REQ_JS, REQ_UNBLOCK_JS, REQ_IMG, REQ_OTHERS, @@ -49,6 +50,8 @@ enum RequestPriority { struct ParserData { std::string base_uri; std::vector> links; + // > 0 if we are inside "head" element. + int inside_head; ParserData(const std::string &base_uri); }; diff --git a/src/nghttp.cc b/src/nghttp.cc index 8f27e9a8..d4b02c3c 100644 --- a/src/nghttp.cc +++ b/src/nghttp.cc @@ -196,6 +196,7 @@ nghttp2_priority_spec Request::resolve_dep(int32_t pri) { int32_t weight; switch (pri) { case REQ_CSS: + case REQ_JS: anchor_id = anchors[ANCHOR_LEADERS].stream_id; weight = 2; break;