@@ -13,6 +13,24 @@ const (
13
13
extractorLimit = 20
14
14
)
15
15
16
+ // ExtractorSource is type to indicate source for extracted value
17
+ type ExtractorSource string
18
+
19
+ const (
20
+ // ExtractorSourceHeader means value was extracted from request header
21
+ ExtractorSourceHeader ExtractorSource = "header"
22
+ // ExtractorSourceQuery means value was extracted from request query parameters
23
+ ExtractorSourceQuery ExtractorSource = "query"
24
+ // ExtractorSourcePathParam means value was extracted from route path parameters
25
+ ExtractorSourcePathParam ExtractorSource = "param"
26
+ // ExtractorSourceCookie means value was extracted from request cookies
27
+ ExtractorSourceCookie ExtractorSource = "cookie"
28
+ // ExtractorSourceForm means value was extracted from request form values
29
+ ExtractorSourceForm ExtractorSource = "form"
30
+ // ExtractorSourceCustom means value was extracted by custom extractor
31
+ ExtractorSourceCustom ExtractorSource = "custom"
32
+ )
33
+
16
34
// ValueExtractorError is error type when middleware extractor is unable to extract value from lookups
17
35
type ValueExtractorError struct {
18
36
message string
@@ -31,7 +49,7 @@ var errCookieExtractorValueMissing = &ValueExtractorError{message: "missing valu
31
49
var errFormExtractorValueMissing = & ValueExtractorError {message : "missing value in the form" }
32
50
33
51
// ValuesExtractor defines a function for extracting values (keys/tokens) from the given context.
34
- type ValuesExtractor func (c echo.Context ) ([]string , error )
52
+ type ValuesExtractor func (c echo.Context ) ([]string , ExtractorSource , error )
35
53
36
54
func createExtractors (lookups string ) ([]ValuesExtractor , error ) {
37
55
if lookups == "" {
@@ -75,10 +93,10 @@ func valuesFromHeader(header string, valuePrefix string) ValuesExtractor {
75
93
prefixLen := len (valuePrefix )
76
94
// standard library parses http.Request header keys in canonical form but we may provide something else so fix this
77
95
header = textproto .CanonicalMIMEHeaderKey (header )
78
- return func (c echo.Context ) ([]string , error ) {
96
+ return func (c echo.Context ) ([]string , ExtractorSource , error ) {
79
97
values := c .Request ().Header .Values (header )
80
98
if len (values ) == 0 {
81
- return nil , errHeaderExtractorValueMissing
99
+ return nil , ExtractorSourceHeader , errHeaderExtractorValueMissing
82
100
}
83
101
84
102
result := make ([]string , 0 )
@@ -100,30 +118,30 @@ func valuesFromHeader(header string, valuePrefix string) ValuesExtractor {
100
118
101
119
if len (result ) == 0 {
102
120
if prefixLen > 0 {
103
- return nil , errHeaderExtractorValueInvalid
121
+ return nil , ExtractorSourceHeader , errHeaderExtractorValueInvalid
104
122
}
105
- return nil , errHeaderExtractorValueMissing
123
+ return nil , ExtractorSourceHeader , errHeaderExtractorValueMissing
106
124
}
107
- return result , nil
125
+ return result , ExtractorSourceHeader , nil
108
126
}
109
127
}
110
128
111
129
// valuesFromQuery returns a function that extracts values from the query string.
112
130
func valuesFromQuery (param string ) ValuesExtractor {
113
- return func (c echo.Context ) ([]string , error ) {
131
+ return func (c echo.Context ) ([]string , ExtractorSource , error ) {
114
132
result := c .QueryParams ()[param ]
115
133
if len (result ) == 0 {
116
- return nil , errQueryExtractorValueMissing
134
+ return nil , ExtractorSourceQuery , errQueryExtractorValueMissing
117
135
} else if len (result ) > extractorLimit - 1 {
118
136
result = result [:extractorLimit ]
119
137
}
120
- return result , nil
138
+ return result , ExtractorSourceQuery , nil
121
139
}
122
140
}
123
141
124
142
// valuesFromParam returns a function that extracts values from the url param string.
125
143
func valuesFromParam (param string ) ValuesExtractor {
126
- return func (c echo.Context ) ([]string , error ) {
144
+ return func (c echo.Context ) ([]string , ExtractorSource , error ) {
127
145
result := make ([]string , 0 )
128
146
for i , p := range c .PathParams () {
129
147
if param == p .Name {
@@ -134,18 +152,18 @@ func valuesFromParam(param string) ValuesExtractor {
134
152
}
135
153
}
136
154
if len (result ) == 0 {
137
- return nil , errParamExtractorValueMissing
155
+ return nil , ExtractorSourcePathParam , errParamExtractorValueMissing
138
156
}
139
- return result , nil
157
+ return result , ExtractorSourcePathParam , nil
140
158
}
141
159
}
142
160
143
161
// valuesFromCookie returns a function that extracts values from the named cookie.
144
162
func valuesFromCookie (name string ) ValuesExtractor {
145
- return func (c echo.Context ) ([]string , error ) {
163
+ return func (c echo.Context ) ([]string , ExtractorSource , error ) {
146
164
cookies := c .Cookies ()
147
165
if len (cookies ) == 0 {
148
- return nil , errCookieExtractorValueMissing
166
+ return nil , ExtractorSourceCookie , errCookieExtractorValueMissing
149
167
}
150
168
151
169
result := make ([]string , 0 )
@@ -158,26 +176,26 @@ func valuesFromCookie(name string) ValuesExtractor {
158
176
}
159
177
}
160
178
if len (result ) == 0 {
161
- return nil , errCookieExtractorValueMissing
179
+ return nil , ExtractorSourceCookie , errCookieExtractorValueMissing
162
180
}
163
- return result , nil
181
+ return result , ExtractorSourceCookie , nil
164
182
}
165
183
}
166
184
167
185
// valuesFromForm returns a function that extracts values from the form field.
168
186
func valuesFromForm (name string ) ValuesExtractor {
169
- return func (c echo.Context ) ([]string , error ) {
187
+ return func (c echo.Context ) ([]string , ExtractorSource , error ) {
170
188
if c .Request ().Form == nil {
171
189
_ = c .Request ().ParseMultipartForm (32 << 20 ) // same what `c.Request().FormValue(name)` does
172
190
}
173
191
values := c .Request ().Form [name ]
174
192
if len (values ) == 0 {
175
- return nil , errFormExtractorValueMissing
193
+ return nil , ExtractorSourceForm , errFormExtractorValueMissing
176
194
}
177
195
if len (values ) > extractorLimit - 1 {
178
196
values = values [:extractorLimit ]
179
197
}
180
198
result := append ([]string {}, values ... )
181
- return result , nil
199
+ return result , ExtractorSourceForm , nil
182
200
}
183
201
}
0 commit comments