2012-05-19 15:10:07 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-05-19 15:10:07 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "HtmlParser.h"
|
|
|
|
|
2013-01-08 16:42:06 +01:00
|
|
|
#include <libxml/uri.h>
|
|
|
|
|
2012-05-19 15:10:07 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
namespace nghttp2 {
|
2012-05-19 15:10:07 +02:00
|
|
|
|
2015-04-17 14:25:31 +02:00
|
|
|
ParserData::ParserData(const std::string &base_uri)
|
|
|
|
: base_uri(base_uri), inside_head(0) {}
|
2012-05-19 15:10:07 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
HtmlParser::HtmlParser(const std::string &base_uri)
|
|
|
|
: base_uri_(base_uri), parser_ctx_(nullptr), parser_data_(base_uri) {}
|
2012-05-19 15:10:07 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
HtmlParser::~HtmlParser() { htmlFreeParserCtxt(parser_ctx_); }
|
2012-05-19 15:10:07 +02:00
|
|
|
|
|
|
|
namespace {
|
2016-03-25 14:45:33 +01:00
|
|
|
StringRef get_attr(const xmlChar **attrs, const StringRef &name) {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (attrs == nullptr) {
|
2016-03-25 14:45:33 +01:00
|
|
|
return StringRef{};
|
2013-09-26 15:56:26 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
for (; *attrs; attrs += 2) {
|
2016-03-25 14:45:33 +01:00
|
|
|
if (util::strieq(StringRef{attrs[0], strlen(reinterpret_cast<const char *>(
|
|
|
|
attrs[0]))},
|
|
|
|
name)) {
|
|
|
|
return StringRef{attrs[1],
|
|
|
|
strlen(reinterpret_cast<const char *>(attrs[1]))};
|
2012-05-19 15:10:07 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-25 14:45:33 +01:00
|
|
|
return StringRef{};
|
2012-05-19 15:10:07 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2017-02-01 15:54:15 +01:00
|
|
|
namespace {
|
2017-02-04 03:29:10 +01:00
|
|
|
ResourceType
|
|
|
|
get_resource_type_for_preload_as(const StringRef &attribute_value) {
|
2017-02-01 15:54:15 +01:00
|
|
|
if (util::strieq_l("image", attribute_value)) {
|
|
|
|
return REQ_IMG;
|
|
|
|
} else if (util::strieq_l("style", attribute_value)) {
|
|
|
|
return REQ_CSS;
|
|
|
|
} else if (util::strieq_l("script", attribute_value)) {
|
|
|
|
return REQ_UNBLOCK_JS;
|
|
|
|
} else {
|
|
|
|
return REQ_OTHERS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-09-29 11:58:46 +02:00
|
|
|
namespace {
|
2016-03-25 14:45:33 +01:00
|
|
|
void add_link(ParserData *parser_data, const StringRef &uri,
|
|
|
|
ResourceType res_type) {
|
2014-11-27 15:39:04 +01:00
|
|
|
auto u = xmlBuildURI(
|
2016-03-25 14:45:33 +01:00
|
|
|
reinterpret_cast<const xmlChar *>(uri.c_str()),
|
2014-11-27 15:39:04 +01:00
|
|
|
reinterpret_cast<const xmlChar *>(parser_data->base_uri.c_str()));
|
|
|
|
if (u) {
|
|
|
|
parser_data->links.push_back(
|
2015-04-17 14:31:11 +02:00
|
|
|
std::make_pair(reinterpret_cast<char *>(u), res_type));
|
2013-09-29 11:58:46 +02:00
|
|
|
free(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-05-19 15:10:07 +02:00
|
|
|
namespace {
|
2016-03-25 14:45:33 +01:00
|
|
|
void start_element_func(void *user_data, const xmlChar *src_name,
|
2014-11-27 15:39:04 +01:00
|
|
|
const xmlChar **attrs) {
|
|
|
|
auto parser_data = static_cast<ParserData *>(user_data);
|
2016-03-25 14:45:33 +01:00
|
|
|
auto name =
|
|
|
|
StringRef{src_name, strlen(reinterpret_cast<const char *>(src_name))};
|
|
|
|
if (util::strieq_l("head", name)) {
|
2015-04-17 14:25:31 +02:00
|
|
|
++parser_data->inside_head;
|
|
|
|
}
|
2016-03-25 14:45:33 +01:00
|
|
|
if (util::strieq_l("link", name)) {
|
|
|
|
auto rel_attr = get_attr(attrs, StringRef::from_lit("rel"));
|
|
|
|
auto href_attr = get_attr(attrs, StringRef::from_lit("href"));
|
|
|
|
if (rel_attr.empty() || href_attr.empty()) {
|
2013-09-29 11:58:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-03-25 14:45:33 +01:00
|
|
|
if (util::strieq_l("shortcut icon", rel_attr)) {
|
2015-04-16 16:42:36 +02:00
|
|
|
add_link(parser_data, href_attr, REQ_OTHERS);
|
2016-03-25 14:45:33 +01:00
|
|
|
} else if (util::strieq_l("stylesheet", rel_attr)) {
|
2015-04-16 16:42:36 +02:00
|
|
|
add_link(parser_data, href_attr, REQ_CSS);
|
2017-02-01 15:54:15 +01:00
|
|
|
} else if (util::strieq_l("preload", rel_attr)) {
|
|
|
|
auto as_attr = get_attr(attrs, StringRef::from_lit("as"));
|
|
|
|
if (as_attr.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-02-04 03:29:10 +01:00
|
|
|
add_link(parser_data, href_attr,
|
|
|
|
get_resource_type_for_preload_as(as_attr));
|
2012-05-19 15:10:07 +02:00
|
|
|
}
|
2016-03-25 14:45:33 +01:00
|
|
|
} else if (util::strieq_l("img", name)) {
|
|
|
|
auto src_attr = get_attr(attrs, StringRef::from_lit("src"));
|
|
|
|
if (src_attr.empty()) {
|
2013-09-29 11:58:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-04-16 16:42:36 +02:00
|
|
|
add_link(parser_data, src_attr, REQ_IMG);
|
2016-03-25 14:45:33 +01:00
|
|
|
} else if (util::strieq_l("script", name)) {
|
|
|
|
auto src_attr = get_attr(attrs, StringRef::from_lit("src"));
|
|
|
|
if (src_attr.empty()) {
|
2013-09-29 11:58:46 +02:00
|
|
|
return;
|
2012-05-19 15:10:07 +02:00
|
|
|
}
|
2015-04-17 14:25:31 +02:00
|
|
|
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<ParserData *>(user_data);
|
2016-03-25 14:45:33 +01:00
|
|
|
if (util::strieq_l(
|
|
|
|
"head",
|
|
|
|
StringRef{name, strlen(reinterpret_cast<const char *>(name))})) {
|
2015-04-17 14:25:31 +02:00
|
|
|
--parser_data->inside_head;
|
2012-05-19 15:10:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
xmlSAXHandler saxHandler = {
|
|
|
|
nullptr, // internalSubsetSAXFunc
|
|
|
|
nullptr, // isStandaloneSAXFunc
|
|
|
|
nullptr, // hasInternalSubsetSAXFunc
|
|
|
|
nullptr, // hasExternalSubsetSAXFunc
|
|
|
|
nullptr, // resolveEntitySAXFunc
|
|
|
|
nullptr, // getEntitySAXFunc
|
|
|
|
nullptr, // entityDeclSAXFunc
|
|
|
|
nullptr, // notationDeclSAXFunc
|
|
|
|
nullptr, // attributeDeclSAXFunc
|
|
|
|
nullptr, // elementDeclSAXFunc
|
|
|
|
nullptr, // unparsedEntityDeclSAXFunc
|
|
|
|
nullptr, // setDocumentLocatorSAXFunc
|
|
|
|
nullptr, // startDocumentSAXFunc
|
|
|
|
nullptr, // endDocumentSAXFunc
|
2013-10-02 16:08:47 +02:00
|
|
|
&start_element_func, // startElementSAXFunc
|
2015-04-17 14:25:31 +02:00
|
|
|
&end_element_func, // endElementSAXFunc
|
2014-11-27 15:39:04 +01:00
|
|
|
nullptr, // referenceSAXFunc
|
|
|
|
nullptr, // charactersSAXFunc
|
|
|
|
nullptr, // ignorableWhitespaceSAXFunc
|
|
|
|
nullptr, // processingInstructionSAXFunc
|
|
|
|
nullptr, // commentSAXFunc
|
|
|
|
nullptr, // warningSAXFunc
|
|
|
|
nullptr, // errorSAXFunc
|
|
|
|
nullptr, // fatalErrorSAXFunc
|
|
|
|
nullptr, // getParameterEntitySAXFunc
|
|
|
|
nullptr, // cdataBlockSAXFunc
|
|
|
|
nullptr, // externalSubsetSAXFunc
|
|
|
|
0, // unsigned int initialized
|
|
|
|
nullptr, // void * _private
|
|
|
|
nullptr, // startElementNsSAX2Func
|
|
|
|
nullptr, // endElementNsSAX2Func
|
|
|
|
nullptr, // xmlStructuredErrorFunc
|
|
|
|
};
|
2012-05-19 15:10:07 +02:00
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int HtmlParser::parse_chunk(const char *chunk, size_t size, int fin) {
|
|
|
|
if (!parser_ctx_) {
|
|
|
|
parser_ctx_ =
|
|
|
|
htmlCreatePushParserCtxt(&saxHandler, &parser_data_, chunk, size,
|
|
|
|
base_uri_.c_str(), XML_CHAR_ENCODING_NONE);
|
|
|
|
if (!parser_ctx_) {
|
2012-05-19 15:10:07 +02:00
|
|
|
return -1;
|
|
|
|
} else {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (fin) {
|
2013-10-02 16:08:47 +02:00
|
|
|
return parse_chunk_internal(nullptr, 0, fin);
|
2012-05-19 15:10:07 +02:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return parse_chunk_internal(chunk, size, fin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int HtmlParser::parse_chunk_internal(const char *chunk, size_t size, int fin) {
|
2012-05-19 15:10:07 +02:00
|
|
|
int rv = htmlParseChunk(parser_ctx_, chunk, size, fin);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == 0) {
|
2012-05-19 15:10:07 +02:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:31:11 +02:00
|
|
|
const std::vector<std::pair<std::string, ResourceType>> &
|
2014-11-27 15:39:04 +01:00
|
|
|
HtmlParser::get_links() const {
|
2012-05-19 15:10:07 +02:00
|
|
|
return parser_data_.links;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void HtmlParser::clear_links() { parser_data_.links.clear(); }
|
2012-05-19 15:10:07 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
} // namespace nghttp2
|