Skip to content

Commit df8faa9

Browse files
committed
Add RE2 example.
Signed-off-by: Martijn Stevenson <[email protected]>
1 parent d505642 commit df8faa9

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

bazel/repositories.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,19 @@ def proxy_wasm_cpp_sdk_repositories():
3131
strip_prefix = "protobuf-3.17.3",
3232
url = "https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protobuf-all-3.17.3.tar.gz",
3333
)
34+
35+
maybe(
36+
http_archive,
37+
name = "com_google_absl",
38+
sha256 = "95e90be7c3643e658670e0dd3c1b27092349c34b632c6e795686355f67eca89f",
39+
strip_prefix = "abseil-cpp-20240722.0",
40+
urls = ["https://github.com/abseil/abseil-cpp/archive/20240722.0.zip"],
41+
)
42+
43+
maybe(
44+
http_archive,
45+
name = "com_google_re2",
46+
sha256 = "18cf85922e27fad3ed9c96a27733037da445f35eb1a2744c306a37c6d11e95c4",
47+
strip_prefix = "re2-2023-07-01",
48+
url = "https://github.com/google/re2/archive/2023-07-01.tar.gz",
49+
)

example/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ proxy_wasm_cc_binary(
66
name = "http_wasm_example.wasm",
77
srcs = ["http_wasm_example.cc"],
88
copts = ["-std=c++17"],
9+
deps = ["@com_google_re2//:re2"],
910
)
1011

1112
proxy_wasm_cc_binary(
1213
name = "http_wasm_example_with_protobuf_lite.wasm",
1314
srcs = ["http_wasm_example.cc"],
1415
copts = ["-std=c++17"],
1516
protobuf = "lite",
17+
deps = ["@com_google_re2//:re2"],
1618
)
1719

1820
proxy_wasm_cc_binary(
1921
name = "http_wasm_example_with_protobuf_full.wasm",
2022
srcs = ["http_wasm_example.cc"],
2123
copts = ["-std=c++17"],
2224
protobuf = "full",
25+
deps = ["@com_google_re2//:re2"],
2326
)

example/http_wasm_example.cc

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include <string>
1717
#include <string_view>
18-
#include <unordered_map>
18+
#include "re2/re2.h"
1919

2020
#include "proxy_wasm_intrinsics.h"
2121

@@ -26,11 +26,14 @@ class ExampleRootContext : public RootContext {
2626
bool onStart(size_t) override;
2727
bool onConfigure(size_t) override;
2828
void onTick() override;
29+
30+
std::optional<re2::RE2> path_match;
2931
};
3032

3133
class ExampleContext : public Context {
3234
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)) {}
3437

3538
void onCreate() override;
3639
FilterHeadersStatus onRequestHeaders(uint32_t headers, bool end_of_stream) override;
@@ -40,6 +43,9 @@ class ExampleContext : public Context {
4043
void onDone() override;
4144
void onLog() override;
4245
void onDelete() override;
46+
47+
private:
48+
const ExampleRootContext *root_;
4349
};
4450
static RegisterContextFactory register_ExampleContext(CONTEXT_FACTORY(ExampleContext),
4551
ROOT_FACTORY(ExampleRootContext),
@@ -62,8 +68,12 @@ bool ExampleRootContext::onStart(size_t) {
6268

6369
bool ExampleRootContext::onConfigure(size_t) {
6470
LOG_TRACE("onConfigure");
71+
// Start a timer.
6572
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();
6777
}
6878

6979
void ExampleRootContext::onTick() { LOG_TRACE("onTick"); }
@@ -78,6 +88,11 @@ FilterHeadersStatus ExampleContext::onRequestHeaders(uint32_t, bool) {
7888
for (auto &p : pairs) {
7989
LOG_INFO(std::string(p.first) + std::string(" -> ") + std::string(p.second));
8090
}
91+
// Regex.
92+
auto path = getRequestHeader(":path");
93+
if (path && re2::RE2::FullMatch(path->view(), *root_->path_match)) {
94+
sendLocalResponse(403, "", "Access forbidden.", {});
95+
}
8196
return FilterHeadersStatus::Continue;
8297
}
8398

0 commit comments

Comments
 (0)