-
Notifications
You must be signed in to change notification settings - Fork 0
/
skipvalidation.go
39 lines (32 loc) · 1.07 KB
/
skipvalidation.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
package requestaddons
import (
"context"
"net/http"
"k8s.io/apiserver/pkg/endpoints/request"
)
type skipValidationKeyType int
const skipValidationKey skipValidationKeyType = iota
// WithSkipValidationHandler adds a query parameter parsing for skipping validation
func WithSkipValidationHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
skipValidation := req.FormValue("skip-validation") == "1"
ctx := req.Context()
req = req.WithContext(WithSkipValidation(ctx, skipValidation))
handler.ServeHTTP(w, req)
})
}
// WithSkipValidation adds the skip-validation info in the request context
func WithSkipValidation(parent context.Context, skipValidation bool) context.Context {
return request.WithValue(parent, skipValidationKey, skipValidation)
}
// SkipValidationFrom gets the skip-validation option value from the context
func SkipValidationFrom(ctx context.Context) bool {
val := ctx.Value(skipValidationKey)
if val == nil {
return false
}
if v, ok := val.(bool); ok {
return v
}
return false
}