From 3798c52b5464707bf0783fd82cc168db26b6c347 Mon Sep 17 00:00:00 2001 From: Bryan Biedenkapp Date: Fri, 14 Mar 2025 12:59:26 -0400 Subject: [PATCH] reorganize SIP handler code slighty; --- src/common/network/sip/RequestDispatcher.h | 235 +++------------------ src/common/network/sip/SIPPayload.h | 5 - 2 files changed, 34 insertions(+), 206 deletions(-) diff --git a/src/common/network/sip/RequestDispatcher.h b/src/common/network/sip/RequestDispatcher.h index f027754b..522b5c80 100644 --- a/src/common/network/sip/RequestDispatcher.h +++ b/src/common/network/sip/RequestDispatcher.h @@ -37,177 +37,97 @@ namespace network // --------------------------------------------------------------------------- /** - * @brief Structure representing a SIP request match. - * @ingroup sip - */ - struct RequestMatch : std::smatch { - /** - * @brief Initializes a new instance of the RequestMatch structure. - * @param m String matcher. - * @param c Content. - */ - RequestMatch(const std::smatch& m, const std::string& c) : std::smatch(m), content(c) { /* stub */ } - - std::string content; - }; - - // --------------------------------------------------------------------------- - // Structure Declaration - // --------------------------------------------------------------------------- - - /** - * @brief Structure representing a request matcher. + * @brief Structure representing a request handler. * @ingroup rest */ template - struct RequestMatcher { - typedef std::function RequestHandlerType; + struct RequestHandler { + typedef std::function RequestHandlerType; /** - * @brief Initializes a new instance of the RequestMatcher structure. - * @param expression Matching expression. + * @brief Initializes a new instance of the RequestHandler structure. */ - explicit RequestMatcher(const std::string& expression) : m_expression(expression), m_isRegEx(false) { /* stub */ } + explicit RequestHandler() { /* stub */} /** * @brief Handler for INVITE requests. * @param handler INVITE request handler. - * @return RequestMatcher* Instance of a RequestMatcher. + * @return RequestHandler* Instance of a RequestHandler. */ - RequestMatcher& invite(RequestHandlerType handler) { + RequestHandler& invite(RequestHandlerType handler) { m_handlers[SIP_INVITE] = handler; return *this; } /** * @brief Handler for ACK requests. * @param handler ACK request handler. - * @return RequestMatcher* Instance of a RequestMatcher. + * @return RequestHandler* Instance of a RequestHandler. */ - RequestMatcher& ack(RequestHandlerType handler) { + RequestHandler& ack(RequestHandlerType handler) { m_handlers[SIP_ACK] = handler; return *this; } /** * @brief Handler for BYE requests. * @param handler BYE request handler. - * @return RequestMatcher* Instance of a RequestMatcher. + * @return RequestHandler* Instance of a RequestHandler. */ - RequestMatcher& bye(RequestHandlerType handler) { + RequestHandler& bye(RequestHandlerType handler) { m_handlers[SIP_BYE] = handler; return *this; } /** * @brief Handler for CANCEL requests. * @param handler CANCEL request handler. - * @return RequestMatcher* Instance of a RequestMatcher. + * @return RequestHandler* Instance of a RequestHandler. */ - RequestMatcher& cancel(RequestHandlerType handler) { + RequestHandler& cancel(RequestHandlerType handler) { m_handlers[SIP_CANCEL] = handler; return *this; } /** * @brief Handler for REGISTER requests. * @param handler REGISTER request handler. - * @return RequestMatcher* Instance of a RequestMatcher. + * @return RequestHandler* Instance of a RequestHandler. */ - RequestMatcher& registerReq(RequestHandlerType handler) { + RequestHandler& registerReq(RequestHandlerType handler) { m_handlers[SIP_REGISTER] = handler; return *this; } /** * @brief Handler for OPTIONS requests. * @param handler OPTIONS request handler. - * @return RequestMatcher* Instance of a RequestMatcher. + * @return RequestHandler* Instance of a RequestHandler. */ - RequestMatcher& options(RequestHandlerType handler) { + RequestHandler& options(RequestHandlerType handler) { m_handlers[SIP_OPTIONS] = handler; return *this; } - /** - * @brief Handler for SUBSCRIBE requests. - * @param handler SUBSCRIBE request handler. - * @return RequestMatcher* Instance of a RequestMatcher. - */ - RequestMatcher& subscribe(RequestHandlerType handler) { - m_handlers[SIP_SUBSCRIBE] = handler; - return *this; - } - /** - * @brief Handler for NOTIFY requests. - * @param handler NOTIFY request handler. - * @return RequestMatcher* Instance of a RequestMatcher. - */ - RequestMatcher& notify(RequestHandlerType handler) { - m_handlers[SIP_NOTIFY] = handler; - return *this; - } - /** - * @brief Handler for PUBLISH requests. - * @param handler PUBLISH request handler. - * @return RequestMatcher* Instance of a RequestMatcher. - */ - RequestMatcher& publish(RequestHandlerType handler) { - m_handlers[SIP_PUBLISH] = handler; - return *this; - } - /** - * @brief Handler for INFO requests. - * @param handler INFO request handler. - * @return RequestMatcher* Instance of a RequestMatcher. - */ - RequestMatcher& info(RequestHandlerType handler) { - m_handlers[SIP_INFO] = handler; - return *this; - } /** * @brief Handler for MESSAGE requests. * @param handler MESSAGE request handler. - * @return RequestMatcher* Instance of a RequestMatcher. + * @return RequestHandler* Instance of a RequestHandler. */ - RequestMatcher& message(RequestHandlerType handler) { + RequestHandler& message(RequestHandlerType handler) { m_handlers[SIP_MESSAGE] = handler; return *this; } - /** - * @brief Handler for UPDATE requests. - * @param handler UPDATE request handler. - * @return RequestMatcher* Instance of a RequestMatcher. - */ - RequestMatcher& update(RequestHandlerType handler) { - m_handlers[SIP_UPDATE] = handler; - return *this; - } - - /** - * @brief Helper to determine if the request matcher is a regular expression. - * @returns bool True, if request matcher is a regular expression, otherwise false. - */ - bool regex() const { return m_isRegEx; } - /** - * @brief Helper to set the regular expression flag. - * @param regEx Flag indicating whether or not the request matcher is a regular expression. - */ - void setRegEx(bool regEx) { m_isRegEx = regEx; } /** * @brief Helper to handle the actual request. * @param request HTTP request. * @param reply HTTP reply. - * @param what What matched. */ - void handleRequest(const Request& request, Reply& reply, const std::smatch &what) { - // dispatching to matching based on handler - RequestMatch match(what, request.content); + void handleRequest(const Request& request, Reply& reply) { + // dispatching to handler auto& handler = m_handlers[request.method]; if (handler) { - handler(request, reply, match); + handler(request, reply); } } private: - std::string m_expression; - bool m_isRegEx; - std::map m_handlers; + std::map m_handlers; }; // --------------------------------------------------------------------------- @@ -221,45 +141,25 @@ namespace network */ template class SIPRequestDispatcher { - typedef RequestMatcher MatcherType; + typedef RequestHandler HandlerType; public: /** * @brief Initializes a new instance of the SIPRequestDispatcher class. */ - SIPRequestDispatcher() : m_basePath(), m_debug(false) { /* stub */ } - /** - * @brief Initializes a new instance of the SIPRequestDispatcher class. - * @param debug Flag indicating whether or not verbose logging should be enabled. - */ - SIPRequestDispatcher(bool debug) : m_basePath(), m_debug(debug) { /* stub */ } + SIPRequestDispatcher() : m_handlers(), m_debug(false) { /* stub */ } /** * @brief Initializes a new instance of the SIPRequestDispatcher class. - * @param basePath * @param debug Flag indicating whether or not verbose logging should be enabled. */ - SIPRequestDispatcher(const std::string& basePath, bool debug) : m_basePath(basePath), m_debug(debug) { /* stub */ } + SIPRequestDispatcher(bool debug) : m_handlers(), m_debug(debug) { /* stub */ } /** - * @brief Helper to match a request patch. - * @param expression Matching expression. - * @param regex Flag indicating whether or not this match is a regular expression. - * @returns MatcherType Instance of a request matcher. + * @brief Helper to set a request handler. + * @returns HandlerType Instance of a request handler. */ - MatcherType& match(const std::string& expression, bool regex = false) + HandlerType& handler() { - MatcherTypePtr& p = m_matchers[expression]; - if (!p) { - if (m_debug) { - ::LogDebug(LOG_REST, "creating SIPRequestDispatcher, expression = %s", expression.c_str()); - } - p = std::make_shared(expression); - } else { - if (m_debug) { - ::LogDebug(LOG_REST, "fetching SIPRequestDispatcher, expression = %s", expression.c_str()); - } - } - - p->setRegEx(regex); + HandlerTypePtr& p = m_handlers; return *p; } @@ -270,40 +170,13 @@ namespace network */ void handleRequest(const Request& request, Reply& reply) { - for (const auto& matcher : m_matchers) { - std::smatch what; - if (!matcher.second->regex()) { - if (request.uri.find(matcher.first) != std::string::npos) { - if (m_debug) { - ::LogDebug(LOG_REST, "non-regex endpoint, uri = %s, expression = %s", request.uri.c_str(), matcher.first.c_str()); - } - - //what = matcher.first; - - matcher.second->handleRequest(request, reply, what); - return; - } - } else { - if (std::regex_match(request.uri, what, std::regex(matcher.first))) { - if (m_debug) { - ::LogDebug(LOG_REST, "regex endpoint, uri = %s, expression = %s", request.uri.c_str(), matcher.first.c_str()); - } - - matcher.second->handleRequest(request, reply, what); - return; - } - } - } - - ::LogError(LOG_REST, "unknown endpoint, uri = %s", request.uri.c_str()); - reply = SIPPayload::statusPayload(SIPPayload::BAD_REQUEST, "application/sdp"); + m_handlers.handleRequest(request, reply); + return; } private: - typedef std::shared_ptr MatcherTypePtr; - - std::string m_basePath; - std::map m_matchers; + typedef std::shared_ptr HandlerTypePtr; + HandlerType m_handlers; bool m_debug; }; @@ -312,46 +185,6 @@ namespace network // Class Declaration // --------------------------------------------------------------------------- - /** - * @brief This class implements a generic basic request dispatcher. - * @tparam Request SIP request. - * @tparam Reply SIP reply. - */ - template - class SIPBasicRequestDispatcher { - public: - typedef std::function RequestHandlerType; - - /** - * @brief Initializes a new instance of the SIPBasicRequestDispatcher class. - */ - SIPBasicRequestDispatcher() { /* stub */ } - /** - * @brief Initializes a new instance of the SIPBasicRequestDispatcher class. - * @param handler Instance of a RequestHandlerType for this dispatcher. - */ - SIPBasicRequestDispatcher(RequestHandlerType handler) : m_handler(handler) { /* stub */ } - - /** - * @brief Helper to handle SIP request. - * @param request SIP request. - * @param reply SIP reply. - */ - void handleRequest(const Request& request, Reply& reply) - { - if (m_handler) { - m_handler(request, reply); - } - } - - private: - RequestHandlerType m_handler; - }; - - // --------------------------------------------------------------------------- - // Class Declaration - // --------------------------------------------------------------------------- - /** * @brief This class implements a generic debug request dispatcher. * @tparam Request SIP request. diff --git a/src/common/network/sip/SIPPayload.h b/src/common/network/sip/SIPPayload.h index 8ea68b99..85978e57 100644 --- a/src/common/network/sip/SIPPayload.h +++ b/src/common/network/sip/SIPPayload.h @@ -40,12 +40,7 @@ namespace network #define SIP_CANCEL "CANCEL" #define SIP_REGISTER "REGISTER" #define SIP_OPTIONS "OPTIONS" - #define SIP_SUBSCRIBE "SUBSCRIBE" - #define SIP_NOTIFY "NOTIFY" - #define SIP_PUBLISH "PUBLISH" - #define SIP_INFO "INFO" #define SIP_MESSAGE "MESSAGE" - #define SIP_UPDATE "UPDATE" // --------------------------------------------------------------------------- // Structure Declaration