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