-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsonparse.go
89 lines (75 loc) · 2.12 KB
/
jsonparse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package jsonparse
import (
"net/http"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
)
// Interface guards
var (
_ caddy.Provisioner = (*JSONParse)(nil)
_ caddyhttp.MiddlewareHandler = (*JSONParse)(nil)
_ caddyfile.Unmarshaler = (*JSONParse)(nil)
)
func init() {
caddy.RegisterModule(JSONParse{})
httpcaddyfile.RegisterHandlerDirective("json_parse", parseCaddyfile)
}
// JSONParse implements an HTTP handler that parses
// json body as placeholders.
type JSONParse struct {
Strict bool `json:"strict,omitempty"`
log *zap.Logger
}
// CaddyModule returns the Caddy module information.
func (JSONParse) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.json_parse",
New: func() caddy.Module { return new(JSONParse) },
}
}
// Provision implements caddy.Provisioner.
func (j *JSONParse) Provision(ctx caddy.Context) error {
j.log = ctx.Logger(j)
return nil
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (j JSONParse) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
replacerFunc, err := newReplacerFunc(r)
if err != nil {
if j.Strict {
return caddyhttp.Error(http.StatusBadRequest, err)
}
j.log.Debug("", zap.Error(err))
}
if err == nil {
repl.Map(replacerFunc)
}
return next.ServeHTTP(w, r)
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (j *JSONParse) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
args := d.RemainingArgs()
switch len(args) {
case 0:
case 1:
if args[0] != "strict" {
return d.Errf("unexpected token '%s'", args[0])
}
j.Strict = true
default:
return d.ArgErr()
}
}
return nil
}
// parseCaddyfile unmarshals tokens from h into a new Middleware.
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m JSONParse
err := m.UnmarshalCaddyfile(h.Dispenser)
return m, err
}