1
#pragma once
2

            
3
#include <memory>
4

            
5
#include "envoy/common/matchers.h"
6
#include "envoy/config/typed_config.h"
7
#include "envoy/server/factory_context.h"
8

            
9
namespace Envoy {
10
namespace Regex {
11

            
12
/**
13
 * A compiled regex expression matcher which uses an abstract regex engine.
14
 */
15
class CompiledMatcher : public Matchers::StringMatcher {
16
public:
17
  // To avoid hiding other implementations of match.
18
  using Matchers::StringMatcher::match;
19

            
20
  /**
21
   * Replaces all non-overlapping occurrences of the pattern in "value" with
22
   * "substitution". The "substitution" string can make references to
23
   * capture groups in the pattern, using the syntax specific to that
24
   * regular expression engine.
25
   */
26
  virtual std::string replaceAll(absl::string_view value,
27
                                 absl::string_view substitution) const PURE;
28

            
29
  /**
30
   * Returns the pattern of the Regex matcher.
31
   */
32
  virtual const std::string& pattern() const PURE;
33
};
34

            
35
using CompiledMatcherPtr = std::unique_ptr<const CompiledMatcher>;
36

            
37
/**
38
 * A regular expression engine which turns regular expressions into compiled matchers.
39
 */
40
class Engine {
41
public:
42
51021
  virtual ~Engine() = default;
43

            
44
  /**
45
   * Create a @ref CompiledMatcher with the given regex expression.
46
   * @param regex the regex expression match string
47
   */
48
  virtual absl::StatusOr<CompiledMatcherPtr> matcher(const std::string& regex) const PURE;
49
};
50

            
51
using EnginePtr = std::shared_ptr<Engine>;
52

            
53
/**
54
 * Factory for Engine.
55
 */
56
class EngineFactory : public Config::TypedFactory {
57
public:
58
  /**
59
   * Creates an Engine from the provided config.
60
   */
61
  virtual EnginePtr
62
  createEngine(const Protobuf::Message& config,
63
               Server::Configuration::ServerFactoryContext& server_factory_context) PURE;
64

            
65
3342
  std::string category() const override { return "envoy.regex_engines"; }
66
};
67

            
68
} // namespace Regex
69
} // namespace Envoy