commit
d002c528f5
5 files changed
+1510
No files matched your search
@@ -0,0 +1,60 @@
|
||||
set(_external_sources
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../common/src/Common.cpp"
|
||||
src/ExternalShortener.cpp)
|
||||
|
||||
add_library(EchoionUrlShortenerExternalPlugin MODULE
|
||||
${_external_sources}
|
||||
src/Plugin.cpp)
|
||||
target_include_directories(EchoionUrlShortenerExternalPlugin PRIVATE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../common/include")
|
||||
target_compile_features(EchoionUrlShortenerExternalPlugin PRIVATE cxx_std_26)
|
||||
target_link_libraries(EchoionUrlShortenerExternalPlugin PRIVATE
|
||||
Echoion::ApiPlugin
|
||||
Echoion::ApiConfig
|
||||
Void::ShortUrl
|
||||
Void::Data
|
||||
Void::Net
|
||||
Void::Text
|
||||
Boost::url
|
||||
Threads::Threads)
|
||||
set_target_properties(EchoionUrlShortenerExternalPlugin PROPERTIES
|
||||
PREFIX ""
|
||||
OUTPUT_NAME "url-shortener-external"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins"
|
||||
BUILD_RPATH_USE_ORIGIN ON
|
||||
INSTALL_RPATH ""
|
||||
INSTALL_RPATH_USE_LINK_PATH FALSE)
|
||||
|
||||
if(MSVC)
|
||||
target_compile_options(EchoionUrlShortenerExternalPlugin PRIVATE /W4 /WX-)
|
||||
else()
|
||||
target_compile_options(EchoionUrlShortenerExternalPlugin PRIVATE
|
||||
-Wall -Wextra -Wpedantic)
|
||||
endif()
|
||||
|
||||
install(TARGETS EchoionUrlShortenerExternalPlugin
|
||||
LIBRARY DESTINATION
|
||||
"${CMAKE_INSTALL_DATADIR}/echoion/bootstrap-plugins/url-shortener-external/${ECHOION_URL_SHORTENER_EXTERNAL_VERSION}"
|
||||
COMPONENT Runtime
|
||||
RUNTIME DESTINATION
|
||||
"${CMAKE_INSTALL_DATADIR}/echoion/bootstrap-plugins/url-shortener-external/${ECHOION_URL_SHORTENER_EXTERNAL_VERSION}"
|
||||
COMPONENT Runtime)
|
||||
|
||||
if(BUILD_TESTS)
|
||||
add_executable(echoion_url_shortener_external_tests
|
||||
${_external_sources}
|
||||
tests/ExternalShortenerTests.cpp)
|
||||
target_include_directories(echoion_url_shortener_external_tests PRIVATE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../common/include")
|
||||
target_compile_features(echoion_url_shortener_external_tests PRIVATE
|
||||
cxx_std_26)
|
||||
target_link_libraries(echoion_url_shortener_external_tests PRIVATE
|
||||
Void::ShortUrl Void::Data Void::Net Void::Text Boost::url Threads::Threads)
|
||||
add_test(NAME echoion_url_shortener_external_tests
|
||||
COMMAND echoion_url_shortener_external_tests)
|
||||
set_tests_properties(echoion_url_shortener_external_tests PROPERTIES
|
||||
TIMEOUT 20)
|
||||
endif()
|
||||
@@ -0,0 +1,97 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Net/Http.hpp>
|
||||
#include <Void/ShortUrl/ShortUrl.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Echoion::Plugins::ShortUrl
|
||||
{
|
||||
enum class ExternalEncoding { query, json, form };
|
||||
enum class ExternalResponseKind { plain, header, jsonPointer };
|
||||
|
||||
struct ExternalFieldTemplate {
|
||||
std::string name;
|
||||
std::string value;
|
||||
};
|
||||
|
||||
struct ExternalHeaderTemplate {
|
||||
std::string name;
|
||||
std::string value;
|
||||
bool sensitive{};
|
||||
};
|
||||
|
||||
struct ExternalResponseConfig {
|
||||
ExternalResponseKind kind{ExternalResponseKind::jsonPointer};
|
||||
std::string selector{"/shortUrl"};
|
||||
};
|
||||
|
||||
struct ExternalConfig {
|
||||
std::string endpoint;
|
||||
Void::Net::Method method{Void::Net::Method::Post};
|
||||
ExternalEncoding encoding{ExternalEncoding::json};
|
||||
std::vector<ExternalFieldTemplate> fields;
|
||||
std::vector<ExternalHeaderTemplate> headers;
|
||||
ExternalResponseConfig response;
|
||||
bool allowHttp{};
|
||||
bool allowPrivateHosts{};
|
||||
bool allowPrivateTargets{};
|
||||
bool allowCrossHostRedirects{};
|
||||
int maxRedirects{};
|
||||
std::uint32_t timeoutSeconds{3};
|
||||
std::size_t maximumResponseBytes{64U * 1024U};
|
||||
std::size_t cacheCapacity{512};
|
||||
std::uint32_t cacheTtlSeconds{3600};
|
||||
std::size_t maximumConcurrency{4};
|
||||
};
|
||||
|
||||
[[nodiscard]] std::expected<ExternalConfig, std::string>
|
||||
parseExternalConfig(std::string_view payload);
|
||||
|
||||
class ExternalShortener
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]] static std::expected<
|
||||
std::unique_ptr<ExternalShortener>, std::string>
|
||||
create(ExternalConfig config);
|
||||
|
||||
~ExternalShortener();
|
||||
ExternalShortener(const ExternalShortener&) = delete;
|
||||
ExternalShortener& operator=(const ExternalShortener&) = delete;
|
||||
|
||||
[[nodiscard]] Void::ShortUrl::Health health() const;
|
||||
[[nodiscard]] Void::ShortUrl::ShortenResult
|
||||
shorten(const Void::ShortUrl::ShortenRequest& request);
|
||||
|
||||
/// Pure request/extraction seams used by focused tests and provider
|
||||
/// configuration diagnostics. They never perform network I/O.
|
||||
[[nodiscard]] std::expected<Void::Net::Request, std::string>
|
||||
previewRequest(std::string_view targetUrl) const;
|
||||
[[nodiscard]] std::expected<std::string, std::string>
|
||||
previewExtract(const Void::Net::Response& response) const;
|
||||
|
||||
private:
|
||||
|
||||
struct ConstructionToken {
|
||||
explicit ConstructionToken() = default;
|
||||
};
|
||||
struct Impl;
|
||||
|
||||
public:
|
||||
|
||||
ExternalShortener(ConstructionToken, ExternalConfig config);
|
||||
|
||||
private:
|
||||
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Plugins::ShortUrl
|
||||
File diff suppressed because it is too large.
Load diff
+143
@@ -0,0 +1,143 @@
|
||||
#include <echoion/plugins/short_url/ExternalShortener.hpp>
|
||||
|
||||
#include <ExtensionPlugin.hpp>
|
||||
#include <Void/Plugin/Plugin.hpp>
|
||||
#include <Void/Service/Service.hpp>
|
||||
#include <Void/ShortUrl/ShortUrl.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
namespace Echoion::Plugins::ShortUrl
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr std::string_view kPluginId{"url-shortener-external"};
|
||||
}
|
||||
|
||||
class ExternalShortenerPlugin final : public Api::IExtensionPlugin,
|
||||
public Void::ShortUrl::IShortener
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]] std::string_view name() const noexcept override
|
||||
{
|
||||
return kPluginId;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string_view provider_id() const noexcept override
|
||||
{
|
||||
return kPluginId;
|
||||
}
|
||||
|
||||
void applyConfig(const char* config)
|
||||
{
|
||||
configPayload_ = config != nullptr ? config : "";
|
||||
}
|
||||
|
||||
void on_load() override
|
||||
{
|
||||
shortener_.reset();
|
||||
activationError_.clear();
|
||||
auto config = parseExternalConfig(configPayload_);
|
||||
if (!config) {
|
||||
activationError_ = std::move(config).error();
|
||||
return;
|
||||
}
|
||||
auto shortener = ExternalShortener::create(std::move(*config));
|
||||
if (!shortener) {
|
||||
activationError_ = std::move(shortener).error();
|
||||
return;
|
||||
}
|
||||
shortener_ = std::move(*shortener);
|
||||
}
|
||||
|
||||
void on_unload() override { shortener_.reset(); }
|
||||
|
||||
[[nodiscard]] Void::ShortUrl::Health health() const override
|
||||
{
|
||||
if (shortener_ != nullptr) {
|
||||
return shortener_->health();
|
||||
}
|
||||
return {
|
||||
.status = Void::ShortUrl::HealthStatus::unhealthy,
|
||||
.message = activationError_.empty()
|
||||
? "plugin is not configured"
|
||||
: activationError_,
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::ShortUrl::ShortenResult
|
||||
shorten(const Void::ShortUrl::ShortenRequest& request) override
|
||||
{
|
||||
if (shortener_ == nullptr) {
|
||||
return {.error = {
|
||||
.code = Void::ShortUrl::ErrorCode::unavailable,
|
||||
.message = "external shortener is unavailable",
|
||||
}};
|
||||
}
|
||||
return shortener_->shorten(request);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::string configPayload_;
|
||||
std::string activationError_;
|
||||
std::unique_ptr<ExternalShortener> shortener_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Plugins::ShortUrl
|
||||
|
||||
extern "C" {
|
||||
|
||||
void* void_plugin_create()
|
||||
{
|
||||
return std::make_unique<
|
||||
Echoion::Plugins::ShortUrl::ExternalShortenerPlugin>()
|
||||
.release();
|
||||
}
|
||||
|
||||
void void_plugin_destroy(void* instance)
|
||||
{
|
||||
std::unique_ptr<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin>{
|
||||
static_cast<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin*>(
|
||||
instance)};
|
||||
}
|
||||
|
||||
Void::Plugin::PluginInfo void_plugin_info()
|
||||
{
|
||||
return {
|
||||
.abi_version = Void::Plugin::ABI_VERSION,
|
||||
.name = "url-shortener-external",
|
||||
.plugin_version = "1.0.0",
|
||||
.description = "Configurable external URL-shortening adapter",
|
||||
};
|
||||
}
|
||||
|
||||
void void_plugin_configure(void* instance, const char* config)
|
||||
{
|
||||
static_cast<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin*>(instance)
|
||||
->applyConfig(config);
|
||||
}
|
||||
|
||||
int void_plugin_ready(void* instance)
|
||||
{
|
||||
const auto health =
|
||||
static_cast<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin*>(
|
||||
instance)
|
||||
->health();
|
||||
return health.status == Void::ShortUrl::HealthStatus::healthy ? 0 : 1;
|
||||
}
|
||||
|
||||
Void::Service::IService* void_plugin_service(void* instance)
|
||||
{
|
||||
auto* plugin =
|
||||
static_cast<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin*>(
|
||||
instance);
|
||||
return static_cast<Void::Service::IService*>(
|
||||
static_cast<Void::ShortUrl::IShortener*>(plugin));
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,168 @@
|
||||
#include <echoion/plugins/short_url/ExternalShortener.hpp>
|
||||
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/ShortUrl/ShortUrl.hpp>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace
|
||||
{
|
||||
class EnvironmentVariable
|
||||
{
|
||||
public:
|
||||
|
||||
EnvironmentVariable(std::string name, std::string value)
|
||||
: name_(std::move(name))
|
||||
{
|
||||
setenv(name_.c_str(), value.c_str(), 1);
|
||||
}
|
||||
|
||||
~EnvironmentVariable() { unsetenv(name_.c_str()); }
|
||||
|
||||
EnvironmentVariable(const EnvironmentVariable&) = delete;
|
||||
EnvironmentVariable& operator=(const EnvironmentVariable&) = delete;
|
||||
|
||||
private:
|
||||
|
||||
std::string name_;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
using Echoion::Plugins::ShortUrl::ExternalShortener;
|
||||
using Echoion::Plugins::ShortUrl::parseExternalConfig;
|
||||
|
||||
int failures{};
|
||||
const auto check = [&failures](const bool condition,
|
||||
const std::string_view message) {
|
||||
if (!condition) {
|
||||
std::cerr << message << '\n';
|
||||
++failures;
|
||||
}
|
||||
};
|
||||
|
||||
EnvironmentVariable token{"ECHOION_SHORTENER_TEST_TOKEN", "test secret"};
|
||||
const auto jsonConfig = R"JSON({
|
||||
"endpoint":"https://short.example/api",
|
||||
"method":"POST",
|
||||
"encoding":"json",
|
||||
"fields":{"longUrl":"${url}","tenant":"${env:ECHOION_SHORTENER_TEST_TOKEN}"},
|
||||
"headers":{"Authorization":"Bearer ${env:ECHOION_SHORTENER_TEST_TOKEN}"},
|
||||
"response":{"type":"json","pointer":"/data/short"}
|
||||
})JSON";
|
||||
auto parsed = parseExternalConfig(jsonConfig);
|
||||
check(parsed.has_value(), "JSON provider configuration was rejected");
|
||||
if (!parsed) {
|
||||
return 1;
|
||||
}
|
||||
auto shortener = ExternalShortener::create(std::move(*parsed));
|
||||
check(shortener.has_value(), "external shortener could not be created");
|
||||
if (!shortener) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto request =
|
||||
(*shortener)->previewRequest("https://example.com/a?x=1&y=2");
|
||||
check(request.has_value() && request->body.has_value(),
|
||||
"JSON request was not constructed");
|
||||
if (request && request->body) {
|
||||
auto body = Void::Json::parse(*request->body);
|
||||
check(body &&
|
||||
body->get_str("longUrl") == "https://example.com/a?x=1&y=2",
|
||||
"target URL was not safely represented in JSON");
|
||||
check(body && body->get_str("tenant") == "test secret",
|
||||
"environment template was not expanded in a field");
|
||||
check(request->headers.size() == 2,
|
||||
"configured and automatic Content-Type headers were not emitted");
|
||||
}
|
||||
|
||||
Void::Net::Response jsonResponse{
|
||||
.status = 200,
|
||||
.body = R"({"data":{"short":"https://sho.rt/Ab1"}})",
|
||||
};
|
||||
auto extracted = (*shortener)->previewExtract(jsonResponse);
|
||||
check(extracted && *extracted == "https://sho.rt/Ab1",
|
||||
"JSON Pointer response extraction failed");
|
||||
|
||||
const auto formConfig = R"JSON({
|
||||
"endpoint":"https://short.example/api",
|
||||
"method":"PATCH",
|
||||
"encoding":"form",
|
||||
"fields":{"long url":"${url}"},
|
||||
"response":{"type":"plain"}
|
||||
})JSON";
|
||||
auto form = parseExternalConfig(formConfig);
|
||||
check(form.has_value(), "form provider configuration was rejected");
|
||||
if (form) {
|
||||
auto formShortener = ExternalShortener::create(std::move(*form));
|
||||
auto formRequest =
|
||||
formShortener
|
||||
? (*formShortener)
|
||||
->previewRequest("https://example.com/a?x=1&y=2")
|
||||
: std::expected<Void::Net::Request, std::string>{
|
||||
std::unexpected{"create failed"}};
|
||||
check(formRequest &&
|
||||
formRequest->body ==
|
||||
"long+url=https%3A%2F%2Fexample.com%2Fa%3Fx%3D1%26y%3D2",
|
||||
"form request was not percent encoded");
|
||||
}
|
||||
|
||||
const auto queryConfig = R"JSON({
|
||||
"endpoint":"https://short.example/api?token=${env:ECHOION_SHORTENER_TEST_TOKEN}",
|
||||
"method":"GET",
|
||||
"encoding":"query",
|
||||
"fields":{"url":"${url}"},
|
||||
"response":{"type":"header","header":"Location"}
|
||||
})JSON";
|
||||
auto query = parseExternalConfig(queryConfig);
|
||||
check(query.has_value(), "query provider configuration was rejected");
|
||||
if (query) {
|
||||
auto queryShortener = ExternalShortener::create(std::move(*query));
|
||||
auto queryRequest =
|
||||
queryShortener
|
||||
? (*queryShortener)->previewRequest("https://example.com/a?x=1")
|
||||
: std::expected<Void::Net::Request, std::string>{
|
||||
std::unexpected{"create failed"}};
|
||||
check(queryRequest &&
|
||||
queryRequest->url.find("token=test+secret") !=
|
||||
std::string::npos &&
|
||||
queryRequest->url.find("url=https%3A%2F%2Fexample.com") !=
|
||||
std::string::npos,
|
||||
"query and environment values were not percent encoded");
|
||||
if (queryShortener) {
|
||||
Void::Net::Response headerResponse{
|
||||
.status = 201,
|
||||
.headers = {{.name = "Location", .value = "https://sho.rt/Q1"}},
|
||||
};
|
||||
auto header = (*queryShortener)->previewExtract(headerResponse);
|
||||
check(header && *header == "https://sho.rt/Q1",
|
||||
"response-header extraction failed");
|
||||
}
|
||||
}
|
||||
|
||||
check(!parseExternalConfig(R"({"endpoint":"http://short.example/api"})"),
|
||||
"plain HTTP endpoint was accepted without opt-in");
|
||||
check(
|
||||
!parseExternalConfig(
|
||||
R"({"endpoint":"https://127.0.0.1/api","fields":{"url":"${url}"}})"),
|
||||
"private endpoint was accepted without opt-in");
|
||||
check(
|
||||
!parseExternalConfig(
|
||||
R"({"endpoint":"https://short.example/api","headers":{"Authorization":"${env:ECHOION_MISSING_SHORTENER_TOKEN}"}})"),
|
||||
"missing environment secret did not reject configuration");
|
||||
|
||||
const Void::ShortUrl::ShortenRequest unsupportedRequest{
|
||||
.target_url = "https://example.com/a",
|
||||
.custom_key = "wanted",
|
||||
};
|
||||
const auto unsupported = (*shortener)->shorten(unsupportedRequest);
|
||||
check(!unsupported &&
|
||||
unsupported.error.code == Void::ShortUrl::ErrorCode::unsupported,
|
||||
"external adapter silently ignored a custom key");
|
||||
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in new issue
Block a user