NAWA 0.9
Web Application Framework for C++
RequestHandler.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 Tobias Flaig.
3 *
4 * This file is part of nawa.
5 *
6 * nawa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License,
8 * version 3, as published by the Free Software Foundation.
9 *
10 * nawa is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24#include <mutex>
30#include <nawa/util/encoding.h>
31#include <shared_mutex>
32
33using namespace nawa;
34using namespace std;
35
36struct RequestHandler::Data {
37 shared_mutex configurationMutex;
38 shared_ptr<HandleRequestFunctionWrapper> handleRequestFunction;
39 shared_ptr<AccessFilterList> accessFilters;
40 shared_ptr<Config> config;
41};
42
44
45void RequestHandler::setAppRequestHandler(std::shared_ptr<HandleRequestFunctionWrapper> handleRequestFunction) noexcept {
46 unique_lock l(data->configurationMutex);
47 data->handleRequestFunction = std::move(handleRequestFunction);
48}
49
50void RequestHandler::setAccessFilters(AccessFilterList accessFilters) noexcept {
51 unique_lock l(data->configurationMutex);
52 data->accessFilters = make_shared<AccessFilterList>(std::move(accessFilters));
53}
54
55void RequestHandler::setConfig(Config config) noexcept {
56 unique_lock l(data->configurationMutex);
57 data->config = make_shared<Config>(std::move(config));
58}
59
60std::shared_ptr<Config const> RequestHandler::getConfig() const noexcept {
61 return data->config;
62}
63
64void RequestHandler::reconfigure(std::optional<std::shared_ptr<HandleRequestFunctionWrapper>> handleRequestFunction,
65 std::optional<AccessFilterList> accessFilters,
66 std::optional<Config> config) noexcept {
67 unique_lock l(data->configurationMutex);
68 if (handleRequestFunction) {
69 data->handleRequestFunction = *handleRequestFunction;
70 }
71 if (accessFilters) {
72 data->accessFilters = make_shared<AccessFilterList>(std::move(*accessFilters));
73 }
74 if (config) {
75 data->config = make_shared<Config>(std::move(*config));
76 }
77}
78
79void nawa::RequestHandler::reconfigure(HandleRequestFunction handleRequestFunction, std::optional<AccessFilterList> accessFilters,
80 std::optional<Config> config) noexcept {
81 reconfigure(make_shared<HandleRequestFunctionWrapper>(std::move(handleRequestFunction)), std::move(accessFilters),
82 std::move(config));
83}
84
86 shared_ptr<HandleRequestFunctionWrapper> handleRequestFunction;
87 shared_ptr<AccessFilterList> accessFilters;
88 shared_ptr<Config> config;
89 {
90 shared_lock l(data->configurationMutex);
91 handleRequestFunction = data->handleRequestFunction;
92 accessFilters = data->accessFilters;
93 }
94 // test filters and run app if no filter was triggered
95 if (!accessFilters || !connection.applyFilters(*accessFilters)) {
96 (*handleRequestFunction)(connection);
97 }
98}
99
100std::unique_ptr<RequestHandler>
101RequestHandler::newRequestHandler(std::shared_ptr<HandleRequestFunctionWrapper> const& handleRequestFunction,
102 Config config, int concurrency) {
103 if (config[{"system", "request_handler"}] == "http") {
104 return make_unique<HttpRequestHandler>(handleRequestFunction, std::move(config), concurrency);
105 }
106 return make_unique<FastcgiRequestHandler>(handleRequestFunction, std::move(config), concurrency);
107}
108
109std::unique_ptr<RequestHandler>
111 int concurrency) {
112 return newRequestHandler(make_shared<HandleRequestFunctionWrapper>(std::move(handleRequestFunction)), std::move(config),
113 concurrency);
114}
115
117 // Avoid segfault during shutdown by clearing session data from here.
118 Session::destroy();
119}
Response object to be passed back to NAWA and accessor to the request.
Class which connects NAWA to the fastcgi++ library.
A request handler which creates a development web server.
Handles and serves incoming requests via the NAWA app.
Class for managing sessions and getting and setting connection-independent session data.
bool applyFilters(AccessFilterList const &accessFilters)
Definition: Connection.cpp:381
static std::unique_ptr< RequestHandler > newRequestHandler(std::shared_ptr< HandleRequestFunctionWrapper > const &handleRequestFunction, Config config, int concurrency)
void handleRequest(Connection &connection)
void reconfigure(std::optional< std::shared_ptr< HandleRequestFunctionWrapper > > handleRequestFunction, std::optional< AccessFilterList > accessFilters, std::optional< Config > config) noexcept
Namespace containing functions for text encoding and decoding.
#define NAWA_DEFAULT_CONSTRUCTOR_IMPL(Class)
Definition: macros.h:40
Definition: AppInit.h:31
std::function< int(nawa::Connection &)> HandleRequestFunction