filter

package module
v0.0.0-...-04be952 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

README

caddy2-filter

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

Documentation

Index

Constants

View Source
const DefaultMaxSize = 2 * 1024 * 1024

Variables

This section is empty.

Functions

This section is empty.

Types

type CappedSizeRecorder

type CappedSizeRecorder struct {
	// contains filtered or unexported fields
}

CappedSizeRecorder is like httptest.ResponseRecorder, but with a cap.

When the size of body exceeds cap, CappedSizeRecorder flushes all contents in ResponseRecorder together with all subsequent writes into the ResponseWriter

func NewCappedSizeRecorder

func NewCappedSizeRecorder(cap int, w http.ResponseWriter) *CappedSizeRecorder

func (*CappedSizeRecorder) Flush

func (csr *CappedSizeRecorder) Flush() (int64, error)

Flush contents to writer

func (*CappedSizeRecorder) FlushHeaders

func (csr *CappedSizeRecorder) FlushHeaders()

func (*CappedSizeRecorder) Header

func (csr *CappedSizeRecorder) Header() http.Header

func (*CappedSizeRecorder) Overflowed

func (csr *CappedSizeRecorder) Overflowed() bool

func (*CappedSizeRecorder) Recorder

func (csr *CappedSizeRecorder) Recorder() *httptest.ResponseRecorder

func (*CappedSizeRecorder) Write

func (csr *CappedSizeRecorder) Write(b []byte) (int, error)

func (*CappedSizeRecorder) WriteHeader

func (csr *CappedSizeRecorder) WriteHeader(statusCode int)

type Middleware

type Middleware struct {
	// Regex to specify which kind of response should we filter
	ContentType string `json:"content_type"`
	// Regex to specify which pattern to look up
	SearchPattern string `json:"search_pattern"`
	// A byte-array specifying the string used to replace matches
	Replacement []byte `json:"replacement"`

	MaxSize int    `json:"max_size"`
	Path    string `json:"path"`
	// contains filtered or unexported fields
}

Middleware implements an HTTP handler that replaces response contents based on regex

Additional configuration is required in addition to adding a filter{} block. See Github page for instructions.

func (Middleware) CaddyModule

func (Middleware) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (*Middleware) Provision

func (m *Middleware) Provision(ctx caddy.Context) error

Provision implements caddy.Provisioner.

func (Middleware) ServeHTTP

func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error

ServeHTTP implements caddyhttp.MiddlewareHandler.

func (*Middleware) UnmarshalCaddyfile

func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile implements caddyfile.Unmarshaler.

func (*Middleware) Validate

func (m *Middleware) Validate() error

Validate implements caddy.Validator.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL