1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-22 20:06:21 +02:00
echo/middleware/extractor.go
toimtoimtoim 6ef5f77bf2 WIP: logger examples
WIP: make default logger implemented custom writer for jsonlike logs
WIP: improve examples
WIP: defaultErrorHandler use errors.As to unwrap errors. Update readme
WIP: default logger logs json, restore e.Start method
WIP: clean router.Match a bit
WIP: func types/fields have echo.Context has first element
WIP: remove yaml tags as functions etc can not be serialized anyway
WIP: change BindPathParams,BindQueryParams,BindHeaders from methods to functions and reverse arguments to be like DefaultBinder.Bind is
WIP: improved comments, logger now extracts status from error
WIP: go mod tidy
WIP: rebase with 4.5.0
WIP:
* removed todos.
* removed StartAutoTLS and StartH2CServer methods from `StartConfig`
* KeyAuth middleware errorhandler can swallow the error and resume next middleware
WIP: add RouterConfig.UseEscapedPathForMatching to use escaped path for matching request against routes
WIP: FIXMEs
WIP: upgrade golang-jwt/jwt to `v4`
WIP: refactor http methods to return RouteInfo
WIP: refactor static not creating multiple routes
WIP: refactor route and middleware adding functions not to return error directly
WIP: Use 401 for problematic/missing headers for key auth and JWT middleware (#1552, #1402).
> In summary, a 401 Unauthorized response should be used for missing or bad authentication
WIP: replace `HTTPError.SetInternal` with `HTTPError.WithInternal` so we could not mutate global error variables
WIP: add RouteInfo and RouteMatchType into Context what we could know from in middleware what route was matched and/or type of that match (200/404/405)
WIP: make notFoundHandler and methodNotAllowedHandler private. encourage that all errors be handled in Echo.HTTPErrorHandler
WIP: server cleanup ideas
WIP: routable.ForGroup
WIP: note about logger middleware
WIP: bind should not default values on second try. use crypto rand for better randomness
WIP: router add route as interface and returns info as interface
WIP: improve flaky test (remains still flaky)
WIP: add notes about bind default values
WIP: every route can have their own path params names
WIP: routerCreator and different tests
WIP: different things
WIP: remove route implementation
WIP: support custom method types
WIP: extractor tests
WIP: v5.0.x proposal
over v4.4.0
2021-10-02 18:36:42 +03:00

149 lines
4.6 KiB
Go

package middleware
import (
"fmt"
"github.com/labstack/echo/v4"
"net/http"
"net/textproto"
"strings"
)
// ErrExtractionValueMissing denotes an error raised when value could not be extracted from request
var ErrExtractionValueMissing = echo.NewHTTPError(http.StatusBadRequest, "missing or malformed value")
// ExtractorType is enum type for where extractor will take its data
type ExtractorType string
const (
// HeaderExtractor tells extractor to take values from request header
HeaderExtractor ExtractorType = "header"
// QueryExtractor tells extractor to take values from request query parameters
QueryExtractor ExtractorType = "query"
// ParamExtractor tells extractor to take values from request route parameters
ParamExtractor ExtractorType = "param"
// CookieExtractor tells extractor to take values from request cookie
CookieExtractor ExtractorType = "cookie"
// FormExtractor tells extractor to take values from request form fields
FormExtractor ExtractorType = "form"
)
func createExtractors(lookups string) ([]valuesExtractor, error) {
sources := strings.Split(lookups, ",")
var extractors []valuesExtractor
for _, source := range sources {
parts := strings.Split(source, ":")
if len(parts) < 2 {
return nil, fmt.Errorf("extractor source for lookup could not be split into needed parts: %v", source)
}
switch ExtractorType(parts[0]) {
case QueryExtractor:
extractors = append(extractors, valuesFromQuery(parts[1]))
case ParamExtractor:
extractors = append(extractors, valuesFromParam(parts[1]))
case CookieExtractor:
extractors = append(extractors, valuesFromCookie(parts[1]))
case FormExtractor:
extractors = append(extractors, valuesFromForm(parts[1]))
case HeaderExtractor:
prefix := ""
if len(parts) > 2 {
prefix = parts[2]
}
extractors = append(extractors, valuesFromHeader(parts[1], prefix))
}
}
return extractors, nil
}
// valuesFromHeader returns a functions that extracts values from the request header.
func valuesFromHeader(header string, valuePrefix string) valuesExtractor {
prefixLen := len(valuePrefix)
return func(c echo.Context) ([]string, ExtractorType, error) {
values := textproto.MIMEHeader(c.Request().Header).Values(header)
if len(values) == 0 {
return nil, HeaderExtractor, ErrExtractionValueMissing
}
result := make([]string, 0)
for _, value := range values {
if prefixLen == 0 {
result = append(result, value)
continue
}
if len(value) > prefixLen && strings.EqualFold(value[:prefixLen], valuePrefix) {
result = append(result, value[prefixLen:])
}
}
if len(result) == 0 {
return nil, HeaderExtractor, ErrExtractionValueMissing
}
return result, HeaderExtractor, nil
}
}
// valuesFromQuery returns a function that extracts values from the query string.
func valuesFromQuery(param string) valuesExtractor {
return func(c echo.Context) ([]string, ExtractorType, error) {
result := c.QueryParams()[param]
if len(result) == 0 {
return nil, QueryExtractor, ErrExtractionValueMissing
}
return result, QueryExtractor, nil
}
}
// valuesFromParam returns a function that extracts values from the url param string.
func valuesFromParam(param string) valuesExtractor {
return func(c echo.Context) ([]string, ExtractorType, error) {
result := make([]string, 0)
for _, p := range c.PathParams() {
if param == p.Name {
result = append(result, p.Value)
}
}
if len(result) == 0 {
return nil, ParamExtractor, ErrExtractionValueMissing
}
return result, ParamExtractor, nil
}
}
// valuesFromCookie returns a function that extracts values from the named cookie.
func valuesFromCookie(name string) valuesExtractor {
return func(c echo.Context) ([]string, ExtractorType, error) {
cookies := c.Cookies()
if len(cookies) == 0 {
return nil, CookieExtractor, ErrExtractionValueMissing
}
result := make([]string, 0)
for _, cookie := range cookies {
if name == cookie.Name {
result = append(result, cookie.Value)
}
}
if len(result) == 0 {
return nil, CookieExtractor, ErrExtractionValueMissing
}
return result, CookieExtractor, nil
}
}
// valuesFromForm returns a function that extracts values from the form field.
func valuesFromForm(name string) valuesExtractor {
return func(c echo.Context) ([]string, ExtractorType, error) {
if err := c.Request().ParseForm(); err != nil {
return nil, FormExtractor, fmt.Errorf("valuesFromForm parse form failed: %w", err)
}
values := c.Request().Form[name]
if len(values) == 0 {
return nil, FormExtractor, ErrExtractionValueMissing
}
result := append([]string{}, values...)
return result, FormExtractor, nil
}
}