diff --git a/Redis/include/Poco/Redis/Client.h b/Redis/include/Poco/Redis/Client.h index c9e1eab98..c7f048899 100644 --- a/Redis/include/Poco/Redis/Client.h +++ b/Redis/include/Poco/Redis/Client.h @@ -115,6 +115,12 @@ public: void connect(const Net::SocketAddress& addrs, const Timespan& timeout); /// Connects to the given Redis server. + bool sendAuth(const std::string& password); + /// Sends password to Redis server + + bool isAuthenticated(); + /// Returns true when the client is authenticated + void disconnect(); /// Disconnects from the Redis server. @@ -198,6 +204,7 @@ private: Net::StreamSocket _socket; RedisInputStream* _input; RedisOutputStream* _output; + bool _authenticated; }; @@ -231,6 +238,10 @@ inline void Client::setReceiveTimeout(const Timespan& timeout) _socket.setReceiveTimeout(timeout); } +inline bool Client::isAuthenticated() +{ + return _authenticated; +} } } // namespace Poco::Redis diff --git a/Redis/include/Poco/Redis/PoolableConnectionFactory.h b/Redis/include/Poco/Redis/PoolableConnectionFactory.h index a2dfd0441..edad80994 100644 --- a/Redis/include/Poco/Redis/PoolableConnectionFactory.h +++ b/Redis/include/Poco/Redis/PoolableConnectionFactory.h @@ -35,23 +35,23 @@ class PoolableObjectFactory { public: PoolableObjectFactory(Net::SocketAddress& address): - _address(address) + _address(address) { } PoolableObjectFactory(const std::string& address): - _address(address) + _address(address) { } Redis::Client::Ptr createObject() { - return new Redis::Client(_address); + return new Redis::Client(_address); } bool validateObject(Redis::Client::Ptr pObject) { - return true; + return true; } void activateObject(Redis::Client::Ptr pObject) @@ -91,7 +91,10 @@ public: { try { - _pool.returnObject(_client); + if (_client) + { + _pool.returnObject(_client); + } } catch (...) { diff --git a/Redis/src/Client.cpp b/Redis/src/Client.cpp index a53a97b28..c4f8aab93 100644 --- a/Redis/src/Client.cpp +++ b/Redis/src/Client.cpp @@ -24,40 +24,44 @@ namespace Poco { namespace Redis { -Client::Client(): - _address(), - _socket(), - _input(0), - _output(0) +Client::Client(): + _address(), + _socket(), + _input(0), + _output(0), + _authenticated(false) { } Client::Client(const std::string& hostAndPort): - _address(hostAndPort), - _socket(), - _input(0), - _output(0) + _address(hostAndPort), + _socket(), + _input(0), + _output(0), + _authenticated(false) { connect(); } -Client::Client(const std::string& host, int port): - _address(host, port), - _socket(), - _input(0), - _output(0) +Client::Client(const std::string& host, int port): + _address(host, port), + _socket(), + _input(0), + _output(0), + _authenticated(false) { connect(); } -Client::Client(const Net::SocketAddress& addrs): - _address(addrs), - _socket(), - _input(0), - _output(0) +Client::Client(const Net::SocketAddress& addrs): + _address(addrs), + _socket(), + _input(0), + _output(0), + _authenticated(false) { connect(); } @@ -67,6 +71,7 @@ Client::~Client() { delete _input; delete _output; + _socket.close(); } @@ -76,7 +81,7 @@ void Client::connect() poco_assert(! _output); _socket.connect(_address); - _input = new RedisInputStream(_socket); + _input = new RedisInputStream(_socket); _output = new RedisOutputStream(_socket); } @@ -134,6 +139,26 @@ void Client::connect(const Net::SocketAddress& addrs, const Timespan& timeout) } +bool Client::sendAuth(const std::string& password) +{ + Array cmd; + cmd << "AUTH" << password; + + bool ret = true; + std::string response; + + try { + response = execute(cmd); + } catch (...) { + ret = false; + } + + _authenticated = (ret && (response == "OK")); + + return _authenticated; +} + + void Client::disconnect() { delete _input;