detach PIHTTPServer::Endpoint to base struct PIHTTP::ServerEndpoint (private header), no functionality changes in HTTP server
Ready to implement this private base struct to MQTT client
This commit is contained in:
@@ -1,14 +1,25 @@
|
||||
#include "pihttpserver.h"
|
||||
|
||||
#include "piliterals_string.h"
|
||||
#include "piserverendpoint_p.h"
|
||||
|
||||
|
||||
struct Endpoint: public PIHTTP::ServerEndpoint {
|
||||
PIHTTP::Method method = PIHTTP::Method::Unknown;
|
||||
PIHTTPServer::RequestFunction function;
|
||||
};
|
||||
|
||||
|
||||
PRIVATE_DEFINITION_START(PIHTTPServer)
|
||||
PIMap<uint, PIVector<Endpoint>> endpoints;
|
||||
PRIVATE_DEFINITION_END(PIHTTPServer)
|
||||
|
||||
|
||||
PIHTTPServer::PIHTTPServer() {
|
||||
setRequestCallback([this](const PIHTTP::MessageConst & r) -> PIHTTP::MessageMutable {
|
||||
PIHTTP::MessageMutable reply;
|
||||
reply.setCode(PIHTTP::Code::NotFound);
|
||||
auto in_path = splitPath(r.path());
|
||||
auto it = endpoints.makeReverseIterator();
|
||||
auto in_path = PIHTTP::ServerEndpoint::splitPath(r.path());
|
||||
auto it = PRIVATE->endpoints.makeReverseIterator();
|
||||
bool found = false;
|
||||
while (it.next()) {
|
||||
for (const auto & ep: it.value()) {
|
||||
@@ -41,159 +52,36 @@ PIHTTPServer::~PIHTTPServer() {
|
||||
}
|
||||
|
||||
|
||||
bool PIHTTPServer::registerPath(const PIString & path, PIHTTP::Method method, RequestFunction functor) {
|
||||
bool PIHTTPServer::registerPath(const PIString & path, PIHTTP::Method method, PIHTTP::RequestFunction functor) {
|
||||
Endpoint ep;
|
||||
if (!ep.create(path)) return false;
|
||||
ep.method = method;
|
||||
ep.function = std::move(functor);
|
||||
endpoints[ep.priority] << ep;
|
||||
PRIVATE->endpoints[ep.priority] << ep;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void PIHTTPServer::registerUnhandled(RequestFunction functor) {
|
||||
void PIHTTPServer::registerUnhandled(PIHTTP::RequestFunction functor) {
|
||||
unhandled = functor;
|
||||
}
|
||||
|
||||
|
||||
void PIHTTPServer::unregisterPath(const PIString & path, PIHTTP::Method method) {
|
||||
auto pl = splitPath(path);
|
||||
auto it = endpoints.makeIterator();
|
||||
auto pl = PIHTTP::ServerEndpoint::splitPath(path);
|
||||
auto it = PRIVATE->endpoints.makeIterator();
|
||||
while (it.next()) {
|
||||
it.value().removeWhere([&pl, method](const Endpoint & ep) { return ep.path == pl && ep.method == method; });
|
||||
}
|
||||
endpoints.removeWhere([](uint, const PIVector<Endpoint> & epl) { return epl.isEmpty(); });
|
||||
PRIVATE->endpoints.removeWhere([](uint, const PIVector<Endpoint> & epl) { return epl.isEmpty(); });
|
||||
}
|
||||
|
||||
|
||||
void PIHTTPServer::unregisterPath(const PIString & path) {
|
||||
auto pl = splitPath(path);
|
||||
auto it = endpoints.makeIterator();
|
||||
auto pl = PIHTTP::ServerEndpoint::splitPath(path);
|
||||
auto it = PRIVATE->endpoints.makeIterator();
|
||||
while (it.next()) {
|
||||
it.value().removeWhere([&pl](const Endpoint & ep) { return ep.path == pl; });
|
||||
}
|
||||
endpoints.removeWhere([](uint, const PIVector<Endpoint> & epl) { return epl.isEmpty(); });
|
||||
}
|
||||
|
||||
|
||||
PIStringList PIHTTPServer::splitPath(const PIString & path) {
|
||||
auto ret = path.split("/");
|
||||
ret.removeAll({});
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIHTTPServer::PathElement::PathElement(const PIString & reg) {
|
||||
source = reg;
|
||||
if (reg == "*"_a) {
|
||||
type = Type::AnyOne;
|
||||
} else if (reg == "**"_a) {
|
||||
type = Type::AnyMany;
|
||||
} else if (reg.contains('*')) {
|
||||
type = Type::AnyPart;
|
||||
parts = reg.split('*');
|
||||
} else if (reg.contains('{')) {
|
||||
type = Type::Arguments;
|
||||
int ind = 0, eind = 0, pind = 0;
|
||||
for (;;) {
|
||||
ind = reg.find('{', ind);
|
||||
if (ind < 0) break;
|
||||
eind = reg.find('}', ind + 1);
|
||||
if (eind < 0) break;
|
||||
arguments.insert(arguments.size_s(), reg.mid(ind + 1, eind - ind - 1));
|
||||
if (ind == 0)
|
||||
parts << PIString();
|
||||
else {
|
||||
if (ind > pind)
|
||||
parts << reg.mid(pind, ind - pind);
|
||||
else if (parts.isNotEmpty()) {
|
||||
piCout << "[PIHTTPServer] Warning: sequential arguments, ignoring this path!";
|
||||
type = Type::Invalid;
|
||||
return;
|
||||
}
|
||||
}
|
||||
ind = pind = eind + 1;
|
||||
}
|
||||
if (eind < reg.size_s() - 1) parts << reg.mid(eind + 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool PIHTTPServer::PathElement::match(const PIString & in, PIMap<PIString, PIString> & ext_args) const {
|
||||
// piCout << "match" << source << "with" << in;
|
||||
if (type == Type::AnyOne) return true;
|
||||
if (type == Type::AnyPart) {
|
||||
int ind = 0;
|
||||
for (const auto & m: parts) {
|
||||
ind = in.find(m, ind);
|
||||
if (ind < 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (type == Type::Arguments) {
|
||||
int ind = 0, eind = 0;
|
||||
for (int i = 0; i < parts.size_s(); ++i) {
|
||||
const auto & m(parts[i]);
|
||||
if (m.isNotEmpty()) {
|
||||
ind = in.find(m, eind);
|
||||
if (ind < 0) return false;
|
||||
}
|
||||
if (i > 0) {
|
||||
ext_args[arguments.value(i - 1)] = in.mid(eind, ind - eind);
|
||||
}
|
||||
eind = ind + m.size_s();
|
||||
}
|
||||
if (parts.size() == arguments.size()) {
|
||||
ext_args[arguments.value(arguments.size_s() - 1)] = in.mid(eind);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return source == in;
|
||||
}
|
||||
|
||||
|
||||
uint PIHTTPServer::PathElement::priority() const {
|
||||
switch (type) {
|
||||
case Type::Fixed: return 0x10000; break;
|
||||
case Type::Arguments: return 0x1000; break;
|
||||
case Type::AnyPart: return 0x100; break;
|
||||
case Type::AnyOne: return 0x10; break;
|
||||
case Type::AnyMany: return 0x1; break;
|
||||
default: break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
bool PIHTTPServer::Endpoint::create(const PIString & p) {
|
||||
path = splitPath(p);
|
||||
prepared_path.clear();
|
||||
priority = 0;
|
||||
for (const auto & i: path) {
|
||||
PathElement pe(i);
|
||||
prepared_path << pe;
|
||||
path_types |= pe.type;
|
||||
priority += pe.priority();
|
||||
}
|
||||
return !path_types[PathElement::Type::Invalid];
|
||||
}
|
||||
|
||||
|
||||
bool PIHTTPServer::Endpoint::match(const PIStringList & in_path, PIMap<PIString, PIString> & ext_args) const {
|
||||
if (path_types[PathElement::Type::AnyMany]) {
|
||||
int any_ind = path.indexOf("**"_a);
|
||||
for (int i = 0; i < any_ind; ++i) {
|
||||
if (!prepared_path[i].match(in_path[i], ext_args)) return false;
|
||||
}
|
||||
int si = prepared_path.size_s() - 1, ii = in_path.size_s() - 1;
|
||||
for (; si > any_ind && ii >= 0; --si, --ii) {
|
||||
if (!prepared_path[si].match(in_path[ii], ext_args)) return false;
|
||||
}
|
||||
} else {
|
||||
if (in_path.size() != prepared_path.size()) return false;
|
||||
for (int i = 0; i < prepared_path.size_s(); ++i) {
|
||||
if (!prepared_path[i].match(in_path[i], ext_args)) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
PRIVATE->endpoints.removeWhere([](uint, const PIVector<Endpoint> & epl) { return epl.isEmpty(); });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user