Skip to content

Commit

Permalink
rename intercept
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Apr 12, 2024
1 parent a17ad8c commit 8aa881f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion caddyconfig/httpcaddyfile/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var defaultDirectiveOrder = []string{
"request_header",
"encode",
"push",
"intercept_response",
"intercept",
"templates",

// special routing & dispatching directives
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ localhost

respond "To intercept"

intercept_response {
intercept {
@500 status 500
replace_status @500 400

Expand Down Expand Up @@ -209,7 +209,7 @@ intercept_response {
]
}
],
"handler": "intercept_response"
"handler": "intercept"
},
{
"body": "To intercept",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/caddyserver/caddy/v2/caddytest"
)

func TestInterceptResponse(t *testing.T) {
func TestIntercept(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`{
skip_install_trust
Expand All @@ -20,7 +20,7 @@ func TestInterceptResponse(t *testing.T) {
respond /intercept "I'm a teapot" 408
respond /no-intercept "I'm not a teapot"
intercept_response {
intercept {
@teapot status 408
handle_response @teapot {
respond /intercept "I'm a combined coffee/tea pot that is temporarily out of coffee" 503
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import (
)

func init() {
caddy.RegisterModule(InterceptResponse{})
httpcaddyfile.RegisterHandlerDirective("intercept_response", parseCaddyfile)
caddy.RegisterModule(Intercept{})
httpcaddyfile.RegisterHandlerDirective("intercept", parseCaddyfile)
}

type InterceptResponse struct {
type Intercept struct {
// List of handlers and their associated matchers to evaluate
// after successful response generation.
// The first handler that matches the original response will
Expand All @@ -44,9 +44,9 @@ type InterceptResponse struct {
// it is up to the handler to finish handling the response.
//
// Three new placeholders are available in this handler chain:
// - `{http.intercept_response.status_code}` The status code from the response
// - `{http.intercept_response.status_text}` The status text from the response
// - `{http.intercept_response.header.*}` The headers from the response
// - `{http.intercept.status_code}` The status code from the response
// - `{http.intercept.status_text}` The status text from the response
// - `{http.intercept.header.*}` The headers from the response
HandleResponse []caddyhttp.ResponseHandler `json:"handle_response,omitempty"`

// Holds the named response matchers from the Caddyfile while adapting
Expand All @@ -59,15 +59,15 @@ type InterceptResponse struct {
}

// CaddyModule returns the Caddy module information.
func (InterceptResponse) CaddyModule() caddy.ModuleInfo {
func (Intercept) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.intercept_response",
New: func() caddy.Module { return new(InterceptResponse) },
ID: "http.handlers.intercept",
New: func() caddy.Module { return new(Intercept) },
}
}

// Provision ensures that i is set up properly before use.
func (irh *InterceptResponse) Provision(ctx caddy.Context) error {
func (irh *Intercept) Provision(ctx caddy.Context) error {
// set up any response routes
for i, rh := range irh.HandleResponse {
err := rh.Provision(ctx)
Expand Down Expand Up @@ -107,7 +107,7 @@ func (irh interceptedResponseHandler) WriteHeader(statusCode int) {
irh.ResponseRecorder.WriteHeader(statusCode)
}

func (ir InterceptResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
func (ir Intercept) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()

Expand Down Expand Up @@ -149,9 +149,9 @@ func (ir InterceptResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, ne
// set up the replacer so that parts of the original response can be
// used for routing decisions
for field, value := range r.Header {
repl.Set("http.intercept_response.header."+field, strings.Join(value, ","))
repl.Set("http.intercept.header."+field, strings.Join(value, ","))
}
repl.Set("http.intercept_response.status_code", rec.Status())
repl.Set("http.intercept.status_code", rec.Status())

ir.logger.Debug("handling response", zap.Int("handler", rec.handlerIndex))

Expand All @@ -161,7 +161,7 @@ func (ir InterceptResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, ne

// UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:
//
// intercept_response [<matcher>] {
// intercept [<matcher>] {
// # intercept original responses
// @name {
// status <code...>
Expand All @@ -175,7 +175,7 @@ func (ir InterceptResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, ne
//
// The FinalizeUnmarshalCaddyfile method should be called after this
// to finalize parsing of "handle_response" blocks, if possible.
func (i *InterceptResponse) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
func (i *Intercept) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// collect the response matchers defined as subdirectives
// prefixed with "@" for use with "handle_response" blocks
i.responseMatchers = make(map[string]caddyhttp.ResponseMatcher)
Expand Down Expand Up @@ -241,7 +241,7 @@ func (i *InterceptResponse) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {

// FinalizeUnmarshalCaddyfile finalizes the Caddyfile parsing which
// requires having an httpcaddyfile.Helper to function, to parse subroutes.
func (i *InterceptResponse) FinalizeUnmarshalCaddyfile(helper httpcaddyfile.Helper) error {
func (i *Intercept) FinalizeUnmarshalCaddyfile(helper httpcaddyfile.Helper) error {
for _, d := range i.handleResponseSegments {
// consume the "handle_response" token
d.Next()
Expand Down Expand Up @@ -313,7 +313,7 @@ func (i *InterceptResponse) FinalizeUnmarshalCaddyfile(helper httpcaddyfile.Help
const matcherPrefix = "@"

func parseCaddyfile(helper httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var ir InterceptResponse
var ir Intercept
if err := ir.UnmarshalCaddyfile(helper.Dispenser); err != nil {
return nil, err
}
Expand All @@ -327,7 +327,7 @@ func parseCaddyfile(helper httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, e

// Interface guards
var (
_ caddy.Provisioner = (*InterceptResponse)(nil)
_ caddyfile.Unmarshaler = (*InterceptResponse)(nil)
_ caddyhttp.MiddlewareHandler = (*InterceptResponse)(nil)
_ caddy.Provisioner = (*Intercept)(nil)
_ caddyfile.Unmarshaler = (*Intercept)(nil)
_ caddyhttp.MiddlewareHandler = (*Intercept)(nil)
)
2 changes: 1 addition & 1 deletion modules/caddyhttp/standard/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
_ "github.com/caddyserver/caddy/v2/modules/caddyhttp/encode/zstd"
_ "github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver"
_ "github.com/caddyserver/caddy/v2/modules/caddyhttp/headers"
_ "github.com/caddyserver/caddy/v2/modules/caddyhttp/interceptresponse"
_ "github.com/caddyserver/caddy/v2/modules/caddyhttp/intercept"
_ "github.com/caddyserver/caddy/v2/modules/caddyhttp/logging"
_ "github.com/caddyserver/caddy/v2/modules/caddyhttp/map"
_ "github.com/caddyserver/caddy/v2/modules/caddyhttp/proxyprotocol"
Expand Down

0 comments on commit 8aa881f

Please sign in to comment.