Skip to content

sjtug/caddy2-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

caddy2-filter

Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. GoDoc Go Report Card

Replace text in HTTP response based on regex. Similar to http.filter in Caddy 1.

Usage

Only the listed fields are supported.

The replacement supports capturing groups of search_pattern (e.g. {1}) and caddy placeholders (e.g. {http.request.hostport})

Caddyfile:

# Add this block in top-level settings:
{
    order filter after encode
}

filter {
    # Only process URL matching this regex
    path <optional, regexp pattern, default: .*>
    # Don't process response body larger than this size
    max_size <optional, int, default: 2097152>
    search_pattern <regexp pattern>
    replacement <replacement string>
    # Only process content_type matching this regex
    content_type <regexp pattern>
}

# If you are using reverse_proxy, add this to its config to ensure
# reverse_proxy returns uncompressed body:

header_up -Accept-Encoding

JSON config (under apps › http › servers › routes › handle)

{
    "handler": "filter",
    "max_size": <int>,
    "path": "<regexp>",
    "search_pattern": "<regexp>",
    "replacement: "<string>",
    "content_type": "<regexp>"
}

Alternatives

As of Jun 2021, https://github.com/caddyserver/replace-response can achieve similar functionalities. This plugin's design differs from that one in the following aspects:

  1. This plugin supports placeholders like {http.host}

  2. This plugin allows capping the max size of buffered response

  3. This plugin supports only replacing responses with certain content_types

  4. replace-response supports stream mode, which features in better performance at the cost of possibilities of omitted replacements

  5. replace-response is a semi-official plugin maintained by the same author of caddy