From 3e6c38e3be2ba7a23a92d2ac88dcfe5d16d7c23f Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 25 Mar 2016 22:45:33 +0900 Subject: [PATCH] src: Made strieq(T, S) template --- src/HtmlParser.cc | 52 +++++++++++++++++++++++++++-------------------- src/shrpx_ssl.cc | 28 ++++++++++++++----------- src/util.h | 8 ++------ 3 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/HtmlParser.cc b/src/HtmlParser.cc index bcbfd13f..2a13738d 100644 --- a/src/HtmlParser.cc +++ b/src/HtmlParser.cc @@ -39,23 +39,27 @@ HtmlParser::HtmlParser(const std::string &base_uri) HtmlParser::~HtmlParser() { htmlFreeParserCtxt(parser_ctx_); } namespace { -const char *get_attr(const xmlChar **attrs, const char *name) { +StringRef get_attr(const xmlChar **attrs, const StringRef &name) { if (attrs == nullptr) { - return nullptr; + return StringRef{}; } for (; *attrs; attrs += 2) { - if (util::strieq(reinterpret_cast(attrs[0]), name)) { - return reinterpret_cast(attrs[1]); + if (util::strieq(StringRef{attrs[0], strlen(reinterpret_cast( + attrs[0]))}, + name)) { + return StringRef{attrs[1], + strlen(reinterpret_cast(attrs[1]))}; } } - return nullptr; + return StringRef{}; } } // namespace namespace { -void add_link(ParserData *parser_data, const char *uri, ResourceType res_type) { +void add_link(ParserData *parser_data, const StringRef &uri, + ResourceType res_type) { auto u = xmlBuildURI( - reinterpret_cast(uri), + reinterpret_cast(uri.c_str()), reinterpret_cast(parser_data->base_uri.c_str())); if (u) { parser_data->links.push_back( @@ -66,32 +70,34 @@ void add_link(ParserData *parser_data, const char *uri, ResourceType res_type) { } // namespace namespace { -void start_element_func(void *user_data, const xmlChar *name, +void start_element_func(void *user_data, const xmlChar *src_name, const xmlChar **attrs) { auto parser_data = static_cast(user_data); - if (util::strieq(reinterpret_cast(name), "head")) { + auto name = + StringRef{src_name, strlen(reinterpret_cast(src_name))}; + if (util::strieq_l("head", name)) { ++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"); - if (!href_attr) { + 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()) { return; } - if (util::strieq(rel_attr, "shortcut icon")) { + if (util::strieq_l("shortcut icon", rel_attr)) { add_link(parser_data, href_attr, REQ_OTHERS); - } else if (util::strieq(rel_attr, "stylesheet")) { + } else if (util::strieq_l("stylesheet", rel_attr)) { add_link(parser_data, href_attr, REQ_CSS); } - } else if (util::strieq(reinterpret_cast(name), "img")) { - auto src_attr = get_attr(attrs, "src"); - if (!src_attr) { + } else if (util::strieq_l("img", name)) { + auto src_attr = get_attr(attrs, StringRef::from_lit("src")); + if (src_attr.empty()) { return; } add_link(parser_data, src_attr, REQ_IMG); - } else if (util::strieq(reinterpret_cast(name), "script")) { - auto src_attr = get_attr(attrs, "src"); - if (!src_attr) { + } else if (util::strieq_l("script", name)) { + auto src_attr = get_attr(attrs, StringRef::from_lit("src")); + if (src_attr.empty()) { return; } if (parser_data->inside_head) { @@ -106,7 +112,9 @@ void start_element_func(void *user_data, const xmlChar *name, 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")) { + if (util::strieq_l( + "head", + StringRef{name, strlen(reinterpret_cast(name))})) { --parser_data->inside_head; } } diff --git a/src/shrpx_ssl.cc b/src/shrpx_ssl.cc index 426d9d2b..15c47f32 100644 --- a/src/shrpx_ssl.cc +++ b/src/shrpx_ssl.cc @@ -437,25 +437,29 @@ int alpn_select_proto_cb(SSL *ssl, const unsigned char **out, } // namespace #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L -namespace { -constexpr const char *tls_names[] = {"TLSv1.2", "TLSv1.1", "TLSv1.0"}; -constexpr size_t tls_namelen = array_size(tls_names); -constexpr long int tls_masks[] = {SSL_OP_NO_TLSv1_2, SSL_OP_NO_TLSv1_1, - SSL_OP_NO_TLSv1}; -} // namespace +struct TLSProtocol { + StringRef name; + long int mask; +}; + +constexpr TLSProtocol TLS_PROTOS[] = { + TLSProtocol{StringRef::from_lit("TLSv1.2"), SSL_OP_NO_TLSv1_2}, + TLSProtocol{StringRef::from_lit("TLSv1.1"), SSL_OP_NO_TLSv1_1}, + TLSProtocol{StringRef::from_lit("TLSv1.0"), SSL_OP_NO_TLSv1}}; long int create_tls_proto_mask(const std::vector &tls_proto_list) { long int res = 0; - for (size_t i = 0; i < tls_namelen; ++i) { - size_t j; - for (j = 0; j < tls_proto_list.size(); ++j) { - if (util::strieq(tls_names[i], tls_proto_list[j])) { + for (auto &supported : TLS_PROTOS) { + auto ok = false; + for (auto &name : tls_proto_list) { + if (util::strieq(supported.name, name)) { + ok = true; break; } } - if (j == tls_proto_list.size()) { - res |= tls_masks[i]; + if (!ok) { + res |= supported.mask; } } return res; diff --git a/src/util.h b/src/util.h index 48b732b1..c395b12f 100644 --- a/src/util.h +++ b/src/util.h @@ -287,12 +287,8 @@ bool strieq(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) { return std::equal(first1, last1, first2, CaseCmp()); } -inline bool strieq(const std::string &a, const std::string &b) { - return strieq(std::begin(a), std::end(a), std::begin(b), std::end(b)); -} - -inline bool strieq(const StringRef &a, const StringRef &b) { - return strieq(std::begin(a), std::end(a), std::begin(b), std::end(b)); +template bool strieq(const T &a, const S &b) { + return strieq(a.begin(), a.end(), b.begin(), b.end()); } template