SNode.C
Loading...
Searching...
No Matches
ApplicationDispatcher.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "express/dispatcher/ApplicationDispatcher.h"
21
22#include "express/Controller.h"
23#include "express/MountPoint.h"
24#include "express/Request.h"
25#include "express/dispatcher/regex_utils.h"
26
27#ifndef DOXYGEN_SHOULD_SKIP_THIS
28
29#endif /* DOXYGEN_SHOULD_SKIP_THIS */
30
31namespace express::dispatcher {
32
34 const std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&)>& lambda)
35 : lambda(lambda) {
36 }
37
38 bool ApplicationDispatcher::dispatch(express::Controller& controller,
39 const std::string& parentMountPath,
40 const express::MountPoint& mountPoint) {
41 bool dispatched = false;
42
43 if ((controller.getFlags() & Controller::NEXT) == 0) {
44 const std::string absoluteMountPath = path_concat(parentMountPath, mountPoint.relativeMountPath);
45 if (((controller.getRequest()->path.rfind(absoluteMountPath, 0) == 0 ||
46 controller.getRequest()->url.rfind(absoluteMountPath, 0) == 0) &&
47 mountPoint.method == "use") ||
48 ((absoluteMountPath == controller.getRequest()->url || absoluteMountPath == controller.getRequest()->path ||
49 checkForUrlMatch(absoluteMountPath, controller.getRequest()->url)) &&
50 (controller.getRequest()->method == mountPoint.method || mountPoint.method == "all"))) {
51 dispatched = true;
52
53 if (hasResult(absoluteMountPath)) {
54 setParams(absoluteMountPath, *controller.getRequest());
55 }
56
57 lambda(controller.getRequest(), controller.getResponse());
58 }
59 }
60
61 return dispatched;
62 }
63
64} // namespace express::dispatcher
int getFlags() const
ApplicationDispatcher(const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &lambda)