1
0
Fork 0
mirror of https://github.com/HackHerz/pusher synced 2025-12-06 02:10:19 +00:00

Forgot to include curlhandler.

This commit is contained in:
Daniel Stein 2016-06-21 14:13:43 +02:00
parent aece4a5de2
commit 65c1816b2f
2 changed files with 88 additions and 0 deletions

71
src/curlhandler.cpp Normal file
View file

@ -0,0 +1,71 @@
#include "curlhandler.h"
using namespace std;
// urlDecode matches
string matches[][2] = {
{"$", "%24"},
{"&", "%26"},
{"+", "%2B"},
{",", "%2C"},
{"/", "%2F"},
{":", "%3A"},
{";", "%3B"},
{"=", "%3D"},
{"?", "%3F"},
{"@", "%40"}
};
// needed for handling curl output
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
string CurlHandler::request(string data, const char* url)
{
CURL *curl;
CURLcode res;
string readBuffer;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERAGENT, USER_AGENT);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res != CURLE_OK)
{
//throw PusherError("Network Error");
}
}
return readBuffer;
}
string CurlHandler::urlDecode(string url)
{
for(unsigned int i = 0; i < (sizeof(matches)/sizeof(matches[0])); i++)
{
size_t start_pos = 0;
while((start_pos = url.find(matches[i][0], start_pos)) != string::npos)
{
url.replace(start_pos, matches[i][0].length(), matches[i][1]);
start_pos += matches[i][1].length();
}
}
return url;
}

17
src/curlhandler.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef H_CURLHANDLER
#define H_CURLHANDLER
#include <curl/curl.h>
#include <string>
#define USER_AGENT "pusher/0.2"
class CurlHandler
{
public:
static std::string request(std::string data, const char* url);
static std::string urlDecode(std::string url);
};
#endif