Skip to content

Commit

Permalink
chore(MongoDB): Mark some old wire protocol functions as deprecated. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
matejk authored Nov 25, 2024
1 parent cecccf7 commit 29b2c3a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
3 changes: 1 addition & 2 deletions MongoDB/include/Poco/MongoDB/Cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
namespace Poco {
namespace MongoDB {

//class [[deprecated]] Cursor;
class Cursor;
class POCO_DEPRECATED("Use new wire protocol") Cursor;

class MongoDB_API Cursor: public Document
/// Cursor is an helper class for querying multiple documents.
Expand Down
4 changes: 2 additions & 2 deletions MongoDB/include/Poco/MongoDB/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class MongoDB_API Database
/// Creates a QueryRequest to count the given collection.
/// The collectionname must not contain the database name. (old wire protocol)

//[[deprecated]]
POCO_DEPRECATED("Use new wire protocol")
Poco::SharedPtr<Poco::MongoDB::DeleteRequest> createDeleteRequest(const std::string& collectionName) const;
/// Creates a DeleteRequest to delete documents in the given collection.
/// The collectionname must not contain the database name. (old wire protocol)
Expand All @@ -96,7 +96,7 @@ class MongoDB_API Database
/// Creates a QueryRequest. (old wire protocol)
/// The collectionname must not contain the database name.

//[[deprecated]]
POCO_DEPRECATED("Use new wire protocol")
Poco::SharedPtr<Poco::MongoDB::UpdateRequest> createUpdateRequest(const std::string& collectionName) const;
/// Creates an UpdateRequest. (old wire protocol)
/// The collectionname must not contain the database name.
Expand Down
3 changes: 1 addition & 2 deletions MongoDB/include/Poco/MongoDB/DeleteRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
namespace Poco {
namespace MongoDB {

//class [[deprecated]] DeleteRequest;
class DeleteRequest;
//class POCO_DEPRECATED("Use new wire protocol") DeleteRequest;

class MongoDB_API DeleteRequest: public RequestMessage
/// A DeleteRequest is used to delete one ore more documents from a database.
Expand Down
3 changes: 1 addition & 2 deletions MongoDB/include/Poco/MongoDB/UpdateRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
namespace Poco {
namespace MongoDB {

//class [[deprecated]] UpdateRequest;
class UpdateRequest;
//class POCO_DEPRECATED("Use new wire protocol") UpdateRequest;

class MongoDB_API UpdateRequest: public RequestMessage
/// This request is used to update a document in a database
Expand Down
46 changes: 29 additions & 17 deletions MongoDB/src/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace
std::string digestToBinaryString(Poco::DigestEngine& engine)
{
Poco::DigestEngine::Digest d = engine.digest();
return std::string(reinterpret_cast<const char*>(&d[0]), d.size());
return { reinterpret_cast<const char*>(&d[0]), d.size() };
}

std::string digestToHexString(Poco::DigestEngine& engine)
Expand Down Expand Up @@ -116,7 +116,7 @@ namespace
}
return digestToHexString(md5);
}
}
} // namespace


Database::Database(const std::string& db):
Expand Down Expand Up @@ -152,14 +152,16 @@ bool Database::authCR(Connection& connection, const std::string& username, const

ResponseMessage response;
connection.sendRequest(*pCommand, response);
if (response.documents().size() > 0)
if (response.documents().empty())
{
throw Poco::ProtocolException("empty response for getnonce");
}
{
Document::Ptr pDoc = response.documents()[0];
if (pDoc->getInteger("ok") != 1) return false;
nonce = pDoc->get<std::string>("nonce", "");
if (nonce.empty()) throw Poco::ProtocolException("no nonce received");
}
else throw Poco::ProtocolException("empty response for getnonce");

std::string credsDigest = hashCredentials(username, password);

Expand All @@ -177,12 +179,12 @@ bool Database::authCR(Connection& connection, const std::string& username, const
.add<std::string>("key", key);

connection.sendRequest(*pCommand, response);
if (response.documents().size() > 0)
if (!response.documents().empty())
{
Document::Ptr pDoc = response.documents()[0];
return pDoc->getInteger("ok") == 1;
}
else throw Poco::ProtocolException("empty response for authenticate");
throw Poco::ProtocolException("empty response for authenticate");
}


Expand All @@ -204,7 +206,10 @@ bool Database::authSCRAM(Connection& connection, const std::string& username, co
Int32 conversationId = 0;
std::string serverFirstMsg;

if (response.documents().size() > 0)
if (response.documents().empty())
{
throw Poco::ProtocolException("empty response for saslStart");
}
{
Document::Ptr pDoc = response.documents()[0];
if (pDoc->getInteger("ok") == 1)
Expand All @@ -213,9 +218,11 @@ bool Database::authSCRAM(Connection& connection, const std::string& username, co
serverFirstMsg = pPayload->toRawString();
conversationId = pDoc->get<Int32>("conversationId");
}
else return false;
else
{
return false;
}
}
else throw Poco::ProtocolException("empty response for saslStart");

std::map<std::string, std::string> kvm = parseKeyValueList(serverFirstMsg);
const std::string serverNonce = kvm["r"];
Expand Down Expand Up @@ -260,17 +267,22 @@ bool Database::authSCRAM(Connection& connection, const std::string& username, co

std::string serverSecondMsg;
connection.sendRequest(*pCommand, response);
if (response.documents().size() > 0)
if (response.documents().empty())
{
throw Poco::ProtocolException("empty response for saslContinue");
}
{
Document::Ptr pDoc = response.documents()[0];
if (pDoc->getInteger("ok") == 1)
{
Binary::Ptr pPayload = pDoc->get<Binary::Ptr>("payload");
serverSecondMsg = pPayload->toRawString();
}
else return false;
else
{
return false;
}
}
else throw Poco::ProtocolException("empty response for saslContinue");

Poco::HMACEngine<Poco::SHA1Engine> hmacSKey(saltedPassword);
hmacSKey.update(std::string("Server Key"));
Expand All @@ -293,12 +305,12 @@ bool Database::authSCRAM(Connection& connection, const std::string& username, co
.add<Binary::Ptr>("payload", new Binary);

connection.sendRequest(*pCommand, response);
if (response.documents().size() > 0)
if (!response.documents().empty())
{
Document::Ptr pDoc = response.documents()[0];
return pDoc->getInteger("ok") == 1;
}
else throw Poco::ProtocolException("empty response for saslContinue");
throw Poco::ProtocolException("empty response for saslContinue");
}


Expand All @@ -312,7 +324,7 @@ Document::Ptr Database::queryBuildInfo(Connection& connection) const
connection.sendRequest(*request, response);

Document::Ptr buildInfo;
if ( response.documents().size() > 0 )
if ( !response.documents().empty() )
{
buildInfo = response.documents()[0];
}
Expand Down Expand Up @@ -353,7 +365,7 @@ Int64 Database::count(Connection& connection, const std::string& collectionName)
Poco::MongoDB::ResponseMessage response;
connection.sendRequest(*countRequest, response);

if (response.documents().size() > 0)
if (!response.documents().empty())
{
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
return doc->getInteger("n");
Expand Down Expand Up @@ -409,7 +421,7 @@ Document::Ptr Database::getLastErrorDoc(Connection& connection) const
Poco::MongoDB::ResponseMessage response;
connection.sendRequest(*request, response);

if (response.documents().size() > 0)
if (!response.documents().empty())
{
errorDoc = response.documents()[0];
}
Expand Down

0 comments on commit 29b2c3a

Please sign in to comment.