SNode.C
Loading...
Searching...
No Matches
Controller.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/Controller.h"
21
22#include "core/EventLoop.h"
23#include "express/Request.h"
24#include "express/Response.h"
25#include "express/RootRoute.h"
26
27#ifndef DOXYGEN_SHOULD_SKIP_THIS
28
29#endif /* DOXYGEN_SHOULD_SKIP_THIS */
30
31namespace express {
32
33 Controller::Controller(const std::shared_ptr<web::http::server::Request>& request,
34 const std::shared_ptr<web::http::server::Response>& response)
35 : request(std::make_shared<Request>(request))
36 , response(std::make_shared<Response>(response))
37 , lastTick(core::EventLoop::getTickCounter()) {
38 }
39
49
50 Controller& Controller::operator=(const Controller& controller) noexcept {
51 if (this != &controller) {
52 request = controller.request;
53 response = controller.response;
54 rootRoute = controller.rootRoute;
55 lastRoute = controller.lastRoute;
56 currentRoute = controller.currentRoute;
57 lastTick = controller.lastTick;
58 flags = controller.flags;
59 }
60
61 return *this;
62 }
63
64 void Controller::setRootRoute(RootRoute* rootRoute) {
65 this->rootRoute = rootRoute;
66 }
67
68 void Controller::setCurrentRoute(Route* currentRoute) {
69 this->currentRoute = currentRoute;
70 }
71
72 const std::shared_ptr<Request>& Controller::getRequest() {
73 return request;
74 }
75
76 const std::shared_ptr<Response>& Controller::getResponse() {
77 return response;
78 }
79
80 int Controller::getFlags() const {
81 return flags;
82 }
83
84 void Controller::next(const std::string& how) const {
85 flags = NEXT;
86
87 if (how == "route") {
89 } else if (how == "router") {
91 }
92
93 lastRoute = currentRoute;
94
95 if (lastTick != core::EventLoop::getTickCounter()) { // If asynchronous next() start traversing of route-tree
96 rootRoute->dispatch(*const_cast<express::Controller*>(this));
97 }
98 }
99
101 const bool breakDispatching = lastRoute == currentRoute && (flags & Controller::NEXT_ROUTER) != 0;
102
103 if (breakDispatching) {
105 }
106
107 return breakDispatching;
108 }
109
110 bool Controller::dispatchNext(const std::string& parentMountPath) {
111 bool dispatched = false;
112
113 if (((flags & Controller::NEXT) != 0)) {
114 if (lastRoute == currentRoute) {
116 if ((flags & Controller::NEXT_ROUTE) != 0) {
118 } else if ((flags & Controller::NEXT_ROUTER) == 0) {
119 dispatched = currentRoute->dispatchNext(*this, parentMountPath);
120 }
121 } else { // ? Optimization: Dispatch only parent route matched path
122 dispatched = currentRoute->dispatchNext(*this, parentMountPath);
123 }
124 }
125
126 return dispatched;
127 }
128
130 bool ret = false;
131
132 if (!request->url.ends_with('/')) {
133 request->url.append("/");
134
135 ret = true;
136 }
137
138 return ret;
139 }
140
141} // namespace express
unsigned long lastTick
Definition Controller.h:77
void next(const std::string &how) const
const std::shared_ptr< Request > & getRequest()
Controller(const Controller &controller)
bool dispatchNext(const std::string &parentMountPath)
int getFlags() const
Controller & operator=(const Controller &controller) noexcept
const std::shared_ptr< Response > & getResponse()