traefik/pkg/muxer/http/mux.go

167 lines
3.3 KiB
Go
Raw Normal View History

2022-03-17 17:02:08 +00:00
package http
import (
"fmt"
"net/http"
2019-08-03 01:58:23 +00:00
"github.com/gorilla/mux"
2022-03-17 17:02:08 +00:00
"github.com/traefik/traefik/v2/pkg/rules"
"github.com/vulcand/predicate"
)
2022-03-17 17:02:08 +00:00
// Muxer handles routing with rules.
type Muxer struct {
*mux.Router
parser predicate.Parser
}
2022-03-17 17:02:08 +00:00
// NewMuxer returns a new muxer instance.
func NewMuxer() (*Muxer, error) {
var matchers []string
for matcher := range httpFuncs {
matchers = append(matchers, matcher)
}
parser, err := rules.NewParser(matchers)
if err != nil {
return nil, err
}
2022-03-17 17:02:08 +00:00
return &Muxer{
Router: mux.NewRouter().SkipClean(true),
parser: parser,
}, nil
}
// AddRoute add a new route to the router.
2022-03-17 17:02:08 +00:00
func (r *Muxer) AddRoute(rule string, priority int, handler http.Handler) error {
parse, err := r.parser.Parse(rule)
if err != nil {
2020-05-11 10:06:07 +00:00
return fmt.Errorf("error while parsing rule %s: %w", rule, err)
}
2022-03-17 17:02:08 +00:00
buildTree, ok := parse.(rules.TreeBuilder)
if !ok {
return fmt.Errorf("error while parsing rule %s", rule)
}
if priority == 0 {
priority = len(rule)
2017-12-19 16:00:12 +00:00
}
route := r.NewRoute().Handler(handler).Priority(priority)
err = addRuleOnRoute(route, buildTree())
if err != nil {
route.BuildOnly()
return err
}
return nil
2017-12-19 16:00:12 +00:00
}
2022-03-17 17:02:08 +00:00
func addRuleOnRouter(router *mux.Router, rule *rules.Tree) error {
switch rule.Matcher {
case "and":
route := router.NewRoute()
2022-03-17 17:02:08 +00:00
err := addRuleOnRoute(route, rule.RuleLeft)
if err != nil {
return err
2016-06-06 07:21:00 +00:00
}
2018-03-23 12:30:03 +00:00
2022-03-17 17:02:08 +00:00
return addRuleOnRoute(route, rule.RuleRight)
case "or":
2022-03-17 17:02:08 +00:00
err := addRuleOnRouter(router, rule.RuleLeft)
if err != nil {
return err
}
2018-03-23 12:30:03 +00:00
2022-03-17 17:02:08 +00:00
return addRuleOnRouter(router, rule.RuleRight)
default:
2022-03-17 17:02:08 +00:00
err := rules.CheckRule(rule)
if err != nil {
return err
2016-06-06 07:21:00 +00:00
}
2022-03-17 17:02:08 +00:00
if rule.Not {
return not(httpFuncs[rule.Matcher])(router.NewRoute(), rule.Value...)
2021-05-31 16:58:05 +00:00
}
2022-03-17 17:02:08 +00:00
return httpFuncs[rule.Matcher](router.NewRoute(), rule.Value...)
}
}
2022-03-17 17:02:08 +00:00
func addRuleOnRoute(route *mux.Route, rule *rules.Tree) error {
switch rule.Matcher {
case "and":
2022-03-17 17:02:08 +00:00
err := addRuleOnRoute(route, rule.RuleLeft)
if err != nil {
return err
}
2022-03-17 17:02:08 +00:00
return addRuleOnRoute(route, rule.RuleRight)
case "or":
subRouter := route.Subrouter()
2018-03-23 12:30:03 +00:00
2022-03-17 17:02:08 +00:00
err := addRuleOnRouter(subRouter, rule.RuleLeft)
if err != nil {
return err
}
2022-03-17 17:02:08 +00:00
return addRuleOnRouter(subRouter, rule.RuleRight)
default:
2022-03-17 17:02:08 +00:00
err := rules.CheckRule(rule)
if err != nil {
return err
}
2018-03-23 12:30:03 +00:00
2022-03-17 17:02:08 +00:00
if rule.Not {
return not(httpFuncs[rule.Matcher])(route, rule.Value...)
2021-05-31 16:58:05 +00:00
}
2022-03-17 17:02:08 +00:00
return httpFuncs[rule.Matcher](route, rule.Value...)
}
}
func not(m func(*mux.Route, ...string) error) func(*mux.Route, ...string) error {
return func(r *mux.Route, v ...string) error {
router := mux.NewRouter()
err := m(router.NewRoute(), v...)
if err != nil {
return err
}
r.MatcherFunc(func(req *http.Request, ma *mux.RouteMatch) bool {
return !router.Match(req, ma)
})
return nil
}
}
// ParseDomains extract domains from rule.
func ParseDomains(rule string) ([]string, error) {
var matchers []string
for matcher := range httpFuncs {
matchers = append(matchers, matcher)
}
parser, err := rules.NewParser(matchers)
if err != nil {
return nil, err
}
parse, err := parser.Parse(rule)
if err != nil {
return nil, err
}
buildTree, ok := parse.(rules.TreeBuilder)
if !ok {
return nil, fmt.Errorf("error while parsing rule %s", rule)
}
return buildTree().ParseMatchers([]string{"Host"}), nil
}