Skip to content

Commit

Permalink
Common: Add HttpRequest to simplify HTTP requests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leoetlino committed Jun 12, 2017
1 parent 2b86cf0 commit bc2e57c
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 79 deletions.
44 changes: 4 additions & 40 deletions Source/Core/Common/Analytics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <cmath>
#include <cstdio>
#include <curl/curl.h>
#include <string>

#include "Common/Analytics.h"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions Source/Core/Common/Analytics.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
#include <utility>
#include <vector>

#include <curl/curl.h>

#include "Common/CommonTypes.h"
#include "Common/Event.h"
#include "Common/FifoQueue.h"
#include "Common/Flag.h"
#include "Common/HttpRequest.h"

// Utilities for analytics reporting in Dolphin. This reporting is designed to
// provide anonymous data about how well Dolphin performs in the wild. It also
Expand Down Expand Up @@ -179,7 +178,8 @@ class HttpAnalyticsBackend : public AnalyticsReportingBackend
void Send(std::string report) override;

protected:
CURL* m_curl = nullptr;
std::string m_endpoint;
HttpRequest m_http;
};

} // namespace Common
1 change: 1 addition & 0 deletions Source/Core/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(SRCS
FileUtil.cpp
GekkoDisassembler.cpp
Hash.cpp
HttpRequest.cpp
IniFile.cpp
JitRegister.cpp
MathUtil.cpp
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Common/Common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<ClInclude Include="GL\GLInterface\WGL.h" />
<ClInclude Include="GL\GLUtil.h" />
<ClInclude Include="Hash.h" />
<ClInclude Include="HttpRequest.h" />
<ClInclude Include="IniFile.h" />
<ClInclude Include="JitRegister.h" />
<ClInclude Include="LinearDiskCache.h" />
Expand Down Expand Up @@ -173,6 +174,7 @@
<ClCompile Include="GL\GLInterface\WGL.cpp" />
<ClCompile Include="GL\GLUtil.cpp" />
<ClCompile Include="Hash.cpp" />
<ClCompile Include="HttpRequest.cpp" />
<ClCompile Include="IniFile.cpp" />
<ClCompile Include="JitRegister.cpp" />
<ClCompile Include="Logging\ConsoleListenerWin.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Common/Common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<ClInclude Include="Flag.h" />
<ClInclude Include="FPURoundMode.h" />
<ClInclude Include="Hash.h" />
<ClInclude Include="HttpRequest.h" />
<ClInclude Include="IniFile.h" />
<ClInclude Include="LinearDiskCache.h" />
<ClInclude Include="MathUtil.h" />
Expand Down Expand Up @@ -263,6 +264,7 @@
<ClCompile Include="FileSearch.cpp" />
<ClCompile Include="FileUtil.cpp" />
<ClCompile Include="Hash.cpp" />
<ClCompile Include="HttpRequest.cpp" />
<ClCompile Include="IniFile.cpp" />
<ClCompile Include="MathUtil.cpp" />
<ClCompile Include="MemArena.cpp" />
Expand Down
90 changes: 90 additions & 0 deletions Source/Core/Common/HttpRequest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "Common/HttpRequest.h"

#include <cstddef>

#include "Common/Logging/Log.h"

namespace Common
{
HttpRequest::HttpRequest()
{
if (!m_curl)
return;

// 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(m_curl.get(), CURLOPT_NOSIGNAL, true);
curl_easy_setopt(m_curl.get(), CURLOPT_TIMEOUT, 3);
#ifdef _WIN32
// ALPN support is enabled by default but requires Windows >= 8.1.
curl_easy_setopt(m_curl.get(), CURLOPT_SSL_ENABLE_ALPN, false);
#endif
}

HttpRequest::~HttpRequest() = default;

bool HttpRequest::IsValid() const
{
return m_curl != nullptr;
}

static size_t CurlCallback(u8* data, size_t size, size_t nmemb, std::vector<u8>* buffer)
{
const size_t actual_size = size * nmemb;
buffer->insert(buffer->end(), data, data + actual_size);
return actual_size;
}

HttpRequest::Response HttpRequest::Get(const std::string& url)
{
return Fetch(url, Method::GET, nullptr, 0);
}

HttpRequest::Response HttpRequest::Post(const std::string& url, const std::vector<u8>& payload)
{
return Fetch(url, Method::POST, payload.data(), payload.size());
}

HttpRequest::Response HttpRequest::Post(const std::string& url, const std::string& payload)
{
return Fetch(url, Method::POST, reinterpret_cast<const u8*>(payload.data()), payload.size());
}

HttpRequest::Response HttpRequest::Fetch(const std::string& url, Method method, const u8* payload,
size_t size)
{
curl_easy_setopt(m_curl.get(), CURLOPT_POST, method == Method::POST);
curl_easy_setopt(m_curl.get(), CURLOPT_URL, url.c_str());
if (method == Method::POST)
{
curl_easy_setopt(m_curl.get(), CURLOPT_POSTFIELDS, payload);
curl_easy_setopt(m_curl.get(), CURLOPT_POSTFIELDSIZE, size);
}

std::vector<u8> buffer;
curl_easy_setopt(m_curl.get(), CURLOPT_WRITEFUNCTION, CurlCallback);
curl_easy_setopt(m_curl.get(), CURLOPT_WRITEDATA, &buffer);

const char* type = method == Method::POST ? "POST" : "GET";
const CURLcode res = curl_easy_perform(m_curl.get());
if (res != CURLE_OK)
{
ERROR_LOG(COMMON, "Failed to %s %s: %s", type, url.c_str(), curl_easy_strerror(res));
return {};
}

long response_code = 0;
curl_easy_getinfo(m_curl.get(), CURLINFO_RESPONSE_CODE, &response_code);
if (response_code != 200)
{
ERROR_LOG(COMMON, "Failed to %s %s: response code was %li", type, url.c_str(), response_code);
return {};
}

return buffer;
}
} // namespace Common
40 changes: 40 additions & 0 deletions Source/Core/Common/HttpRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <memory>
#include <optional>
#include <string>
#include <vector>

#include <curl/curl.h>

#include "Common/CommonTypes.h"

namespace Common
{
class HttpRequest final
{
public:
HttpRequest();
~HttpRequest();
bool IsValid() const;

using Response = std::optional<std::vector<u8>>;
Response Get(const std::string& url);
Response Post(const std::string& url, const std::vector<u8>& payload);
Response Post(const std::string& url, const std::string& payload);

private:
enum class Method
{
GET,
POST,
};
Response Fetch(const std::string& url, Method method, const u8* payload, size_t size);

std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> m_curl{curl_easy_init(), curl_easy_cleanup};
};
} // namespace Common
42 changes: 6 additions & 36 deletions Source/Core/Core/GeckoCodeConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,17 @@
#include "Core/GeckoCodeConfig.h"

#include <algorithm>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

#include <curl/curl.h>

#include "Common/HttpRequest.h"
#include "Common/IniFile.h"
#include "Common/Logging/Log.h"
#include "Common/StringUtil.h"

namespace Gecko
{
static size_t DownloadCodesWriteCallback(void* contents, size_t size, size_t nmemb,
std::string* body)
{
size_t realsize = size * nmemb;
body->insert(body->end(), reinterpret_cast<char*>(contents),
reinterpret_cast<char*>(contents) + realsize);
return realsize;
}

std::vector<GeckoCode> DownloadCodes(std::string gameid, bool* succeeded)
{
switch (gameid[0])
Expand All @@ -41,37 +30,18 @@ std::vector<GeckoCode> DownloadCodes(std::string gameid, bool* succeeded)
break;
}

std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> curl{curl_easy_init(), curl_easy_cleanup};

std::string endpoint{"http://geckocodes.org/txt.php?txt=" + gameid};
curl_easy_setopt(curl.get(), CURLOPT_URL, endpoint.c_str());
curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT, 5);
std::string response_body;
curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, DownloadCodesWriteCallback);
curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &response_body);

*succeeded = true;
CURLcode res = curl_easy_perform(curl.get());
if (res != CURLE_OK)
{
ERROR_LOG(COMMON, "DownloadCodes: Curl error: %s", curl_easy_strerror(res));
*succeeded = false;
Common::HttpRequest http;
const Common::HttpRequest::Response response = http.Get(endpoint);
*succeeded = response.has_value();
if (!response)
return {};
}
long response_code{0};
curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &response_code);
if (response_code != 200)
{
WARN_LOG(COMMON, "DownloadCodes: Curl response code: %li", response_code);
*succeeded = false;
return {};
}

// temp vector containing parsed codes
std::vector<GeckoCode> gcodes;

// parse the codes
std::istringstream ss(response_body);
std::istringstream ss(reinterpret_cast<const char*>(response->data()));

std::string line;

Expand Down

0 comments on commit bc2e57c

Please sign in to comment.