mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-30 13:47:10 +01:00
MongoDB: add connection string URI support
This commit is contained in:
@@ -39,6 +39,24 @@ class MongoDB_API Connection
|
||||
public:
|
||||
typedef Poco::SharedPtr<Connection> Ptr;
|
||||
|
||||
class MongoDB_API SocketFactory
|
||||
{
|
||||
public:
|
||||
SocketFactory();
|
||||
/// Creates the SocketFactory.
|
||||
|
||||
virtual ~SocketFactory();
|
||||
/// Destroys the SocketFactory.
|
||||
|
||||
virtual Poco::Net::StreamSocket createSocket(const std::string& host, int port, Poco::Timespan connectTimeout, bool secure);
|
||||
/// Creates a Poco::Net::StreamSocket (if secure is false), or a
|
||||
/// Poco::Net::SecureStreamSocket (if secure is true) connected to the
|
||||
/// given host and port number.
|
||||
///
|
||||
/// The default implementation will throw a Poco::NotImplementedException
|
||||
/// if secure is true.
|
||||
};
|
||||
|
||||
Connection();
|
||||
/// Creates an unconnected Connection.
|
||||
///
|
||||
@@ -49,6 +67,12 @@ public:
|
||||
///
|
||||
/// The host and port must be separated with a colon.
|
||||
|
||||
Connection(const std::string& uri, SocketFactory& socketFactory);
|
||||
/// Creates a Connection connected to the given MongoDB instance at the
|
||||
/// given URI.
|
||||
///
|
||||
/// See the corresponding connect() method for more information.
|
||||
|
||||
Connection(const std::string& host, int port);
|
||||
/// Creates a Connection connected to the given MongoDB instance at host and port.
|
||||
|
||||
@@ -70,6 +94,29 @@ public:
|
||||
///
|
||||
/// The host and port must be separated with a colon.
|
||||
|
||||
void connect(const std::string& uri, SocketFactory& socketFactory);
|
||||
/// Connects to the given MongoDB instance at the given URI.
|
||||
///
|
||||
/// The URI must be in standard MongoDB connection string URI format:
|
||||
///
|
||||
/// mongodb://<user>:<password>@hostname.com:<port>/database-name?options
|
||||
///
|
||||
/// The following options are supported:
|
||||
///
|
||||
/// - ssl: If ssl=true is specified, a custom SocketFactory subclass creating
|
||||
/// a SecureStreamSocket must be supplied.
|
||||
/// - connectTimeoutMS: Socket connection timeout in milliseconds.
|
||||
/// - socketTimeoutMS: Socket send/receive timeout in milliseconds.
|
||||
/// - authMechanism: Authentication mechanism. Only "SCRAM-SHA-1" (default)
|
||||
/// and "MONGODB-CR" are supported.
|
||||
///
|
||||
/// Unknown options are silently ignored.
|
||||
///
|
||||
/// Will also attempt to authenticate using the specified credentials,
|
||||
/// using Database::authenticate().
|
||||
///
|
||||
/// Throws a Poco::NoPermissionException if authentication fails.
|
||||
|
||||
void connect(const std::string& host, int port);
|
||||
/// Connects to the given MongoDB server.
|
||||
|
||||
|
||||
@@ -14,12 +14,43 @@
|
||||
|
||||
#include "Poco/Net/SocketStream.h"
|
||||
#include "Poco/MongoDB/Connection.h"
|
||||
#include "Poco/MongoDB/Database.h"
|
||||
#include "Poco/URI.h"
|
||||
#include "Poco/Format.h"
|
||||
#include "Poco/NumberParser.h"
|
||||
#include "Poco/Exception.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
Connection::SocketFactory::SocketFactory()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Connection::SocketFactory::~SocketFactory()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Poco::Net::StreamSocket Connection::SocketFactory::createSocket(const std::string& host, int port, Poco::Timespan connectTimeout, bool secure)
|
||||
{
|
||||
if (!secure)
|
||||
{
|
||||
Poco::Net::SocketAddress addr(host, port);
|
||||
Poco::Net::StreamSocket socket;
|
||||
if (connectTimeout > 0)
|
||||
socket.connect(addr, connectTimeout);
|
||||
else
|
||||
socket.connect(addr);
|
||||
return socket;
|
||||
}
|
||||
else throw Poco::NotImplementedException("Default SocketFactory implementation does not support SecureStreamSocket");
|
||||
}
|
||||
|
||||
|
||||
Connection::Connection():
|
||||
_address(),
|
||||
_socket()
|
||||
@@ -35,6 +66,14 @@ Connection::Connection(const std::string& hostAndPort):
|
||||
}
|
||||
|
||||
|
||||
Connection::Connection(const std::string& uri, SocketFactory& socketFactory):
|
||||
_address(),
|
||||
_socket()
|
||||
{
|
||||
connect(uri, socketFactory);
|
||||
}
|
||||
|
||||
|
||||
Connection::Connection(const std::string& host, int port):
|
||||
_address(host, port),
|
||||
_socket()
|
||||
@@ -104,6 +143,70 @@ void Connection::connect(const Poco::Net::StreamSocket& socket)
|
||||
}
|
||||
|
||||
|
||||
void Connection::connect(const std::string& uri, SocketFactory& socketFactory)
|
||||
{
|
||||
Poco::URI theURI(uri);
|
||||
if (theURI.getScheme() != "mongodb") throw Poco::UnknownURISchemeException(uri);
|
||||
|
||||
std::string userInfo = theURI.getUserInfo();
|
||||
std::string host = theURI.getHost();
|
||||
Poco::UInt16 port = theURI.getPort();
|
||||
if (port == 0) port = 27017;
|
||||
std::string databaseName = theURI.getPath();
|
||||
if (databaseName.empty()) databaseName = "admin";
|
||||
bool secure = false;
|
||||
Poco::Timespan connectTimeout;
|
||||
Poco::Timespan socketTimeout;
|
||||
std::string authMethod = Database::AUTH_SCRAM_SHA1;
|
||||
|
||||
Poco::URI::QueryParameters params = theURI.getQueryParameters();
|
||||
for (Poco::URI::QueryParameters::const_iterator it = params.begin(); it != params.end(); ++it)
|
||||
{
|
||||
if (it->first == "ssl")
|
||||
{
|
||||
secure = (it->second == "true");
|
||||
}
|
||||
else if (it->first == "connectTimeoutMS")
|
||||
{
|
||||
connectTimeout = 1000*Poco::NumberParser::parse(it->second);
|
||||
}
|
||||
else if (it->first == "socketTimeoutMS")
|
||||
{
|
||||
socketTimeout = 1000*Poco::NumberParser::parse(it->second);
|
||||
}
|
||||
else if (it->first == "authMechanism")
|
||||
{
|
||||
authMethod = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
connect(socketFactory.createSocket(host, port, connectTimeout, secure));
|
||||
|
||||
if (socketTimeout > 0)
|
||||
{
|
||||
_socket.setSendTimeout(socketTimeout);
|
||||
_socket.setReceiveTimeout(socketTimeout);
|
||||
}
|
||||
|
||||
if (!userInfo.empty())
|
||||
{
|
||||
std::string username;
|
||||
std::string password;
|
||||
std::string::size_type pos = userInfo.find(':');
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
username.assign(userInfo, 0, pos++);
|
||||
password.assign(userInfo, pos, userInfo.size() - pos);
|
||||
}
|
||||
else username = userInfo;
|
||||
|
||||
Database database(databaseName);
|
||||
if (!database.authenticate(*this, username, password, authMethod))
|
||||
throw Poco::NoPermissionException(Poco::format("Access to MongoDB database %s denied for user %s", databaseName, username));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Connection::disconnect()
|
||||
{
|
||||
_socket.close();
|
||||
|
||||
Reference in New Issue
Block a user