Skip to content

Commit

Permalink
fix(HTTPSClientSession): There is no way to resolve host in advance a…
Browse files Browse the repository at this point in the history
…nd connect to HTTPS server with SNI. #4395 (#4751)
  • Loading branch information
aleks-f authored Nov 5, 2024
1 parent ca63bf0 commit c156f0b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
4 changes: 3 additions & 1 deletion NetSSL_OpenSSL/include/Poco/Net/HTTPSClientSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ class NetSSL_API HTTPSClientSession: public HTTPClientSession
HTTPSClientSession();
/// Creates an unconnected HTTPSClientSession.

explicit HTTPSClientSession(const SecureStreamSocket& socket);
explicit HTTPSClientSession(const SecureStreamSocket& socket, const std::string& host, Poco::UInt16 port = HTTPS_PORT);
/// Creates a HTTPSClientSession using the given socket.
/// The socket must not be connected. The session
/// takes ownership of the socket.
///
/// The given host name is used for certificate verification.

HTTPSClientSession(const SecureStreamSocket& socket, Session::Ptr pSession);
/// Creates a HTTPSClientSession using the given socket.
Expand Down
12 changes: 12 additions & 0 deletions NetSSL_OpenSSL/include/Poco/Net/SecureStreamSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,25 @@ class NetSSL_API SecureStreamSocket: public StreamSocket
///
/// The given host name is used for certificate verification.

SecureStreamSocket(const std::string& hostName);
/// Creates a secure stream socket using the default
/// client SSL context. The created socket is not connected.
///
/// The given host name is used for certificate verification.

SecureStreamSocket(const SocketAddress& address, const std::string& hostName, Context::Ptr pContext);
/// Creates a secure stream socket using the given
/// client SSL context and connects it to
/// the socket specified by address.
///
/// The given host name is used for certificate verification.

SecureStreamSocket(const std::string& hostName, Context::Ptr pContext);
/// Creates a secure stream socket using the given
/// client SSL context. The created socket is not connected.
///
/// The given host name is used for certificate verification.

SecureStreamSocket(const SocketAddress& address, const std::string& hostName, Context::Ptr pContext, Session::Ptr pSession);
/// Creates a secure stream socket using the given
/// client SSL context and connects it to
Expand Down
5 changes: 3 additions & 2 deletions NetSSL_OpenSSL/src/HTTPSClientSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ HTTPSClientSession::HTTPSClientSession():
}


HTTPSClientSession::HTTPSClientSession(const SecureStreamSocket& socket):
HTTPSClientSession::HTTPSClientSession(const SecureStreamSocket& socket, const std::string& host, Poco::UInt16 port):
HTTPClientSession(socket),
_pContext(socket.context())
{
setPort(HTTPS_PORT);
setHost(host);
setPort(port);
}


Expand Down
14 changes: 14 additions & 0 deletions NetSSL_OpenSSL/src/SecureStreamSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ SecureStreamSocket::SecureStreamSocket(const SocketAddress& address, const std::
}


SecureStreamSocket::SecureStreamSocket(const std::string& hostName):
StreamSocket(new SecureStreamSocketImpl(SSLManager::instance().defaultClientContext()))
{
static_cast<SecureStreamSocketImpl*>(impl())->setPeerHostName(hostName);
}


SecureStreamSocket::SecureStreamSocket(const SocketAddress& address, Context::Ptr pContext):
StreamSocket(new SecureStreamSocketImpl(pContext))
{
Expand All @@ -83,6 +90,13 @@ SecureStreamSocket::SecureStreamSocket(const SocketAddress& address, const std::
}


SecureStreamSocket::SecureStreamSocket(const std::string& hostName, Context::Ptr pContext):
StreamSocket(new SecureStreamSocketImpl(pContext))
{
static_cast<SecureStreamSocketImpl*>(impl())->setPeerHostName(hostName);
}


SecureStreamSocket::SecureStreamSocket(const SocketAddress& address, const std::string& hostName, Context::Ptr pContext, Session::Ptr pSession):
StreamSocket(new SecureStreamSocketImpl(pContext))
{
Expand Down
21 changes: 20 additions & 1 deletion NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ HTTPSClientSessionTest::~HTTPSClientSessionTest()
}


void HTTPSClientSessionTest::testFromSocket()
{
HTTPSTestServer srv;
SecureStreamSocket sss("localhost");
HTTPSClientSession s(sss, "127.0.0.1", srv.port());
HTTPRequest request(HTTPRequest::HTTP_GET, "/small");
s.sendRequest(request);
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
assertTrue (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
assertTrue (response.getContentType() == "text/plain");
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
assertTrue (ostr.str() == HTTPSTestServer::SMALL_BODY);
}


void HTTPSClientSessionTest::testGetSmall()
{
HTTPSTestServer srv;
Expand Down Expand Up @@ -458,6 +475,7 @@ void HTTPSClientSessionTest::testUnknownContentLength()
assertTrue (ostr.str() == HTTPSTestServer::SMALL_BODY);
}


void HTTPSClientSessionTest::testServerAbort()
{
HTTPSTestServer srv;
Expand All @@ -471,7 +489,7 @@ void HTTPSClientSessionTest::testServerAbort()
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
assertTrue (ostr.str() == HTTPSTestServer::SMALL_BODY);
assertTrue ( dynamic_cast<const Poco::Net::SSLConnectionUnexpectedlyClosedException*>(
assertTrue (dynamic_cast<const Poco::Net::SSLConnectionUnexpectedlyClosedException*>(
s.networkException()) != NULL );
}

Expand All @@ -490,6 +508,7 @@ CppUnit::Test* HTTPSClientSessionTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSClientSessionTest");

CppUnit_addTest(pSuite, HTTPSClientSessionTest, testFromSocket);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testGetSmall);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testGetLarge);
CppUnit_addTest(pSuite, HTTPSClientSessionTest, testHead);
Expand Down
1 change: 1 addition & 0 deletions NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class HTTPSClientSessionTest: public CppUnit::TestCase
HTTPSClientSessionTest(const std::string& name);
~HTTPSClientSessionTest();

void testFromSocket();
void testGetSmall();
void testGetLarge();
void testHead();
Expand Down

0 comments on commit c156f0b

Please sign in to comment.