|
| 1 | +/* |
| 2 | + * ModSecurity, http://www.modsecurity.org/ |
| 3 | + * Copyright (c) 2019 |
| 4 | + * |
| 5 | + * You may not use this file except in compliance with |
| 6 | + * the License. You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * If any of the files related to licensing are missing or if you have any |
| 11 | + * other questions related to licensing please contact Trustwave Holdings, Inc. |
| 12 | + * directly using the email address [email protected]. |
| 13 | + * |
| 14 | + */ |
| 15 | +#ifndef SRC_REGEX_BACKEND_FALLBACK_H_ |
| 16 | +#define SRC_REGEX_BACKEND_FALLBACK_H_ |
| 17 | + |
| 18 | +#include <memory> |
| 19 | + |
| 20 | +#include "src/regex/backend/backend.h" |
| 21 | + |
| 22 | +namespace modsecurity { |
| 23 | +namespace regex { |
| 24 | + |
| 25 | +template<typename T> |
| 26 | +static backend::Backend* compile_regex_fallback(const std::string& pattern) { |
| 27 | + return new T(pattern); |
| 28 | +} |
| 29 | + |
| 30 | +template<typename T, typename T2, typename... Args> |
| 31 | +static backend::Backend* compile_regex_fallback(const std::string& pattern) { |
| 32 | + T *regex = new T{pattern}; |
| 33 | + if (regex->ok()) { |
| 34 | + return regex; |
| 35 | + } else { |
| 36 | + delete regex; |
| 37 | + return compile_regex_fallback<T2, Args...>(pattern); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +template<typename... Args> |
| 42 | +class BackendFallback : public backend::Backend { |
| 43 | +public: |
| 44 | + BackendFallback(const std::string& pattern) |
| 45 | + : backend(compile_regex_fallback<Args...>(pattern)) |
| 46 | + {} |
| 47 | + |
| 48 | + virtual bool ok(std::string *error = nullptr) const override { |
| 49 | + return backend->ok(error); |
| 50 | + } |
| 51 | + |
| 52 | + std::vector<RegexMatch> searchAll(const std::string& s, bool overlapping = false) const override { |
| 53 | + return backend->searchAll(s, overlapping); |
| 54 | + } |
| 55 | + |
| 56 | + bool search(const std::string &s, RegexMatch *m = nullptr, ssize_t max_groups = -1) const override { |
| 57 | + return backend->search(s, m, max_groups); |
| 58 | + } |
| 59 | + |
| 60 | + const std::string& getPattern() const override { |
| 61 | + return backend->getPattern(); |
| 62 | + } |
| 63 | +private: |
| 64 | + std::unique_ptr<backend::Backend> backend; |
| 65 | +}; |
| 66 | + |
| 67 | +} // namespace regex |
| 68 | +} // namespace modsecurity |
| 69 | + |
| 70 | +#endif // SRC_REGEX_BACKEND_FALLBACK_H_ |
0 commit comments