From ab93a700cefd4e5618c35f93ce5ead0e138cf1c9 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 20 Feb 2015 23:50:17 +0900 Subject: [PATCH] src: Announce h2 ALPN --- src/shrpx.cc | 2 +- src/template.h | 4 ++++ src/util.cc | 25 +++++++++++++++---------- src/util.h | 12 +++++++++--- src/util_test.cc | 5 +++-- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/shrpx.cc b/src/shrpx.cc index 6219683c..bbb9808e 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -636,7 +636,7 @@ bool conf_exists(const char *path) { } // namespace namespace { -const char *DEFAULT_NPN_LIST = "h2-16," NGHTTP2_PROTO_VERSION_ID "," +const char *DEFAULT_NPN_LIST = "h2,h2-16," NGHTTP2_PROTO_VERSION_ID "," #ifdef HAVE_SPDYLAY "spdy/3.1," #endif // HAVE_SPDYLAY diff --git a/src/template.h b/src/template.h index c1fea1d7..41295fc7 100644 --- a/src/template.h +++ b/src/template.h @@ -55,6 +55,10 @@ template constexpr size_t array_size(T (&)[N]) { return N; } +template constexpr size_t str_size(T (&)[N]) { + return N - 1; +} + // inspired by , but our // template can take functions returning other than void. template struct Defer { diff --git a/src/util.cc b/src/util.cc index 7a91783a..72301216 100644 --- a/src/util.cc +++ b/src/util.cc @@ -44,6 +44,7 @@ #include #include "timegm.h" +#include "template.h" namespace nghttp2 { @@ -756,9 +757,9 @@ int64_t to_time64(const timeval &tv) { } bool check_h2_is_selected(const unsigned char *proto, size_t len) { - return streq(NGHTTP2_PROTO_VERSION_ID, NGHTTP2_PROTO_VERSION_ID_LEN, proto, - len) || - streq(NGHTTP2_H2_16_ID, NGHTTP2_H2_16_ID_LEN, proto, len); + return streq_l(NGHTTP2_H2, proto, len) || + streq_l(NGHTTP2_H2_16, proto, len) || + streq_l(NGHTTP2_PROTO_VERSION_ID, proto, len); } namespace { @@ -778,19 +779,23 @@ 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) { - return select_h2(out, outlen, in, inlen, NGHTTP2_H2_16_ALPN, - NGHTTP2_H2_16_ALPN_LEN) || + return select_h2(out, outlen, in, inlen, NGHTTP2_H2_ALPN, + str_size(NGHTTP2_H2_ALPN)) || + select_h2(out, outlen, in, inlen, NGHTTP2_H2_16_ALPN, + str_size(NGHTTP2_H2_16_ALPN)) || select_h2(out, outlen, in, inlen, NGHTTP2_PROTO_ALPN, - NGHTTP2_PROTO_ALPN_LEN); + str_size(NGHTTP2_PROTO_ALPN)); } std::vector get_default_alpn() { - auto res = std::vector(NGHTTP2_PROTO_ALPN_LEN + - NGHTTP2_H2_16_ALPN_LEN); + auto res = std::vector(str_size(NGHTTP2_PROTO_ALPN) + + str_size(NGHTTP2_H2_16_ALPN) + + str_size(NGHTTP2_H2_ALPN)); auto p = std::begin(res); - p = std::copy_n(NGHTTP2_H2_16_ALPN, NGHTTP2_H2_16_ALPN_LEN, p); - p = std::copy_n(NGHTTP2_PROTO_ALPN, NGHTTP2_PROTO_ALPN_LEN, p); + p = std::copy_n(NGHTTP2_H2_ALPN, str_size(NGHTTP2_H2_ALPN), p); + p = std::copy_n(NGHTTP2_H2_16_ALPN, str_size(NGHTTP2_H2_16_ALPN), p); + p = std::copy_n(NGHTTP2_PROTO_ALPN, str_size(NGHTTP2_PROTO_ALPN), p); return res; } diff --git a/src/util.h b/src/util.h index ea0b3803..c396a7c3 100644 --- a/src/util.h +++ b/src/util.h @@ -48,9 +48,10 @@ namespace nghttp2 { // supports for our applications. This will be removed once HTTP/2 // specification is finalized. #define NGHTTP2_H2_16_ALPN "\x5h2-16" -#define NGHTTP2_H2_16_ALPN_LEN (sizeof(NGHTTP2_H2_16_ALPN) - 1) -#define NGHTTP2_H2_16_ID "h2-16" -#define NGHTTP2_H2_16_ID_LEN (sizeof(NGHTTP2_H2_16_ID) - 1) +#define NGHTTP2_H2_16 "h2-16" + +#define NGHTTP2_H2_ALPN "\x2h2" +#define NGHTTP2_H2 "h2" namespace util { @@ -322,6 +323,11 @@ bool streq(InputIt1 a, size_t alen, InputIt2 b, size_t blen) { return std::equal(a, a + alen, b); } +template +bool streq_l(const char (&a)[N], InputIt b, size_t blen) { + return streq(a, N - 1, b, blen); +} + bool strifind(const char *a, const char *b); // Lowercase |s| in place. diff --git a/src/util_test.cc b/src/util_test.cc index 3bc9ba15..9e6dc624 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -32,6 +32,7 @@ #include #include "util.h" +#include "template.h" using namespace nghttp2; @@ -197,8 +198,8 @@ 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_ID, out, NGHTTP2_H2_16_ID_LEN) == 0); - CU_ASSERT(NGHTTP2_H2_16_ID_LEN == outlen); + CU_ASSERT(memcmp(NGHTTP2_H2_16, out, str_size(NGHTTP2_H2_16)) == 0); + CU_ASSERT(str_size(NGHTTP2_H2_16) == outlen); } void test_util_ipv6_numeric_addr(void) {