Compare commits
3
Commits
d74a69aa34
...
9ff27483ab
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ff27483ab | ||
|
|
914eb0a232 | ||
|
|
00759450ff |
No files matched your search
@@ -102,6 +102,11 @@ target_link_libraries(EchoionCore
|
||||
Void::Async
|
||||
Void::Containers
|
||||
Void::Resilience
|
||||
Void::Text
|
||||
Void::Time
|
||||
Void::Math
|
||||
Void::Net
|
||||
Void::Sqlite
|
||||
PRIVATE
|
||||
Threads::Threads
|
||||
qjs
|
||||
|
||||
@@ -1,28 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "boost/asio/ssl/context.hpp"
|
||||
#include <NetworkInterface.hpp>
|
||||
#include <NetworkTypes.hpp>
|
||||
#include <Types.hpp>
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <format>
|
||||
#include <future>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
|
||||
// Boost.Beast includes needed for template function
|
||||
#include <boost/beast.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
// Forward declaration for URL parsing
|
||||
struct UrlComponents;
|
||||
|
||||
// HTTP client implementing Api::INetworkClient. A thin adapter over
|
||||
// Void::Net::HttpClient (HTTP/HTTPS, TLS, SOCKS5 + HTTP-CONNECT proxies);
|
||||
// retry/backoff is layered here via Void::Retry.
|
||||
class NetworkClient : public Api::INetworkClient
|
||||
{
|
||||
public:
|
||||
@@ -30,7 +26,6 @@ namespace Echoion::Core
|
||||
NetworkClient();
|
||||
~NetworkClient() override;
|
||||
|
||||
// Initialize with optional proxy
|
||||
Api::Result<void> initialize(
|
||||
const std::optional<Api::ProxyConfig>& proxy = std::nullopt);
|
||||
void shutdown();
|
||||
@@ -86,9 +81,17 @@ namespace Echoion::Core
|
||||
return parseJson<T>(response->bodyAsString());
|
||||
}
|
||||
|
||||
// Proxy configuration
|
||||
void setProxy(const std::optional<Api::ProxyConfig>& proxy);
|
||||
[[nodiscard]] std::optional<Api::ProxyConfig> getProxy() const;
|
||||
void clearProxy();
|
||||
|
||||
// Retry configuration
|
||||
void setRetryConfig(const Api::RetryConfig& config);
|
||||
[[nodiscard]] Api::RetryConfig getRetryConfig() const;
|
||||
|
||||
private:
|
||||
|
||||
// Parse a response body into a reflectable type T via Void::Json.
|
||||
template <typename T>
|
||||
[[nodiscard]] static Api::Result<T>
|
||||
parseJson(const std::string& body)
|
||||
@@ -111,128 +114,17 @@ namespace Echoion::Core
|
||||
return std::move(*value);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
// Proxy configuration
|
||||
void setProxy(const std::optional<Api::ProxyConfig>& proxy);
|
||||
[[nodiscard]] std::optional<Api::ProxyConfig> getProxy() const;
|
||||
void clearProxy();
|
||||
|
||||
// Retry configuration
|
||||
void setRetryConfig(const Api::RetryConfig& config);
|
||||
[[nodiscard]] Api::RetryConfig getRetryConfig() const;
|
||||
|
||||
private:
|
||||
|
||||
// Request execution
|
||||
// Execute with retry/backoff (Void::Retry); single attempt via
|
||||
// Void::Net::HttpClient.
|
||||
[[nodiscard]] Api::Result<Api::HttpResponse>
|
||||
executeWithRetry(const Api::HttpRequest& req);
|
||||
[[nodiscard]] Api::Result<Api::HttpResponse>
|
||||
executeRequest(const Api::HttpRequest& req);
|
||||
|
||||
// Proxy and direct execution methods
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
executeWithProxy(boost::asio::io_context& ioc,
|
||||
boost::asio::ssl::context& ctx,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
const Api::ProxyConfig& proxy,
|
||||
std::chrono::seconds timeout);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
executeWithHttpProxy(boost::asio::io_context& ioc,
|
||||
boost::asio::ssl::context& ctx,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
const Api::ProxyConfig& proxy,
|
||||
std::chrono::seconds timeout);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
executeWithSocks5Proxy(boost::asio::io_context& ioc,
|
||||
boost::asio::ssl::context& ctx,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
const Api::ProxyConfig& proxy,
|
||||
std::chrono::seconds timeout);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
executeDirect(boost::asio::io_context& ioc,
|
||||
boost::asio::ssl::context& ctx,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
std::chrono::seconds timeout);
|
||||
|
||||
// HTTP request methods
|
||||
// Note: sendHttpsRequest is a template, implemented in header below
|
||||
template <typename Stream>
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
sendHttpsRequest(Stream& stream,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
sendHttpRequest(boost::asio::ip::tcp::socket& socket,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse sendHttpRequestThroughProxy(
|
||||
boost::asio::ip::tcp::socket& socket,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
const Api::ProxyConfig& proxy);
|
||||
|
||||
// Request/Response building
|
||||
[[nodiscard]] boost::beast::http::request<
|
||||
boost::beast::http::string_body>
|
||||
buildBeastRequest(const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse convertBeastResponse(
|
||||
const boost::beast::http::response<
|
||||
boost::beast::http::dynamic_body>& beastResponse);
|
||||
|
||||
[[nodiscard]] std::vector<Api::HttpHeader>
|
||||
addDefaultHeaders(const std::vector<Api::HttpHeader>& headers);
|
||||
|
||||
std::optional<Api::ProxyConfig> proxy_;
|
||||
Api::RetryConfig retryConfig_;
|
||||
mutable std::mutex mutex_;
|
||||
std::atomic<bool> initialized_{false};
|
||||
std::atomic<uint32_t> requestId_{0};
|
||||
|
||||
// Thread pool for async operations
|
||||
std::vector<std::jthread> threadPool_;
|
||||
std::queue<std::packaged_task<Api::Result<Api::HttpResponse>()>>
|
||||
taskQueue_;
|
||||
std::mutex taskMutex_;
|
||||
std::condition_variable taskCv_;
|
||||
std::atomic<bool> stopThreads_{false};
|
||||
static constexpr size_t THREAD_POOL_SIZE = 4;
|
||||
};
|
||||
|
||||
// Template implementation for sendHttpsRequest
|
||||
template <typename Stream>
|
||||
Api::HttpResponse
|
||||
NetworkClient::sendHttpsRequest(Stream& stream,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req)
|
||||
{
|
||||
namespace http = boost::beast::http;
|
||||
|
||||
// Build request
|
||||
http::request<http::string_body> request =
|
||||
buildBeastRequest(urlComp, req);
|
||||
|
||||
// Send
|
||||
http::write(stream, request);
|
||||
|
||||
// Read response
|
||||
boost::beast::flat_buffer buffer;
|
||||
http::response<http::dynamic_body> response;
|
||||
http::read(stream, buffer, response);
|
||||
|
||||
// Convert to Api::HttpResponse
|
||||
return convertBeastResponse(response);
|
||||
}
|
||||
|
||||
} // namespace Echoion::Core
|
||||
+29
-28
@@ -1,5 +1,7 @@
|
||||
#include <Database.hpp>
|
||||
#include "echoion/core/Logger.hpp"
|
||||
#include <Void/Math/Random.hpp>
|
||||
#include <Void/Sqlite/Sqlite.hpp>
|
||||
#include <cstring>
|
||||
#include <format>
|
||||
#include <shared_mutex>
|
||||
@@ -8,12 +10,20 @@
|
||||
namespace Echoion::Api
|
||||
{
|
||||
|
||||
// Internal implementation
|
||||
// Internal implementation. The connection lifecycle (open/close, WAL,
|
||||
// busy-timeout, foreign keys) is owned by Void::Sqlite::Database; `db` is a
|
||||
// non-owning handle into it so the existing raw-sqlite3 query methods and
|
||||
// the schema_migrations framework keep working byte-identically. The
|
||||
// migration mechanism is deliberately NOT switched to Void's user_version
|
||||
// Migrator: existing databases track versions in schema_migrations with
|
||||
// user_version=0, and migrations 2/3 DROP tables — re-running them would
|
||||
// destroy live data.
|
||||
class DatabaseImpl
|
||||
{
|
||||
public:
|
||||
|
||||
sqlite3* db = nullptr;
|
||||
Void::Sqlite::Database voidDb;
|
||||
sqlite3* db = nullptr; // non-owning, == voidDb.handle()
|
||||
DatabaseConfig config;
|
||||
bool initialized = false;
|
||||
int version = 0;
|
||||
@@ -35,25 +45,21 @@ namespace Echoion::Api
|
||||
|
||||
impl_->config = config;
|
||||
|
||||
int rc = sqlite3_open(config.path.c_str(), &impl_->db);
|
||||
if (rc != SQLITE_OK) {
|
||||
// Open via Void::Sqlite (RAII; sets WAL + busy_timeout + foreign_keys
|
||||
// for read-write modes). The raw handle is borrowed for the existing
|
||||
// query methods.
|
||||
auto opened = Void::Sqlite::Database::open(
|
||||
config.path, Void::Sqlite::OpenMode::ReadWriteCreate);
|
||||
if (!opened) {
|
||||
return std::unexpected(
|
||||
Error(ErrorCode::DatabaseError,
|
||||
std::format("Failed to open database: {}",
|
||||
sqlite3_errmsg(impl_->db))));
|
||||
opened.error().message())));
|
||||
}
|
||||
impl_->voidDb = std::move(*opened);
|
||||
impl_->db = impl_->voidDb.handle();
|
||||
|
||||
// Enable WAL mode for better concurrency
|
||||
if (config.walMode) {
|
||||
char* errMsg = nullptr;
|
||||
sqlite3_exec(impl_->db, "PRAGMA journal_mode=WAL;", nullptr,
|
||||
nullptr, &errMsg);
|
||||
if (errMsg) {
|
||||
sqlite3_free(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
// Set busy timeout
|
||||
// Honor the configured busy timeout (Void defaults to 5s).
|
||||
sqlite3_busy_timeout(impl_->db,
|
||||
static_cast<int>(config.busyTimeout.count()));
|
||||
|
||||
@@ -82,10 +88,10 @@ namespace Echoion::Api
|
||||
|
||||
std::unique_lock<std::shared_mutex> lock(mutex_);
|
||||
|
||||
if (impl_->db) {
|
||||
sqlite3_close(impl_->db);
|
||||
impl_->db = nullptr;
|
||||
}
|
||||
// Drop the borrowed handle first, then let Void::Sqlite close the
|
||||
// connection via RAII (move-assign an empty Database).
|
||||
impl_->db = nullptr;
|
||||
impl_->voidDb = Void::Sqlite::Database{};
|
||||
impl_->initialized = false;
|
||||
}
|
||||
|
||||
@@ -1026,14 +1032,9 @@ namespace Echoion::Api
|
||||
Error(ErrorCode::NotFound, "Subscriber not found"));
|
||||
}
|
||||
|
||||
// Generate a random token (32 characters)
|
||||
const char charset[] =
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
std::string token;
|
||||
token.reserve(32);
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
token += charset[rand() % (sizeof(charset) - 1)];
|
||||
}
|
||||
// Generate a random 32-character alphanumeric token (Void::Random is
|
||||
// seeded from std::random_device, unlike the former rand()).
|
||||
const std::string token = Void::Random::random_string(32);
|
||||
|
||||
// Insert token
|
||||
const char* sql = R"(
|
||||
|
||||
+124
-814
File diff suppressed because it is too large.
Load diff
+9
-195
@@ -2,6 +2,9 @@
|
||||
#include "echoion/core/YouTubeClient.hpp"
|
||||
#include <LoggerInterface.hpp>
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Text/Html.hpp>
|
||||
#include <Void/Text/Url.hpp>
|
||||
#include <Void/Time/Format.hpp>
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cctype>
|
||||
@@ -58,168 +61,13 @@ namespace Echoion::Core
|
||||
// URL encode helper function
|
||||
std::string YouTubeClient::urlEncode(std::string_view value)
|
||||
{
|
||||
std::ostringstream encoded;
|
||||
encoded.fill('0');
|
||||
encoded << std::hex;
|
||||
|
||||
for (char c : value) {
|
||||
if (std::isalnum(static_cast<unsigned char>(c)) || c == '-' ||
|
||||
c == '_' || c == '.' || c == '~') {
|
||||
encoded << c;
|
||||
} else {
|
||||
encoded << std::uppercase;
|
||||
encoded << '%' << std::setw(2)
|
||||
<< static_cast<int>(static_cast<unsigned char>(c));
|
||||
encoded << std::nouppercase;
|
||||
}
|
||||
}
|
||||
|
||||
return encoded.str();
|
||||
return Void::Encoding::Url::encode(value);
|
||||
}
|
||||
|
||||
// HTML entity decode helper function
|
||||
std::string YouTubeClient::decodeHtmlEntities(std::string_view value)
|
||||
{
|
||||
std::string result;
|
||||
result.reserve(value.size());
|
||||
|
||||
for (size_t i = 0; i < value.size(); ++i) {
|
||||
if (value[i] == '&' && i + 1 < value.size()) {
|
||||
// Check for numeric entities &#nnnn; or &#xhhhh;
|
||||
if (value[i + 1] == '#') {
|
||||
size_t endPos = value.find(';', i + 2);
|
||||
if (endPos != std::string_view::npos) {
|
||||
try {
|
||||
if (i + 2 < value.size() && value[i + 2] == 'x') {
|
||||
// Hexadecimal: &#xhhhh;
|
||||
std::string hex(
|
||||
value.substr(i + 3, endPos - i - 3));
|
||||
char32_t codePoint =
|
||||
std::stoul(hex, nullptr, 16);
|
||||
// Convert to UTF-8
|
||||
if (codePoint <= 0x7F) {
|
||||
result += static_cast<char>(codePoint);
|
||||
} else if (codePoint <= 0x7FF) {
|
||||
result += static_cast<char>(
|
||||
0xC0 | (codePoint >> 6));
|
||||
result += static_cast<char>(
|
||||
0x80 | (codePoint & 0x3F));
|
||||
} else if (codePoint <= 0xFFFF) {
|
||||
result += static_cast<char>(
|
||||
0xE0 | (codePoint >> 12));
|
||||
result += static_cast<char>(
|
||||
0x80 | ((codePoint >> 6) & 0x3F));
|
||||
result += static_cast<char>(
|
||||
0x80 | (codePoint & 0x3F));
|
||||
} else {
|
||||
result += static_cast<char>(
|
||||
0xF0 | (codePoint >> 18));
|
||||
result += static_cast<char>(
|
||||
0x80 | ((codePoint >> 12) & 0x3F));
|
||||
result += static_cast<char>(
|
||||
0x80 | ((codePoint >> 6) & 0x3F));
|
||||
result += static_cast<char>(
|
||||
0x80 | (codePoint & 0x3F));
|
||||
}
|
||||
} else {
|
||||
// Decimal: &#nnnn;
|
||||
std::string num(
|
||||
value.substr(i + 2, endPos - i - 2));
|
||||
char32_t codePoint = std::stoul(num);
|
||||
// Convert to UTF-8
|
||||
if (codePoint <= 0x7F) {
|
||||
result += static_cast<char>(codePoint);
|
||||
} else if (codePoint <= 0x7FF) {
|
||||
result += static_cast<char>(
|
||||
0xC0 | (codePoint >> 6));
|
||||
result += static_cast<char>(
|
||||
0x80 | (codePoint & 0x3F));
|
||||
} else if (codePoint <= 0xFFFF) {
|
||||
result += static_cast<char>(
|
||||
0xE0 | (codePoint >> 12));
|
||||
result += static_cast<char>(
|
||||
0x80 | ((codePoint >> 6) & 0x3F));
|
||||
result += static_cast<char>(
|
||||
0x80 | (codePoint & 0x3F));
|
||||
} else {
|
||||
result += static_cast<char>(
|
||||
0xF0 | (codePoint >> 18));
|
||||
result += static_cast<char>(
|
||||
0x80 | ((codePoint >> 12) & 0x3F));
|
||||
result += static_cast<char>(
|
||||
0x80 | ((codePoint >> 6) & 0x3F));
|
||||
result += static_cast<char>(
|
||||
0x80 | (codePoint & 0x3F));
|
||||
}
|
||||
}
|
||||
i = endPos;
|
||||
continue;
|
||||
} catch (...) {
|
||||
// Fall through to append '&'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for named entities
|
||||
size_t endPos = value.find(';', i + 1);
|
||||
if (endPos != std::string_view::npos && endPos - i <= 10) {
|
||||
std::string_view entity =
|
||||
value.substr(i + 1, endPos - i - 1);
|
||||
if (entity == "quot") {
|
||||
result += '"';
|
||||
i = endPos;
|
||||
continue;
|
||||
} else if (entity == "amp") {
|
||||
result += '&';
|
||||
i = endPos;
|
||||
continue;
|
||||
} else if (entity == "apos") {
|
||||
result += '\'';
|
||||
i = endPos;
|
||||
continue;
|
||||
} else if (entity == "lt") {
|
||||
result += '<';
|
||||
i = endPos;
|
||||
continue;
|
||||
} else if (entity == "gt") {
|
||||
result += '>';
|
||||
i = endPos;
|
||||
continue;
|
||||
} else if (entity == "nbsp") {
|
||||
result += ' ';
|
||||
i = endPos;
|
||||
continue;
|
||||
} else if (entity == "copy") {
|
||||
result += '\xC2\xA9';
|
||||
i = endPos;
|
||||
continue;
|
||||
} else if (entity == "reg") {
|
||||
result += '\xC2\xAE';
|
||||
i = endPos;
|
||||
continue;
|
||||
} else if (entity == "trade") {
|
||||
result += '\xE2\x84\xA2';
|
||||
i = endPos;
|
||||
continue;
|
||||
} else if (entity == "mdash") {
|
||||
result += '\xE2\x80\x94';
|
||||
i = endPos;
|
||||
continue;
|
||||
} else if (entity == "ndash") {
|
||||
result += '\xE2\x80\x93';
|
||||
i = endPos;
|
||||
continue;
|
||||
} else if (entity == "hellip") {
|
||||
result += '\xE2\x80\xA6';
|
||||
i = endPos;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
result += value[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
return Void::Text::html_unescape(value);
|
||||
}
|
||||
|
||||
YouTubeClient::YouTubeClient(std::shared_ptr<NetworkClient> networkClient)
|
||||
@@ -900,45 +748,11 @@ namespace Echoion::Core
|
||||
std::chrono::seconds
|
||||
YouTubeClient::parseIso8601Duration(const std::string& duration)
|
||||
{
|
||||
// Parse ISO 8601 duration format (PT#M#S)
|
||||
// Examples: PT3M30S, PT1H2M3S, PT45S
|
||||
std::chrono::seconds result{0};
|
||||
|
||||
if (duration.empty() || duration[0] != 'P' || duration[1] != 'T') {
|
||||
return result;
|
||||
// Delegate to Void; an unparseable value yields zero (prior behaviour).
|
||||
if (auto d = Void::Time::parse_iso8601_interval(duration)) {
|
||||
return *d;
|
||||
}
|
||||
|
||||
size_t pos = 2;
|
||||
int hours = 0, minutes = 0, seconds = 0;
|
||||
|
||||
while (pos < duration.size()) {
|
||||
size_t numEnd = pos;
|
||||
while (numEnd < duration.size() && std::isdigit(duration[numEnd])) {
|
||||
++numEnd;
|
||||
}
|
||||
|
||||
if (numEnd == pos) {
|
||||
break;
|
||||
}
|
||||
|
||||
int value = std::stoi(duration.substr(pos, numEnd - pos));
|
||||
|
||||
if (numEnd < duration.size()) {
|
||||
char unit = duration[numEnd];
|
||||
if (unit == 'H') {
|
||||
hours = value;
|
||||
} else if (unit == 'M') {
|
||||
minutes = value;
|
||||
} else if (unit == 'S') {
|
||||
seconds = value;
|
||||
}
|
||||
pos = numEnd + 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return std::chrono::seconds(hours * 3600 + minutes * 60 + seconds);
|
||||
return std::chrono::seconds{0};
|
||||
}
|
||||
|
||||
Api::Result<Api::SearchResult> YouTubeClient::searchWithYtDlp(
|
||||
|
||||
@@ -3,9 +3,8 @@
|
||||
#include "echoion/core/state/StateSerializer.hpp"
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
#include <Void/Time/Format.hpp>
|
||||
#include <format>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
@@ -133,12 +132,7 @@ namespace Echoion::Core
|
||||
|
||||
std::string StateSerializer::getCurrentTimestampISO8601()
|
||||
{
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto time_t_now = std::chrono::system_clock::to_time_t(now);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << std::put_time(std::gmtime(&time_t_now), "%Y-%m-%dT%H:%M:%SZ");
|
||||
return ss.str();
|
||||
return Void::Time::utc_now_iso8601();
|
||||
}
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user