valuesFromHeader returns a functions that extracts values from the request header. valuePrefix is parameter to remove first part (prefix) of the extracted value. This is useful if header value has static prefix like `Authorization: ` where part that we want to
(header string, valuePrefix string, limit uint)
| 120 | // is `Basic `. In case of JWT tokens `Authorization: Bearer <token>` prefix is `Bearer `. |
| 121 | // If prefix is left empty the whole value is returned. |
| 122 | func valuesFromHeader(header string, valuePrefix string, limit uint) ValuesExtractor { |
| 123 | prefixLen := len(valuePrefix) |
| 124 | // standard library parses http.Request header keys in canonical form but we may provide something else so fix this |
| 125 | header = textproto.CanonicalMIMEHeaderKey(header) |
| 126 | if limit == 0 { |
| 127 | limit = 1 |
| 128 | } |
| 129 | return func(c *echo.Context) ([]string, ExtractorSource, error) { |
| 130 | values := c.Request().Header.Values(header) |
| 131 | if len(values) == 0 { |
| 132 | return nil, ExtractorSourceHeader, errHeaderExtractorValueMissing |
| 133 | } |
| 134 | |
| 135 | i := uint(0) |
| 136 | result := make([]string, 0) |
| 137 | for _, value := range values { |
| 138 | if prefixLen == 0 { |
| 139 | result = append(result, value) |
| 140 | i++ |
| 141 | if i >= limit { |
| 142 | break |
| 143 | } |
| 144 | } else if len(value) > prefixLen && strings.EqualFold(value[:prefixLen], valuePrefix) { |
| 145 | result = append(result, value[prefixLen:]) |
| 146 | i++ |
| 147 | if i >= limit { |
| 148 | break |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if len(result) == 0 { |
| 154 | if prefixLen > 0 { |
| 155 | return nil, ExtractorSourceHeader, errHeaderExtractorValueInvalid |
| 156 | } |
| 157 | return nil, ExtractorSourceHeader, errHeaderExtractorValueMissing |
| 158 | } |
| 159 | return result, ExtractorSourceHeader, nil |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // valuesFromQuery returns a function that extracts values from the query string. |
| 164 | func valuesFromQuery(param string, limit uint) ValuesExtractor { |
searching dependent graphs…