Fix/debug BOREALIS (#2086)

Improved the .dll to catch any and all unspecified exceptions. To avoid .dll crashing. They will now be logged into a .txt file.
Made the .dll's code to be more reusable, and look a bit better. Also removed a few hidden files (header).
Added a few debug logs for BOREALIS, specifically towards new features.
Removed a potential runtime error for scheduling a push without a scheduler.
Tested as functional on local build.
This commit is contained in:
skull132
2017-04-14 13:29:21 +03:00
committed by GitHub
parent f35680ba7a
commit a3516d7069
4 changed files with 211 additions and 101 deletions
+198 -99
View File
@@ -14,73 +14,89 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "stdafx.h"
#include <curl\curl.h>
extern "C" __declspec(dllexport) char *send_post_request(int argc, char *argv[])
#define EXTERN_DLL_EXPORT extern "C" __declspec(dllexport)
#include <string>
#include <thread>
#include <iostream>
#include <fstream>
using namespace std;
// Global buffer for the GET request callback.
static string getBuffer;
// Helper function prototypes. Definitions at the bottom.
CURL *SetupCurl(char *url, struct curl_slist *header);
void LogException(exception& e);
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp);
EXTERN_DLL_EXPORT char *send_post_request(int argc, char *argv[])
{
if (argc < 2)
{
return "proc=1";
}
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl = curl_easy_init();
if (!curl)
// Giant try-catch block, for simplicity sake.
try
{
return "proc=2";
// Initialize variables.
CURL *curl = NULL;
static char return_value[33] = {0};
long http_code = 0;
CURLcode res;
struct curl_slist *headers = NULL;
for (int i = 2; i < argc; i++)
{
headers = curl_slist_append(headers, argv[i]);
}
// Set up cURL.
curl = SetupCurl(argv[0], headers);
if (!curl)
{
return "proc=2";
}
// Set additional cURL options.
curl_easy_setopt(curl, CURLOPT_POST, 1L);
char *data = argv[1];
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
// Save the response.
res = curl_easy_perform(curl);
// Get the response code and save it to http_code.
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
// Clean up the session info.
curl_global_cleanup();
// Create the feedback message.
// Format used is key=value&key=value.
snprintf(return_value, 32, "http=%d&curl=%d", http_code, res);
return return_value;
}
// Initialize variables.
static char return_value[33] = {0};
long http_code = 0;
CURLcode res;
struct curl_slist *chunk = NULL;
for (int i = 2; i < argc; i++)
catch (exception& e)
{
chunk = curl_slist_append(chunk, argv[i]);
// Catch any exceptions that are encountered and log them.
LogException(e);
return "proc=3";
}
// Set curl options.
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2L);
curl_easy_setopt(curl, CURLOPT_URL, argv[0]);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
char *data = argv[1];
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
// Save the response.
res = curl_easy_perform(curl);
// Get the response code and save it to http_code.
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
// Clean up the session info.
curl_global_cleanup();
// Create the feedback message.
// Format used is key=value&key=value.
snprintf(return_value, 32, "http=%d&curl=%d", http_code, res);
return return_value;
}
static std::string getBuffer;
// Callback function to write into the return string.
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
getBuffer.append((char*)contents, realsize);
return realsize;
}
extern "C" __declspec(dllexport) char *send_get_request(int argc, char *argv[])
EXTERN_DLL_EXPORT char *send_get_request(int argc, char *argv[])
{
// If you're not using this with at least 1 custom header, why /are/ you using it?
// Use world.Export() instead you dolt.
@@ -89,55 +105,138 @@ extern "C" __declspec(dllexport) char *send_get_request(int argc, char *argv[])
return "proc=1";
}
// A lot of code duplication from send_post_request. But. It's like 2AM.
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl = curl_easy_init();
if (!curl)
// Catch any exceptions you find.
try
{
return "proc=2";
// Initialize variables.
getBuffer.clear();
CURL *curl = NULL;
long http_code = 0;
CURLcode res;
struct curl_slist *headers = NULL;
// Generate headers.
for (int i = 1; i < argc; i++)
{
headers = curl_slist_append(headers, argv[i]);
}
// Initialize cURL.
curl = SetupCurl(argv[0], headers);
if (!curl)
{
return "proc=2";
}
// Set additional options.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &getBuffer);
// Save the response.
res = curl_easy_perform(curl);
// Get the response code and save it to http_code.
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
// Clean up the session info.
curl_global_cleanup();
if (res != CURLE_OK || http_code != 200)
{
static char return_value[33] = { 0 };
// Create the feedback message.
// Format used is key=value&key=value.
snprintf(return_value, 32, "http=%d&curl=%d", http_code, res);
return return_value;
}
else
{
// No error, return the body.
return (char *)getBuffer.c_str();
}
}
// Initialize variables.
getBuffer.clear();
long http_code = 0;
CURLcode res;
struct curl_slist *chunk = NULL;
for (int i = 1; i < argc; i++)
catch (exception& e)
{
chunk = curl_slist_append(chunk, argv[i]);
}
LogException(e);
// Set curl options.
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2L);
curl_easy_setopt(curl, CURLOPT_URL, argv[0]);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &getBuffer);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
// Save the response.
res = curl_easy_perform(curl);
// Get the response code and save it to http_code.
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
// Clean up the session info.
curl_global_cleanup();
if (res != CURLE_OK || http_code != 200)
{
static char return_value[33] = {0};
// Create the feedback message.
// Format used is key=value&key=value.
snprintf(return_value, 32, "http=%d&curl=%d", http_code, res);
return return_value;
}
else
{
// No error, return the body.
return (char *)getBuffer.c_str();
return "proc=3";
}
}
/**
* A function to set up a CURL object.
*
* Expects a URL and a header. Header can be NULL.
* Will also run curl_global_init() with default settings.
*
* Will return a pointer to the CURL object. NULL upon failure.
*/
CURL *SetupCurl(char *url, struct curl_slist *header)
{
CURL *curl = NULL;
if (!url || !strlen(url))
{
return NULL;
}
// Run cURL global init.
curl_global_init(CURL_GLOBAL_DEFAULT);
// Easy init the thing itself.
curl = curl_easy_init();
// Init failed. RIP.
if (!curl)
{
return NULL;
}
// Set timeout to 2 seconds per request.
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2L);
// Set the URL.
curl_easy_setopt(curl, CURLOPT_URL, url);
// If we have headers, set them.
if (header)
{
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
}
return curl;
}
/**
* Function for exception logging.
*
* Excepts an exception reference as an argument.
*/
void LogException(exception& e)
{
ofstream fp;
fp.open("CURL-ERROR.txt");
if (fp.is_open())
{
fp << e.what() << std::endl;
fp.close();
}
return;
}
/**
* A callback function that cURL uses for writing down request results.
*/
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size *nmemb;
getBuffer.append((char *)contents, realsize);
return realsize;
}