NAWA  0.8
Web Application Framework for C++
AccessFilter.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 
25 #include <nawa/util/utils.h>
26 #include <sstream>
27 
28 using namespace nawa;
29 using namespace std;
30 
31 struct AccessFilter::Data {
32  bool invert = false;
33  std::vector<std::vector<std::string>> pathFilter;
34  bool invertPathFilter = false;
35  std::vector<std::string> extensionFilter;
36  bool invertExtensionFilter = false;
37  bool regexFilterEnabled = false;
38  std::regex regexFilter;
39  std::string response;
40 };
41 
43 
45 
47 
49 
51 
53 
55 
56 NAWA_COMPLEX_DATA_ACCESSORS_IMPL(AccessFilter, pathFilter, vector<vector<string>>)
57 
58 NAWA_PRIMITIVE_DATA_ACCESSORS_IMPL(AccessFilter, invertPathFilter, bool)
59 
60 NAWA_COMPLEX_DATA_ACCESSORS_IMPL(AccessFilter, extensionFilter, vector<string>)
61 
62 NAWA_PRIMITIVE_DATA_ACCESSORS_IMPL(AccessFilter, invertExtensionFilter, bool)
63 
64 NAWA_PRIMITIVE_DATA_ACCESSORS_IMPL(AccessFilter, regexFilterEnabled, bool)
65 
67 
69 
70 bool nawa::AccessFilter::matches(vector<string> const& requestPath) const {
71  if (!data->pathFilter.empty()) {
72  // one of the paths in the path filter must match for the path filter to match
73  bool pathFilterMatches = false;
74  for (auto const& filter : data->pathFilter) {
75  // path condition is set but does not match -> the whole filter does not match
76  // all elements of the filter path must be in the request path
77  if (requestPath.size() < filter.size()) {
78  continue;
79  }
80  pathFilterMatches = true;
81  for (size_t i = 0; i < filter.size(); ++i) {
82  if (filter.at(i) != requestPath.at(i)) {
83  pathFilterMatches = false;
84  break;
85  }
86  }
87  if (pathFilterMatches) {
88  break;
89  }
90  }
91  if ((!pathFilterMatches && !data->invertPathFilter) || (pathFilterMatches && data->invertPathFilter)) {
92  return false;
93  }
94  // path condition matches -> continue to the next filter condition
95  }
96 
97  if (!data->extensionFilter.empty()) {
98  auto fileExtension = utils::getFileExtension(requestPath.back());
99  bool extensionFilterMatches = false;
100  for (auto const& e : data->extensionFilter) {
101  if (fileExtension == e) {
102  extensionFilterMatches = true;
103  break;
104  }
105  }
106  if ((!extensionFilterMatches && !data->invertExtensionFilter) ||
107  (extensionFilterMatches && data->invertExtensionFilter)) {
108  return false;
109  }
110  // extension condition matches -> continue to the next filter condition
111  }
112 
113  if (data->regexFilterEnabled) {
114  // merge request path to string
115  stringstream pathStr;
116  for (auto const& e : requestPath) {
117  pathStr << '/' << e;
118  }
119  if (!regex_match(pathStr.str(), data->regexFilter))
120  return false;
121  }
122 
123  // all conditions match or no condition has been set -> the filter matches
124  return true;
125 }
Options to check the path and invoke certain actions before forwarding the request to the app.
#define NAWA_DEFAULT_DESTRUCTOR_IMPL(Class)
Definition: macros.h:36
#define NAWA_PRIMITIVE_DATA_ACCESSORS_IMPL(Class, Member, Type)
Definition: macros.h:137
#define NAWA_COPY_CONSTRUCTOR_IMPL(Class)
Definition: macros.h:46
#define NAWA_COMPLEX_DATA_ACCESSORS_IMPL(Class, Member, Type)
Definition: macros.h:144
#define NAWA_MOVE_ASSIGNMENT_OPERATOR_IMPL(Class)
Definition: macros.h:98
#define NAWA_DEFAULT_CONSTRUCTOR_IMPL(Class)
Definition: macros.h:40
#define NAWA_COPY_ASSIGNMENT_OPERATOR_IMPL(Class)
Definition: macros.h:56
#define NAWA_MOVE_CONSTRUCTOR_IMPL(Class)
Definition: macros.h:88
std::string getFileExtension(std::string const &filename)
Definition: utils.cpp:343
Definition: AppInit.h:31
Contains useful functions that improve the readability and facilitate maintenance of the NAWA code.