Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ for your own plugin. Extend them to fit your particular use case.
initial numbers will be masked.
* [Validate client token on query string using HMAC](samples/hmac_authtoken):
Check the client request URL for a valid token signed using HMAC.
* [Rewrite domains in html response body](samples/html_domain_rewrite/): Parse
html in response body chunks and replace insances of "foo.com" with
"bar.com" in `<a href=***>`.

# Feature set / ABI

Expand Down
2 changes: 1 addition & 1 deletion plugins/bazel/cargo/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ crates_vendor(
repository_name = "cu", # shorten generated paths for Windows...
tags = ["manual"],
vendor_path = "remote",
supported_platform_triples = ["x86_64-unknown-linux-gnu", "wasm32-wasi"],
supported_platform_triples = ["x86_64-unknown-linux-gnu", "wasm32-wasi"], # needed x86_64-unknown-linux-gnu to fix issue with getrandom building for wrong environment
)
26 changes: 21 additions & 5 deletions plugins/bazel/cargo/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,18 @@ checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if",
"libc",
"wasi",
"wasi 0.9.0+wasi-snapshot-preview1",
]

[[package]]
name = "getrandom"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
]

[[package]]
Expand Down Expand Up @@ -359,7 +370,7 @@ version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"getrandom 0.1.16",
"libc",
"rand_chacha",
"rand_core",
Expand All @@ -383,7 +394,7 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
"getrandom 0.1.16",
]

[[package]]
Expand Down Expand Up @@ -474,7 +485,6 @@ version = "0.0.0"
dependencies = [
"log",
"lol_html",
"ppv-lite86",
"proxy-wasm",
"regex",
"url",
Expand Down Expand Up @@ -610,7 +620,7 @@ version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3758f5e68192bb96cc8f9b7e2c2cfdabb435499a28499a42f8f984092adad4b"
dependencies = [
"getrandom",
"getrandom 0.2.15",
]

[[package]]
Expand All @@ -625,6 +635,12 @@ version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"

[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

[[package]]
name = "zerocopy"
version = "0.6.6"
Expand Down
3 changes: 2 additions & 1 deletion plugins/bazel/cargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ proxy-wasm = "0.2.1"
log = "0.4"
regex = "1.9"
url = "2.4"
# Pin version 2.0.0 until rust-version includes https://github.com/rust-lang/rust/issues/119128
Comment thread
martijneken marked this conversation as resolved.
lol_html = "=2.0.0"
ppv-lite86 = "=0.2.18"
uuid = { version = "1.12.1", features = [ "v4" ] }

[lib]
crate-type = ["cdylib"]
Expand Down
6 changes: 0 additions & 6 deletions plugins/bazel/cargo/remote/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ alias(
tags = ["manual"],
)

alias(
name = "ppv-lite86",
actual = "@cu__ppv-lite86-0.2.18//:ppv_lite86",
tags = ["manual"],
)

alias(
name = "proxy-wasm",
actual = "@cu__proxy-wasm-0.2.1//:proxy_wasm",
Expand Down
3 changes: 3 additions & 0 deletions plugins/bazel/cargo/remote/BUILD.getrandom-0.2.15.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ rust_library(
"@rules_rust//rust/platform:wasm32-wasi": [
"@cu__wasi-0.11.0-wasi-snapshot-preview1//:wasi", # cfg(target_os = "wasi")
],
"@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [
"@cu__libc-0.2.169//:libc", # cfg(unix)
],
"//conditions:default": [],
}),
)
1 change: 0 additions & 1 deletion plugins/bazel/cargo/remote/BUILD.ppv-lite86-0.2.18.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ rust_library(
],
),
crate_features = [
"default",
"simd",
"std",
],
Expand Down
22 changes: 20 additions & 2 deletions plugins/bazel/cargo/remote/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ _NORMAL_DEPENDENCIES = {
_COMMON_CONDITION: {
"log": Label("@cu__log-0.4.25//:log"),
"lol_html": Label("@cu__lol_html-2.0.0//:lol_html"),
"ppv-lite86": Label("@cu__ppv-lite86-0.2.18//:ppv_lite86"),
"proxy-wasm": Label("@cu__proxy-wasm-0.2.1//:proxy_wasm"),
"regex": Label("@cu__regex-1.9.6//:regex"),
"url": Label("@cu__url-2.4.1//:url"),
Expand Down Expand Up @@ -542,6 +541,16 @@ def crate_repositories():
build_file = Label("@//bazel/cargo/remote:BUILD.getrandom-0.1.16.bazel"),
)

maybe(
http_archive,
name = "cu__getrandom-0.2.15",
sha256 = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7",
type = "tar.gz",
urls = ["https://static.crates.io/crates/getrandom/0.2.15/download"],
strip_prefix = "getrandom-0.2.15",
build_file = Label("@//bazel/cargo/remote:BUILD.getrandom-0.2.15.bazel"),
)

maybe(
http_archive,
name = "cu__hashbrown-0.13.2",
Expand Down Expand Up @@ -1072,6 +1081,16 @@ def crate_repositories():
build_file = Label("@//bazel/cargo/remote:BUILD.version_check-0.9.5.bazel"),
)

maybe(
http_archive,
name = "cu__wasi-0.11.0-wasi-snapshot-preview1",
sha256 = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423",
type = "tar.gz",
urls = ["https://static.crates.io/crates/wasi/0.11.0+wasi-snapshot-preview1/download"],
strip_prefix = "wasi-0.11.0+wasi-snapshot-preview1",
build_file = Label("@//bazel/cargo/remote:BUILD.wasi-0.11.0+wasi-snapshot-preview1.bazel"),
)

maybe(
http_archive,
name = "cu__wasi-0.9.0-wasi-snapshot-preview1",
Expand Down Expand Up @@ -1125,7 +1144,6 @@ def crate_repositories():
return [
struct(repo = "cu__log-0.4.25", is_dev_dep = False),
struct(repo = "cu__lol_html-2.0.0", is_dev_dep = False),
struct(repo = "cu__ppv-lite86-0.2.18", is_dev_dep = False),
struct(repo = "cu__proxy-wasm-0.2.1", is_dev_dep = False),
struct(repo = "cu__regex-1.9.6", is_dev_dep = False),
struct(repo = "cu__url-2.4.1", is_dev_dep = False),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// [START serviceextensions_plugin_domain_rewrite]
// [START serviceextensions_plugin_html_domain_rewrite]
use lol_html::*;
use proxy_wasm::traits::*;
use proxy_wasm::types::*;
Expand All @@ -27,14 +27,20 @@ proxy_wasm::main! {{

struct MyHttpContext<'a> {
// Some member variables need to be wrapped in Rc<Refcell<T>> so that we can
// modify the variable in multiple places as well as share ownership. By
// using Rc, we are commiting to not modfying the variable in multiple places
// simultaneously, otherwise the code will panic and plugin will crash.
// modify the variable in multiple places as well as share ownership.
// By using Rc, we are allowing multiple references to the same obeject to
// exist at the same time.
// By using Refcell, we are evaluating borrow checks at runtime, thus we are
// commiting to not modfying the variable in multiple places simultaneously,
// otherwise the code will panic and plugin will crash.

// Stores HTML as rewriter parses and modifies HTML.
// Only stores completed sections(e.g., when rewriter parses "<di", output
// will be empty until the next chunk comes in. If the next chunk was
// "v> <h1>foo</h", the output will then contain "<div><h1>foo").
Comment thread
dallen68 marked this conversation as resolved.
Outdated
// Only stores sections of HTML that have not yet been seen back to client.
// After sending data to client, we clear output to avoid unnecessary memory
// growth.
output: Rc<RefCell<Vec<u8>>>,
Comment thread
dallen68 marked this conversation as resolved.
Outdated
// HTML rewriter. Member of MyHttpContext a.k.a "StreamContext" so that the
// rewriter persists across multiple body callbacks.
Expand All @@ -50,7 +56,7 @@ impl<'a> MyHttpContext<'a> {
Settings {
element_content_handlers: vec![element!("a[href]", move |el| {
let href = el.get_attribute("href").unwrap();
let modified_href = href.replace("foo", "bar");
let modified_href = href.replace("foo.com", "bar.com");
if modified_href != href {
el.set_attribute("href", &modified_href).unwrap();
}
Expand Down Expand Up @@ -100,4 +106,4 @@ impl<'a> HttpContext for MyHttpContext<'a> {
return Action::Continue;
}
}
// [END serviceextensions_plugin_domain_rewrite]
// [END serviceextensions_plugin_html_domain_rewrite]