injection

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2021 License: Apache-2.0 Imports: 15 Imported by: 1

README

caddy2-filter (modified for injection)

GoDoc Go Report Card

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

Originally released on https://github.com/sjtug/caddy2-filter

Usage

Only the listed fields are supported.

Caddyfile:

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

injection {
    # File to inject as inline text
    inject <file to inject>
    # Where to inject
    before "</body>"
    # Only process content_type matching this regex
    content_type <regexp pattern>
}

# If you are using reverse_proxy, you may need to 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": "injection",
    "path": "<regexp>",
    "inject": "<file>",
    "before": "<suffix>",
    "content_type": "<regexp>"
}

License

   Copyright 2020 Zheng Luo
   Copyright 2020-2021 Toowoxx IT GmbH

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateRandomBytes added in v0.4.0

func GenerateRandomBytes(len int) ([]byte, error)

func GenerateRandomStringURLSafe added in v0.4.0

func GenerateRandomStringURLSafe(len int) (string, error)

Types

type ContentTypeStatus

type ContentTypeStatus int

type InjectedWriter

type InjectedWriter struct {
	OriginalWriter http.ResponseWriter
	Request        *http.Request
	RecordedHTML   bytes.Buffer

	Logger *zap.Logger

	LineHandler LineHandler

	M *Middleware
	// contains filtered or unexported fields
}

func CreateInjectedWriter added in v0.3.0

func CreateInjectedWriter(
	w http.ResponseWriter, r *http.Request, m *Middleware,
) *InjectedWriter

func (*InjectedWriter) Flush added in v0.3.2

func (i *InjectedWriter) Flush() error

func (*InjectedWriter) HandleCSP added in v0.4.0

func (i *InjectedWriter) HandleCSP() error

func (*InjectedWriter) HandleCSPForText added in v0.4.0

func (i *InjectedWriter) HandleCSPForText(text string) string

func (*InjectedWriter) HandleLine added in v0.3.1

func (i *InjectedWriter) HandleLine(line string) (string, error)

func (InjectedWriter) Header

func (i InjectedWriter) Header() http.Header

func (*InjectedWriter) Write

func (i *InjectedWriter) Write(bytes []byte) (int, error)

func (*InjectedWriter) WriteHeader

func (i *InjectedWriter) WriteHeader(statusCode int)

type LineHandler

type LineHandler interface {
	HandleLine(line string) (string, error)
}

type Middleware

type Middleware struct {
	// Regex to specify which kind of response should we filter
	ContentType string `json:"content_type"`
	Inject      string `json:"inject"`
	Before      string `json:"before"`

	Logger *zap.Logger
	// contains filtered or unexported fields
}

func (Middleware) CaddyModule

func (Middleware) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (Middleware) IsWebSocket added in v0.5.0

func (m Middleware) IsWebSocket(r *http.Request) bool

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) ShouldBypassForRequest added in v0.5.0

func (m Middleware) ShouldBypassForRequest(w http.ResponseWriter, r *http.Request) bool

func (Middleware) ShouldBypassForResponse added in v0.5.0

func (m Middleware) ShouldBypassForResponse(w http.ResponseWriter) bool

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