Skip to content

Commit 913e16c

Browse files
committed
default http implementation
1 parent 2b08477 commit 913e16c

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "Http/GAHttpCurl.h"
2+
#include "GAHTTPApi.h"
3+
#include "GALogger.h"
4+
5+
namespace gameanalytics
6+
{
7+
size_t writefunc(void *ptr, size_t size, size_t nmemb, GAHttpWrapper::Response *s)
8+
{
9+
if(!s || !ptr)
10+
{
11+
return 0;
12+
}
13+
14+
const size_t new_len = s->packet.size() + size * nmemb + 1;
15+
s->packet.reserve(new_len);
16+
17+
s->packet.insert(s->packet.end(), reinterpret_cast<char*>(ptr), reinterpret_cast<char*>(ptr) + size * nmemb);
18+
s->packet.push_back('\0');
19+
20+
return size*nmemb;
21+
}
22+
23+
void GAHttpCurl::initialize()
24+
{
25+
curl_global_init(CURL_GLOBAL_DEFAULT);
26+
}
27+
28+
void GAHttpCurl::cleanup()
29+
{
30+
curl_global_cleanup();
31+
}
32+
33+
GAHttpWrapper::Response GAHttpCurl::sendRequest(std::string const& url, std::string const& auth, std::vector<uint8_t> const& payloadData, bool useGzip, void* userData)
34+
{
35+
CURL* curl = nullptr;
36+
CURLcode res{};
37+
curl = curl_easy_init();
38+
if (!curl)
39+
{
40+
return {};
41+
}
42+
43+
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
44+
45+
GAHttpWrapper::Response s = {};
46+
47+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
48+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
49+
50+
createRequest(curl, url, auth, payloadData, useGzip);
51+
52+
res = curl_easy_perform(curl);
53+
if (res != CURLE_OK)
54+
{
55+
logging::GALogger::d("%s", curl_easy_strerror(res));
56+
return {};
57+
}
58+
59+
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &s.code);
60+
curl_easy_cleanup(curl);
61+
62+
return s;
63+
}
64+
65+
void GAHttpCurl::createRequest(CURL *curl, std::string const& url, std::string const& auth, const std::vector<uint8_t>& payloadData, bool gzip)
66+
{
67+
if(!curl)
68+
{
69+
return;
70+
}
71+
72+
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
73+
curl_easy_setopt(curl, CURLOPT_POST, 1L);
74+
struct curl_slist *header = NULL;
75+
76+
if (gzip)
77+
{
78+
header = curl_slist_append(header, "Content-Encoding: gzip");
79+
}
80+
81+
header = curl_slist_append(header, auth.c_str());
82+
83+
// always JSON
84+
header = curl_slist_append(header, "Content-Type: application/json");
85+
86+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
87+
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payloadData.data());
88+
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
89+
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, payloadData.size());
90+
}
91+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include "Http/GAHttpWrapper.h"
4+
#include <curl/curl.h>
5+
6+
namespace gameanalytics
7+
{
8+
class GAHttpCurl: public GAHttpWrapper
9+
{
10+
virtual void initialize() override;
11+
12+
virtual void cleanup() override;
13+
14+
virtual Response sendRequest(
15+
std::string const& url,
16+
std::string const& auth,
17+
std::vector<uint8_t> const& payloadData,
18+
bool useGzip,
19+
void* userData) override;
20+
21+
private:
22+
23+
void createRequest(CURL *curl, std::string const& url, std::string const& auth, const std::vector<uint8_t>& payloadData, bool gzip);
24+
};
25+
}

0 commit comments

Comments
 (0)