PIProtectedVariable - user now can`t mistake

PIHTTPServer improvements
This commit is contained in:
2024-11-18 11:11:19 +03:00
parent 3483edbd76
commit c9a5ddd89f
8 changed files with 169 additions and 53 deletions

View File

@@ -9,15 +9,20 @@ PIHTTPServer::PIHTTPServer() {
rep.setCode(404);
auto in_path = r.path.split("/");
in_path.removeAll("");
auto it = functions.makeReverseIterator();
auto it = functions.makeReverseIterator();
bool found = false;
while (it.next()) {
if (it.value().function) {
if (it.value().match(in_path)) {
rep = it.value().function(r);
break;
if (it.value().method == r.method) {
if (it.value().match(in_path)) {
rep = it.value().function(r);
found = true;
break;
}
}
}
}
if (!found && unhandled) rep = unhandled(r);
auto hit = reply_headers.makeIterator();
while (hit.next())
rep.addHeader(hit.key(), hit.value());
@@ -31,14 +36,50 @@ PIHTTPServer::~PIHTTPServer() {
}
void PIHTTPServer::registerPath(const PIString & path, RequestFunction functor) {
auto & ep(functions[path]);
void PIHTTPServer::registerPath(const PIString & path, Method method, RequestFunction functor) {
auto & ep(functions[path + PIString::fromNumber((int)method)]);
ep.path = path.split("/");
ep.method = method;
ep.function = functor;
ep.path.removeAll("");
}
void PIHTTPServer::registerUnhandled(RequestFunction functor) {
unhandled = functor;
}
void PIHTTPServer::unregisterPath(const PIString & path, Method method) {
auto pl = path.split("/");
pl.removeAll("");
auto it = functions.makeIterator();
while (it.next()) {
if (it.value().method == method) {
if (it.value().path == pl) {
functions.remove(it.key());
break;
}
}
}
}
void PIHTTPServer::unregisterPath(const PIString & path) {
auto pl = path.split("/");
pl.removeAll("");
auto it = functions.makeIterator();
PIStringList keys;
while (it.next()) {
if (it.value().path == pl) {
keys << it.key();
}
}
for (const auto & k: keys)
functions.remove(k);
}
bool PIHTTPServer::Endpoint::match(const PIStringList & in_path) const {
if (in_path.size() != path.size()) return false;
for (int i = 0; i < path.size_s(); ++i) {