Search code examples
c++sslnetwork-programmingboost-asionghttp2

tls: no application protocol negotiated


First time to using the libnghttp2-asio library despite it being deprecated.

I'm experimenting how I think would give the reddit webpage as follows:

boost::system::error_code ec;
namespace net = nghttp2::asio_http2;
namespace ssl = boost::asio::ssl;
using boost::asio::ip::tcp;
boost::asio::io_context ioc;
ssl::context tls(ssl::context::tlsv12_client);
tls.set_options(ssl::context::default_workarounds);
// tls.set_verify_mode(boost::asio::ssl::verify_peer);
tls.set_verify_mode(ssl::verify_none);
tls.set_default_verify_paths();
net::client::session session(ioc, tls, "www.reddit.com", "443");
session.on_connect([&session](tcp::resolver::iterator endpoint_it) {
    std::cout << "Connected" << std::endl;
});
session.on_error([](const boost::system::error_code &ec) {
    std::cout << "error: " << ec.message() << std::endl;
});
ioc.run();

Output:

error: tls: no application protocol negotiated

I would like to know how I can connect successfully, perhaps I'm missing the handshake procedure? If so, how should I do it?


Solution

  • nghttp2 doesn't do ALPN by default. Solved by

    SSL_CTX_set_alpn_protos(tls.native_handle(), (const unsigned char *)"\x02h2", 3);