Skip to content

danielbjornadal/traefik-response-transform-plugin

Repository files navigation

Traefik Response Transform Plugin

A Traefik middleware plugin that buffers upstream responses, optionally rewrites the response body with regex replacements, and applies ordered response header rules (set / add / remove). It can decode and re-encode gzip and deflate bodies so replacements work when the origin compresses responses.

Body and header transforms run only when the request method and response Content-Type match configurable monitoring rules (defaults: GET and text/html). The plugin sets Accept-Encoding on the upstream request to encodings it supports (gzip, deflate, identity) so backends prefer compressions it can round-trip.

This plugin was inspired by the response-buffering approach used in packruler/rewrite-body (Apache-2.0) and follows the same Traefik middleware shape as traefik-cloudflare-plugin.

Memory and buffering

The full response body is held in memory while processing. For large payloads, combine this middleware with Traefik’s Buffering middleware and tune limits appropriately.

Building

git clone https://github.com/danielbjornadal/traefik-response-transform-plugin.git
cd traefik-response-transform-plugin
go build -buildmode=plugin -o traefik_response_transform_plugin.so

Static configuration (Traefik)

experimental:
  plugins:
    traefik_response_transform_plugin:
      moduleName: github.com/danielbjornadal/traefik-response-transform-plugin
      version: v0.1.0

Dynamic configuration (example)

http:
  routers:
    app:
      rule: Host(`example.com`)
      middlewares:
        - response-transform
      service: my-service

  middlewares:
    response-transform:
      plugin:
        traefik_response_transform_plugin:
          lastModified: false
          monitoring:
            methods:
              - GET
            types:
              - text/html
          rewrites:
            - regex: "foo"
              replacement: "bar"
          responseHeaders:
            - name: "X-Transformed"
              value: "1"
              action: set
            - name: "Server"
              action: remove

Configuration fields

Field Description
rewrites List of { regex, replacement } pairs applied to the decoded body (regex compiled at startup).
lastModified If false (default), the Last-Modified response header is removed when the response is processed.
monitoring.methods HTTP methods that may be transformed (default GET).
monitoring.types Substrings matched against the response Content-Type (default text/html).
responseHeaders Ordered rules: action is set, add, or remove; name is required; value used for set/add.

If both rewrites and responseHeaders are empty, the middleware forwards the request without buffering.

WebSockets

WebSocket upgrade requests are passed through to the next handler without buffering.

Handlers that hijack the underlying connection cannot have their response buffered by this middleware (the same limitation applies to similar body-rewriting plugins).

License

See LICENSE.

About

No description or website provided.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages