/* NPN TLS extension client callback. We check that server advertised the HTTP/2 protocol the nghttp2 library supports. If not, exit the program. */ static int select_next_proto_cb(SSL* ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { if(nghttp2_select_next_protocol(out, outlen, in, inlen) <= 0) { errx(1, "Server did not advertise " NGHTTP2_PROTO_VERSION_ID); } return SSL_TLSEXT_ERR_OK; }
static void http2(void) { const unsigned char p[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '1', 5, 'h', '2', '-', '1', '0', 6, 's', 'p', 'd', 'y', '/', '3' }; unsigned char outlen; unsigned char* out; CU_ASSERT(1 == nghttp2_select_next_protocol(&out, &outlen, p, sizeof(p))); CU_ASSERT(NGHTTP2_PROTO_VERSION_ID_LEN == outlen); CU_ASSERT(memcmp(NGHTTP2_PROTO_VERSION_ID, out, outlen) == 0); }
static void no_overlap(void) { const unsigned char spdy[] = { 6, 's', 'p', 'd', 'y', '/', '4', 8, 's', 'p', 'd', 'y', '/', '2', '.', '1', 8, 'h', 't', 't', 'p', '/', '1', '.', '0', }; unsigned char outlen = 0; unsigned char* out = NULL; CU_ASSERT(-1 == nghttp2_select_next_protocol(&out, &outlen, spdy, sizeof(spdy))); CU_ASSERT(0 == outlen); CU_ASSERT(NULL == out); }
static void http11(void) { const unsigned char spdy[] = { 6, 's', 'p', 'd', 'y', '/', '4', 8, 's', 'p', 'd', 'y', '/', '2', '.', '1', 8, 'h', 't', 't', 'p', '/', '1', '.', '1', }; unsigned char outlen; unsigned char* out; CU_ASSERT(0 == nghttp2_select_next_protocol(&out, &outlen, spdy, sizeof(spdy))); CU_ASSERT(8 == outlen); CU_ASSERT(memcmp("http/1.1", out, outlen) == 0); }
static void http2(void) { const unsigned char p[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '1', 17, 'H', 'T', 'T', 'P', '-', 'd', 'r', 'a', 'f', 't', '-', '0', '4', '/', '2', '.', '0', 6, 's', 'p', 'd', 'y', '/', '3' }; unsigned char outlen; unsigned char* out; CU_ASSERT(1 == nghttp2_select_next_protocol(&out, &outlen, p, sizeof(p))); CU_ASSERT(17 == outlen); CU_ASSERT(memcmp("HTTP-draft-04/2.0", out, outlen) == 0); }
/* * Callback function for TLS NPN. Since this program only supports * HTTP/2 protocol, if server does not offer HTTP/2 the nghttp2 * library supports, we terminate program. */ static int select_next_proto_cb(SSL* ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { int rv; /* nghttp2_select_next_protocol() selects HTTP/2 protocol the nghttp2 library supports. */ rv = nghttp2_select_next_protocol(out, outlen, in, inlen); if(rv <= 0) { die("Server did not advertise HTTP/2 protocol"); } return SSL_TLSEXT_ERR_OK; }
static int alpn_select_proto_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { int rv; (void)ssl; (void)arg; rv = nghttp2_select_next_protocol((unsigned char **)out, outlen, in, inlen); if (rv != 1) { return SSL_TLSEXT_ERR_NOACK; } return SSL_TLSEXT_ERR_OK; }