421 lines
11 KiB
C++
421 lines
11 KiB
C++
#pragma once
|
|
|
|
#include <algorithm>
|
|
|
|
#include "../common/adc_utils.h"
|
|
|
|
|
|
/*
|
|
|
|
ABSTRACT DEVICE COMPONENTS LIBRARY
|
|
|
|
*/
|
|
|
|
|
|
#include "../common/adc_traits.h"
|
|
|
|
namespace adc
|
|
{
|
|
|
|
|
|
/*
|
|
* Very simple various protocols endpoint parser and holder class
|
|
*
|
|
* endpoint: proto_mark://host_name:port_num/path
|
|
* where "part" is optional for all non-local protocol kinds;
|
|
*
|
|
* for local kind of protocols the endpoint must be given as:
|
|
* local://stream/PATH
|
|
* local://datagram/PATH
|
|
* local://seqpacket/PATH
|
|
* where 'stream', 'datagram' and 'seqpacket' "host_name"-field marks the
|
|
* stream-type, datagram-type and seqpacket-type UNIX domain sockets protocols.
|
|
* here, possible "port_num" field is allowed but ignored.
|
|
*
|
|
* NOTE: "proto_mark" and "host_name" (for local kind) fields are parsed in case-insensitive manner!
|
|
*
|
|
*
|
|
*/
|
|
|
|
|
|
class AdcEndpoint
|
|
{
|
|
public:
|
|
static constexpr std::string_view protoHostDelim = "://";
|
|
static constexpr std::string_view hostPortDelim = ":";
|
|
static constexpr std::string_view portPathDelim = "/";
|
|
|
|
enum proto_id_t : uint8_t {
|
|
PROTO_ID_LOCAL,
|
|
PROTO_ID_SEQLOCAL,
|
|
PROTO_ID_TCP,
|
|
PROTO_ID_TLS,
|
|
PROTO_ID_UDP,
|
|
PROTO_ID_WS,
|
|
PROTO_ID_WSS,
|
|
PROTO_ID_UNKNOWN
|
|
};
|
|
|
|
static constexpr std::string_view protoMarkLocal{"local"}; // UNIX domain
|
|
static constexpr std::string_view protoMarkTCP{"tcp"}; // TCP
|
|
static constexpr std::string_view protoMarkTLS{"tls"}; // TLS
|
|
static constexpr std::string_view protoMarkUDP{"udp"}; // UDP
|
|
static constexpr std::string_view protoMarkWS{"ws"}; // Websocket
|
|
static constexpr std::string_view protoMarkWSS{"wss"}; // Secure Websocket
|
|
|
|
static constexpr std::array validProtoMarks{protoMarkLocal, protoMarkTCP, protoMarkTLS,
|
|
protoMarkUDP, protoMarkWS, protoMarkWSS};
|
|
|
|
|
|
static constexpr std::string_view localProtoTypeStream{"stream"}; // UNIX domain stream
|
|
static constexpr std::string_view localProtoTypeDatagram{"datagram"}; // UNIX domain datagram
|
|
static constexpr std::string_view localProtoTypeSeqpacket{"seqpacket"}; // UNIX domain seqpacket
|
|
|
|
static constexpr std::array validLocalProtoTypes{localProtoTypeStream, localProtoTypeDatagram,
|
|
localProtoTypeSeqpacket};
|
|
|
|
|
|
template <traits::adc_input_char_range R>
|
|
AdcEndpoint(const R& ept)
|
|
{
|
|
fromRange(ept);
|
|
}
|
|
|
|
AdcEndpoint(const AdcEndpoint& other)
|
|
{
|
|
copyInst(other);
|
|
}
|
|
|
|
AdcEndpoint(AdcEndpoint&& other)
|
|
{
|
|
moveInst(std::move(other));
|
|
}
|
|
|
|
virtual ~AdcEndpoint() = default;
|
|
|
|
|
|
AdcEndpoint& operator=(const AdcEndpoint& other)
|
|
{
|
|
copyInst(other);
|
|
|
|
return *this;
|
|
}
|
|
|
|
|
|
AdcEndpoint& operator=(AdcEndpoint&& other)
|
|
{
|
|
moveInst(std::move(other));
|
|
|
|
return *this;
|
|
}
|
|
|
|
template <traits::adc_input_char_range R>
|
|
requires std::ranges::contiguous_range<R>
|
|
bool fromRange(const R& ept)
|
|
{
|
|
_isValid = false;
|
|
|
|
// at least 'ws://a' (proto, proto-host delimiter and at least a single character of hostname)
|
|
if (std::ranges::size(ept) < 6) {
|
|
return _isValid;
|
|
}
|
|
|
|
if constexpr (std::is_array_v<std::remove_cvref_t<R>>) {
|
|
_endpoint = ept;
|
|
} else {
|
|
_endpoint.clear();
|
|
std::ranges::copy(ept, std::back_inserter(_endpoint));
|
|
}
|
|
|
|
auto found = std::ranges::search(_endpoint, protoHostDelim);
|
|
if (found.empty()) {
|
|
return _isValid;
|
|
}
|
|
|
|
|
|
ssize_t idx;
|
|
if ((idx = checkProtoMark(std::string_view{_endpoint.begin(), found.begin()})) < 0) {
|
|
return _isValid;
|
|
}
|
|
|
|
_proto = validProtoMarks[idx];
|
|
|
|
_host = std::string_view{found.end(), _endpoint.end()};
|
|
|
|
auto f1 = std::ranges::search(_host, portPathDelim);
|
|
// std::string_view port_sv;
|
|
if (f1.empty() && isLocal()) { // no path, but it is mandatory for 'local'!
|
|
return _isValid;
|
|
} else {
|
|
_host = std::string_view(_host.begin(), f1.begin());
|
|
|
|
_path = std::string_view(f1.end(), &*_endpoint.end());
|
|
|
|
f1 = std::ranges::search(_host, hostPortDelim);
|
|
if (f1.empty() && !isLocal()) { // no port, but it is mandatory for non-local!
|
|
return _isValid;
|
|
}
|
|
|
|
_portView = std::string_view(f1.end(), _host.end());
|
|
if (_portView.size()) {
|
|
_host = std::string_view(_host.begin(), f1.begin());
|
|
|
|
if (!isLocal()) {
|
|
// convert port string to int
|
|
auto end_ptr = _portView.data() + _portView.size();
|
|
|
|
auto [ptr, ec] = std::from_chars(_portView.data(), end_ptr, _port);
|
|
if (ec != std::errc() || ptr != end_ptr) {
|
|
return _isValid;
|
|
}
|
|
} else { // ignore for local
|
|
_port = -1;
|
|
}
|
|
} else {
|
|
_port = -1;
|
|
}
|
|
|
|
if (isLocal()) { // check for special values
|
|
idx = 0;
|
|
if (std::ranges::any_of(validLocalProtoTypes, [&idx, this](const auto& el) {
|
|
bool ok = utils::AdcCharRangeCompare(_host, el, true);
|
|
if (!ok) {
|
|
++idx;
|
|
}
|
|
return ok;
|
|
})) {
|
|
_host = validLocalProtoTypes[idx];
|
|
} else {
|
|
return _isValid;
|
|
}
|
|
}
|
|
}
|
|
|
|
_isValid = true;
|
|
|
|
return _isValid;
|
|
}
|
|
|
|
|
|
bool isValid() const
|
|
{
|
|
return _isValid;
|
|
}
|
|
|
|
|
|
auto endpoint() const
|
|
{
|
|
return _endpoint;
|
|
}
|
|
|
|
template <traits::adc_view_or_output_char_range R>
|
|
R proto() const
|
|
{
|
|
return part<R>(PROTO_PART);
|
|
}
|
|
|
|
std::string_view proto() const
|
|
{
|
|
return proto<std::string_view>();
|
|
}
|
|
|
|
template <traits::adc_view_or_output_char_range R>
|
|
R host() const
|
|
{
|
|
return part<R>(HOST_PART);
|
|
}
|
|
|
|
std::string_view host() const
|
|
{
|
|
return host<std::string_view>();
|
|
}
|
|
|
|
int port() const
|
|
{
|
|
return _port;
|
|
}
|
|
|
|
template <traits::adc_view_or_output_char_range R>
|
|
R portView() const
|
|
{
|
|
return part<R>(PORT_PART);
|
|
}
|
|
|
|
std::string_view portView() const
|
|
{
|
|
return portView<std::string_view>();
|
|
}
|
|
|
|
template <traits::adc_view_or_output_char_range R>
|
|
R path() const
|
|
{
|
|
return part<R>(PATH_PART);
|
|
}
|
|
|
|
std::string_view path() const
|
|
{
|
|
return path<std::string_view>();
|
|
}
|
|
|
|
|
|
bool isLocal() const
|
|
{
|
|
return proto() == protoMarkLocal;
|
|
}
|
|
|
|
bool isLocalStream() const
|
|
{
|
|
return host() == localProtoTypeStream;
|
|
}
|
|
|
|
bool isLocalDatagram() const
|
|
{
|
|
return host() == localProtoTypeDatagram;
|
|
}
|
|
|
|
bool isLocalSeqpacket() const
|
|
{
|
|
return host() == localProtoTypeSeqpacket;
|
|
}
|
|
|
|
|
|
bool isTCP() const
|
|
{
|
|
return proto() == protoMarkTCP;
|
|
}
|
|
|
|
bool isTLS() const
|
|
{
|
|
return proto() == protoMarkTLS;
|
|
}
|
|
|
|
bool isUDP() const
|
|
{
|
|
return proto() == protoMarkUDP;
|
|
}
|
|
|
|
bool isWS() const
|
|
{
|
|
return proto() == protoMarkWS;
|
|
}
|
|
|
|
bool isWSS() const
|
|
{
|
|
return proto() == protoMarkWSS;
|
|
}
|
|
|
|
protected:
|
|
std::string _endpoint;
|
|
std::string_view _proto, _host, _path, _portView;
|
|
int _port;
|
|
bool _isValid;
|
|
|
|
|
|
virtual ssize_t checkProtoMark(std::string_view proto_mark)
|
|
{
|
|
ssize_t idx = 0;
|
|
|
|
// case-insensitive look-up
|
|
bool found = std::ranges::any_of(AdcEndpoint::validProtoMarks, [&idx, &proto_mark](const auto& el) {
|
|
bool ok = utils::AdcCharRangeCompare(proto_mark, el, true);
|
|
|
|
if (!ok) {
|
|
++idx;
|
|
}
|
|
|
|
return ok;
|
|
});
|
|
|
|
return found ? idx : -1;
|
|
}
|
|
|
|
enum EndpointPart { PROTO_PART, HOST_PART, PATH_PART, PORT_PART };
|
|
|
|
template <traits::adc_view_or_output_char_range R>
|
|
R part(EndpointPart what) const
|
|
{
|
|
R res;
|
|
|
|
// if (!_isValid) {
|
|
// return res;
|
|
// }
|
|
|
|
auto part = _proto;
|
|
|
|
switch (what) {
|
|
case PROTO_PART:
|
|
part = _proto;
|
|
break;
|
|
case HOST_PART:
|
|
part = _host;
|
|
break;
|
|
case PATH_PART:
|
|
part = _path;
|
|
break;
|
|
case PORT_PART:
|
|
part = _portView;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if constexpr (std::ranges::view<R>) {
|
|
return {part.begin(), part.end()};
|
|
} else {
|
|
std::ranges::copy(part, std::back_inserter(res));
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
void copyInst(const AdcEndpoint& other)
|
|
{
|
|
if (&other != this) {
|
|
if (other._isValid) {
|
|
_isValid = other._isValid;
|
|
_endpoint = other._endpoint;
|
|
_proto = other._proto;
|
|
|
|
auto idx = std::distance(other._endpoint.c_str(), other._host.data());
|
|
_host = std::string_view(_endpoint.c_str() + idx, other._host.size());
|
|
|
|
idx = std::distance(other._endpoint.c_str(), other._path.data());
|
|
_path = std::string_view(_endpoint.c_str() + idx, other._path.size());
|
|
|
|
_port = other._port;
|
|
} else {
|
|
_isValid = false;
|
|
_endpoint = std::string();
|
|
_proto = std::string_view();
|
|
_host = std::string_view();
|
|
_path = std::string_view();
|
|
_port = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void moveInst(AdcEndpoint&& other)
|
|
{
|
|
if (&other != this) {
|
|
if (other._isValid) {
|
|
_isValid = std::move(other._isValid);
|
|
_endpoint = std::move(other._endpoint);
|
|
_proto = other._proto;
|
|
_host = std::move(other._host);
|
|
_path = std::move(other._path);
|
|
_port = std::move(other._port);
|
|
} else {
|
|
_isValid = false;
|
|
_endpoint = std::string();
|
|
_proto = std::string_view();
|
|
_host = std::string_view();
|
|
_path = std::string_view();
|
|
_port = -1;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
} // namespace adc
|