#include "pihttpserver.h" #include "piliterals_string.h" PIHTTPServer::PIHTTPServer() { setRequestCallback([this](const PIHTTP::MessageConst & r) -> PIHTTP::MessageMutable { PIHTTP::MessageMutable reply; reply.setCode(PIHTTP::Code::NotFound); auto in_path = r.path().split("/"); in_path.removeAll(""); auto it = functions.makeReverseIterator(); bool found = false; while (it.next()) { if (it.value().function) { if (it.value().method == r.method()) { if (it.value().match(in_path)) { reply = it.value().function(r); found = true; break; } } } } if (!found && unhandled) reply = unhandled(r); auto hit = reply_headers.makeIterator(); while (hit.next()) reply.addHeader(hit.key(), hit.value()); return reply; }); } PIHTTPServer::~PIHTTPServer() { stop(); } void PIHTTPServer::registerPath(const PIString & path, PIHTTP::Method method, RequestFunction functor) { auto & ep(functions[path + PIString::fromNumber(static_cast(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, PIHTTP::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) { if (path[i] == "*"_a) continue; if (path[i] != in_path[i]) return false; } return true; }