2023-12-22 11:57:49 +00:00
|
|
|
// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0+
|
2022-04-18 10:07:08 +00:00
|
|
|
|
|
|
|
#pragma once
|
2023-11-21 06:12:58 +00:00
|
|
|
|
2022-04-18 10:07:08 +00:00
|
|
|
#include "common/Pcsx2Defs.h"
|
2023-11-21 06:12:58 +00:00
|
|
|
|
2022-04-18 10:07:08 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2023-11-21 06:12:58 +00:00
|
|
|
#include <variant>
|
2022-04-18 10:07:08 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-11-21 06:12:58 +00:00
|
|
|
class ProgressCallback;
|
|
|
|
|
2023-11-06 13:23:44 +00:00
|
|
|
class HTTPDownloader
|
2022-04-18 10:07:08 +00:00
|
|
|
{
|
2023-11-06 13:23:44 +00:00
|
|
|
public:
|
|
|
|
enum : s32
|
2022-04-18 10:07:08 +00:00
|
|
|
{
|
2023-11-06 13:23:44 +00:00
|
|
|
HTTP_STATUS_CANCELLED = -3,
|
|
|
|
HTTP_STATUS_TIMEOUT = -2,
|
|
|
|
HTTP_STATUS_ERROR = -1,
|
|
|
|
HTTP_STATUS_OK = 200
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Request
|
|
|
|
{
|
|
|
|
using Data = std::vector<u8>;
|
|
|
|
using Callback = std::function<void(s32 status_code, const std::string& content_type, Data data)>;
|
|
|
|
|
|
|
|
enum class Type
|
2022-04-18 10:07:08 +00:00
|
|
|
{
|
2023-11-06 13:23:44 +00:00
|
|
|
Get,
|
|
|
|
Post,
|
2022-04-18 10:07:08 +00:00
|
|
|
};
|
|
|
|
|
2023-11-06 13:23:44 +00:00
|
|
|
enum class State
|
2022-04-18 10:07:08 +00:00
|
|
|
{
|
2023-11-06 13:23:44 +00:00
|
|
|
Pending,
|
|
|
|
Cancelled,
|
|
|
|
Started,
|
|
|
|
Receiving,
|
|
|
|
Complete,
|
2022-04-18 10:07:08 +00:00
|
|
|
};
|
|
|
|
|
2023-11-06 13:23:44 +00:00
|
|
|
HTTPDownloader* parent;
|
|
|
|
Callback callback;
|
2023-11-21 06:12:58 +00:00
|
|
|
ProgressCallback* progress;
|
2023-11-06 13:23:44 +00:00
|
|
|
std::string url;
|
|
|
|
std::string post_data;
|
|
|
|
std::string content_type;
|
|
|
|
Data data;
|
|
|
|
u64 start_time;
|
|
|
|
s32 status_code = 0;
|
|
|
|
u32 content_length = 0;
|
2023-11-21 06:12:58 +00:00
|
|
|
u32 last_progress_update = 0;
|
2023-11-06 13:23:44 +00:00
|
|
|
Type type = Type::Get;
|
|
|
|
std::atomic<State> state{State::Pending};
|
|
|
|
};
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:23:44 +00:00
|
|
|
HTTPDownloader();
|
|
|
|
virtual ~HTTPDownloader();
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-21 06:12:58 +00:00
|
|
|
static std::unique_ptr<HTTPDownloader> Create(std::string user_agent = DEFAULT_USER_AGENT);
|
2023-11-06 13:23:44 +00:00
|
|
|
static std::string URLEncode(const std::string_view& str);
|
|
|
|
static std::string URLDecode(const std::string_view& str);
|
|
|
|
static std::string GetExtensionForContentType(const std::string& content_type);
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:23:44 +00:00
|
|
|
void SetTimeout(float timeout);
|
|
|
|
void SetMaxActiveRequests(u32 max_active_requests);
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-21 06:12:58 +00:00
|
|
|
void CreateRequest(std::string url, Request::Callback callback, ProgressCallback* progress = nullptr);
|
|
|
|
void CreatePostRequest(std::string url, std::string post_data, Request::Callback callback, ProgressCallback* progress = nullptr);
|
2023-11-06 13:23:44 +00:00
|
|
|
void PollRequests();
|
|
|
|
void WaitForAllRequests();
|
|
|
|
bool HasAnyRequests();
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:23:44 +00:00
|
|
|
static const char DEFAULT_USER_AGENT[];
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:23:44 +00:00
|
|
|
protected:
|
|
|
|
virtual Request* InternalCreateRequest() = 0;
|
|
|
|
virtual void InternalPollRequests() = 0;
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:23:44 +00:00
|
|
|
virtual bool StartRequest(Request* request) = 0;
|
|
|
|
virtual void CloseRequest(Request* request) = 0;
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:23:44 +00:00
|
|
|
void LockedAddRequest(Request* request);
|
|
|
|
u32 LockedGetActiveRequestCount();
|
|
|
|
void LockedPollRequests(std::unique_lock<std::mutex>& lock);
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:23:44 +00:00
|
|
|
float m_timeout;
|
|
|
|
u32 m_max_active_requests;
|
2022-04-18 10:07:08 +00:00
|
|
|
|
2023-11-06 13:23:44 +00:00
|
|
|
std::mutex m_pending_http_request_lock;
|
|
|
|
std::vector<Request*> m_pending_http_requests;
|
|
|
|
};
|