From 938f0631cc64da1fca61004d79240ce286f59c00 Mon Sep 17 00:00:00 2001
From: Lais Frigerio <laaisfrigerio@gmail.com>
Date: Tue, 10 Oct 2017 08:45:10 -0300
Subject: [PATCH] SCRUM#145 - Remove c++11 flag and not make web service
 request with cpr lib

Signed-off-by: Lais Frigerio <laaisfrigerio@gmail.com>
---
 CMakeLists.txt             | 11 +++---
 include/agent/connection.h | 10 +++---
 include/agent/get.h        |  5 ++-
 include/agent/main.h       |  1 -
 include/agent/post.h       |  1 -
 src/agent/connection.cpp   | 71 +++++++++++++++++++++++---------------
 src/agent/get.cpp          | 51 ++++++++++-----------------
 src/agent/post.cpp         | 49 ++++++++++----------------
 8 files changed, 94 insertions(+), 105 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 872d260..057dec0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,17 +7,19 @@ set ( VERSION_MAJOR 0 )
 set ( VERSION_MINOR 0 )
 
 # cpr requires c++11
-set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
 if(WIN32)
 	add_definitions( -DBOOST_ALL_NO_LIB )
 	set(Boost_USE_STATIC_LIBS ON)
 	add_definitions(-DCURL_STATICLIB)
 	add_definitions(-D_CRT_SECURE_NO_WARNINGS)
 	add_definitions(-D_WIN32_WINNT=0x0501)
+    # add_compile_options("/std:c++11")
+    # set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++11)
+else()
+    set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
+    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")
 endif()
 
-set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")
-
 # src : main, collect functions  + jsoncpp library
 file ( GLOB SOURCES src/agent/*.cpp )
 
@@ -37,7 +39,6 @@ option( RUN_CURL_TESTS "Set to ON to run cURL tests." OFF )
 option( CURL_STATICLIB "Set to ON to generated curl statically." ON )
 
 add_subdirectory ( lib/cpr )
-include_directories ( ${CPR_INCLUDE_DIRS} )
 include_directories ( ${CURL_INCLUDE_DIRS} )
 
 # headers
@@ -53,7 +54,7 @@ include ( "modules/boost.cmake" )
 # compile
 set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY "../bin" )
 add_executable ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${SOURCES} )
-target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${CPR_LIBRARIES} ${CURL_LIBRARIES} ${FILESYSTEM_LIB} ${SYSTEM_LIB} ${REGEX_LIB} ${PROGRAM_OPTIONS_LIB} ${CROSS_STATIC} )
+target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${CURL_LIBRARIES} ${FILESYSTEM_LIB} ${SYSTEM_LIB} ${REGEX_LIB} ${PROGRAM_OPTIONS_LIB} ${CROSS_STATIC} )
 
 add_dependencies ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} Boost)
 
diff --git a/include/agent/connection.h b/include/agent/connection.h
index b127f31..d0aa5f3 100644
--- a/include/agent/connection.h
+++ b/include/agent/connection.h
@@ -26,11 +26,11 @@ class Connection{
         long httpCode = 0; /*!< Code return from webSerice (200, 404, 502 and so on) */
 
     public:
-        bool downloadAgent (std::string, Agent*, int);
-        bool getVersionByProxy(Agent*, std::string*, std::string);
-        int getInstallerNameByProxy(std::string, Agent*, std::string*);
-        void setCurlOption(std::string, Agent*, std::string*);
-        int sendJsonByProxy(Agent*, std::string, int);
+        bool downloadAgent (std::string, Agent*, bool);
+        bool getVersion(Agent*, std::string*, std::string, bool);
+        int getInstallerName(std::string, Agent*, std::string*, bool);
+        void setCurlOption(std::string, Agent*, std::string*, bool);
+        int sendJson(Agent*, std::string, int, bool);
 
         /** Default callback function that is necessary to make a curl request.
         *  @brief get agent binary (agent.run or agent.exe)
diff --git a/include/agent/get.h b/include/agent/get.h
index b7d5ffc..474c3e3 100644
--- a/include/agent/get.h
+++ b/include/agent/get.h
@@ -2,7 +2,6 @@
 #include <iostream>
 #include <fstream>
 #include <stdlib.h>
-#include <cpr/cpr.h>
 #include <json/json.h>
 #include <agent/common.h>
 #include <agent/connection.h>
@@ -24,6 +23,6 @@ const std::string url = "http://localhost:3001";
 int check_update(Agent*);
 bool create_directories(std::string);
 bool compare_version(Agent*, std::string);
-bool download(Agent*, std::string, int);
+bool download(Agent*, std::string, bool);
 void execute_agent(Agent*, boost::filesystem::path);
-bool get_agent_installer_name(Agent*, std::string*, int);
+bool get_agent_installer_name(Agent*, std::string*, bool);
diff --git a/include/agent/main.h b/include/agent/main.h
index ac6b3c0..2c513f3 100644
--- a/include/agent/main.h
+++ b/include/agent/main.h
@@ -17,7 +17,6 @@
 #include <boost/program_options/parsers.hpp>
 #include <boost/program_options/variables_map.hpp>
 #include <boost/filesystem/fstream.hpp>
-#include <cpr/cpr.h>
 
 namespace pt = boost::posix_time;
 namespace po = boost::program_options;
diff --git a/include/agent/post.h b/include/agent/post.h
index dfd9e5d..70d3af6 100644
--- a/include/agent/post.h
+++ b/include/agent/post.h
@@ -1,6 +1,5 @@
 #pragma once
 #include <iostream>
-#include <cpr/cpr.h>
 #include <json/json.h>
 #include <agent/agent.h>
 #include <agent/connection.h>
diff --git a/src/agent/connection.cpp b/src/agent/connection.cpp
index 89dab77..b9a4a02 100644
--- a/src/agent/connection.cpp
+++ b/src/agent/connection.cpp
@@ -31,7 +31,7 @@
 * @param agent    object agent
 * @param option   use proxy or not (option == 0 - use proxy)
 */
-bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
+bool Connection::downloadAgent(std::string url, Agent* agent, bool option) {
     curl = curl_easy_init();
     if (!curl) {
         agent->logFile << __DATE__ + std::string(" - ERROR: Curl ") +
@@ -79,13 +79,15 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
                     &Connection::callBackDownload);
 
-    if (option == 0) {
+    if (option) {
         std::string::size_type sz; // NOLINT [runtime/int]
         long port; // NOLINT [runtime/int]
 
         try {
             port = std::stol(agent->getProxy()->getPort(), &sz); // NOLINT [runtime/int]
         } catch (std::invalid_argument& ex) {
+            agent->logFile << __DATE__ + std::string(" - ERROR: ") +
+                    std::string(ex.what()) << std::endl;
             port = 0;
         }
 
@@ -147,28 +149,34 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) {
 * @param version  pointer variable thats receive version or filename from web service.
 */
 void Connection::setCurlOption(std::string url, Agent* agent,
-                                std::string* out) {
+                                std::string* out, bool opt) {
     std::string::size_type sz; // NOLINT [runtime/int]
     long port; // NOLINT [runtime/int]
 
     try {
         port = std::stol(agent->getProxy()->getPort(), &sz); // NOLINT [runtime/int]
     } catch (std::invalid_argument& ex) {
+        agent->logFile << __DATE__ + std::string(" - ERROR: ") +
+                    std::string(ex.what()) << std::endl;
         port = 0;
     }
 
     curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
-    curl_easy_setopt(curl, CURLOPT_PROXY, agent->getProxy()->getHost().c_str());
-    curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);
-    curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
+    if (opt) {
+        curl_easy_setopt(curl, CURLOPT_PROXY,
+                    agent->getProxy()->getHost().c_str());
+        curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);
+        curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
                     agent->getProxy()->getUser().c_str());
-    curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
+        curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
                 agent->getProxy()->getPassword().c_str());
+    }
+
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, out);
     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Connection::callback);
     curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
-    // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
-    curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
+    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
+    // curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
     response = curl_easy_perform(curl);
     curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
     curl_easy_cleanup(curl);
@@ -181,9 +189,10 @@ void Connection::setCurlOption(std::string url, Agent* agent,
 * @param url      references to webService
 * @param agent    object agent
 * @param version  pointer variable thats receive version from web service;
+* @param opt      is proxy request or not
 */
-bool Connection::getVersionByProxy(Agent* agent, std::string *version,
-                                std::string url) {
+bool Connection::getVersion(Agent* agent, std::string *version,
+                                std::string url, bool opt) {
     struct curl_slist *headers = NULL;
     headers = curl_slist_append(headers, "Accept: application/json");
     headers = curl_slist_append(headers, "Content-Type: application/json");
@@ -193,8 +202,8 @@ bool Connection::getVersionByProxy(Agent* agent, std::string *version,
 
     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
 
-    this->setCurlOption(url, agent, version);
-    if (httpCode == 200)
+    this->setCurlOption(url, agent, version, opt);
+    if (this->httpCode == 200)
         return true;
     else
         return false;
@@ -207,12 +216,13 @@ bool Connection::getVersionByProxy(Agent* agent, std::string *version,
 * @param url      references to webService
 * @param agent    object agent
 * @param version  pointer variable thats receive file name from web service;
+* @param opt      is proxy request or not
 */
-int Connection::getInstallerNameByProxy(std::string url, Agent* agent,
-                            std::string* filename) {
+int Connection::getInstallerName(std::string url, Agent* agent,
+                            std::string* filename, bool opt) {
     curl = curl_easy_init();
-    this->setCurlOption(url, agent, filename);
-    return httpCode;
+    this->setCurlOption(url, agent, filename, opt);
+    return this->httpCode;
 }
 
 /** Contact webservice server using proxy to send json (inventory, net_usage or user_history)
@@ -221,15 +231,19 @@ int Connection::getInstallerNameByProxy(std::string url, Agent* agent,
 *
 * @param agent    object agent
 * @param json     references to json
-* @param opt      opt == 0 (send inventory) | opt == 2 (send net bandwidth) | otherwise (send user history)
+* @param send     send == 0 (send inventory) | send == 2 (send net bandwidth) | otherwise (send user history)
+* @param opt      is proxy request or not
 */
-int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) {
+int Connection::sendJson(Agent* agent, std::string json,
+                            int send, bool opt) {
     std::string::size_type sz; // NOLINT [runtime/int]
     long port; // NOLINT [runtime/int]
 
     try {
         port = std::stol(agent->getProxy()->getPort(), &sz); // NOLINT [runtime/int]
     } catch (std::invalid_argument& ex) {
+        agent->logFile << __DATE__ + std::string(" - ERROR: ") +
+                    std::string(ex.what()) << std::endl;
         port = 0;
     }
 
@@ -243,9 +257,9 @@ int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) {
     // opt == 1 {send inventory}
     // opt = 2 {send net bandwidth}
     // otherwise, {send user history}
-    if (opt == 1)
+    if (send == 1)
         base_url = base_url + std::string("/api/agent/collect/inventory");
-    else if (opt == 2)
+    else if (send == 2)
         base_url = base_url + std::string("/api/agent/collect/net_usage");
     else
         base_url = base_url + std::string("/api/agent/collect/user_history");
@@ -254,12 +268,15 @@ int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) {
     curl = curl_easy_init();
     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
     curl_easy_setopt(curl, CURLOPT_URL, base_url.c_str());
-    curl_easy_setopt(curl, CURLOPT_PROXY, agent->getProxy()->getHost().c_str());
-    curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);
-    curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
-                    agent->getProxy()->getUser().c_str());
-    curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
-                agent->getProxy()->getPassword().c_str());
+    if (opt) {
+        curl_easy_setopt(curl, CURLOPT_PROXY,
+                        agent->getProxy()->getHost().c_str());
+        curl_easy_setopt(curl, CURLOPT_PROXYPORT, port);
+        curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
+                        agent->getProxy()->getUser().c_str());
+        curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
+                    agent->getProxy()->getPassword().c_str());
+    }
     curl_easy_setopt(curl, CURLOPT_POST, 1);
     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
     curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
diff --git a/src/agent/get.cpp b/src/agent/get.cpp
index b3047e3..e1eca5f 100644
--- a/src/agent/get.cpp
+++ b/src/agent/get.cpp
@@ -35,29 +35,22 @@ int check_update(Agent* agent) {
     Json::Reader reader;
     Json::Value version_json;
     Json::FastWriter fastWriter;
-    int option = 1;
-
-    /* Contact webservice server to get client version. */
-    auto p = cpr::Get(cpr::Url{agent->getConf()->getWebService() +
-                                "/api/agent/version"},
-              cpr::Header{{"content-type", "application/json"}});
-
-    // Check error with connection
-    if (p.status_code != 200) {
-        option = 0;
-        // Case if not connected before, try it with proxy
-        std::string web_service = agent->getConf()->getWebService() +
+    bool option = false;
+
+    std::string web_service = agent->getConf()->getWebService() +
                         std::string("/api/agent/version");
-        if (!agent->getConnection()->getVersionByProxy(agent, &version,
-                                                web_service)) {
+    if (!agent->getConnection()->getVersion(agent, &version,
+                                                web_service, 0)) {
             agent->logFile << __DATE__ << " - ERROR: Couldn't get current " <<
-                    "version from simmc web service." << std::endl;
+                "version from simmc web service without pŕoxy." << std::endl;
+        option = true;
+        if (!agent->getConnection()->getVersion(agent, &version,
+                                                web_service, 1)) {
+            agent->logFile << __DATE__ << " - ERROR: Couldn't get current " <<
+                    "version from simmc web service with proxy." << std::endl;
         }
-    } else {
-        version = p.text;
     }
 
-       // p.text - response from api
     reader.parse(version, version_json, false);
     version = fastWriter.write(version_json["version"]);
     boost::regex re("(\")|(\n)");
@@ -130,7 +123,7 @@ bool compare_version(Agent *agent, std::string version) {
 }
 
 /* Contact webservice server to request a agent installer. */
-bool get_agent_installer_name(Agent* agent, std::string* filename, int opt) {
+bool get_agent_installer_name(Agent* agent, std::string* filename, bool opt) {
     Json::FastWriter fastWriter;
     Json::Value filename_json;
     Json::Reader reader;
@@ -147,17 +140,13 @@ bool get_agent_installer_name(Agent* agent, std::string* filename, int opt) {
                      + std::string(os) + std::string("/")
                      + std::to_string(agent->getConf()->getIdPoint());
 
-    if (opt == 1) {
-        auto p = cpr::Get(cpr::Url{ws}, // NOLINT [whitespace/line_length]
-                cpr::Header{{"content-type", "application/json"}});
-        status_code = p.status_code;
-        *filename = p.text;
-    } else {
-        status_code = agent->getConnection()->getInstallerNameByProxy(ws, agent, filename); // NOLINT [whitespace/line_length]
-    }
+    if (opt)
+        status_code = agent->getConnection()->getInstallerName(ws, agent, filename, opt); // NOLINT [whitespace/line_length]
+    else
+        status_code = agent->getConnection()->getInstallerName(ws, agent, filename, opt); // NOLINT [whitespace/line_length]
+
 
     if (status_code == 200) {
-        // p.text - response from api
         reader.parse(*filename, filename_json, false);
         *filename = fastWriter.write(filename_json["filename"]);
 
@@ -173,11 +162,9 @@ bool get_agent_installer_name(Agent* agent, std::string* filename, int opt) {
  /**
  * To make agent download.
  * Curl library is used to make a webService request to get agent zipfile.
- * option -> indicate proxy or not
- *              0 -> proxy
- *              1 -> not proxy
+ * option -> indicate request with proxy or not
  */
-bool download(Agent* agent, std::string filename, int option) {
+bool download(Agent* agent, std::string filename, bool option) {
     std::string web_service = agent->getConf()->getWebService() +
                 std::string("/install/") +
                 std::string(filename);
diff --git a/src/agent/post.cpp b/src/agent/post.cpp
index 6489c93..6c275b0 100644
--- a/src/agent/post.cpp
+++ b/src/agent/post.cpp
@@ -42,18 +42,14 @@ int send_inventory(bool print, Agent* agent) {
             std::cout << "Empty inventory object" << std::endl;
     }
 
-    auto p = cpr::Post(cpr::Url{base_url + "/api/agent/collect/inventory"},
-                      cpr::Body{fastWriter.write(inventory)},
-                      cpr::Header{{"content-type", "application/json"}});
-
-    if (p.status_code == 200)
+    if (agent->getConnection()->sendJson(agent,
+                fastWriter.write(inventory), 1, 0) == 200)
+        return 1;
+    else if (agent->getConnection()->sendJson(agent,
+                fastWriter.write(inventory), 1, 1) == 200)
         return 1;
     else
-        if (agent->getConnection()->sendJsonByProxy(agent,
-                fastWriter.write(inventory), 1) == 200)
-            return 1;
-
-    return 0;
+        return 0;
 }
 
 /***************************************
@@ -77,18 +73,14 @@ int send_net_bandwidth(bool print, Agent* agent) {
             std::cout << "Empty network_bandwidth object" << std::endl;
     }
 
-    auto p = cpr::Post(cpr::Url{base_url + "/api/agent/collect/net_usage"},
-                      cpr::Body{fastWriter.write(net)},
-                      cpr::Header{{"content-type", "application/json"}});
-
-    if (p.status_code == 200)
+    if (agent->getConnection()->sendJson(agent,
+                fastWriter.write(net), 2, 0) == 200)
+        return 1;
+    else if (agent->getConnection()->sendJson(agent,
+                fastWriter.write(net), 2, 1) == 200)
         return 1;
     else
-        if (agent->getConnection()->sendJsonByProxy(agent,
-                fastWriter.write(net), 2) == 200)
-            return 1;
-
-    return 0;
+        return 0;
 }
 
 /**********************************
@@ -112,19 +104,14 @@ int send_user_history(bool print, Agent* agent) {
             std::cout << "Empty user_history object" << std::endl;
     }
 
-    auto p = cpr::Post(cpr::Url{base_url + "/api/agent/collect/user_history"},
-                      cpr::Parameters{{"type", "user_history"}},
-                      cpr::Body{fastWriter.write(u_h)},
-                      cpr::Header{{"content-type", "application/json"}});
-
-    if (p.status_code == 200)
+    if (agent->getConnection()->sendJson(agent,
+                fastWriter.write(u_h), 3, 0) == 200)
+        return 1;
+    else if (agent->getConnection()->sendJson(agent,
+                fastWriter.write(u_h), 3, 1) == 200)
         return 1;
     else
-        if (agent->getConnection()->sendJsonByProxy(agent,
-                fastWriter.write(u_h), 3) == 200)
-            return 1;
-
-    return 0;
+        return 0;
 }
 
 /**
-- 
GitLab