Skip to content

Commit

Permalink
src: StringRef-fy
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuhiro-t committed Mar 24, 2016
1 parent 186d440 commit a46c815
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 44 deletions.
27 changes: 14 additions & 13 deletions src/h2load.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Http2Session>(this);
} else if (util::streq_l(NGHTTP2_H1_1, proto)) {
} else if (util::streq(NGHTTP2_H1_1, proto)) {
session = make_unique<Http1Session>(this);
}
#ifdef HAVE_SPDYLAY
Expand All @@ -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<Http1Session>(this);
selected_proto = NGHTTP2_H1_1;
selected_proto = NGHTTP2_H1_1.str();
break;
}
}
Expand Down Expand Up @@ -828,7 +826,7 @@ int Client::connection_made() {
break;
case Config::PROTO_HTTP1_1:
session = make_unique<Http1Session>(this);
selected_proto = NGHTTP2_H1_1;
selected_proto = NGHTTP2_H1_1.str();
break;
#ifdef HAVE_SPDYLAY
case Config::PROTO_SPDY2:
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions src/shrpx_ssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char *>(in) + 1;
*outlen = in[0];
return SSL_TLSEXT_ERR_OK;
Expand Down Expand Up @@ -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
}

Expand Down
36 changes: 16 additions & 20 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<std::string> proto_list) {
for (const auto &proto : proto_list) {
if (select_proto(out, outlen, in, inlen, proto.c_str(),
static_cast<unsigned int>(proto.size()))) {
if (select_proto(out, outlen, in, inlen, StringRef{proto})) {
return true;
}
}
Expand All @@ -791,14 +787,14 @@ bool select_protocol(const unsigned char **out, unsigned char *outlen,
}

std::vector<unsigned char> get_default_alpn() {
auto res = std::vector<unsigned char>(str_size(NGHTTP2_PROTO_ALPN) +
str_size(NGHTTP2_H2_16_ALPN) +
str_size(NGHTTP2_H2_14_ALPN));
auto res = std::vector<unsigned char>(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;
}
Expand Down
15 changes: 9 additions & 6 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
3 changes: 1 addition & 2 deletions src/util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a46c815

Please sign in to comment.