Skip to content

Commit

Permalink
Added routing via session metadaa
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed Sep 23, 2017
1 parent 33775c5 commit 2ec970a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
9 changes: 5 additions & 4 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ type StringRegexMap struct {
}

type RoutingTriggerOptions struct {
HeaderMatches map[string]StringRegexMap `bson:"header_matches" json:"header_matches"`
QueryValMatches map[string]StringRegexMap `bson:"query_val_matches" json:"query_val_matches"`
PathPartMatches map[string]StringRegexMap `bson:"path_part_matches" json:"path_part_matches"`
PayloadMatches StringRegexMap `bson:"payload_matches" json:"payload_matches"`
HeaderMatches map[string]StringRegexMap `bson:"header_matches" json:"header_matches"`
QueryValMatches map[string]StringRegexMap `bson:"query_val_matches" json:"query_val_matches"`
PathPartMatches map[string]StringRegexMap `bson:"path_part_matches" json:"path_part_matches"`
SessionMetaMatches map[string]StringRegexMap `bson:"session_meta_matches" json:"session_meta_matches"`
PayloadMatches StringRegexMap `bson:"payload_matches" json:"payload_matches"`
}

type RoutingTrigger struct {
Expand Down
44 changes: 44 additions & 0 deletions mw_url_rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ func urlRewrite(meta *apidef.URLRewriteMeta, r *http.Request) (string, error) {
}
}

// Check session meta

if session := ctxGetSession(r); session != nil {
if len(triggerOpts.Options.SessionMetaMatches) > 0 {
if checkSessionTrigger(r, session, triggerOpts.Options.SessionMetaMatches, checkAny, tn) {
setCount += 1
if checkAny {
rewriteToPath = triggerOpts.RewriteTo
break
}
}
}
}

// Check payload
if triggerOpts.Options.PayloadMatches.MatchPattern != "" {
if checkPayload(r, triggerOpts.Options.PayloadMatches, tn) {
Expand Down Expand Up @@ -363,6 +377,36 @@ func checkPathParts(r *http.Request, options map[string]apidef.StringRegexMap, a
return false
}

func checkSessionTrigger(r *http.Request, sess *SessionState, options map[string]apidef.StringRegexMap, any bool, triggernum int) bool {
contextData := ctxGetData(r)
fCount := 0
for mh, mr := range options {
rawVal, ok := sess.MetaData[mh]
if ok {
val, valOk := rawVal.(string)
if valOk {
b := mr.Check(val)
if len(b) > 0 {
kn := fmt.Sprintf("trigger-%d-%s", triggernum, mh)
contextData[kn] = b
fCount++
}
}
}
}

if fCount > 0 {
ctxSetData(r, contextData)
if any {
return true
}

return len(options) <= fCount
}

return false
}

func checkPayload(r *http.Request, options apidef.StringRegexMap, triggernum int) bool {
contextData := ctxGetData(r)
cp := copyRequest(r)
Expand Down
30 changes: 30 additions & 0 deletions mw_url_rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,29 @@ func TestRewriterTriggers(t *testing.T) {
r,
}
},
func() TestDef {
r, _ := http.NewRequest("GET", "/test/foo/rewrite", nil)
hOpt := apidef.StringRegexMap{MatchPattern: "bar"}
hOpt.Init()

return TestDef{
"Meta Simple",
"/test/foo/rewrite", "/change/to/me/ignore",
"/test/foo/rewrite", "/change/to/me/bar",
[]apidef.RoutingTrigger{
{
On: apidef.Any,
Options: apidef.RoutingTriggerOptions{
SessionMetaMatches: map[string]apidef.StringRegexMap{
"rewrite": hOpt,
},
},
RewriteTo: "/change/to/me/$tyk_context.trigger-0-rewrite",
},
},
r,
}
},
}
for _, tf := range tests {
tc := tf()
Expand All @@ -469,6 +492,13 @@ func TestRewriterTriggers(t *testing.T) {
RewriteTo: tc.to,
Triggers: tc.triggerConf,
}

ctxSetSession(tc.req, &SessionState{
MetaData: map[string]interface{}{
"rewrite": "bar",
},
})

got, err := urlRewrite(&testConf, tc.req)
if err != nil {
t.Error("compile failed:", err)
Expand Down

0 comments on commit 2ec970a

Please sign in to comment.