5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-13 05:40:26 +00:00
matterbridge/vendor/github.com/labstack/echo/v4/middleware/middleware.go

82 lines
1.9 KiB
Go
Raw Normal View History

2017-02-18 22:00:46 +00:00
package middleware
2018-02-20 23:48:10 +00:00
import (
2021-03-20 21:40:23 +00:00
"net/http"
"net/url"
2018-02-20 23:48:10 +00:00
"regexp"
"strconv"
"strings"
"github.com/labstack/echo/v4"
2018-02-20 23:48:10 +00:00
)
2017-02-18 22:00:46 +00:00
type (
// Skipper defines a function to skip middleware. Returning true skips processing
// the middleware.
Skipper func(echo.Context) bool
// BeforeFunc defines a function which is executed just before the middleware.
BeforeFunc func(echo.Context)
2017-02-18 22:00:46 +00:00
)
2018-02-20 23:48:10 +00:00
func captureTokens(pattern *regexp.Regexp, input string) *strings.Replacer {
groups := pattern.FindAllStringSubmatch(input, -1)
if groups == nil {
return nil
}
values := groups[0][1:]
replace := make([]string, 2*len(values))
for i, v := range values {
j := 2 * i
replace[j] = "$" + strconv.Itoa(i+1)
replace[j+1] = v
}
return strings.NewReplacer(replace...)
}
2021-03-20 21:40:23 +00:00
func rewriteRulesRegex(rewrite map[string]string) map[*regexp.Regexp]string {
// Initialize
rulesRegex := map[*regexp.Regexp]string{}
for k, v := range rewrite {
k = regexp.QuoteMeta(k)
k = strings.Replace(k, `\*`, "(.*?)", -1)
if strings.HasPrefix(k, `\^`) {
k = strings.Replace(k, `\^`, "^", -1)
}
k = k + "$"
rulesRegex[regexp.MustCompile(k)] = v
}
return rulesRegex
}
func rewritePath(rewriteRegex map[*regexp.Regexp]string, req *http.Request) {
for k, v := range rewriteRegex {
rawPath := req.URL.RawPath
if rawPath != "" {
// RawPath is only set when there has been escaping done. In that case Path must be deduced from rewritten RawPath
// because encoded Path could match rules that RawPath did not
if replacer := captureTokens(k, rawPath); replacer != nil {
rawPath = replacer.Replace(v)
req.URL.RawPath = rawPath
req.URL.Path, _ = url.PathUnescape(rawPath)
return // rewrite only once
}
continue
}
if replacer := captureTokens(k, req.URL.Path); replacer != nil {
req.URL.Path = replacer.Replace(v)
return // rewrite only once
}
}
}
2017-02-18 22:00:46 +00:00
// DefaultSkipper returns false which processes the middleware.
2017-06-05 22:01:05 +00:00
func DefaultSkipper(echo.Context) bool {
2017-02-18 22:00:46 +00:00
return false
}