summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/encode
diff options
context:
space:
mode:
Diffstat (limited to 'modules/caddyhttp/encode')
-rw-r--r--modules/caddyhttp/encode/caddyfile.go69
1 files changed, 1 insertions, 68 deletions
diff --git a/modules/caddyhttp/encode/caddyfile.go b/modules/caddyhttp/encode/caddyfile.go
index c45f159..b236520 100644
--- a/modules/caddyhttp/encode/caddyfile.go
+++ b/modules/caddyhttp/encode/caddyfile.go
@@ -15,9 +15,7 @@
package encode
import (
- "net/http"
"strconv"
- "strings"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
@@ -95,7 +93,7 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
}
enc.Prefer = encs
case "match":
- err := enc.parseNamedResponseMatcher(d.NewFromNextSegment(), responseMatchers)
+ err := caddyhttp.ParseNamedResponseMatcher(d.NewFromNextSegment(), responseMatchers)
if err != nil {
return err
}
@@ -123,70 +121,5 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return nil
}
-// Parse the tokens of a named response matcher.
-//
-// match {
-// header <field> [<value>]
-// status <code...>
-// }
-//
-// Or, single line syntax:
-//
-// match [header <field> [<value>]] | [status <code...>]
-//
-func (enc *Encode) parseNamedResponseMatcher(d *caddyfile.Dispenser, matchers map[string]caddyhttp.ResponseMatcher) error {
- for d.Next() {
- definitionName := d.Val()
-
- if _, ok := matchers[definitionName]; ok {
- return d.Errf("matcher is defined more than once: %s", definitionName)
- }
-
- matcher := caddyhttp.ResponseMatcher{}
- for nesting := d.Nesting(); d.NextArg() || d.NextBlock(nesting); {
- switch d.Val() {
- case "header":
- if matcher.Headers == nil {
- matcher.Headers = http.Header{}
- }
-
- // reuse the header request matcher's unmarshaler
- headerMatcher := caddyhttp.MatchHeader(matcher.Headers)
- err := headerMatcher.UnmarshalCaddyfile(d.NewFromNextSegment())
- if err != nil {
- return err
- }
-
- matcher.Headers = http.Header(headerMatcher)
- case "status":
- if matcher.StatusCode == nil {
- matcher.StatusCode = []int{}
- }
-
- args := d.RemainingArgs()
- if len(args) == 0 {
- return d.ArgErr()
- }
-
- for _, arg := range args {
- if len(arg) == 3 && strings.HasSuffix(arg, "xx") {
- arg = arg[:1]
- }
- statusNum, err := strconv.Atoi(arg)
- if err != nil {
- return d.Errf("bad status value '%s': %v", arg, err)
- }
- matcher.StatusCode = append(matcher.StatusCode, statusNum)
- }
- default:
- return d.Errf("unrecognized response matcher %s", d.Val())
- }
- }
-
- matchers[definitionName] = matcher
- }
- return nil
-}
-
// Interface guard
var _ caddyfile.Unmarshaler = (*Encode)(nil)