2017-06-12 15:17:05 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-06-12 15:17:05 +00:00
|
|
|
|
|
|
|
#include "Common/HttpRequest.h"
|
|
|
|
|
2017-06-18 19:36:39 +00:00
|
|
|
#include <chrono>
|
2017-06-12 15:17:05 +00:00
|
|
|
#include <cstddef>
|
2018-03-18 20:09:11 +00:00
|
|
|
#include <mutex>
|
2017-06-12 15:17:05 +00:00
|
|
|
|
2018-03-27 22:28:26 +00:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2017-06-12 15:17:05 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2017-06-13 10:52:10 +00:00
|
|
|
#include "Common/ScopeGuard.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2017-06-12 15:17:05 +00:00
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
class HttpRequest::Impl final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum class Method
|
|
|
|
{
|
|
|
|
GET,
|
|
|
|
POST,
|
|
|
|
};
|
|
|
|
|
2018-03-27 22:28:26 +00:00
|
|
|
explicit Impl(std::chrono::milliseconds timeout_ms, ProgressCallback callback);
|
2017-06-12 15:17:05 +00:00
|
|
|
|
|
|
|
bool IsValid() const;
|
2018-05-29 01:55:52 +00:00
|
|
|
void SetCookies(const std::string& cookies);
|
2018-08-06 21:56:40 +00:00
|
|
|
void UseIPv4();
|
2019-02-01 13:41:28 +00:00
|
|
|
void FollowRedirects(long max);
|
2023-04-07 02:10:05 +00:00
|
|
|
s32 GetLastResponseCode();
|
2017-06-13 10:52:10 +00:00
|
|
|
Response Fetch(const std::string& url, Method method, const Headers& headers, const u8* payload,
|
2019-04-12 20:10:24 +00:00
|
|
|
size_t size, AllowedReturnCodes codes = AllowedReturnCodes::Ok_Only);
|
2017-06-12 15:17:05 +00:00
|
|
|
|
2023-03-23 16:01:13 +00:00
|
|
|
static int CurlProgressCallback(Impl* impl, curl_off_t dltotal, curl_off_t dlnow,
|
|
|
|
curl_off_t ultotal, curl_off_t ulnow);
|
2019-03-30 13:50:57 +00:00
|
|
|
std::string EscapeComponent(const std::string& string);
|
2018-03-27 22:28:26 +00:00
|
|
|
|
2017-06-12 15:17:05 +00:00
|
|
|
private:
|
2019-05-27 13:46:54 +00:00
|
|
|
static inline std::once_flag s_curl_was_initialized;
|
2018-03-27 22:28:26 +00:00
|
|
|
ProgressCallback m_callback;
|
2018-03-18 20:09:11 +00:00
|
|
|
std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> m_curl{nullptr, curl_easy_cleanup};
|
2019-06-20 17:44:51 +00:00
|
|
|
std::string m_error_string;
|
2017-06-12 15:17:05 +00:00
|
|
|
};
|
|
|
|
|
2018-03-27 22:28:26 +00:00
|
|
|
HttpRequest::HttpRequest(std::chrono::milliseconds timeout_ms, ProgressCallback callback)
|
2019-05-27 13:25:05 +00:00
|
|
|
: m_impl(std::make_unique<Impl>(timeout_ms, std::move(callback)))
|
2017-06-12 15:17:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpRequest::~HttpRequest() = default;
|
|
|
|
|
|
|
|
bool HttpRequest::IsValid() const
|
|
|
|
{
|
|
|
|
return m_impl->IsValid();
|
|
|
|
}
|
|
|
|
|
2018-05-29 01:55:52 +00:00
|
|
|
void HttpRequest::SetCookies(const std::string& cookies)
|
|
|
|
{
|
|
|
|
m_impl->SetCookies(cookies);
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:56:40 +00:00
|
|
|
void HttpRequest::UseIPv4()
|
|
|
|
{
|
|
|
|
m_impl->UseIPv4();
|
|
|
|
}
|
|
|
|
|
2019-02-01 13:41:28 +00:00
|
|
|
void HttpRequest::FollowRedirects(long max)
|
|
|
|
{
|
|
|
|
m_impl->FollowRedirects(max);
|
|
|
|
}
|
|
|
|
|
2019-03-30 13:50:57 +00:00
|
|
|
std::string HttpRequest::EscapeComponent(const std::string& string)
|
2019-03-30 13:49:18 +00:00
|
|
|
{
|
|
|
|
return m_impl->EscapeComponent(string);
|
|
|
|
}
|
|
|
|
|
2023-04-07 02:10:05 +00:00
|
|
|
s32 HttpRequest::GetLastResponseCode() const
|
|
|
|
{
|
|
|
|
return m_impl->GetLastResponseCode();
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:10:24 +00:00
|
|
|
HttpRequest::Response HttpRequest::Get(const std::string& url, const Headers& headers,
|
|
|
|
AllowedReturnCodes codes)
|
2017-06-12 15:17:05 +00:00
|
|
|
{
|
2019-04-12 20:10:24 +00:00
|
|
|
return m_impl->Fetch(url, Impl::Method::GET, headers, nullptr, 0, codes);
|
2017-06-12 15:17:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 10:52:10 +00:00
|
|
|
HttpRequest::Response HttpRequest::Post(const std::string& url, const std::vector<u8>& payload,
|
2019-04-12 20:10:24 +00:00
|
|
|
const Headers& headers, AllowedReturnCodes codes)
|
2017-06-12 15:17:05 +00:00
|
|
|
{
|
2019-04-12 20:10:24 +00:00
|
|
|
return m_impl->Fetch(url, Impl::Method::POST, headers, payload.data(), payload.size(), codes);
|
2017-06-12 15:17:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 10:52:10 +00:00
|
|
|
HttpRequest::Response HttpRequest::Post(const std::string& url, const std::string& payload,
|
2019-04-12 20:10:24 +00:00
|
|
|
const Headers& headers, AllowedReturnCodes codes)
|
2017-06-12 15:17:05 +00:00
|
|
|
{
|
2017-06-13 10:52:10 +00:00
|
|
|
return m_impl->Fetch(url, Impl::Method::POST, headers,
|
2019-04-12 20:10:24 +00:00
|
|
|
reinterpret_cast<const u8*>(payload.data()), payload.size(), codes);
|
2017-06-12 15:17:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 16:01:13 +00:00
|
|
|
int HttpRequest::Impl::CurlProgressCallback(Impl* impl, curl_off_t dltotal, curl_off_t dlnow,
|
|
|
|
curl_off_t ultotal, curl_off_t ulnow)
|
2018-03-27 22:28:26 +00:00
|
|
|
{
|
|
|
|
// Abort if callback isn't true
|
2023-03-23 16:01:13 +00:00
|
|
|
return !impl->m_callback(static_cast<s64>(dltotal), static_cast<s64>(dlnow),
|
|
|
|
static_cast<s64>(ultotal), static_cast<s64>(ulnow));
|
2018-03-27 22:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HttpRequest::Impl::Impl(std::chrono::milliseconds timeout_ms, ProgressCallback callback)
|
2019-05-27 13:25:05 +00:00
|
|
|
: m_callback(std::move(callback))
|
2017-06-12 15:17:05 +00:00
|
|
|
{
|
2019-05-27 13:46:54 +00:00
|
|
|
std::call_once(s_curl_was_initialized, [] { curl_global_init(CURL_GLOBAL_DEFAULT); });
|
2018-03-18 20:09:11 +00:00
|
|
|
|
|
|
|
m_curl.reset(curl_easy_init());
|
2017-06-12 15:17:05 +00:00
|
|
|
if (!m_curl)
|
|
|
|
return;
|
|
|
|
|
2018-03-27 22:28:26 +00:00
|
|
|
curl_easy_setopt(m_curl.get(), CURLOPT_NOPROGRESS, m_callback == nullptr);
|
|
|
|
|
|
|
|
if (m_callback)
|
|
|
|
{
|
|
|
|
curl_easy_setopt(m_curl.get(), CURLOPT_PROGRESSDATA, this);
|
2023-03-23 16:01:13 +00:00
|
|
|
curl_easy_setopt(m_curl.get(), CURLOPT_XFERINFOFUNCTION, CurlProgressCallback);
|
2018-03-27 22:28:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 17:44:51 +00:00
|
|
|
// Set up error buffer
|
|
|
|
m_error_string.resize(CURL_ERROR_SIZE);
|
|
|
|
curl_easy_setopt(m_curl.get(), CURLOPT_ERRORBUFFER, m_error_string.data());
|
|
|
|
|
2017-06-12 15:17:05 +00:00
|
|
|
// 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);
|
2018-05-04 11:30:49 +00:00
|
|
|
curl_easy_setopt(m_curl.get(), CURLOPT_CONNECTTIMEOUT_MS, static_cast<long>(timeout_ms.count()));
|
|
|
|
// Sadly CURLOPT_LOW_SPEED_TIME doesn't have a millisecond variant so we have to use seconds
|
|
|
|
curl_easy_setopt(
|
|
|
|
m_curl.get(), CURLOPT_LOW_SPEED_TIME,
|
|
|
|
static_cast<long>(std::chrono::duration_cast<std::chrono::seconds>(timeout_ms).count()));
|
|
|
|
curl_easy_setopt(m_curl.get(), CURLOPT_LOW_SPEED_LIMIT, 1);
|
2017-06-12 15:17:05 +00:00
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HttpRequest::Impl::IsValid() const
|
|
|
|
{
|
|
|
|
return m_curl != nullptr;
|
|
|
|
}
|
|
|
|
|
2023-04-07 02:10:05 +00:00
|
|
|
s32 HttpRequest::Impl::GetLastResponseCode()
|
|
|
|
{
|
|
|
|
s32 response_code{};
|
|
|
|
curl_easy_getinfo(m_curl.get(), CURLINFO_RESPONSE_CODE, &response_code);
|
|
|
|
return response_code;
|
|
|
|
}
|
|
|
|
|
2018-05-29 01:55:52 +00:00
|
|
|
void HttpRequest::Impl::SetCookies(const std::string& cookies)
|
|
|
|
{
|
|
|
|
curl_easy_setopt(m_curl.get(), CURLOPT_COOKIE, cookies.c_str());
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:56:40 +00:00
|
|
|
void HttpRequest::Impl::UseIPv4()
|
|
|
|
{
|
|
|
|
curl_easy_setopt(m_curl.get(), CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
|
|
|
|
}
|
|
|
|
|
2019-02-01 13:41:28 +00:00
|
|
|
void HttpRequest::Impl::FollowRedirects(long max)
|
|
|
|
{
|
|
|
|
curl_easy_setopt(m_curl.get(), CURLOPT_FOLLOWLOCATION, 1);
|
|
|
|
curl_easy_setopt(m_curl.get(), CURLOPT_MAXREDIRS, max);
|
|
|
|
}
|
|
|
|
|
2019-03-30 13:50:57 +00:00
|
|
|
std::string HttpRequest::Impl::EscapeComponent(const std::string& string)
|
2019-03-30 13:49:18 +00:00
|
|
|
{
|
2019-04-06 11:23:55 +00:00
|
|
|
char* escaped = curl_easy_escape(m_curl.get(), string.c_str(), static_cast<int>(string.size()));
|
|
|
|
std::string escaped_str(escaped);
|
|
|
|
curl_free(escaped);
|
|
|
|
|
|
|
|
return escaped_str;
|
2019-03-30 13:49:18 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:28:26 +00:00
|
|
|
static size_t CurlWriteCallback(char* data, size_t size, size_t nmemb, void* userdata)
|
2017-06-12 15:17:05 +00:00
|
|
|
{
|
|
|
|
auto* buffer = static_cast<std::vector<u8>*>(userdata);
|
|
|
|
const size_t actual_size = size * nmemb;
|
|
|
|
buffer->insert(buffer->end(), data, data + actual_size);
|
|
|
|
return actual_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpRequest::Response HttpRequest::Impl::Fetch(const std::string& url, Method method,
|
2017-06-13 10:52:10 +00:00
|
|
|
const Headers& headers, const u8* payload,
|
2019-04-12 20:10:24 +00:00
|
|
|
size_t size, AllowedReturnCodes codes)
|
2017-06-12 15:17:05 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-06-13 10:52:10 +00:00
|
|
|
curl_slist* list = nullptr;
|
|
|
|
Common::ScopeGuard list_guard{[&list] { curl_slist_free_all(list); }};
|
2019-05-27 13:31:13 +00:00
|
|
|
for (const auto& [name, value] : headers)
|
2017-06-13 10:52:10 +00:00
|
|
|
{
|
2019-05-27 13:31:13 +00:00
|
|
|
if (!value)
|
|
|
|
list = curl_slist_append(list, (name + ':').c_str());
|
|
|
|
else if (value->empty())
|
|
|
|
list = curl_slist_append(list, (name + ';').c_str());
|
2017-06-13 10:52:10 +00:00
|
|
|
else
|
2019-05-27 13:31:13 +00:00
|
|
|
list = curl_slist_append(list, (name + ": " + *value).c_str());
|
2017-06-13 10:52:10 +00:00
|
|
|
}
|
|
|
|
curl_easy_setopt(m_curl.get(), CURLOPT_HTTPHEADER, list);
|
|
|
|
|
2017-06-12 15:17:05 +00:00
|
|
|
std::vector<u8> buffer;
|
2018-03-27 22:28:26 +00:00
|
|
|
curl_easy_setopt(m_curl.get(), CURLOPT_WRITEFUNCTION, CurlWriteCallback);
|
2017-06-12 15:17:05 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-10-23 18:41:30 +00:00
|
|
|
ERROR_LOG_FMT(COMMON, "Failed to {} {}: {}", type, url, m_error_string);
|
2017-06-12 15:17:05 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:10:24 +00:00
|
|
|
if (codes == AllowedReturnCodes::All)
|
|
|
|
return buffer;
|
|
|
|
|
2017-06-12 15:17:05 +00:00
|
|
|
long response_code = 0;
|
|
|
|
curl_easy_getinfo(m_curl.get(), CURLINFO_RESPONSE_CODE, &response_code);
|
|
|
|
if (response_code != 200)
|
|
|
|
{
|
2020-10-27 11:05:53 +00:00
|
|
|
if (buffer.empty())
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(COMMON, "Failed to {} {}: server replied with code {}", type, url,
|
|
|
|
response_code);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(COMMON, "Failed to {} {}: server replied with code {} and body\n\x1b[0m{:.{}}",
|
2022-01-13 07:05:45 +00:00
|
|
|
type, url, response_code, reinterpret_cast<char*>(buffer.data()),
|
|
|
|
static_cast<int>(buffer.size()));
|
2020-10-27 11:05:53 +00:00
|
|
|
}
|
2017-06-12 15:17:05 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
} // namespace Common
|