Skip to content

Commit

Permalink
Merge pull request #12 from oatpp/better_error_messages
Browse files Browse the repository at this point in the history
Better error messages
  • Loading branch information
lganzzzo authored Sep 29, 2020
2 parents 8a7f0f4 + 0e6a04d commit cd2e9a5
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 68 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
## use these variables to configure module installation

set(OATPP_THIS_MODULE_NAME oatpp-libressl) ## name of the module (also name of folders in installation dirs)
set(OATPP_THIS_MODULE_VERSION "1.1.0") ## version of the module (also sufix of folders in installation dirs)
set(OATPP_THIS_MODULE_VERSION "1.2.0") ## version of the module (also sufix of folders in installation dirs)
set(OATPP_THIS_MODULE_LIBRARIES oatpp-libressl) ## list of libraries to find when find_package is called
set(OATPP_THIS_MODULE_TARGETS oatpp-libressl) ## list of targets to install
set(OATPP_THIS_MODULE_DIRECTORIES oatpp-libressl) ## list of directories to install
Expand Down
3 changes: 2 additions & 1 deletion src/oatpp-libressl/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
***************************************************************************/

#include "Connection.hpp"
#include <openssl/err.h>

namespace oatpp { namespace libressl {

Expand Down Expand Up @@ -65,7 +66,7 @@ void Connection::ConnectionContext::init() {
tlsObject->annul();

if (res != 0) {
OATPP_LOGD("[oatpp::libressl::Connection::ConnectionContext::init()]", "Error on call to 'tls_connect_cbs'. res=%d", res);
OATPP_LOGD("[oatpp::libressl::Connection::ConnectionContext::init()]", "Error on call to 'tls_connect_cbs'. %s", tls_error(m_connection->m_tlsHandle));
}

} else {
Expand Down
26 changes: 16 additions & 10 deletions src/oatpp-libressl/client/ConnectionProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "oatpp-libressl/Connection.hpp"

#include "oatpp/network/client/SimpleTCPConnectionProvider.hpp"
#include "oatpp/network/tcp/client/ConnectionProvider.hpp"

#include <openssl/crypto.h>

Expand All @@ -53,16 +53,22 @@ ConnectionProvider::ConnectionProvider(const std::shared_ptr<Config>& config,
}

std::shared_ptr<ConnectionProvider> ConnectionProvider::createShared(const std::shared_ptr<Config>& config,
const std::shared_ptr<oatpp::network::ClientConnectionProvider>& streamProvider) {
const std::shared_ptr<oatpp::network::ClientConnectionProvider>& streamProvider)
{
return std::shared_ptr<ConnectionProvider>(new ConnectionProvider(config, streamProvider));
}

std::shared_ptr<ConnectionProvider> ConnectionProvider::createShared(const std::shared_ptr<Config>& config, const oatpp::String& host, v_uint16 port) {
return createShared(config, oatpp::network::client::SimpleTCPConnectionProvider::createShared(host, port));
std::shared_ptr<ConnectionProvider> ConnectionProvider::createShared(const std::shared_ptr<Config>& config,
const network::Address& address)
{
return createShared(
config,
network::tcp::client::ConnectionProvider::createShared(address)
);
}


std::shared_ptr<oatpp::data::stream::IOStream> ConnectionProvider::getConnection(){
std::shared_ptr<data::stream::IOStream> ConnectionProvider::get() {

Connection::TLSHandle tlsHandle = tls_client();
tls_configure(tlsHandle, m_config->getTLSConfig());
Expand All @@ -74,7 +80,7 @@ std::shared_ptr<oatpp::data::stream::IOStream> ConnectionProvider::getConnection
}

auto tlsObject = std::make_shared<TLSObject>(tlsHandle, TLSObject::Type::CLIENT, host);
auto connection = std::make_shared<Connection>(tlsObject, m_streamProvider->getConnection());
auto connection = std::make_shared<Connection>(tlsObject, m_streamProvider->get());

connection->setOutputStreamIOMode(oatpp::data::stream::IOMode::BLOCKING);
connection->setInputStreamIOMode(oatpp::data::stream::IOMode::BLOCKING);
Expand All @@ -84,7 +90,7 @@ std::shared_ptr<oatpp::data::stream::IOStream> ConnectionProvider::getConnection

}

oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::data::stream::IOStream>&> ConnectionProvider::getConnectionAsync() {
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<data::stream::IOStream>&> ConnectionProvider::getAsync() {


class ConnectCoroutine : public oatpp::async::CoroutineWithResult<ConnectCoroutine, const std::shared_ptr<oatpp::data::stream::IOStream>&> {
Expand All @@ -103,7 +109,7 @@ oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::data::strea

Action act() override {
/* get transport stream */
return m_streamProvider->getConnectionAsync().callbackTo(&ConnectCoroutine::onConnected);
return m_streamProvider->getAsync().callbackTo(&ConnectCoroutine::onConnected);
}

Action onConnected(const std::shared_ptr<oatpp::data::stream::IOStream>& stream) {
Expand Down Expand Up @@ -144,7 +150,7 @@ oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::data::strea

}

void ConnectionProvider::invalidateConnection(const std::shared_ptr<IOStream>& connection) {
void ConnectionProvider::invalidate(const std::shared_ptr<data::stream::IOStream>& connection) {

auto c = std::static_pointer_cast<oatpp::libressl::Connection>(connection);

Expand All @@ -160,7 +166,7 @@ void ConnectionProvider::invalidateConnection(const std::shared_ptr<IOStream>& c

/* Invalidate underlying transport */
auto s = c->getTransportStream();
m_streamProvider->invalidateConnection(s);
m_streamProvider->invalidate(s);

}

Expand Down
20 changes: 11 additions & 9 deletions src/oatpp-libressl/client/ConnectionProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "oatpp-libressl/Config.hpp"
#include "oatpp-libressl/TLSObject.hpp"

#include "oatpp/network/Address.hpp"
#include "oatpp/network/ConnectionProvider.hpp"

namespace oatpp { namespace libressl { namespace client {
Expand Down Expand Up @@ -61,39 +62,40 @@ class ConnectionProvider : public base::Countable, public oatpp::network::Client
const std::shared_ptr<oatpp::network::ClientConnectionProvider>& streamProvider);

/**
* Create shared ConnectionProvider using &id:oatpp::network::client::SimpleTCPConnectionProvider;
* as a provider of underlying transport stream.
* Create shared ConnectionProvider.
* @param config - &id:oatpp::libressl::Config;.
* @param host - host.
* @param port - port.
* @param address - &id:oatpp::network::Address;.
* @param useExtendedConnections - set `true` to use &l:ConnectionProvider::ExtendedConnection;.
* `false` to use &id:oatpp::network::tcp::Connection;.
* @return - `std::shared_ptr` to ConnectionProvider.
*/
static std::shared_ptr<ConnectionProvider> createShared(const std::shared_ptr<Config>& config, const oatpp::String& host, v_uint16 port);
static std::shared_ptr<ConnectionProvider> createShared(const std::shared_ptr<Config>& config,
const network::Address& address);

/**
* Implements &id:oatpp::network::ConnectionProvider::close;. Here does nothing.
*/
void close() override {
void stop() override {
// DO NOTHING
}

/**
* Get connection.
* @return - `std::shared_ptr` to &id:oatpp::data::stream::IOStream;.
*/
std::shared_ptr<IOStream> getConnection() override;
std::shared_ptr<data::stream::IOStream> get() override;

/**
* Get connection in asynchronous manner.
* @return - &id:oatpp::async::CoroutineStarterForResult;.
*/
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::data::stream::IOStream>&> getConnectionAsync() override;
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::data::stream::IOStream>&> getAsync() override;

/**
* Will call `invalidateConnection()` for the underlying transport stream.
* @param connection - **MUST** be an instance of &id:oatpp::libressl::Connection;.
*/
void invalidateConnection(const std::shared_ptr<IOStream>& connection) override;
void invalidate(const std::shared_ptr<data::stream::IOStream>& connection) override;

};

Expand Down
29 changes: 18 additions & 11 deletions src/oatpp-libressl/server/ConnectionProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "oatpp-libressl/Connection.hpp"

#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
#include "oatpp/core/utils/ConversionUtils.hpp"

namespace oatpp { namespace libressl { namespace server {
Expand All @@ -46,16 +46,23 @@ ConnectionProvider::ConnectionProvider(const std::shared_ptr<Config>& config,
}

std::shared_ptr<ConnectionProvider> ConnectionProvider::createShared(const std::shared_ptr<Config>& config,
const std::shared_ptr<oatpp::network::ServerConnectionProvider>& streamProvider){
const std::shared_ptr<oatpp::network::ServerConnectionProvider>& streamProvider)
{
return std::shared_ptr<ConnectionProvider>(new ConnectionProvider(config, streamProvider));
}

std::shared_ptr<ConnectionProvider> ConnectionProvider::createShared(const std::shared_ptr<Config>& config, v_uint16 port) {
return createShared(config, oatpp::network::server::SimpleTCPConnectionProvider::createShared(port));
std::shared_ptr<ConnectionProvider> ConnectionProvider::createShared(const std::shared_ptr<Config>& config,
const network::Address& address,
bool useExtendedConnections)
{
return createShared(
config,
network::tcp::server::ConnectionProvider::createShared(address, useExtendedConnections)
);
}

ConnectionProvider::~ConnectionProvider() {
close();
stop();
}

std::shared_ptr<TLSObject> ConnectionProvider::instantiateTLSServer() {
Expand All @@ -75,25 +82,25 @@ std::shared_ptr<TLSObject> ConnectionProvider::instantiateTLSServer() {

}

void ConnectionProvider::close() {
void ConnectionProvider::stop() {
if(!m_closed) {
m_closed = true;
if(m_tlsObject) {
m_tlsObject->close();
}
m_streamProvider->close();
m_streamProvider->stop();
}
}

std::shared_ptr<oatpp::data::stream::IOStream> ConnectionProvider::getConnection(){
auto transportStream = m_streamProvider->getConnection();
std::shared_ptr<data::stream::IOStream> ConnectionProvider::get(){
auto transportStream = m_streamProvider->get();
if(transportStream) {
return std::make_shared<Connection>(m_tlsObject, transportStream);
}
return nullptr;
}

void ConnectionProvider::invalidateConnection(const std::shared_ptr<IOStream>& connection) {
void ConnectionProvider::invalidate(const std::shared_ptr<data::stream::IOStream>& connection) {

auto c = std::static_pointer_cast<oatpp::libressl::Connection>(connection);

Expand All @@ -109,7 +116,7 @@ void ConnectionProvider::invalidateConnection(const std::shared_ptr<IOStream>& c

/* Invalidate underlying transport */
auto s = c->getTransportStream();
m_streamProvider->invalidateConnection(s);
m_streamProvider->invalidate(s);

}

Expand Down
22 changes: 13 additions & 9 deletions src/oatpp-libressl/server/ConnectionProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
#include "oatpp-libressl/Config.hpp"
#include "oatpp-libressl/TLSObject.hpp"

#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
#include "oatpp/network/Address.hpp"
#include "oatpp/network/ConnectionProvider.hpp"

namespace oatpp { namespace libressl { namespace server {

Expand Down Expand Up @@ -64,13 +65,16 @@ class ConnectionProvider : public oatpp::base::Countable, public oatpp::network:
const std::shared_ptr<oatpp::network::ServerConnectionProvider>& streamProvider);

/**
* Create shared ConnectionProvider using &id:oatpp::network::server::SimpleTCPConnectionProvider;
* as a provider of underlying transport stream.
* Create shared ConnectionProvider.
* @param config - &id:oatpp::libressl::Config;.
* @param port - port to listen on.
* @param address - &id:oatpp::network::Address;.
* @param useExtendedConnections - set `true` to use &l:ConnectionProvider::ExtendedConnection;.
* `false` to use &id:oatpp::network::tcp::Connection;.
* @return - `std::shared_ptr` to ConnectionProvider.
*/
static std::shared_ptr<ConnectionProvider> createShared(const std::shared_ptr<Config>& config, v_uint16 port);
static std::shared_ptr<ConnectionProvider> createShared(const std::shared_ptr<Config>& config,
const network::Address& address,
bool useExtendedConnections = false);


/**
Expand All @@ -81,13 +85,13 @@ class ConnectionProvider : public oatpp::base::Countable, public oatpp::network:
/**
* Close all handles.
*/
void close() override;
void stop() override;

/**
* Get incoming connection.
* @return &id:oatpp::data::stream::IOStream;.
*/
std::shared_ptr<IOStream> getConnection() override;
std::shared_ptr<data::stream::IOStream> get() override;

/**
* No need to implement this.<br>
Expand All @@ -97,7 +101,7 @@ class ConnectionProvider : public oatpp::base::Countable, public oatpp::network:
* <br>
* *It may be implemented later*
*/
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::data::stream::IOStream>&> getConnectionAsync() override {
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<data::stream::IOStream>&> getAsync() override {
/*
* No need to implement this.
* For Asynchronous IO in oatpp it is considered to be a good practice
Expand All @@ -111,7 +115,7 @@ class ConnectionProvider : public oatpp::base::Countable, public oatpp::network:
* Will call `invalidateConnection()` for the underlying transport stream.
* @param connection - **MUST** be an instance of &id:oatpp::libressl::Connection;.
*/
void invalidateConnection(const std::shared_ptr<IOStream>& connection) override;
void invalidate(const std::shared_ptr<data::stream::IOStream>& connection) override;

};

Expand Down
14 changes: 7 additions & 7 deletions test/oatpp-libressl/FullAsyncClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

#include "oatpp/parser/json/mapping/ObjectMapper.hpp"

#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
#include "oatpp/network/client/SimpleTCPConnectionProvider.hpp"
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
#include "oatpp/network/tcp/client/ConnectionProvider.hpp"

#include "oatpp/network/virtual_/client/ConnectionProvider.hpp"
#include "oatpp/network/virtual_/server/ConnectionProvider.hpp"
Expand All @@ -57,10 +57,10 @@ typedef oatpp::web::protocol::http::incoming::Response IncomingResponse;

class TestComponent {
private:
v_int32 m_port;
v_uint16 m_port;
public:

TestComponent(v_int32 port)
TestComponent(v_uint16 port)
: m_port(port)
{}

Expand All @@ -80,7 +80,7 @@ class TestComponent {
OATPP_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, interface);
streamProvider = oatpp::network::virtual_::server::ConnectionProvider::createShared(interface);
} else {
streamProvider = oatpp::network::server::SimpleTCPConnectionProvider::createShared(m_port);
streamProvider = oatpp::network::tcp::server::ConnectionProvider::createShared({"localhost", m_port});
}

OATPP_LOGD("oatpp::libressl::Config", "pem='%s'", CERT_PEM_PATH);
Expand All @@ -95,7 +95,7 @@ class TestComponent {
return oatpp::web::server::HttpRouter::createShared();
}());

OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, serverConnectionHandler)([] {
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
OATPP_COMPONENT(std::shared_ptr<oatpp::async::Executor>, executor);
return oatpp::web::server::AsyncHttpConnectionHandler::createShared(router, executor);
Expand All @@ -113,7 +113,7 @@ class TestComponent {
OATPP_COMPONENT(std::shared_ptr<oatpp::network::virtual_::Interface>, interface);
streamProvider = oatpp::network::virtual_::client::ConnectionProvider::createShared(interface);
} else {
streamProvider = oatpp::network::client::SimpleTCPConnectionProvider::createShared("127.0.0.1", m_port);
streamProvider = oatpp::network::tcp::client::ConnectionProvider::createShared({"localhost", m_port});
}

auto config = oatpp::libressl::Config::createDefaultClientConfigShared();
Expand Down
4 changes: 2 additions & 2 deletions test/oatpp-libressl/FullAsyncClientTest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ namespace oatpp { namespace test { namespace libressl {

class FullAsyncClientTest : public UnitTest {
private:
v_int32 m_port;
v_uint16 m_port;
v_int32 m_connectionsPerEndpoint;
public:

FullAsyncClientTest(v_int32 port, v_int32 connectionsPerEndpoint)
FullAsyncClientTest(v_uint16 port, v_int32 connectionsPerEndpoint)
: UnitTest("TEST[web::FullAsyncClientTest]")
, m_port(port)
, m_connectionsPerEndpoint(connectionsPerEndpoint)
Expand Down
Loading

0 comments on commit cd2e9a5

Please sign in to comment.