I'm having trouble to get the protocol (HTTP or HTTPS) of a request in boost beast webserver.
I can get the host using:
req[http::field::host]
but no such luck with the protocol.
req[http::field::protocol]
is empty.
I'm having trouble to get the protocol (HTTP or HTTPS) of a request in boost beast webserver.
I can get the host using:
req[http::field::host]
but no such luck with the protocol.
req[http::field::protocol]
is empty.
There is no http::field for HTTPS.
For using HTTPS basically you are doing following (example):
1.Create io_context and ssl_context
boost::asio::io_context ioc;
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12_client)
2.create ssl stream from io_context and ssl::context
boost::beast::ssl_stream<boost::beast::tcp_stream> stream(ioc, ctx)
3.create tcp connection using lowest_layer
beast::get_lowest_layer(stream).connect(...endpoints iterator from query resolver..)
4.do the ssl handshake
stream.handshake(ssl::stream_base::client);
5.now you can make requests writing to stream as usual:
http::request<http::string_body> req{http::verb::get, target, version};
req.set(http::field::host, host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
// Send the HTTP request to the remote host
http::write(stream, req);
Check the official boost beast examples.
Considering the fact that you are behind a nginx server, Beast cannot solve your problem. But the additional headers that nginx will send to your server will.
You may rely on the HTTP_X_FORWARDED_PROTO or HTTP_X_FORWARDED_SSL headers. Here's what I came up with by following an answer to a similar question:
using namespace boost::beast;
using namespace std;
string find_scheme(const http::request<http::string_body>& request)
{
http::request<http::string_body>::const_iterator header;
header = request.find("HTTP_X_FORWARDED_PROTO");
if (header != request.end())
return header->value().data();
else
{
header = request.find("HTTP_X_FORWARDED_SSL");
if (header != request.end())
return header->value() == "on" ? "https" : "http";
}
return "http";
}