mirror of
https://github.com/nghttp2/nghttp2.git
synced 2025-12-07 02:28:53 +08:00
src: StringRef-fy
This commit is contained in:
@@ -773,7 +773,7 @@ int Client::connection_made() {
|
|||||||
auto proto = StringRef{next_proto, next_proto_len};
|
auto proto = StringRef{next_proto, next_proto_len};
|
||||||
if (util::check_h2_is_selected(proto)) {
|
if (util::check_h2_is_selected(proto)) {
|
||||||
session = make_unique<Http2Session>(this);
|
session = make_unique<Http2Session>(this);
|
||||||
} else if (util::streq_l(NGHTTP2_H1_1, proto)) {
|
} else if (util::streq(NGHTTP2_H1_1, proto)) {
|
||||||
session = make_unique<Http1Session>(this);
|
session = make_unique<Http1Session>(this);
|
||||||
}
|
}
|
||||||
#ifdef HAVE_SPDYLAY
|
#ifdef HAVE_SPDYLAY
|
||||||
@@ -793,14 +793,12 @@ int Client::connection_made() {
|
|||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
for (const auto &proto : config.npn_list) {
|
for (const auto &proto : config.npn_list) {
|
||||||
if (std::equal(NGHTTP2_H1_1_ALPN,
|
if (util::streq(NGHTTP2_H1_1_ALPN, StringRef{proto})) {
|
||||||
NGHTTP2_H1_1_ALPN + str_size(NGHTTP2_H1_1_ALPN),
|
|
||||||
proto.c_str())) {
|
|
||||||
std::cout
|
std::cout
|
||||||
<< "Server does not support NPN/ALPN. Falling back to HTTP/1.1."
|
<< "Server does not support NPN/ALPN. Falling back to HTTP/1.1."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
session = make_unique<Http1Session>(this);
|
session = make_unique<Http1Session>(this);
|
||||||
selected_proto = NGHTTP2_H1_1;
|
selected_proto = NGHTTP2_H1_1.str();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -828,7 +826,7 @@ int Client::connection_made() {
|
|||||||
break;
|
break;
|
||||||
case Config::PROTO_HTTP1_1:
|
case Config::PROTO_HTTP1_1:
|
||||||
session = make_unique<Http1Session>(this);
|
session = make_unique<Http1Session>(this);
|
||||||
selected_proto = NGHTTP2_H1_1;
|
selected_proto = NGHTTP2_H1_1.str();
|
||||||
break;
|
break;
|
||||||
#ifdef HAVE_SPDYLAY
|
#ifdef HAVE_SPDYLAY
|
||||||
case Config::PROTO_SPDY2:
|
case Config::PROTO_SPDY2:
|
||||||
@@ -1855,24 +1853,27 @@ int main(int argc, char **argv) {
|
|||||||
case 'i':
|
case 'i':
|
||||||
config.ifile = optarg;
|
config.ifile = optarg;
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p': {
|
||||||
if (util::strieq(NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, optarg)) {
|
auto proto = StringRef{optarg};
|
||||||
|
if (util::strieq(StringRef::from_lit(NGHTTP2_CLEARTEXT_PROTO_VERSION_ID),
|
||||||
|
proto)) {
|
||||||
config.no_tls_proto = Config::PROTO_HTTP2;
|
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;
|
config.no_tls_proto = Config::PROTO_HTTP1_1;
|
||||||
#ifdef HAVE_SPDYLAY
|
#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;
|
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;
|
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;
|
config.no_tls_proto = Config::PROTO_SPDY3_1;
|
||||||
#endif // HAVE_SPDYLAY
|
#endif // HAVE_SPDYLAY
|
||||||
} else {
|
} else {
|
||||||
std::cerr << "-p: unsupported protocol " << optarg << std::endl;
|
std::cerr << "-p: unsupported protocol " << proto << std::endl;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case 'r':
|
case 'r':
|
||||||
config.rate = strtoul(optarg, nullptr, 10);
|
config.rate = strtoul(optarg, nullptr, 10);
|
||||||
if (config.rate == 0) {
|
if (config.rate == 0) {
|
||||||
|
|||||||
@@ -648,7 +648,7 @@ int select_h1_next_proto_cb(SSL *ssl, unsigned char **out,
|
|||||||
unsigned int inlen, void *arg) {
|
unsigned int inlen, void *arg) {
|
||||||
auto end = in + inlen;
|
auto end = in + inlen;
|
||||||
for (; in < end;) {
|
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<unsigned char *>(in) + 1;
|
*out = const_cast<unsigned char *>(in) + 1;
|
||||||
*outlen = in[0];
|
*outlen = in[0];
|
||||||
return SSL_TLSEXT_ERR_OK;
|
return SSL_TLSEXT_ERR_OK;
|
||||||
@@ -1362,8 +1362,7 @@ void setup_downstream_http2_alpn(SSL *ssl) {
|
|||||||
void setup_downstream_http1_alpn(SSL *ssl) {
|
void setup_downstream_http1_alpn(SSL *ssl) {
|
||||||
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
||||||
// ALPN advertisement
|
// ALPN advertisement
|
||||||
auto alpn = StringRef::from_lit(NGHTTP2_H1_1_ALPN);
|
SSL_set_alpn_protos(ssl, NGHTTP2_H1_1_ALPN.byte(), NGHTTP2_H1_1_ALPN.size());
|
||||||
SSL_set_alpn_protos(ssl, alpn.byte(), alpn.size());
|
|
||||||
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
36
src/util.cc
36
src/util.cc
@@ -748,16 +748,16 @@ int64_t to_time64(const timeval &tv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool check_h2_is_selected(const StringRef &proto) {
|
bool check_h2_is_selected(const StringRef &proto) {
|
||||||
return streq_l(NGHTTP2_PROTO_VERSION_ID, proto) ||
|
return streq(NGHTTP2_H2, proto) || streq(NGHTTP2_H2_16, proto) ||
|
||||||
streq_l(NGHTTP2_H2_16, proto) || streq_l(NGHTTP2_H2_14, proto);
|
streq(NGHTTP2_H2_14, proto);
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
bool select_proto(const unsigned char **out, unsigned char *outlen,
|
bool select_proto(const unsigned char **out, unsigned char *outlen,
|
||||||
const unsigned char *in, unsigned int inlen, const char *key,
|
const unsigned char *in, unsigned int inlen,
|
||||||
unsigned int keylen) {
|
const StringRef &key) {
|
||||||
for (auto p = in, end = in + inlen; p + keylen <= end; p += *p + 1) {
|
for (auto p = in, end = in + inlen; p + key.size() <= end; p += *p + 1) {
|
||||||
if (std::equal(key, key + keylen, p)) {
|
if (std::equal(std::begin(key), std::end(key), p)) {
|
||||||
*out = p + 1;
|
*out = p + 1;
|
||||||
*outlen = *p;
|
*outlen = *p;
|
||||||
return true;
|
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,
|
bool select_h2(const unsigned char **out, unsigned char *outlen,
|
||||||
const unsigned char *in, unsigned int inlen) {
|
const unsigned char *in, unsigned int inlen) {
|
||||||
return select_proto(out, outlen, in, inlen, NGHTTP2_PROTO_ALPN,
|
return select_proto(out, outlen, in, inlen, NGHTTP2_H2_ALPN) ||
|
||||||
str_size(NGHTTP2_PROTO_ALPN)) ||
|
select_proto(out, outlen, in, inlen, NGHTTP2_H2_16_ALPN) ||
|
||||||
select_proto(out, outlen, in, inlen, NGHTTP2_H2_16_ALPN,
|
select_proto(out, outlen, in, inlen, NGHTTP2_H2_14_ALPN);
|
||||||
str_size(NGHTTP2_H2_16_ALPN)) ||
|
|
||||||
select_proto(out, outlen, in, inlen, NGHTTP2_H2_14_ALPN,
|
|
||||||
str_size(NGHTTP2_H2_14_ALPN));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool select_protocol(const unsigned char **out, unsigned char *outlen,
|
bool select_protocol(const unsigned char **out, unsigned char *outlen,
|
||||||
const unsigned char *in, unsigned int inlen,
|
const unsigned char *in, unsigned int inlen,
|
||||||
std::vector<std::string> proto_list) {
|
std::vector<std::string> proto_list) {
|
||||||
for (const auto &proto : proto_list) {
|
for (const auto &proto : proto_list) {
|
||||||
if (select_proto(out, outlen, in, inlen, proto.c_str(),
|
if (select_proto(out, outlen, in, inlen, StringRef{proto})) {
|
||||||
static_cast<unsigned int>(proto.size()))) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -791,14 +787,14 @@ bool select_protocol(const unsigned char **out, unsigned char *outlen,
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> get_default_alpn() {
|
std::vector<unsigned char> get_default_alpn() {
|
||||||
auto res = std::vector<unsigned char>(str_size(NGHTTP2_PROTO_ALPN) +
|
auto res = std::vector<unsigned char>(NGHTTP2_H2_ALPN.size() +
|
||||||
str_size(NGHTTP2_H2_16_ALPN) +
|
NGHTTP2_H2_16_ALPN.size() +
|
||||||
str_size(NGHTTP2_H2_14_ALPN));
|
NGHTTP2_H2_14_ALPN.size());
|
||||||
auto p = std::begin(res);
|
auto p = std::begin(res);
|
||||||
|
|
||||||
p = std::copy_n(NGHTTP2_PROTO_ALPN, str_size(NGHTTP2_PROTO_ALPN), p);
|
p = std::copy_n(std::begin(NGHTTP2_H2_ALPN), NGHTTP2_H2_ALPN.size(), p);
|
||||||
p = std::copy_n(NGHTTP2_H2_16_ALPN, str_size(NGHTTP2_H2_16_ALPN), p);
|
p = std::copy_n(std::begin(NGHTTP2_H2_16_ALPN), NGHTTP2_H2_16_ALPN.size(), p);
|
||||||
p = std::copy_n(NGHTTP2_H2_14_ALPN, str_size(NGHTTP2_H2_14_ALPN), p);
|
p = std::copy_n(std::begin(NGHTTP2_H2_14_ALPN), NGHTTP2_H2_14_ALPN.size(), p);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
15
src/util.h
15
src/util.h
@@ -55,17 +55,20 @@
|
|||||||
|
|
||||||
namespace nghttp2 {
|
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
|
// The additional HTTP/2 protocol ALPN protocol identifier we also
|
||||||
// supports for our applications to make smooth migration into final
|
// supports for our applications to make smooth migration into final
|
||||||
// h2 ALPN ID.
|
// h2 ALPN ID.
|
||||||
constexpr char NGHTTP2_H2_16_ALPN[] = "\x5h2-16";
|
constexpr auto NGHTTP2_H2_16_ALPN = StringRef::from_lit("\x5h2-16");
|
||||||
constexpr char NGHTTP2_H2_16[] = "h2-16";
|
constexpr auto NGHTTP2_H2_16 = StringRef::from_lit("h2-16");
|
||||||
|
|
||||||
constexpr char NGHTTP2_H2_14_ALPN[] = "\x5h2-14";
|
constexpr auto NGHTTP2_H2_14_ALPN = StringRef::from_lit("\x5h2-14");
|
||||||
constexpr char NGHTTP2_H2_14[] = "h2-14";
|
constexpr auto NGHTTP2_H2_14 = StringRef::from_lit("h2-14");
|
||||||
|
|
||||||
constexpr char NGHTTP2_H1_1_ALPN[] = "\x8http/1.1";
|
constexpr auto NGHTTP2_H1_1_ALPN = StringRef::from_lit("\x8http/1.1");
|
||||||
constexpr char NGHTTP2_H1_1[] = "http/1.1";
|
constexpr auto NGHTTP2_H1_1 = StringRef::from_lit("http/1.1");
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
|
|
||||||
|
|||||||
@@ -230,8 +230,7 @@ void test_util_select_h2(void) {
|
|||||||
// picked up because it has precedence over the other.
|
// picked up because it has precedence over the other.
|
||||||
const unsigned char t6[] = "\x5h2-14\x5h2-16";
|
const unsigned char t6[] = "\x5h2-14\x5h2-16";
|
||||||
CU_ASSERT(util::select_h2(&out, &outlen, t6, sizeof(t6) - 1));
|
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(util::streq(NGHTTP2_H2_16, StringRef{out, outlen}));
|
||||||
CU_ASSERT(str_size(NGHTTP2_H2_16) == outlen);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_util_ipv6_numeric_addr(void) {
|
void test_util_ipv6_numeric_addr(void) {
|
||||||
|
|||||||
Reference in New Issue
Block a user