15
15
16
16
#include < string>
17
17
#include < string_view>
18
- #include < unordered_map >
18
+ #include " re2/re2.h "
19
19
20
20
#include " proxy_wasm_intrinsics.h"
21
21
@@ -26,11 +26,14 @@ class ExampleRootContext : public RootContext {
26
26
bool onStart (size_t ) override ;
27
27
bool onConfigure (size_t ) override ;
28
28
void onTick () override ;
29
+
30
+ std::optional<re2::RE2> path_match;
29
31
};
30
32
31
33
class ExampleContext : public Context {
32
34
public:
33
- explicit ExampleContext (uint32_t id, RootContext *root) : Context(id, root) {}
35
+ explicit ExampleContext (uint32_t id, RootContext *root)
36
+ : Context(id, root), root_(static_cast <ExampleRootContext *>(root)) {}
34
37
35
38
void onCreate () override ;
36
39
FilterHeadersStatus onRequestHeaders (uint32_t headers, bool end_of_stream) override ;
@@ -40,6 +43,9 @@ class ExampleContext : public Context {
40
43
void onDone () override ;
41
44
void onLog () override ;
42
45
void onDelete () override ;
46
+
47
+ private:
48
+ const ExampleRootContext *root_;
43
49
};
44
50
static RegisterContextFactory register_ExampleContext (CONTEXT_FACTORY(ExampleContext),
45
51
ROOT_FACTORY(ExampleRootContext),
@@ -62,8 +68,12 @@ bool ExampleRootContext::onStart(size_t) {
62
68
63
69
bool ExampleRootContext::onConfigure (size_t ) {
64
70
LOG_TRACE (" onConfigure" );
71
+ // Start a timer.
65
72
proxy_set_tick_period_milliseconds (1000 ); // 1 sec
66
- return true ;
73
+
74
+ // Compile a regular expression.
75
+ path_match.emplace (" /foo-([^/]+)/" );
76
+ return path_match->ok ();
67
77
}
68
78
69
79
void ExampleRootContext::onTick () { LOG_TRACE (" onTick" ); }
@@ -78,6 +88,11 @@ FilterHeadersStatus ExampleContext::onRequestHeaders(uint32_t, bool) {
78
88
for (auto &p : pairs) {
79
89
LOG_INFO (std::string (p.first ) + std::string (" -> " ) + std::string (p.second ));
80
90
}
91
+ // Regex.
92
+ auto path = getRequestHeader (" :path" );
93
+ if (path && re2::RE2::FullMatch (path->view (), *root_->path_match )) {
94
+ sendLocalResponse (403 , " " , " Access forbidden." , {});
95
+ }
81
96
return FilterHeadersStatus::Continue;
82
97
}
83
98
0 commit comments