src: Remove streq(const char *) overload

This commit is contained in:
Tatsuhiro Tsujikawa 2016-03-25 00:02:07 +09:00
parent 4dfae3484f
commit 0875e66aab
13 changed files with 56 additions and 81 deletions

View File

@ -879,11 +879,11 @@ int Http2Handler::verify_npn_result() {
SSL_get0_next_proto_negotiated(ssl_, &next_proto, &next_proto_len);
for (int i = 0; i < 2; ++i) {
if (next_proto) {
auto proto = StringRef{next_proto, next_proto_len};
if (sessions_->get_config()->verbose) {
std::string proto(next_proto, next_proto + next_proto_len);
std::cout << "The negotiated protocol: " << proto << std::endl;
}
if (util::check_h2_is_selected(next_proto, next_proto_len)) {
if (util::check_h2_is_selected(proto)) {
return 0;
}
break;

View File

@ -187,7 +187,7 @@ bool tls_h2_negotiated(ssl_socket &socket) {
return false;
}
return util::check_h2_is_selected(next_proto, next_proto_len);
return util::check_h2_is_selected(StringRef{next_proto, next_proto_len});
}
} // namespace asio_http2

View File

@ -770,9 +770,10 @@ int Client::connection_made() {
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
if (next_proto) {
if (util::check_h2_is_selected(next_proto, next_proto_len)) {
auto proto = StringRef{next_proto, next_proto_len};
if (util::check_h2_is_selected(proto)) {
session = make_unique<Http2Session>(this);
} else if (util::streq_l(NGHTTP2_H1_1, next_proto, next_proto_len)) {
} else if (util::streq_l(NGHTTP2_H1_1, proto)) {
session = make_unique<Http1Session>(this);
}
#ifdef HAVE_SPDYLAY
@ -786,7 +787,7 @@ int Client::connection_made() {
// Just assign next_proto to selected_proto anyway to show the
// negotiation result.
selected_proto.assign(next_proto, next_proto + next_proto_len);
selected_proto = proto.str();
} else {
std::cout << "No protocol negotiated. Fallback behaviour may be activated"
<< std::endl;

View File

@ -966,13 +966,11 @@ int HttpClient::connection_made() {
SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
for (int i = 0; i < 2; ++i) {
if (next_proto) {
auto proto = StringRef{next_proto, next_proto_len};
if (config.verbose) {
std::cout << "The negotiated protocol: ";
std::cout.write(reinterpret_cast<const char *>(next_proto),
next_proto_len);
std::cout << std::endl;
std::cout << "The negotiated protocol: " << proto << std::endl;
}
if (!util::check_h2_is_selected(next_proto, next_proto_len)) {
if (!util::check_h2_is_selected(proto)) {
next_proto = nullptr;
}
break;

View File

@ -548,26 +548,26 @@ int ClientHandler::validate_next_proto() {
return 0;
}
auto proto = StringRef{next_proto, next_proto_len};
if (LOG_ENABLED(INFO)) {
std::string proto(next_proto, next_proto + next_proto_len);
CLOG(INFO, this) << "The negotiated next protocol: " << proto;
}
if (!ssl::in_proto_list(get_config()->tls.npn_list, next_proto,
next_proto_len)) {
if (!ssl::in_proto_list(get_config()->tls.npn_list, proto)) {
if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "The negotiated protocol is not supported";
CLOG(INFO, this) << "The negotiated protocol is not supported: " << proto;
}
return -1;
}
if (util::check_h2_is_selected(next_proto, next_proto_len)) {
if (util::check_h2_is_selected(proto)) {
on_read_ = &ClientHandler::upstream_http2_connhd_read;
auto http2_upstream = make_unique<Http2Upstream>(this);
upstream_ = std::move(http2_upstream);
alpn_.assign(next_proto, next_proto + next_proto_len);
alpn_.assign(std::begin(proto), std::end(proto));
// At this point, input buffer is already filled with some bytes.
// The read callback is not called until new data come. So consume
@ -580,7 +580,7 @@ int ClientHandler::validate_next_proto() {
}
#ifdef HAVE_SPDYLAY
auto spdy_version = spdylay_npn_get_version(next_proto, next_proto_len);
auto spdy_version = spdylay_npn_get_version(proto.byte(), proto.size());
if (spdy_version) {
upstream_ = make_unique<SpdyUpstream>(spdy_version, this);
@ -609,9 +609,9 @@ int ClientHandler::validate_next_proto() {
}
#endif // HAVE_SPDYLAY
if (next_proto_len == 8 && memcmp("http/1.1", next_proto, 8) == 0) {
if (proto == StringRef::from_lit("http/1.1")) {
upstream_ = make_unique<HttpsUpstream>(this);
alpn_ = "http/1.1";
alpn_ = proto.str();
// At this point, input buffer is already filled with some bytes.
// The read callback is not called until new data come. So consume

View File

@ -489,8 +489,8 @@ std::vector<LogFragment> parse_log_format(const StringRef &optarg) {
if (type == SHRPX_LOGF_NONE) {
if (util::istarts_with(var_name, var_namelen, "http_")) {
if (util::streq("host", var_name + str_size("http_"),
var_namelen - str_size("http_"))) {
if (util::streq_l("host", StringRef{var_name + str_size("http_"),
var_namelen - str_size("http_")})) {
// Special handling of host header field. We will use
// :authority header field if host header is missing. This
// is a typical case in HTTP/2.

View File

@ -486,7 +486,7 @@ int Connection::check_http2_requirement() {
}
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
if (next_proto == nullptr ||
!util::check_h2_is_selected(next_proto, next_proto_len)) {
!util::check_h2_is_selected(StringRef{next_proto, next_proto_len})) {
return 0;
}
if (!nghttp2::ssl::check_http2_tls_version(tls.ssl)) {

View File

@ -1448,11 +1448,11 @@ int Http2Session::connection_made() {
SSL_get0_next_proto_negotiated(conn_.tls.ssl, &next_proto, &next_proto_len);
for (int i = 0; i < 2; ++i) {
if (next_proto) {
auto proto = StringRef{next_proto, next_proto_len};
if (LOG_ENABLED(INFO)) {
std::string proto(next_proto, next_proto + next_proto_len);
SSLOG(INFO, this) << "Negotiated next protocol: " << proto;
}
if (!util::check_h2_is_selected(next_proto, next_proto_len)) {
if (!util::check_h2_is_selected(proto)) {
return -1;
}
break;

View File

@ -419,8 +419,8 @@ int alpn_select_proto_cb(SSL *ssl, const unsigned char **out,
auto proto_len = *p;
if (proto_id + proto_len <= end &&
util::streq(target_proto_id.c_str(), target_proto_id.size(), proto_id,
proto_len)) {
util::streq(StringRef{target_proto_id},
StringRef{proto_id, proto_len})) {
*out = reinterpret_cast<const unsigned char *>(proto_id);
*outlen = proto_len;
@ -1252,9 +1252,9 @@ int cert_lookup_tree_add_cert_from_file(CertLookupTree *lt, SSL_CTX *ssl_ctx,
}
bool in_proto_list(const std::vector<std::string> &protos,
const unsigned char *needle, size_t len) {
const StringRef &needle) {
for (auto &proto : protos) {
if (util::streq(proto.c_str(), proto.size(), needle, len)) {
if (util::streq(StringRef{proto}, needle)) {
return true;
}
}

View File

@ -163,10 +163,10 @@ private:
int cert_lookup_tree_add_cert_from_file(CertLookupTree *lt, SSL_CTX *ssl_ctx,
const char *certfile);
// Returns true if |needle| which has |len| bytes is included in the
// Returns true if |proto| is included in the
// protocol list |protos|.
bool in_proto_list(const std::vector<std::string> &protos,
const unsigned char *needle, size_t len);
const StringRef &proto);
// Returns true if security requirement for HTTP/2 is fulfilled.
bool check_http2_requirement(SSL *ssl);

View File

@ -791,10 +791,9 @@ int64_t to_time64(const timeval &tv) {
return tv.tv_sec * 1000000 + tv.tv_usec;
}
bool check_h2_is_selected(const unsigned char *proto, size_t len) {
return streq_l(NGHTTP2_PROTO_VERSION_ID, proto, len) ||
streq_l(NGHTTP2_H2_16, proto, len) ||
streq_l(NGHTTP2_H2_14, proto, len);
bool check_h2_is_selected(const StringRef &proto) {
return streq_l(NGHTTP2_PROTO_VERSION_ID, proto) ||
streq_l(NGHTTP2_H2_16, proto) || streq_l(NGHTTP2_H2_14, proto);
}
namespace {

View File

@ -316,16 +316,6 @@ bool strieq_l(const CharT(&a)[N], const StringRef &b) {
return strieq(a, a + (N - 1), std::begin(b), std::end(b));
}
template <typename InputIt> bool streq(const char *a, InputIt b, size_t bn) {
if (!a) {
return false;
}
auto blast = b + bn;
for (; *a && b != blast && *a == *b; ++a, ++b)
;
return !*a && b == blast;
}
template <typename InputIt1, typename InputIt2>
bool streq(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) {
if (std::distance(first1, last1) != std::distance(first2, last2)) {
@ -334,38 +324,23 @@ bool streq(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) {
return std::equal(first1, last1, first2);
}
template <typename InputIt1, typename InputIt2>
bool streq(InputIt1 a, size_t alen, InputIt2 b, size_t blen) {
if (alen != blen) {
return false;
}
return std::equal(a, a + alen, b);
}
inline bool streq(const char *a, const char *b) {
if (!a || !b) {
return false;
}
return streq(a, strlen(a), b, strlen(b));
}
inline bool streq(const StringRef &a, const StringRef &b) {
return streq(std::begin(a), std::end(a), std::begin(b), std::end(b));
}
template <typename CharT, typename InputIt, size_t N>
bool streq_l(const CharT(&a)[N], InputIt b, size_t blen) {
return streq(a, N - 1, b, blen);
return streq(a, a + (N - 1), b, b + blen);
}
template <typename CharT, size_t N>
bool streq_l(const CharT(&a)[N], const std::string &b) {
return streq(a, N - 1, std::begin(b), b.size());
return streq(a, a + (N - 1), std::begin(b), std::end(b));
}
template <typename CharT, size_t N>
bool streq_l(const CharT(&a)[N], const StringRef &b) {
return streq(a, N - 1, std::begin(b), b.size());
return streq(a, a + (N - 1), std::begin(b), std::end(b));
}
bool strifind(const char *a, const char *b);
@ -563,9 +538,9 @@ bool check_path(const std::string &path);
// unit.
int64_t to_time64(const timeval &tv);
// Returns true if ALPN ID |proto| of length |len| is supported HTTP/2
// protocol identifier.
bool check_h2_is_selected(const unsigned char *alpn, size_t len);
// Returns true if ALPN ID |proto| is supported HTTP/2 protocol
// identifier.
bool check_h2_is_selected(const StringRef &proto);
// Selects h2 protocol ALPN ID if one of supported h2 versions are
// present in |in| of length inlen. Returns true if h2 version is

View File

@ -39,22 +39,24 @@ using namespace nghttp2;
namespace shrpx {
void test_util_streq(void) {
CU_ASSERT(util::streq("alpha", "alpha", 5));
CU_ASSERT(util::streq("alpha", "alphabravo", 5));
CU_ASSERT(!util::streq("alpha", "alphabravo", 6));
CU_ASSERT(!util::streq("alphabravo", "alpha", 5));
CU_ASSERT(!util::streq("alpha", "alphA", 5));
CU_ASSERT(!util::streq("", "a", 1));
CU_ASSERT(util::streq("", "", 0));
CU_ASSERT(!util::streq("alpha", "", 0));
CU_ASSERT(
util::streq(StringRef::from_lit("alpha"), StringRef::from_lit("alpha")));
CU_ASSERT(!util::streq(StringRef::from_lit("alpha"),
StringRef::from_lit("alphabravo")));
CU_ASSERT(!util::streq(StringRef::from_lit("alphabravo"),
StringRef::from_lit("alpha")));
CU_ASSERT(
!util::streq(StringRef::from_lit("alpha"), StringRef::from_lit("alphA")));
CU_ASSERT(!util::streq(StringRef{}, StringRef::from_lit("a")));
CU_ASSERT(util::streq(StringRef{}, StringRef{}));
CU_ASSERT(!util::streq(StringRef::from_lit("alpha"), StringRef{}));
CU_ASSERT(util::streq("alpha", 5, "alpha", 5));
CU_ASSERT(!util::streq("alpha", 4, "alpha", 5));
CU_ASSERT(!util::streq("alpha", 5, "alpha", 4));
CU_ASSERT(!util::streq("alpha", 5, "alphA", 5));
char *a = nullptr;
char *b = nullptr;
CU_ASSERT(util::streq(a, 0, b, 0));
CU_ASSERT(
!util::streq(StringRef::from_lit("alph"), StringRef::from_lit("alpha")));
CU_ASSERT(
!util::streq(StringRef::from_lit("alpha"), StringRef::from_lit("alph")));
CU_ASSERT(
!util::streq(StringRef::from_lit("alpha"), StringRef::from_lit("alphA")));
CU_ASSERT(util::streq_l("alpha", "alpha", 5));
CU_ASSERT(util::streq_l("alpha", "alphabravo", 5));