Added param extractor

This commit is contained in:
Maxim Lebedev 2022-02-26 03:18:49 +05:00
parent a214a7e9e8
commit 7375a6e8ed
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 18 additions and 0 deletions

View File

@ -48,6 +48,8 @@ func createExtractors(lookups, authScheme string) ([]ValuesExtractor, error) {
switch parts[0] {
case "query":
extractors = append(extractors, valuesFromQuery(parts[1]))
case "param":
extractors = append(extractors, valuesFromParam(parts[1]))
case "cookie":
extractors = append(extractors, valuesFromCookie(parts[1]))
case "form":
@ -121,6 +123,22 @@ func valuesFromQuery(param string) ValuesExtractor {
}
}
// valuesFromParam returns a function that extracts values from the url param string.
func valuesFromParam(param string) ValuesExtractor {
return func(ctx *http.RequestCtx) ([][]byte, error) {
if !ctx.PostArgs().Has(param) {
return nil, errParamExtractorValueMissing
}
result := ctx.PostArgs().PeekMulti(param)
if len(result) > extractorLimit-1 {
result = result[:extractorLimit]
}
return result, nil
}
}
// valuesFromCookie returns a function that extracts values from the named cookie.
func valuesFromCookie(name string) ValuesExtractor {
return func(ctx *http.RequestCtx) ([][]byte, error) {