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

Method UnmarshalCaddyfile

modules/caddyhttp/staticerror.go:62–95  ·  view source on GitHub ↗

UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax: error [<matcher>] <status>|<message> [<status>] { message <text> } If there is just one argument (other than the matcher), it is considered to be a status code if it's a valid positive integer of 3 digits.

(d *caddyfile.Dispenser)

Source from the content-addressed store, hash-verified

60// If there is just one argument (other than the matcher), it is considered
61// to be a status code if it's a valid positive integer of 3 digits.
62func (e *StaticError) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
63 d.Next() // consume directive name
64 args := d.RemainingArgs()
65 switch len(args) {
66 case 1:
67 if len(args[0]) == 3 {
68 if num, err := strconv.Atoi(args[0]); err == nil && num > 0 {
69 e.StatusCode = WeakString(args[0])
70 break
71 }
72 }
73 e.Error = args[0]
74 case 2:
75 e.Error = args[0]
76 e.StatusCode = WeakString(args[1])
77 default:
78 return d.ArgErr()
79 }
80
81 for d.NextBlock(0) {
82 switch d.Val() {
83 case "message":
84 if e.Error != "" {
85 return d.Err("message already specified")
86 }
87 if !d.AllArgs(&e.Error) {
88 return d.ArgErr()
89 }
90 default:
91 return d.Errf("unrecognized subdirective '%s'", d.Val())
92 }
93 }
94 return nil
95}
96
97func (e StaticError) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Handler) error {
98 repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)

Callers

nothing calls this directly

Calls 9

WeakStringTypeAlias · 0.85
RemainingArgsMethod · 0.80
ArgErrMethod · 0.80
NextBlockMethod · 0.80
ValMethod · 0.80
ErrMethod · 0.80
AllArgsMethod · 0.80
ErrfMethod · 0.80
NextMethod · 0.65

Tested by

no test coverage detected