diff options
| author | Léo Lam <leo@innovatetechnologi.es> | 2017-06-12 17:17:05 +0200 |
|---|---|---|
| committer | Léo Lam <leo@innovatetechnologi.es> | 2017-06-13 12:52:31 +0200 |
| commit | 18678afa6de918a17800a27dfc85027eb717facd (patch) | |
| tree | f87e36f01d9ed4d6911b8f50dd5a587025dd35b9 /Source/Core/Common/Analytics.cpp | |
| parent | 77c0539b5ed87ddfdb0d0c53e8db0172254ac13e (diff) | |
Common: Add HttpRequest to simplify HTTP requests
Too much boilerplate that is duplicated if we use curl directly.
Let's add a simple wrapper class that hides the implementation details
and just allows to simply make HTTP requests and get responses.
Diffstat (limited to 'Source/Core/Common/Analytics.cpp')
| -rw-r--r-- | Source/Core/Common/Analytics.cpp | 44 |
1 files changed, 4 insertions, 40 deletions
diff --git a/Source/Core/Common/Analytics.cpp b/Source/Core/Common/Analytics.cpp index 2761e86943..57670fd1e7 100644 --- a/Source/Core/Common/Analytics.cpp +++ b/Source/Core/Common/Analytics.cpp @@ -4,7 +4,6 @@ #include <cmath> #include <cstdio> -#include <curl/curl.h> #include <string> #include "Common/Analytics.h" @@ -60,13 +59,6 @@ void AppendType(std::string* out, TypeId type) { out->push_back(static_cast<u8>(type)); } - -// Dummy write function for curl. -size_t DummyCurlWriteFunction(char* ptr, size_t size, size_t nmemb, void* userdata) -{ - return size * nmemb; -} - } // namespace AnalyticsReportBuilder::AnalyticsReportBuilder() @@ -187,44 +179,16 @@ void StdoutAnalyticsBackend::Send(std::string report) HexDump(reinterpret_cast<const u8*>(report.data()), report.size()).c_str()); } -HttpAnalyticsBackend::HttpAnalyticsBackend(const std::string& endpoint) +HttpAnalyticsBackend::HttpAnalyticsBackend(const std::string& endpoint) : m_endpoint(endpoint) { - CURL* curl = curl_easy_init(); - if (curl) - { - // libcurl may not have been built with async DNS support, so we disable - // signal handlers to avoid a possible and likely crash if a resolve times out. - curl_easy_setopt(curl, CURLOPT_NOSIGNAL, true); - curl_easy_setopt(curl, CURLOPT_URL, endpoint.c_str()); - curl_easy_setopt(curl, CURLOPT_POST, true); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &DummyCurlWriteFunction); - curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 3000); - -#ifdef _WIN32 - // ALPN support is enabled by default but requires Windows >= 8.1. - curl_easy_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, false); -#endif - - m_curl = curl; - } } -HttpAnalyticsBackend::~HttpAnalyticsBackend() -{ - if (m_curl) - { - curl_easy_cleanup(m_curl); - } -} +HttpAnalyticsBackend::~HttpAnalyticsBackend() = default; void HttpAnalyticsBackend::Send(std::string report) { - if (!m_curl) - return; - - curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, report.c_str()); - curl_easy_setopt(m_curl, CURLOPT_POSTFIELDSIZE, report.size()); - curl_easy_perform(m_curl); + if (m_http.IsValid()) + m_http.Post(m_endpoint, report); } } // namespace Common |
