-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvhttp.go
More file actions
176 lines (150 loc) · 4.76 KB
/
vhttp.go
File metadata and controls
176 lines (150 loc) · 4.76 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package vhttp
import (
"fmt"
"net/http"
"regexp"
"github.com/hashicorp/go-multierror"
)
// Common headers, used for convenience
const (
HeaderContentType = "Content-Type"
HeaderAccept = "Accept"
HeaderHost = "Host"
HeaderAuthorization = "Authorization"
HeaderConnection = "Connection"
)
// Common Content-Type / MIME type values
const (
MimePlain = "text/plain"
MimeHTML = "text/html"
MimeCSS = "text/css"
MimeTextJavascript = "text/javascript"
MimeJSON = "application/json"
MimeXML = "application/xml"
MimeImageAPNG = "image/apng"
MimeImageAVIF = "image/avif"
MimeImageGIF = "image/gif"
MimeImageJPEG = "image/jpeg"
MimeImagePNG = "image/png"
MimeImageSVGXML = "image/svg+xml"
MimeImageWEBP = "image/webp"
)
// Regular expressions for matching against (simplified)
// Authentication HTTP headers.
var (
BasicAuthMatch = regexp.MustCompile(`^Basic .+$`)
BearerAuthMatch = regexp.MustCompile(`^Bearer .+$`)
)
// CanonicallHeaderKey converts the given string to a canonical form.
//
// This function is used in Header validation function, on the keys of the
// HeaderValidators parameter. By copying the function from the net/http
// package, this can be replaced with a custom implementation, if that
// functionality needs to be changed.
var CanonicalHeaderKey func(string) string = http.CanonicalHeaderKey
// RequestValidator is a validator that validates an http.Request.
type RequestValidator interface {
ValidateRequest(req *http.Request) error
}
// RequestFunc is a function that validates an http.Request and
// can act as a RequestValidator.
type RequestFunc func(req *http.Request) error
func (v RequestFunc) ValidateRequest(req *http.Request) error {
return v(req)
}
// ResponseValidator is a validator that validates an http.Response.
type ResponseValidator interface {
ValidateResponse(res *http.Response) error
}
// ResponseFunc is a function that validates an http.Response and
// can act as a ResponseValidator.
type ResponseFunc func(res *http.Response) error
func (v ResponseFunc) ValidateResponse(res *http.Response) error {
return v(res)
}
// ValidateRequest validates the request against the given validators.
//
// err := vhttp.ValidateRequest(req,
// vhttp.MethodIsGet(), // Method == GET
// vhttp.BodyIsValidJSON(), // Body can be parsed as JSON
// vhttp.HeaderContentTypeJSON(), // Body has JSON content-type header
// vhttp.HeaderAuthorizationMatchesBearer(), // "Authorization" header matches regex `^Bearer .+$`
// vhttp.URLPathIs("/users/all"), // URL Path == "/users/all"
// )
func ValidateRequest(req *http.Request, vs ...RequestValidator) error {
// Check that the request is not nil
if req == nil {
return fmt.Errorf("request is nil")
}
// Iterate through the request validators.
var merr *multierror.Error
for _, v := range vs {
err := v.ValidateRequest(req)
if err != nil {
merr = multierror.Append(merr, err)
}
}
// Return the multi-error as an error (or nil if there are no errors).
if merr != nil {
return multierror.Flatten(merr)
}
return nil
}
// ValidateRequestFF validates the request against the given validators
// but fails fast.
//
// This is a version of the ValidateRequest function that stops at the
// first validation error returned (if any).
func ValidateRequestFF(req *http.Request, vs ...RequestValidator) error {
// Check that the request is not nil
if req == nil {
return fmt.Errorf("request is nil")
}
// Iterate through the request validators.
for _, v := range vs {
if err := v.ValidateRequest(req); err != nil {
return err
}
}
// Success!
return nil
}
// ValidateResponse validates the response against the given validators.
func ValidateResponse(res *http.Response, vs ...ResponseValidator) error {
// Check that the response is not nil
if res == nil {
return fmt.Errorf("response is nil")
}
// Iterate through the response validators.
var merr *multierror.Error
for _, v := range vs {
err := v.ValidateResponse(res)
if err != nil {
merr = multierror.Append(merr, err)
}
}
// Return the multi-error as an error (or nil if there are no errors).
if merr != nil {
return multierror.Flatten(merr)
}
return nil
}
// ValidateResponseFF validates the response against the given validators
// but fails fast.
//
// This is a version of the ValidateResponse function that stops at the
// first validation error returned (if any).
func ValidateResponseFF(res *http.Response, vs ...ResponseValidator) error {
// Check that the response is not nil
if res == nil {
return fmt.Errorf("response is nil")
}
// Iterate through the response validators.
for _, v := range vs {
if err := v.ValidateResponse(res); err != nil {
return err
}
}
// Success!
return nil
}