Commit 17cb47bb authored by armin's avatar armin
Browse files

Merge branch 'fixReturnCodes'

parents 7ce94065 feb3742c
......@@ -52,15 +52,15 @@ void ApplePushHandler::onEOM() noexcept {
ResponseBuilder(downstream_).status(200, "OK").body("").sendWithEOM();
}else if(Settings::forwardGatewayEnabled()){
ForwardGatewayModel forwardModel;
ForwardGatewayModel forwardModel{};
if(forwardModel.forwardMessage(std::move(mHeaders), body)){
ResponseBuilder(downstream_).status(200, "OK").body("").sendWithEOM();
}else{
ResponseBuilder(downstream_).status(500, "FAILURE").body("failed to send push message through forwardgateway").sendWithEOM();
ResponseBuilder(downstream_).status(forwardModel.returnStatusCode(), "FAILURE").body("failed to send push message through forwardgateway").sendWithEOM();
}
}
else {
ResponseBuilder(downstream_).status(500, "FAILURE").body("failed to send push message").sendWithEOM();
ResponseBuilder(downstream_).status(applePushModel.returnStatusCode(), "FAILURE").body("failed to send push message").sendWithEOM();
}
......
......@@ -50,14 +50,14 @@ void GooglePushHandler::onEOM() noexcept {
ResponseBuilder(downstream_).status(200, "OK").body("").sendWithEOM();
} else if(Settings::forwardGatewayEnabled()){
ForwardGatewayModel forwardModel;
ForwardGatewayModel forwardModel{};
if(forwardModel.forwardMessage(std::move(mHeaders), body)){
ResponseBuilder(downstream_).status(200, "OK").body("").sendWithEOM();
}else{
ResponseBuilder(downstream_).status(500, "FAILURE").body("failed to send push message through forwardgateway").sendWithEOM();
ResponseBuilder(downstream_).status(forwardModel.returnStatusCode(), "FAILURE").body("failed to send push message through forwardgateway").sendWithEOM();
}
}else {
ResponseBuilder(downstream_).status(500, "FAILURE").body("failed to send push message").sendWithEOM();
ResponseBuilder(downstream_).status(googlePushModel.returnStatusCode(), "FAILURE").body("failed to send push message").sendWithEOM();
}
......
......@@ -120,10 +120,11 @@ size_t ApplePushModel::curlWriteCallback(void *buffer, size_t size, size_t nmemb
reader.parse(bufferString, obj);
if (obj.isMember("reason") && obj["reason"].asString() == "DeviceTokenNotForTopic") {
ForwardGatewayModel::claimRegistrationId(thiz->mDeviceToken);
thiz->mReturnStatusCode = 406;
return 0;
}
}
return 1;
return nmemb;
}
bool ApplePushModel::sendMessage() {
......@@ -211,22 +212,26 @@ bool ApplePushModel::sendMessage() {
LOG(INFO) << "\tApple push device token rejected, forward message to forwardgateway";
} else {
LOG(ERROR) << "\tApple push conn error: " << curl_easy_strerror(res);
mReturnStatusCode = 500;
}
} else {
LOG(INFO) << uuidString << "\tApple push conn status: OK";
curl_easy_cleanup(curl);
curl_slist_free_all(chunk);
mReturnStatusCode = 200;
return true;
}
} catch (std::exception &e) {
LOG(INFO) << e.what();
curl_easy_cleanup(curl);
curl_slist_free_all(chunk);
mReturnStatusCode = 500;
return false;
}
curl_easy_cleanup(curl);
curl_slist_free_all(chunk);
}else{
mReturnStatusCode = 500;
}
return false;
}
......@@ -267,3 +272,7 @@ void ApplePushModel::initFromSettings() {
mTeamId = Settings::apnsTeamId();
mKey = Settings::apnsKey();
}
int ApplePushModel::returnStatusCode() const {
return mReturnStatusCode;
}
......@@ -35,10 +35,9 @@ public:
static size_t curlWriteCallback(void *buffer, size_t size, size_t nmemb,
void *this_ptr);
int returnStatusCode() const;
private:
// Pusher mPusher;
const std::string mApiUrl{"https://api.push.apple.com/3/device/"};
static std::string mPem;
......@@ -57,8 +56,10 @@ private:
bool mSent{false};
int mSendind{0};
std::string mTopic;
int mBadge{0};
int mReturnStatusCode;
};
......
......@@ -5,6 +5,7 @@
#include <curl/curl.h>
#include <iostream>
#include <cstdlib>
#include <jsoncpp/json/json.h>
#include "../date.h"
#include "ForwardGatewayModel.h"
......@@ -30,12 +31,20 @@ void ForwardGatewayModel::unClaimRegistrationId(const std::string &pRegistration
size_t ForwardGatewayModel::curlWriteCallback(void *buffer, size_t size, size_t nmemb, void *this_ptr) {
auto thiz = static_cast<ForwardGatewayModel *>(this_ptr);
Json::Reader reader;
Json::Value obj;
if (buffer != nullptr) {
std::string bufferString(static_cast<char *>(buffer), nmemb);
reader.parse(bufferString, obj);
if(obj.isMember("status")){
thiz->mReturnStatusCode = obj["status"].asInt();
}
LOG(ERROR)<<"forwardgateway error response: "<<bufferString;
return 0;
}
return 1;
return nmemb;
}
bool ForwardGatewayModel::forwardMessage(std::unique_ptr<HTTPMessage> pHeaders, const std::string &pBody) {
......@@ -62,6 +71,7 @@ bool ForwardGatewayModel::forwardMessage(std::unique_ptr<HTTPMessage> pHeaders,
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, true);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
res = curl_easy_perform(curl);
......@@ -71,21 +81,30 @@ bool ForwardGatewayModel::forwardMessage(std::unique_ptr<HTTPMessage> pHeaders,
}else{
LOG(ERROR)<< "\tForwardgateway error: " << curl_easy_strerror(res);
mReturnStatusCode = 500;
}
} else {
LOG(INFO) << "\tForwardgateway conn status: OK ";
curl_easy_cleanup(curl);
curl_slist_free_all(chunk);
mReturnStatusCode = 200;
return true;
}
} catch (std::exception &e) {
LOG(ERROR) << e.what();
curl_easy_cleanup(curl);
curl_slist_free_all(chunk);
mReturnStatusCode = 500;
return false;
}
curl_easy_cleanup(curl);
curl_slist_free_all(chunk);
}else{
mReturnStatusCode = 500;
}
return false;
}
int ForwardGatewayModel::returnStatusCode() const {
return mReturnStatusCode;
}
......@@ -25,8 +25,9 @@ public:
static size_t curlWriteCallback(void *buffer, size_t size, size_t nmemb,
void *this_ptr);
int returnStatusCode() const;
private:
int mReturnStatusCode;
static std::mutex mRegistrationIdsMutex;
static std::unordered_set<std::string> mRegistrationIds;
......
......@@ -174,12 +174,13 @@ size_t GooglePushModel::curlWriteCallback(void *buffer, size_t size, size_t nmem
for(const auto &elem: results){
if(elem.isMember("error") && elem["error"].asString() == "MismatchSenderId"){
ForwardGatewayModel::claimRegistrationId(thiz->mDeviceToken);
thiz->mReturnStatusCode = 406;
return 0;
}
}
}
}
return 1;
return nmemb;
}
bool GooglePushModel::sendMessage() {
......@@ -243,16 +244,20 @@ bool GooglePushModel::sendMessage() {
LOG(INFO) << uuidString << "\tGoogle push device token rejected, forward message to forwardgateway";
}else{
LOG(ERROR) << uuidString << "\tGoogle push conn error: " << curl_easy_strerror(res);
mReturnStatusCode = 500;
}
} else {
LOG(INFO) << uuidString << "\tGoogle push conn status: OK";
curl_easy_cleanup(curl);
curl_slist_free_all(chunk);
mReturnStatusCode = 200;
return true;
}
curl_easy_cleanup(curl);
curl_slist_free_all(chunk);
}else{
mReturnStatusCode = 500;
}
return false;
}
......@@ -261,3 +266,7 @@ void GooglePushModel::initFromSettings() {
mApiKey = Settings::fcmServerKey();
}
int GooglePushModel::returnStatusCode() const {
return mReturnStatusCode;
}
......@@ -41,10 +41,12 @@ public:
char *data, size_t size,
void *userp);
static void initFromSettings();
static size_t curlWriteCallback(void *buffer, size_t size, size_t nmemb,
void *this_ptr);
static void initFromSettings();
int returnStatusCode() const;
private:
static std::string mApiKey;
......@@ -65,6 +67,7 @@ private:
std::string mTopic;
int mBadge{0};
int mReturnStatusCode;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment