szaszm commented on a change in pull request #743: Minificpp 1169 - Simplify C2
metrics collection and reporting
URL: https://github.com/apache/nifi-minifi-cpp/pull/743#discussion_r392202093
##########
File path: extensions/http-curl/tests/C2VerifyHeartbeatAndStop.cpp
##########
@@ -50,98 +50,45 @@
#include "protocols/RESTReceiver.h"
#include "protocols/RESTSender.h"
#include "HTTPIntegrationBase.h"
+#include "HTTPHandlers.h"
#include "agent/build_description.h"
#include "processors/LogAttribute.h"
-class Responder : public CivetHandler {
+class LightWeightC2Handler : public HeartbeatHandler {
public:
- explicit Responder(bool isSecure)
- : isSecure(isSecure) {
+ explicit LightWeightC2Handler(bool isSecure)
+ : HeartbeatHandler(isSecure),
+ calls_(0) {
}
- std::string readPost(struct mg_connection *conn) {
- std::string response;
- int blockSize = 1024 * sizeof(char), readBytes;
+ virtual ~LightWeightC2Handler() = default;
- char buffer[1024];
- while ((readBytes = mg_read(conn, buffer, blockSize)) > 0) {
- response.append(buffer, 0, (readBytes / sizeof(char)));
+ virtual void handleHeartbeat(const rapidjson::Document& root, struct
mg_connection * conn) {
+ (void)conn;
+ if (calls_ == 0) {
+ verifyJsonHasAgentManifest(root);
+ } else {
+ assert(root.HasMember("agentInfo") == true);
+ assert(root["agentInfo"].HasMember("agentManifest") == false);
}
- return response;
+ calls_++;
}
- bool handlePost(CivetServer *server, struct mg_connection *conn) {
- auto post_data = readPost(conn);
-
- std::cerr << post_data << std::endl;
-
- if (!IsNullOrEmpty(post_data)) {
- rapidjson::Document root;
- rapidjson::ParseResult ok = root.Parse(post_data.data(),
post_data.size());
- bool found = false;
- std::string operation = root["operation"].GetString();
- if (operation == "heartbeat") {
- assert(root.HasMember("agentInfo") == true);
- assert(root["agentInfo"]["agentManifest"].HasMember("bundles") ==
true);
-
- for (auto &bundle :
root["agentInfo"]["agentManifest"]["bundles"].GetArray()) {
- assert(bundle.HasMember("artifact"));
- std::string str = bundle["artifact"].GetString();
- if (str == "minifi-system") {
-
- std::vector<std::string> classes;
- for (auto &proc :
bundle["componentManifest"]["processors"].GetArray()) {
- classes.push_back(proc["type"].GetString());
- }
-
- auto group = minifi::BuildDescription::getClassDescriptions(str);
- for (auto proc : group.processors_) {
- assert(std::find(classes.begin(), classes.end(),
proc.class_name_) != std::end(classes));
- found = true;
- }
-
- }
- }
- assert(found == true);
- }
- }
- std::string resp = "{\"operation\" : \"heartbeat\",
\"requested_operations\" : [{ \"operationid\" : 41, \"operation\" : \"stop\",
\"name\" : \"invoke\" }, "
- "{ \"operationid\" : 42, \"operation\" : \"stop\", \"name\" :
\"FlowController\" } ]}";
- mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: "
- "text/plain\r\nContent-Length: %lu\r\nConnection: close\r\n\r\n",
- resp.length());
- mg_printf(conn, "%s", resp.c_str());
- return true;
- }
-
- protected:
- bool isSecure;
+ private:
+ std::atomic<size_t> calls_;
Review comment:
C.48: Prefer in-class initializers to member initializers in constructors
for constant initializers
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c48-prefer-in-class-initializers-to-member-initializers-in-constructors-for-constant-initializers
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services