Wasm C++ filter

    Sandbox environment

    Setup your sandbox environment with Docker and Docker Compose, and clone the Envoy repository with Git.

    Used to make requests.

    Compatibility

    The provided Wasm binary was compiled for the x86_64 architecture. If you would like to use this sandbox with the arm64 architecture, change directory to examples/wasm-cc and skip to Step 3.

    This sandbox demonstrates a basic Envoy Wasm filter written in C++ which injects content into the body of an HTTP response, and adds and updates some headers.

    It also takes you through the steps required to build your own C++ , and run it with Envoy.

    First lets start the containers - an Envoy proxy which uses a Wasm Filter, and a backend which echos back our request.

    Change to the examples/wasm-cc folder in the Envoy repo, and start the composition:

    1. $ curl -s http://localhost:8000 | grep "Hello, world"
    2. }Hello, world

    The filter also sets the content-type header to text/plain, and adds a custom x-wasm-custom header.

    1. content-type: text/plain; charset=utf-8
    2. $ curl -v http://localhost:8000 | grep "x-wasm-custom: "
    3. x-wasm-custom: FOO

    There are two source code files provided for the Wasm filter.

    provides the source code for the included prebuilt binary.

    envoy_filter_http_wasm_updated_example.cc makes a few changes to the original.

    The following diff shows the changes that have been made:

    Warning

    These instructions for compiling an updated Wasm binary use the image. You will need 4-5GB of disk space to accommodate this image.

    Stop the proxy server and compile the Wasm binary with the updated code:

    1. $ docker-compose stop proxy
    2. $ docker-compose -f docker-compose-wasm.yaml up --remove-orphans wasm_compile_update

    The compiled binary should now be in the lib folder.

    1. $ ls -l lib
    2. -r-xr-xr-x 1 root root 59641 Oct 20 00:00 envoy_filter_http_wasm_example.wasm

    Edit the Dockerfile-proxy recipe provided in the example to use the updated binary you created in step 3.

    Replace this line with the following:

    1. COPY ./lib/envoy_filter_http_wasm_updated_example.wasm /lib/envoy_filter_http_wasm_example.wasm

    Now, rebuild and start the proxy container.

    1. $ docker-compose up --build -d proxy

    The Wasm filter should instead inject “Hello, Wasm world” at the end of the response body.

    The content-type and x-wasm-custom headers should also have changed

    1. $ curl -v http://localhost:8000 | grep "content-type: "
    2. content-type: text/html; charset=utf-8

    See also

    Envoy Wasm filter

    Further information about the Envoy Wasm filter.

    The Envoy Wasm API - version 3.

    Proxy Wasm C++ SDK