Skip to content

Latest commit

 

History

History

http-filter-example

Envoy filter example

This project demonstrates the linking of additional HTTP filters with the Envoy binary. A new filter sample which adds a HTTP header is introduced. Integration tests demonstrating the filter's end-to-end behavior are also provided.

Building

To build the Envoy static binary:

  1. git submodule update --init
  2. bazel build //http-filter-example:envoy

Testing

To run the sample integration test:

bazel test //http-filter-example:http_filter_integration_test

How it works

See the network filter example.

How to write and use an HTTP filter

  • The main task is to write a class that implements the interface Envoy::Http::StreamDecoderFilter as in http_filter.h and http_filter.cc, which contains functions that handle http headers, data, and trailers. Note that this is an example of decoder filters, and to write encoder filters or decoder/encoder filters you need to implement Envoy::Http::StreamEncoderFilter or Envoy::Http::StreamFilter instead.
  • You also need a class that implements Envoy::Server::Configuration::NamedHttpFilterConfigFactory to enable the Envoy binary to find your filter, as in http_filter_config.cc. It should be linked to the Envoy binary by modifying BUILD file.
  • Finally, you need to modify the Envoy config file to add your filter to the filter chain for a particular HTTP route configuration. For instance, if you wanted to change the front-proxy example to chain our sample filter, you'd need to modify its config to look like
http_filters:
- name: sample          # before envoy.router because order matters!
  typed_config:
    "@type": type.googleapis.com/sample.Decoder
    key: via
    val: sample-filter
- name: envoy.router
  typed_config: {}