diff --git a/makefile b/makefile index 98e94cf..e81d13c 100644 --- a/makefile +++ b/makefile @@ -13,8 +13,8 @@ LIBS = `pkg-config libcurl --cflags --libs` # Build all: pusher -pusher: simpleini pushhandler main - $(BUILDCOMMAND) src/simpleini/ConvertUTF.o src/pushhandler.o src/main.o $(LIBS) -o $(TARGET) +$(TARGET): simpleini curlhandler pushhandler main + $(BUILDCOMMAND) src/simpleini/ConvertUTF.o src/curlhandler.o src/pushhandler.o src/main.o $(LIBS) -o $(TARGET) # simpleini @@ -25,6 +25,14 @@ src/simpleini/ConvertUTF.o: src/simpleini/ConvertUTF.c $(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 .PHONY: pushhandler pushhandler: src/pushhandler.o diff --git a/src/main.cpp b/src/main.cpp index 2d84d00..0ba81fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ /* * pusher - * (c) 2014-2015 Daniel Stein + * (c) 2014-2016 Daniel Stein * https://github.com/HackHerz/pusher * * TODO diff --git a/src/pushhandler.cpp b/src/pushhandler.cpp index c609902..fe45e74 100644 --- a/src/pushhandler.cpp +++ b/src/pushhandler.cpp @@ -1,83 +1,15 @@ #include "pushhandler.h" #include "json/json.hpp" - +#include "curlhandler.h" #include #include -#include using namespace std; 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 PushHandler::PushHandler(string username) { @@ -104,7 +36,7 @@ string PushHandler::login(string password) // network request string readBuffer; - readBuffer = curlHandler(requestData.str(), URL_PN_LOGIN); + readBuffer = CurlHandler::request(requestData.str(), URL_PN_LOGIN); // json parsing stringstream jsonData; @@ -133,7 +65,7 @@ vector PushHandler::getDevices() // network request string readBuffer; - readBuffer = curlHandler(requestData.str(), URL_PN_GET_DEVICES); + readBuffer =CurlHandler::request(requestData.str(), URL_PN_GET_DEVICES); // json parsing stringstream jsonData; @@ -180,7 +112,7 @@ bool PushHandler::verifyToken() // network request string readBuffer; - readBuffer = curlHandler(requestData.str(), URL_PN_CHECK_TOKEN); + readBuffer = CurlHandler::request(requestData.str(), URL_PN_CHECK_TOKEN); // json parser stringstream jsonData; @@ -216,11 +148,11 @@ void PushHandler::sendToDevice(string id, string message) requestData << "&app=" << APP_PACKAGE; requestData << "&deviceID=" << id; requestData << "&type=" << "MESSAGE"; - requestData << "&content=" << urlDecode(message); + requestData << "&content=" << CurlHandler::urlDecode(message); // network request string readBuffer; - readBuffer = curlHandler(requestData.str(), URL_PN_SEND_TO_DEVICE); + readBuffer = CurlHandler::request(requestData.str(), URL_PN_SEND_TO_DEVICE); // json parsing stringstream jsonData; diff --git a/src/pushhandler.h b/src/pushhandler.h index 32058ac..b59ecfb 100644 --- a/src/pushhandler.h +++ b/src/pushhandler.h @@ -6,7 +6,7 @@ // Change this line if you are using your own 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, 0x37, 0x44, 0x36, 0x43, 0x33, 0x56, 0x35, 0x32, 0x56, 0x42, 0x35, 0x32, 0x56, 0x42, @@ -14,7 +14,6 @@ const char API_TOKEN[] = { 0x54, 0x54, 0x54, 0x4b, 0x46, 0x46, 0x42 }; #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_CHECK_TOKEN "http://a.pushnotifier.de/1/checkToken/"