replaceresponseonce

package module
v0.0.0-...-c4ce0ff Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

README

Caddy replace_response_once handler module

A fork of the replace_response module, with modifications to only allow single-instance replacements (i.e., replacing only the first occurrence of a match).

Note: Does not support regex-based replacements.

Module name: http.handlers.replace_response

JSON examples

Substring substitution:

{
	"handler": "replace_response_once",
	"replacements": [
		{
			"search": "Foo",
			"replace": "Bar"
		}
	]
}

Same, but with streaming mode (we just set "stream": true in the handler):

{
	"handler": "replace_response_once",
	"replacements": [
		{
			"search": "Foo",
			"replace": "Bar"
		}
	],
	"stream": true

With a response matcher:

{
	"handler": "replace_response_once",
	"replacements": [
		{
			"search": "Foo",
			"replace": "Bar"
		}
	],
	"match": {
		"headers": {
			"Content-Type": ["application/json*"]
		}
	}
}

Caddyfile

This module has Caddyfile support. It registers the replace directive, by default to be after the standard encode directive. Make sure to change it with the order global option in case that is not suitable for your needs.

Syntax:

replace [<matcher>] [stream | <search> <replace>] {
	stream
	match {
		header Content-Type application/json*
	}
	[re] <search> <replace>
}
  • stream enables streaming mode.
  • match defines a response matcher. If defined, replacements in this directive will only be performed on responses that match the matcher.
  • Note that you can use a matcher token to filter which requests have replacements performed.

Simple substring substitution:

replace Foo Bar

Streaming mode:

replace stream {
	Foo Bar
}

Multiple replacements:

replace {
	Foo Bar
	A B
}

Limitations:

  • Compressed responses (e.g. from an upstream proxy which gzipped the response body) will not be decoded before attempting to replace. To work around this, you may send the Accept-Encoding: identity request header to the upstream to tell it not to compress the response. For example:

    reverse_proxy localhost:8080 {
        header_up Accept-Encoding identity
    }
    

Documentation

Overview

Package replaceresponseonce registers a Caddy HTTP handler module that performs replacements on response bodies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler struct {
	// The list of replacements to make on the response body.
	Replacements []*Replacement `json:"replacements,omitempty"`

	// If true, perform replacements in a streaming fashion.
	// This is more memory-efficient but can remove the
	// Content-Length header since knowing the correct length
	// is impossible without buffering, and getting it wrong
	// can break HTTP/2 streams.
	Stream bool `json:"stream,omitempty"`

	// Only run replacements on responses that match against this ResponseMmatcher.
	Matcher *caddyhttp.ResponseMatcher `json:"match,omitempty"`
	// contains filtered or unexported fields
}

Handler manipulates response bodies by performing substring or regex replacements.

func (Handler) CaddyModule

func (Handler) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (*Handler) Provision

func (h *Handler) Provision(ctx caddy.Context) error

Provision implements caddy.Provisioner.

func (*Handler) ServeHTTP

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

ServeHTTP implements caddyhttp.MiddlewareHandler.

func (*Handler) UnmarshalCaddyfile

func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile implements caddyfile.Unmarshaler. Syntax:

replace [stream | [re] <search> <replace>] {
    stream
	match {
		header Content-Type application/json*
	}
    [re] <search> <replace>
}

If 're' is specified, the search string will be treated as a regular expression. If 'stream' is specified, the replacement will happen without buffering the whole response body; this might remove the Content-Length header.

type Replacement

type Replacement struct {
	// A substring to search for. Mutually exclusive with search_regexp.
	Search string `json:"search,omitempty"`

	// The replacement string/value. Required.
	Replace string `json:"replace"`
	// contains filtered or unexported fields
}

Replacement is either a substring or regular expression replacement to perform; precisely one must be specified, not both.

Jump to

Keyboard shortcuts

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