MCPcopy
hub / github.com/caddyserver/caddy / parseCaddyfileRewrite

Function parseCaddyfileRewrite

modules/caddyhttp/rewrite/caddyfile.go:41–70  ·  view source on GitHub ↗

parseCaddyfileRewrite sets up a basic rewrite handler from Caddyfile tokens. Syntax: rewrite [<matcher>] <to> Only URI components which are given in <to> will be set in the resulting URI. See the docs for the rewrite handler for more information.

(h httpcaddyfile.Helper)

Source from the content-addressed store, hash-verified

39// Only URI components which are given in <to> will be set in the resulting URI.
40// See the docs for the rewrite handler for more information.
41func parseCaddyfileRewrite(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) {
42 h.Next() // consume directive name
43
44 // count the tokens to determine what to do
45 argsCount := h.CountRemainingArgs()
46 if argsCount == 0 {
47 return nil, h.Errf("too few arguments; must have at least a rewrite URI")
48 }
49 if argsCount > 2 {
50 return nil, h.Errf("too many arguments; should only be a matcher and a URI")
51 }
52
53 // with only one arg, assume it's a rewrite URI with no matcher token
54 if argsCount == 1 {
55 if !h.NextArg() {
56 return nil, h.ArgErr()
57 }
58 return h.NewRoute(nil, Rewrite{URI: h.Val()}), nil
59 }
60
61 // parse the matcher token into a matcher set
62 userMatcherSet, err := h.ExtractMatcherSet()
63 if err != nil {
64 return nil, err
65 }
66 h.Next() // consume directive name again, matcher parsing does a reset
67 h.Next() // advance to the rewrite URI
68
69 return h.NewRoute(userMatcherSet, Rewrite{URI: h.Val()}), nil
70}
71
72// parseCaddyfileMethod sets up a basic method rewrite handler from Caddyfile tokens. Syntax:
73//

Callers

nothing calls this directly

Calls 8

CountRemainingArgsMethod · 0.80
ErrfMethod · 0.80
NextArgMethod · 0.80
ArgErrMethod · 0.80
NewRouteMethod · 0.80
ValMethod · 0.80
ExtractMatcherSetMethod · 0.80
NextMethod · 0.65

Tested by

no test coverage detected