From a46c815e4ee841c3a5acb78b8d56984d37c7dfe6 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 25 Mar 2016 01:41:06 +0900 Subject: [PATCH] src: StringRef-fy --- src/h2load.cc | 27 ++++++++++++++------------- src/shrpx_ssl.cc | 5 ++--- src/util.cc | 36 ++++++++++++++++-------------------- src/util.h | 15 +++++++++------ src/util_test.cc | 3 +-- 5 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/h2load.cc b/src/h2load.cc index 1791fdc1..10d2f88f 100644 --- a/src/h2load.cc +++ b/src/h2load.cc @@ -773,7 +773,7 @@ int Client::connection_made() { auto proto = StringRef{next_proto, next_proto_len}; if (util::check_h2_is_selected(proto)) { session = make_unique(this); - } else if (util::streq_l(NGHTTP2_H1_1, proto)) { + } else if (util::streq(NGHTTP2_H1_1, proto)) { session = make_unique(this); } #ifdef HAVE_SPDYLAY @@ -793,14 +793,12 @@ int Client::connection_made() { << std::endl; for (const auto &proto : config.npn_list) { - if (std::equal(NGHTTP2_H1_1_ALPN, - NGHTTP2_H1_1_ALPN + str_size(NGHTTP2_H1_1_ALPN), - proto.c_str())) { + if (util::streq(NGHTTP2_H1_1_ALPN, StringRef{proto})) { std::cout << "Server does not support NPN/ALPN. Falling back to HTTP/1.1." << std::endl; session = make_unique(this); - selected_proto = NGHTTP2_H1_1; + selected_proto = NGHTTP2_H1_1.str(); break; } } @@ -828,7 +826,7 @@ int Client::connection_made() { break; case Config::PROTO_HTTP1_1: session = make_unique(this); - selected_proto = NGHTTP2_H1_1; + selected_proto = NGHTTP2_H1_1.str(); break; #ifdef HAVE_SPDYLAY case Config::PROTO_SPDY2: @@ -1855,24 +1853,27 @@ int main(int argc, char **argv) { case 'i': config.ifile = optarg; break; - case 'p': - if (util::strieq(NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, optarg)) { + case 'p': { + auto proto = StringRef{optarg}; + if (util::strieq(StringRef::from_lit(NGHTTP2_CLEARTEXT_PROTO_VERSION_ID), + proto)) { config.no_tls_proto = Config::PROTO_HTTP2; - } else if (util::strieq(NGHTTP2_H1_1, optarg)) { + } else if (util::strieq(NGHTTP2_H1_1, proto)) { config.no_tls_proto = Config::PROTO_HTTP1_1; #ifdef HAVE_SPDYLAY - } else if (util::strieq("spdy/2", optarg)) { + } else if (util::strieq_l("spdy/2", proto)) { config.no_tls_proto = Config::PROTO_SPDY2; - } else if (util::strieq("spdy/3", optarg)) { + } else if (util::strieq_l("spdy/3", proto)) { config.no_tls_proto = Config::PROTO_SPDY3; - } else if (util::strieq("spdy/3.1", optarg)) { + } else if (util::strieq_l("spdy/3.1", proto)) { config.no_tls_proto = Config::PROTO_SPDY3_1; #endif // HAVE_SPDYLAY } else { - std::cerr << "-p: unsupported protocol " << optarg << std::endl; + std::cerr << "-p: unsupported protocol " << proto << std::endl; exit(EXIT_FAILURE); } break; + } case 'r': config.rate = strtoul(optarg, nullptr, 10); if (config.rate == 0) { diff --git a/src/shrpx_ssl.cc b/src/shrpx_ssl.cc index 164057af..426d9d2b 100644 --- a/src/shrpx_ssl.cc +++ b/src/shrpx_ssl.cc @@ -648,7 +648,7 @@ int select_h1_next_proto_cb(SSL *ssl, unsigned char **out, unsigned int inlen, void *arg) { auto end = in + inlen; for (; in < end;) { - if (util::streq_l(NGHTTP2_H1_1_ALPN, in, in[0] + 1)) { + if (util::streq(NGHTTP2_H1_1_ALPN, StringRef{in, in + (in[0] + 1)})) { *out = const_cast(in) + 1; *outlen = in[0]; return SSL_TLSEXT_ERR_OK; @@ -1362,8 +1362,7 @@ void setup_downstream_http2_alpn(SSL *ssl) { void setup_downstream_http1_alpn(SSL *ssl) { #if OPENSSL_VERSION_NUMBER >= 0x10002000L // ALPN advertisement - auto alpn = StringRef::from_lit(NGHTTP2_H1_1_ALPN); - SSL_set_alpn_protos(ssl, alpn.byte(), alpn.size()); + SSL_set_alpn_protos(ssl, NGHTTP2_H1_1_ALPN.byte(), NGHTTP2_H1_1_ALPN.size()); #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L } diff --git a/src/util.cc b/src/util.cc index e22d5f47..6595c8d6 100644 --- a/src/util.cc +++ b/src/util.cc @@ -748,16 +748,16 @@ int64_t to_time64(const timeval &tv) { } 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); + return streq(NGHTTP2_H2, proto) || streq(NGHTTP2_H2_16, proto) || + streq(NGHTTP2_H2_14, proto); } namespace { bool select_proto(const unsigned char **out, unsigned char *outlen, - const unsigned char *in, unsigned int inlen, const char *key, - unsigned int keylen) { - for (auto p = in, end = in + inlen; p + keylen <= end; p += *p + 1) { - if (std::equal(key, key + keylen, p)) { + const unsigned char *in, unsigned int inlen, + const StringRef &key) { + for (auto p = in, end = in + inlen; p + key.size() <= end; p += *p + 1) { + if (std::equal(std::begin(key), std::end(key), p)) { *out = p + 1; *outlen = *p; return true; @@ -769,20 +769,16 @@ bool select_proto(const unsigned char **out, unsigned char *outlen, bool select_h2(const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen) { - return select_proto(out, outlen, in, inlen, NGHTTP2_PROTO_ALPN, - str_size(NGHTTP2_PROTO_ALPN)) || - select_proto(out, outlen, in, inlen, NGHTTP2_H2_16_ALPN, - str_size(NGHTTP2_H2_16_ALPN)) || - select_proto(out, outlen, in, inlen, NGHTTP2_H2_14_ALPN, - str_size(NGHTTP2_H2_14_ALPN)); + return select_proto(out, outlen, in, inlen, NGHTTP2_H2_ALPN) || + select_proto(out, outlen, in, inlen, NGHTTP2_H2_16_ALPN) || + select_proto(out, outlen, in, inlen, NGHTTP2_H2_14_ALPN); } bool select_protocol(const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, std::vector proto_list) { for (const auto &proto : proto_list) { - if (select_proto(out, outlen, in, inlen, proto.c_str(), - static_cast(proto.size()))) { + if (select_proto(out, outlen, in, inlen, StringRef{proto})) { return true; } } @@ -791,14 +787,14 @@ bool select_protocol(const unsigned char **out, unsigned char *outlen, } std::vector get_default_alpn() { - auto res = std::vector(str_size(NGHTTP2_PROTO_ALPN) + - str_size(NGHTTP2_H2_16_ALPN) + - str_size(NGHTTP2_H2_14_ALPN)); + auto res = std::vector(NGHTTP2_H2_ALPN.size() + + NGHTTP2_H2_16_ALPN.size() + + NGHTTP2_H2_14_ALPN.size()); auto p = std::begin(res); - p = std::copy_n(NGHTTP2_PROTO_ALPN, str_size(NGHTTP2_PROTO_ALPN), p); - p = std::copy_n(NGHTTP2_H2_16_ALPN, str_size(NGHTTP2_H2_16_ALPN), p); - p = std::copy_n(NGHTTP2_H2_14_ALPN, str_size(NGHTTP2_H2_14_ALPN), p); + p = std::copy_n(std::begin(NGHTTP2_H2_ALPN), NGHTTP2_H2_ALPN.size(), p); + p = std::copy_n(std::begin(NGHTTP2_H2_16_ALPN), NGHTTP2_H2_16_ALPN.size(), p); + p = std::copy_n(std::begin(NGHTTP2_H2_14_ALPN), NGHTTP2_H2_14_ALPN.size(), p); return res; } diff --git a/src/util.h b/src/util.h index 52e25aae..48b732b1 100644 --- a/src/util.h +++ b/src/util.h @@ -55,17 +55,20 @@ namespace nghttp2 { +constexpr auto NGHTTP2_H2_ALPN = StringRef::from_lit("\x2h2"); +constexpr auto NGHTTP2_H2 = StringRef::from_lit("h2"); + // The additional HTTP/2 protocol ALPN protocol identifier we also // supports for our applications to make smooth migration into final // h2 ALPN ID. -constexpr char NGHTTP2_H2_16_ALPN[] = "\x5h2-16"; -constexpr char NGHTTP2_H2_16[] = "h2-16"; +constexpr auto NGHTTP2_H2_16_ALPN = StringRef::from_lit("\x5h2-16"); +constexpr auto NGHTTP2_H2_16 = StringRef::from_lit("h2-16"); -constexpr char NGHTTP2_H2_14_ALPN[] = "\x5h2-14"; -constexpr char NGHTTP2_H2_14[] = "h2-14"; +constexpr auto NGHTTP2_H2_14_ALPN = StringRef::from_lit("\x5h2-14"); +constexpr auto NGHTTP2_H2_14 = StringRef::from_lit("h2-14"); -constexpr char NGHTTP2_H1_1_ALPN[] = "\x8http/1.1"; -constexpr char NGHTTP2_H1_1[] = "http/1.1"; +constexpr auto NGHTTP2_H1_1_ALPN = StringRef::from_lit("\x8http/1.1"); +constexpr auto NGHTTP2_H1_1 = StringRef::from_lit("http/1.1"); namespace util { diff --git a/src/util_test.cc b/src/util_test.cc index d1ee525b..25259658 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -230,8 +230,7 @@ void test_util_select_h2(void) { // picked up because it has precedence over the other. const unsigned char t6[] = "\x5h2-14\x5h2-16"; CU_ASSERT(util::select_h2(&out, &outlen, t6, sizeof(t6) - 1)); - CU_ASSERT(memcmp(NGHTTP2_H2_16, out, str_size(NGHTTP2_H2_16)) == 0); - CU_ASSERT(str_size(NGHTTP2_H2_16) == outlen); + CU_ASSERT(util::streq(NGHTTP2_H2_16, StringRef{out, outlen})); } void test_util_ipv6_numeric_addr(void) {