/
handler.go
144 lines (126 loc) · 3.83 KB
/
handler.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package openapi
import (
"fmt"
"strings"
"net/http"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
func (oapi OpenAPI) ServeHTTP(w http.ResponseWriter, req *http.Request, next caddyhttp.Handler) error {
url := req.URL
if oapi.ValidateServers {
url.Host = req.Host
if nil == req.TLS {
url.Scheme = "http"
} else {
url.Scheme = "https"
}
}
replacer := req.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
replacer.Set(OPENAPI_ERROR, "")
replacer.Set(OPENAPI_STATUS_CODE, "")
replacer.Set(OPENAPI_RESPONSE_ERROR, "")
route, pathParams, err := oapi.router.FindRoute(req)
if nil != err {
replacer.Set(OPENAPI_ERROR, err.Error())
replacer.Set(OPENAPI_STATUS_CODE, 404)
if oapi.LogError {
oapi.err(fmt.Sprintf("%s %s %s: %s", getIP(req), req.Method, req.RequestURI, err))
}
if !oapi.FallThrough {
return err
}
}
// don't check if we have a 404 on the route
if (nil == err) && (nil != oapi.Check) {
if oapi.Check.RequestParams {
validateReqInput := &openapi3filter.RequestValidationInput{
Request: req,
PathParams: pathParams,
Route: route,
Options: &openapi3filter.Options{
ExcludeRequestBody: !oapi.Check.RequestBody,
},
}
err = openapi3filter.ValidateRequest(req.Context(), validateReqInput)
if err != nil {
if reqErr, ok := err.(*openapi3filter.RequestError); ok {
// Handle request validation error
replacer.Set(OPENAPI_ERROR, reqErr.Error())
replacer.Set(OPENAPI_STATUS_CODE, 400)
} else {
// Handle security requirements validation error
securityReqErr := err.(*openapi3filter.SecurityRequirementsError)
replacer.Set(OPENAPI_ERROR, securityReqErr.Error())
replacer.Set(OPENAPI_STATUS_CODE, 500)
}
if oapi.LogError {
oapi.err(fmt.Sprintf(">> %s %s %s: %s", getIP(req), req.Method, req.RequestURI, err))
}
if !oapi.FallThrough {
return err
}
}
}
}
if query, exists := resolvePolicy(route, req.Method); exists {
result, err := evalPolicy(query, oapi.policy, req, pathParams)
if nil != err {
replacer.Set(OPENAPI_ERROR, err.Error())
replacer.Set(OPENAPI_STATUS_CODE, 403)
if oapi.LogError {
oapi.err(err.Error())
}
return nil
}
if !result {
err = fmt.Errorf("Denied: %s", query)
replacer.Set(OPENAPI_ERROR, err.Error())
replacer.Set(OPENAPI_STATUS_CODE, 403)
if oapi.LogError {
oapi.err(err.Error())
}
return err
}
}
wrapper := &WrapperResponseWriter{ResponseWriter: w}
if err := next.ServeHTTP(wrapper, req); nil != err {
return err
}
if nil != oapi.contentMap {
contentType := w.Header().Get("Content-Type")
if "" == contentType {
return nil
}
contentType = strings.ToLower(strings.TrimSpace(strings.Split(contentType, ";")[0]))
_, ok := oapi.contentMap[contentType]
if !ok {
return nil
}
validateReqInput := &openapi3filter.RequestValidationInput{
Request: req,
PathParams: pathParams,
Route: route,
Options: &openapi3filter.Options{
ExcludeRequestBody: true,
ExcludeResponseBody: false,
IncludeResponseStatus: true,
},
}
if (nil != wrapper.Buffer) && (len(wrapper.Buffer) > 0) {
validateRespInput := &openapi3filter.ResponseValidationInput{
RequestValidationInput: validateReqInput,
Status: wrapper.StatusCode,
Header: http.Header{"Content-Type": oapi.Check.ResponseBody},
}
validateRespInput.SetBodyBytes(wrapper.Buffer)
if err := openapi3filter.ValidateResponse(req.Context(), validateRespInput); nil != err {
respErr := err.(*openapi3filter.ResponseError)
replacer.Set(OPENAPI_RESPONSE_ERROR, respErr.Error())
oapi.err(fmt.Sprintf("<< %s %s %s: %s", getIP(req), req.Method, req.RequestURI, respErr.Error()))
}
}
}
return nil
}