README
caddy2-filter (modified for injection)
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 ¶
- func GenerateRandomBytes(len int) ([]byte, error)
- func GenerateRandomStringURLSafe(len int) (string, error)
- type ContentTypeStatus
- type InjectedWriter
- func (i *InjectedWriter) Flush() error
- func (i *InjectedWriter) HandleCSP() error
- func (i *InjectedWriter) HandleCSPForText(text string) string
- func (i *InjectedWriter) HandleLine(line string) (string, error)
- func (i InjectedWriter) Header() http.Header
- func (i *InjectedWriter) Write(bytes []byte) (int, error)
- func (i *InjectedWriter) WriteHeader(statusCode int)
- type LineHandler
- type Middleware
- func (Middleware) CaddyModule() caddy.ModuleInfo
- func (m Middleware) IsWebSocket(r *http.Request) bool
- func (m *Middleware) Provision(ctx caddy.Context) error
- func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error
- func (m Middleware) ShouldBypassForRequest(w http.ResponseWriter, r *http.Request) bool
- func (m Middleware) ShouldBypassForResponse(w http.ResponseWriter) bool
- func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
- func (m *Middleware) Validate() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateRandomBytes ¶
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 ¶
func CreateInjectedWriter( w http.ResponseWriter, r *http.Request, m *Middleware, ) *InjectedWriter
func (*InjectedWriter) Flush ¶
func (i *InjectedWriter) Flush() error
func (*InjectedWriter) HandleCSP ¶
func (i *InjectedWriter) HandleCSP() error
func (*InjectedWriter) HandleCSPForText ¶
func (i *InjectedWriter) HandleCSPForText(text string) string
func (*InjectedWriter) HandleLine ¶
func (i *InjectedWriter) HandleLine(line string) (string, error)
func (InjectedWriter) Header ¶
func (i InjectedWriter) Header() http.Header
func (*InjectedWriter) WriteHeader ¶
func (i *InjectedWriter) WriteHeader(statusCode int)
type LineHandler ¶
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 ¶
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 ¶
func (m Middleware) ShouldBypassForRequest(w http.ResponseWriter, r *http.Request) bool
func (Middleware) ShouldBypassForResponse ¶
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.