2024-07-30 11:42:36 +00:00
|
|
|
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team
|
|
|
|
// SPDX-License-Identifier: GPL-3.0+
|
2022-04-18 10:07:08 +00:00
|
|
|
|
|
|
|
#include "common/HTTPDownloaderCurl.h"
|
|
|
|
#include "common/Assertions.h"
|
|
|
|
#include "common/Console.h"
|
|
|
|
#include "common/StringUtil.h"
|
|
|
|
#include "common/Timer.h"
|
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
#include "fmt/format.h"
|
|
|
|
|
2022-04-18 10:07:08 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
HTTPDownloaderCurl::HTTPDownloaderCurl()
|
|
|
|
: HTTPDownloader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
HTTPDownloaderCurl::~HTTPDownloaderCurl()
|
|
|
|
{
|
|
|
|
if (m_multi_handle)
|
|
|
|
curl_multi_cleanup(m_multi_handle);
|
|
|
|
}
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-21 06:12:58 +00:00
|
|
|
std::unique_ptr<HTTPDownloader> HTTPDownloader::Create(std::string user_agent)
|
2022-04-18 10:07:08 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<HTTPDownloaderCurl> instance(std::make_unique<HTTPDownloaderCurl>());
|
2023-11-21 06:12:58 +00:00
|
|
|
if (!instance->Initialize(std::move(user_agent)))
|
2022-04-18 10:07:08 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool s_curl_initialized = false;
|
|
|
|
static std::once_flag s_curl_initialized_once_flag;
|
|
|
|
|
2023-11-21 06:12:58 +00:00
|
|
|
bool HTTPDownloaderCurl::Initialize(std::string user_agent)
|
2022-04-18 10:07:08 +00:00
|
|
|
{
|
|
|
|
if (!s_curl_initialized)
|
|
|
|
{
|
|
|
|
std::call_once(s_curl_initialized_once_flag, []() {
|
|
|
|
s_curl_initialized = curl_global_init(CURL_GLOBAL_ALL) == CURLE_OK;
|
|
|
|
if (s_curl_initialized)
|
|
|
|
{
|
|
|
|
std::atexit([]() {
|
|
|
|
curl_global_cleanup();
|
|
|
|
s_curl_initialized = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!s_curl_initialized)
|
|
|
|
{
|
|
|
|
Console.Error("curl_global_init() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
m_multi_handle = curl_multi_init();
|
|
|
|
if (!m_multi_handle)
|
|
|
|
{
|
|
|
|
Console.Error("curl_multi_init() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-21 06:12:58 +00:00
|
|
|
m_user_agent = std::move(user_agent);
|
2022-04-18 10:07:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t HTTPDownloaderCurl::WriteCallback(char* ptr, size_t size, size_t nmemb, void* userdata)
|
|
|
|
{
|
|
|
|
Request* req = static_cast<Request*>(userdata);
|
|
|
|
const size_t current_size = req->data.size();
|
|
|
|
const size_t transfer_size = size * nmemb;
|
|
|
|
const size_t new_size = current_size + transfer_size;
|
|
|
|
req->data.resize(new_size);
|
2023-11-21 06:12:58 +00:00
|
|
|
req->start_time = Common::Timer::GetCurrentValue();
|
2022-04-18 10:07:08 +00:00
|
|
|
std::memcpy(&req->data[current_size], ptr, transfer_size);
|
2023-11-21 06:12:58 +00:00
|
|
|
|
|
|
|
if (req->content_length == 0)
|
|
|
|
{
|
|
|
|
curl_off_t length;
|
|
|
|
if (curl_easy_getinfo(req->handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &length) == CURLE_OK)
|
|
|
|
req->content_length = static_cast<u32>(length);
|
|
|
|
}
|
|
|
|
|
2022-04-18 10:07:08 +00:00
|
|
|
return nmemb;
|
|
|
|
}
|
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
HTTPDownloader::Request* HTTPDownloaderCurl::InternalCreateRequest()
|
2022-04-18 10:07:08 +00:00
|
|
|
{
|
2023-11-06 13:27:14 +00:00
|
|
|
Request* req = new Request();
|
|
|
|
req->handle = curl_easy_init();
|
|
|
|
if (!req->handle)
|
|
|
|
{
|
|
|
|
delete req;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
return req;
|
|
|
|
}
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
void HTTPDownloaderCurl::InternalPollRequests()
|
|
|
|
{
|
2022-04-18 10:07:08 +00:00
|
|
|
// Apparently OpenSSL can fire SIGPIPE...
|
|
|
|
sigset_t old_block_mask = {};
|
|
|
|
sigset_t new_block_mask = {};
|
|
|
|
sigemptyset(&old_block_mask);
|
|
|
|
sigemptyset(&new_block_mask);
|
|
|
|
sigaddset(&new_block_mask, SIGPIPE);
|
|
|
|
if (pthread_sigmask(SIG_BLOCK, &new_block_mask, &old_block_mask) != 0)
|
|
|
|
Console.Warning("Failed to block SIGPIPE");
|
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
int running_handles;
|
|
|
|
const CURLMcode err = curl_multi_perform(m_multi_handle, &running_handles);
|
|
|
|
if (err != CURLM_OK)
|
|
|
|
Console.Error(fmt::format("curl_multi_perform() returned {}", static_cast<int>(err)));
|
|
|
|
|
|
|
|
for (;;)
|
2022-04-18 10:07:08 +00:00
|
|
|
{
|
2023-11-06 13:27:14 +00:00
|
|
|
int msgq;
|
|
|
|
struct CURLMsg* msg = curl_multi_info_read(m_multi_handle, &msgq);
|
|
|
|
if (!msg)
|
|
|
|
break;
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
if (msg->msg != CURLMSG_DONE)
|
|
|
|
{
|
|
|
|
Console.Warning(fmt::format("Unexpected multi message {}", static_cast<int>(msg->msg)));
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
Request* req;
|
|
|
|
if (curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &req) != CURLE_OK)
|
|
|
|
{
|
|
|
|
Console.Error("curl_easy_getinfo() failed");
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
if (msg->data.result == CURLE_OK)
|
|
|
|
{
|
|
|
|
long response_code = 0;
|
|
|
|
curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &response_code);
|
|
|
|
req->status_code = static_cast<s32>(response_code);
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
char* content_type = nullptr;
|
|
|
|
if (curl_easy_getinfo(req->handle, CURLINFO_CONTENT_TYPE, &content_type) == CURLE_OK && content_type)
|
|
|
|
req->content_type = content_type;
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
DevCon.WriteLn(fmt::format("Request for '{}' returned status code {} and {} bytes", req->url, req->status_code, req->data.size()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.Error(fmt::format("Request for '{}' returned error {}", req->url, static_cast<int>(msg->data.result)));
|
|
|
|
}
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
req->state.store(Request::State::Complete, std::memory_order_release);
|
2022-04-18 10:07:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
if (pthread_sigmask(SIG_UNBLOCK, &new_block_mask, &old_block_mask) != 0)
|
|
|
|
Console.Warning("Failed to unblock SIGPIPE");
|
2022-04-18 10:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTPDownloaderCurl::StartRequest(HTTPDownloader::Request* request)
|
|
|
|
{
|
|
|
|
Request* req = static_cast<Request*>(request);
|
|
|
|
curl_easy_setopt(req->handle, CURLOPT_URL, request->url.c_str());
|
|
|
|
curl_easy_setopt(req->handle, CURLOPT_USERAGENT, m_user_agent.c_str());
|
|
|
|
curl_easy_setopt(req->handle, CURLOPT_WRITEFUNCTION, &HTTPDownloaderCurl::WriteCallback);
|
|
|
|
curl_easy_setopt(req->handle, CURLOPT_WRITEDATA, req);
|
2023-11-21 06:12:58 +00:00
|
|
|
curl_easy_setopt(req->handle, CURLOPT_NOSIGNAL, 1L);
|
2023-11-06 13:27:14 +00:00
|
|
|
curl_easy_setopt(req->handle, CURLOPT_PRIVATE, req);
|
2023-11-21 06:12:58 +00:00
|
|
|
curl_easy_setopt(req->handle, CURLOPT_FOLLOWLOCATION, 1L);
|
2022-04-18 10:07:08 +00:00
|
|
|
|
|
|
|
if (request->type == Request::Type::Post)
|
|
|
|
{
|
|
|
|
curl_easy_setopt(req->handle, CURLOPT_POST, 1L);
|
|
|
|
curl_easy_setopt(req->handle, CURLOPT_POSTFIELDS, request->post_data.c_str());
|
|
|
|
}
|
|
|
|
|
2023-11-06 13:27:14 +00:00
|
|
|
DevCon.WriteLn(fmt::format("Started HTTP request for '{}'", req->url));
|
|
|
|
req->state.store(Request::State::Started, std::memory_order_release);
|
2022-04-18 10:07:08 +00:00
|
|
|
req->start_time = Common::Timer::GetCurrentValue();
|
2023-11-06 13:27:14 +00:00
|
|
|
|
|
|
|
const CURLMcode err = curl_multi_add_handle(m_multi_handle, req->handle);
|
|
|
|
if (err != CURLM_OK)
|
|
|
|
{
|
|
|
|
Console.Error(fmt::format("curl_multi_add_handle() returned {}", static_cast<int>(err)));
|
|
|
|
req->callback(HTTP_STATUS_ERROR, std::string(), req->data);
|
|
|
|
curl_easy_cleanup(req->handle);
|
|
|
|
delete req;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-18 10:07:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPDownloaderCurl::CloseRequest(HTTPDownloader::Request* request)
|
|
|
|
{
|
|
|
|
Request* req = static_cast<Request*>(request);
|
2023-11-06 13:27:14 +00:00
|
|
|
pxAssert(req->handle);
|
|
|
|
curl_multi_remove_handle(m_multi_handle, req->handle);
|
|
|
|
curl_easy_cleanup(req->handle);
|
|
|
|
delete req;
|
2022-04-18 10:07:08 +00:00
|
|
|
}
|