Skip to content

Commit

Permalink
Add NotMatchPattern to advanced url rewriting
Browse files Browse the repository at this point in the history
  • Loading branch information
furkansenharputlu committed Feb 15, 2019
1 parent 31252a2 commit 2a78bb1
Show file tree
Hide file tree
Showing 3 changed files with 316 additions and 18 deletions.
36 changes: 30 additions & 6 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ type CircuitBreakerMeta struct {

type StringRegexMap struct {
MatchPattern string `bson:"match_rx" json:"match_rx"`
Reverse bool `bson:"reverse" json:"reverse"`
matchRegex *regexp.Regexp
}

Expand Down Expand Up @@ -543,25 +544,48 @@ func (a *APIDefinition) DecodeFromDB() {
}
}

func (s *StringRegexMap) Check(value string) string {
func (s *StringRegexMap) Check(value string) (match string) {
if s.matchRegex == nil {
return
}

return s.matchRegex.FindString(value)
}

func (s *StringRegexMap) FindStringSubmatch(value string) []string {
return s.matchRegex.FindStringSubmatch(value)
func (s *StringRegexMap) FindStringSubmatch(value string) (matched bool, match []string) {
if s.matchRegex == nil {
return
}

match = s.matchRegex.FindStringSubmatch(value)
if !s.Reverse {
matched = len(match) > 0
} else {
matched = len(match) == 0
}

return
}

func (s *StringRegexMap) FindAllStringSubmatch(value string, n int) [][]string {
return s.matchRegex.FindAllStringSubmatch(value, n)
func (s *StringRegexMap) FindAllStringSubmatch(value string, n int) (matched bool, matches [][]string) {
matches = s.matchRegex.FindAllStringSubmatch(value, n)
if !s.Reverse {
matched = len(matches) > 0
} else {
matched = len(matches) == 0
}

return
}

func (s *StringRegexMap) Init() error {
var err error
if s.matchRegex, err = regexp.Compile(s.MatchPattern); err != nil {
log.WithError(err).WithField("MatchPattern", s.MatchPattern).
Error("Could not compile regexp for StringRegexMap")
Error("Could not compile matchRegex for StringRegexMap")
return err
}

return nil
}

Expand Down
31 changes: 19 additions & 12 deletions mw_url_rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ func checkHeaderTrigger(r *http.Request, options map[string]apidef.StringRegexMa
vals, ok := r.Header[mhCN]
if ok {
for i, v := range vals {
match := mr.FindStringSubmatch(v)
if len(match) > 0 {
matched, match := mr.FindStringSubmatch(v)
if matched {
addMatchToContextData(contextData, match, triggernum, mhCN, i)
fCount++
}
Expand Down Expand Up @@ -400,8 +400,8 @@ func checkQueryString(r *http.Request, options map[string]apidef.StringRegexMap,
vals, ok := qvals[mv]
if ok {
for i, v := range vals {
match := mr.FindStringSubmatch(v)
if len(match) > 0 {
matched, match := mr.FindStringSubmatch(v)
if matched {
addMatchToContextData(contextData, match, triggernum, mv, i)
fCount++
}
Expand All @@ -428,8 +428,8 @@ func checkPathParts(r *http.Request, options map[string]apidef.StringRegexMap, a
pathParts := strings.Split(r.URL.Path, "/")

for _, part := range pathParts {
match := mr.FindStringSubmatch(part)
if len(match) > 0 {
matched, match := mr.FindStringSubmatch(part)
if matched {
addMatchToContextData(contextData, match, triggernum, mv, fCount)
fCount++
}
Expand All @@ -456,8 +456,8 @@ func checkSessionTrigger(r *http.Request, sess *user.SessionState, options map[s
if ok {
val, valOk := rawVal.(string)
if valOk {
match := mr.FindStringSubmatch(val)
if len(match) > 0 {
matched, match := mr.FindStringSubmatch(val)
if matched {
addMatchToContextData(contextData, match, triggernum, mh)
fCount++
}
Expand Down Expand Up @@ -487,8 +487,8 @@ func checkContextTrigger(r *http.Request, options map[string]apidef.StringRegexM
if ok {
val, valOk := rawVal.(string)
if valOk {
match := mr.FindStringSubmatch(val)
if len(match) > 0 {
matched, match := mr.FindStringSubmatch(val)
if matched {
addMatchToContextData(contextData, match, triggernum, mh)
fCount++
}
Expand All @@ -512,10 +512,13 @@ func checkPayload(r *http.Request, options apidef.StringRegexMap, triggernum int
contextData := ctxGetData(r)
bodyBytes, _ := ioutil.ReadAll(r.Body)

matches := options.FindAllStringSubmatch(string(bodyBytes), -1)
matched, matches := options.FindAllStringSubmatch(string(bodyBytes), -1)

if len(matches) > 0 {
if matched {
kn := buildTriggerKey(triggernum, "payload")
if len(matches) == 0 {
return true
}
contextData[kn] = matches[0][0]

for i, match := range matches {
Expand All @@ -531,6 +534,10 @@ func checkPayload(r *http.Request, options apidef.StringRegexMap, triggernum int

func addMatchToContextData(cd map[string]interface{}, match []string, trNum int, trName string, indices ...int) {
kn := buildTriggerKey(trNum, trName, indices...)
if len(match) == 0 {
return
}

cd[kn] = match[0]

if len(match) > 1 {
Expand Down
Loading

0 comments on commit 2a78bb1

Please sign in to comment.