mirror of
https://github.com/HackHerz/pusher
synced 2025-12-06 02:10:19 +00:00
Putting curlhandling in its own class.
This commit is contained in:
parent
bf4d174431
commit
aece4a5de2
4 changed files with 18 additions and 79 deletions
12
makefile
12
makefile
|
|
@ -13,8 +13,8 @@ LIBS = `pkg-config libcurl --cflags --libs`
|
||||||
# Build
|
# Build
|
||||||
all: pusher
|
all: pusher
|
||||||
|
|
||||||
pusher: simpleini pushhandler main
|
$(TARGET): simpleini curlhandler pushhandler main
|
||||||
$(BUILDCOMMAND) src/simpleini/ConvertUTF.o src/pushhandler.o src/main.o $(LIBS) -o $(TARGET)
|
$(BUILDCOMMAND) src/simpleini/ConvertUTF.o src/curlhandler.o src/pushhandler.o src/main.o $(LIBS) -o $(TARGET)
|
||||||
|
|
||||||
|
|
||||||
# simpleini
|
# simpleini
|
||||||
|
|
@ -25,6 +25,14 @@ src/simpleini/ConvertUTF.o: src/simpleini/ConvertUTF.c
|
||||||
$(BUILDCOMMAND) -c src/simpleini/ConvertUTF.c -o src/simpleini/ConvertUTF.o
|
$(BUILDCOMMAND) -c src/simpleini/ConvertUTF.c -o src/simpleini/ConvertUTF.o
|
||||||
|
|
||||||
|
|
||||||
|
# curlhandler
|
||||||
|
.PHONY: curlhandler
|
||||||
|
curlhandler: src/curlhandler.o
|
||||||
|
|
||||||
|
src/curlhandler.o: src/curlhandler.cpp
|
||||||
|
$(BUILDCOMMAND) -c src/curlhandler.cpp -o src/curlhandler.o
|
||||||
|
|
||||||
|
|
||||||
# pushhandler
|
# pushhandler
|
||||||
.PHONY: pushhandler
|
.PHONY: pushhandler
|
||||||
pushhandler: src/pushhandler.o
|
pushhandler: src/pushhandler.o
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* pusher
|
* pusher
|
||||||
* (c) 2014-2015 Daniel Stein
|
* (c) 2014-2016 Daniel Stein
|
||||||
* https://github.com/HackHerz/pusher
|
* https://github.com/HackHerz/pusher
|
||||||
*
|
*
|
||||||
* TODO
|
* TODO
|
||||||
|
|
|
||||||
|
|
@ -1,83 +1,15 @@
|
||||||
#include "pushhandler.h"
|
#include "pushhandler.h"
|
||||||
#include "json/json.hpp"
|
#include "json/json.hpp"
|
||||||
|
#include "curlhandler.h"
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <curl/curl.h>
|
|
||||||
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
|
||||||
// urlDecode matches
|
|
||||||
string matches[][2] = {
|
|
||||||
{"$", "%24"},
|
|
||||||
{"&", "%26"},
|
|
||||||
{"+", "%2B"},
|
|
||||||
{",", "%2C"},
|
|
||||||
{"/", "%2F"},
|
|
||||||
{":", "%3A"},
|
|
||||||
{";", "%3B"},
|
|
||||||
{"=", "%3D"},
|
|
||||||
{"?", "%3F"},
|
|
||||||
{"@", "%40"}
|
|
||||||
};
|
|
||||||
|
|
||||||
// needed for urlencoding
|
|
||||||
string 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// curl wrapper
|
|
||||||
string curlHandler(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// constructor only username
|
// constructor only username
|
||||||
PushHandler::PushHandler(string username)
|
PushHandler::PushHandler(string username)
|
||||||
{
|
{
|
||||||
|
|
@ -104,7 +36,7 @@ string PushHandler::login(string password)
|
||||||
|
|
||||||
// network request
|
// network request
|
||||||
string readBuffer;
|
string readBuffer;
|
||||||
readBuffer = curlHandler(requestData.str(), URL_PN_LOGIN);
|
readBuffer = CurlHandler::request(requestData.str(), URL_PN_LOGIN);
|
||||||
|
|
||||||
// json parsing
|
// json parsing
|
||||||
stringstream jsonData;
|
stringstream jsonData;
|
||||||
|
|
@ -133,7 +65,7 @@ vector<PushHandler::Device> PushHandler::getDevices()
|
||||||
|
|
||||||
// network request
|
// network request
|
||||||
string readBuffer;
|
string readBuffer;
|
||||||
readBuffer = curlHandler(requestData.str(), URL_PN_GET_DEVICES);
|
readBuffer =CurlHandler::request(requestData.str(), URL_PN_GET_DEVICES);
|
||||||
|
|
||||||
// json parsing
|
// json parsing
|
||||||
stringstream jsonData;
|
stringstream jsonData;
|
||||||
|
|
@ -180,7 +112,7 @@ bool PushHandler::verifyToken()
|
||||||
|
|
||||||
// network request
|
// network request
|
||||||
string readBuffer;
|
string readBuffer;
|
||||||
readBuffer = curlHandler(requestData.str(), URL_PN_CHECK_TOKEN);
|
readBuffer = CurlHandler::request(requestData.str(), URL_PN_CHECK_TOKEN);
|
||||||
|
|
||||||
// json parser
|
// json parser
|
||||||
stringstream jsonData;
|
stringstream jsonData;
|
||||||
|
|
@ -216,11 +148,11 @@ void PushHandler::sendToDevice(string id, string message)
|
||||||
requestData << "&app=" << APP_PACKAGE;
|
requestData << "&app=" << APP_PACKAGE;
|
||||||
requestData << "&deviceID=" << id;
|
requestData << "&deviceID=" << id;
|
||||||
requestData << "&type=" << "MESSAGE";
|
requestData << "&type=" << "MESSAGE";
|
||||||
requestData << "&content=" << urlDecode(message);
|
requestData << "&content=" << CurlHandler::urlDecode(message);
|
||||||
|
|
||||||
// network request
|
// network request
|
||||||
string readBuffer;
|
string readBuffer;
|
||||||
readBuffer = curlHandler(requestData.str(), URL_PN_SEND_TO_DEVICE);
|
readBuffer = CurlHandler::request(requestData.str(), URL_PN_SEND_TO_DEVICE);
|
||||||
|
|
||||||
// json parsing
|
// json parsing
|
||||||
stringstream jsonData;
|
stringstream jsonData;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
// Change this line if you are using your own API-Token
|
// Change this line if you are using your own API-Token
|
||||||
const char API_TOKEN[] = {
|
const char API_TOKEN[] = {
|
||||||
0x38, 0x45, 0x37, 0x44, 0x38, 0x42, 0x32,
|
0x38, 0x45, 0x37, 0x44, 0x38, 0x42, 0x32,
|
||||||
0x44, 0x44, 0x45, 0x37, 0x44, 0x44, 0x45,
|
0x44, 0x44, 0x45, 0x37, 0x44, 0x44, 0x45,
|
||||||
0x37, 0x44, 0x36, 0x43, 0x33, 0x56, 0x35,
|
0x37, 0x44, 0x36, 0x43, 0x33, 0x56, 0x35,
|
||||||
0x32, 0x56, 0x42, 0x35, 0x32, 0x56, 0x42,
|
0x32, 0x56, 0x42, 0x35, 0x32, 0x56, 0x42,
|
||||||
|
|
@ -14,7 +14,6 @@ const char API_TOKEN[] = {
|
||||||
0x54, 0x54, 0x54, 0x4b, 0x46, 0x46, 0x42 };
|
0x54, 0x54, 0x54, 0x4b, 0x46, 0x46, 0x42 };
|
||||||
|
|
||||||
#define APP_PACKAGE "com.hackherz.pusher"
|
#define APP_PACKAGE "com.hackherz.pusher"
|
||||||
#define USER_AGENT "pusher/0.2"
|
|
||||||
|
|
||||||
#define URL_PN_LOGIN "http://a.pushnotifier.de/1/login/"
|
#define URL_PN_LOGIN "http://a.pushnotifier.de/1/login/"
|
||||||
#define URL_PN_CHECK_TOKEN "http://a.pushnotifier.de/1/checkToken/"
|
#define URL_PN_CHECK_TOKEN "http://a.pushnotifier.de/1/checkToken/"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue