UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax: encode [<matcher>] <formats...> { gzip [<level>] zstd [<level>] { level <level> disable_checksum } minimum_length <length> # response matcher block ma
(d *caddyfile.Dispenser)
| 57 | // |
| 58 | // Specifying the formats on the first line will use those formats' defaults. |
| 59 | func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { |
| 60 | d.Next() // consume directive name |
| 61 | |
| 62 | prefer := []string{} |
| 63 | remainingArgs := d.RemainingArgs() |
| 64 | |
| 65 | responseMatchers := make(map[string]caddyhttp.ResponseMatcher) |
| 66 | for d.NextBlock(0) { |
| 67 | switch d.Val() { |
| 68 | case "minimum_length": |
| 69 | if !d.NextArg() { |
| 70 | return d.ArgErr() |
| 71 | } |
| 72 | minLength, err := strconv.Atoi(d.Val()) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | enc.MinLength = minLength |
| 77 | case "match": |
| 78 | err := caddyhttp.ParseNamedResponseMatcher(d.NewFromNextSegment(), responseMatchers) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | matcher := responseMatchers["match"] |
| 83 | enc.Matcher = &matcher |
| 84 | default: |
| 85 | name := d.Val() |
| 86 | modID := "http.encoders." + name |
| 87 | unm, err := caddyfile.UnmarshalModule(d, modID) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | encoding, ok := unm.(Encoding) |
| 92 | if !ok { |
| 93 | return d.Errf("module %s is not an HTTP encoding; is %T", modID, unm) |
| 94 | } |
| 95 | if enc.EncodingsRaw == nil { |
| 96 | enc.EncodingsRaw = make(caddy.ModuleMap) |
| 97 | } |
| 98 | enc.EncodingsRaw[name] = caddyconfig.JSON(encoding, nil) |
| 99 | prefer = append(prefer, name) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if len(prefer) == 0 && len(remainingArgs) == 0 { |
| 104 | remainingArgs = []string{"zstd", "gzip"} |
| 105 | } |
| 106 | |
| 107 | for _, arg := range remainingArgs { |
| 108 | mod, err := caddy.GetModule("http.encoders." + arg) |
| 109 | if err != nil { |
| 110 | return d.Errf("finding encoder module '%s': %v", mod, err) |
| 111 | } |
| 112 | encoding, ok := mod.New().(Encoding) |
| 113 | if !ok { |
| 114 | return d.Errf("module %s is not an HTTP encoding", mod) |
| 115 | } |
| 116 | if enc.EncodingsRaw == nil { |
nothing calls this directly
no test coverage detected