NAWA 0.9
Web Application Framework for C++
AccessFilter.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
25#include <nawa/util/utils.h>
26#include <sstream>
27
28using namespace nawa;
29using namespace std;
30
31struct 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
56NAWA_COMPLEX_DATA_ACCESSORS_IMPL(AccessFilter, pathFilter, vector<vector<string>>)
57
59
60NAWA_COMPLEX_DATA_ACCESSORS_IMPL(AccessFilter, extensionFilter, vector<string>)
61
62NAWA_PRIMITIVE_DATA_ACCESSORS_IMPL(AccessFilter, invertExtensionFilter, bool)
63
64NAWA_PRIMITIVE_DATA_ACCESSORS_IMPL(AccessFilter, regexFilterEnabled, bool)
65
67
69
70bool nawa::AccessFilter::matches(std::vector<std::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.