Skip to content

Commit

Permalink
all: use helper funcs for ctx's tracking info
Browse files Browse the repository at this point in the history
Also use a bool for DoNotTrack, since the value stored in it is never
used.

And simplify the DoNotTrack logic, since it only sets the vars to their
original values.

For TykTechnologies#683.
  • Loading branch information
mvdan authored and buger committed May 15, 2017
1 parent 8de366f commit da6b55c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
22 changes: 22 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1863,3 +1863,25 @@ func ctxSetAuthToken(r *http.Request, t string) {
}
context.Set(r, AuthHeaderValue, t)
}

func ctxGetTrackedPath(r *http.Request) string {
if v := context.Get(r, TrackThisEndpoint); v != nil {
return v.(string)
}
return ""
}

func ctxSetTrackedPath(r *http.Request, p string) {
if p == "" {
panic("setting a nil context TrackThisEndpoint")
}
context.Set(r, TrackThisEndpoint, p)
}

func ctxGetDoNotTrack(r *http.Request) bool {
return context.Get(r, DoNotTrackThisEndpoint) == true
}

func ctxSetDoNotTrack(r *http.Request, b bool) {
context.Set(r, DoNotTrackThisEndpoint, b)
}
12 changes: 3 additions & 9 deletions handler_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,11 @@ func (e *ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, errMs
rawRequest = base64.StdEncoding.EncodeToString(wireFormatReq.Bytes())
}

trackThisEndpoint := context.Get(r, TrackThisEndpoint)
trackedPath := r.URL.Path
trackEP := false
if trackThisEndpoint != nil {
trackedPath := r.URL.Path
if p := ctxGetTrackedPath(r); p != "" && !ctxGetDoNotTrack(r) {
trackEP = true
trackedPath = trackThisEndpoint.(string)
}

if context.Get(r, DoNotTrackThisEndpoint) != nil {
trackEP = false
trackedPath = r.URL.Path
trackedPath = p
}

record := AnalyticsRecord{
Expand Down
12 changes: 3 additions & 9 deletions handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,11 @@ func (s *SuccessHandler) RecordHit(r *http.Request, timing int64, code int, requ
rawResponse = base64.StdEncoding.EncodeToString(wireFormatRes.Bytes())
}

trackThisEndpoint := context.Get(r, TrackThisEndpoint)
trackedPath := r.URL.Path
trackEP := false
if trackThisEndpoint != nil {
trackedPath := r.URL.Path
if p := ctxGetTrackedPath(r); p != "" && !ctxGetDoNotTrack(r) {
trackEP = true
trackedPath = trackThisEndpoint.(string)
}

if context.Get(r, DoNotTrackThisEndpoint) != nil {
trackEP = false
trackedPath = r.URL.Path
trackedPath = p
}

record := AnalyticsRecord{
Expand Down
8 changes: 3 additions & 5 deletions middleware_track_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"net/http"

"github.com/gorilla/context"

"github.com/TykTechnologies/tyk/apidef"
)

Expand Down Expand Up @@ -32,12 +30,12 @@ func (a *TrackEndpointMiddleware) ProcessRequest(w http.ResponseWriter, r *http.
_, versionPaths, _, _ := a.TykMiddleware.Spec.GetVersionData(r)
foundTracked, metaTrack := a.TykMiddleware.Spec.CheckSpecMatchesStatus(r.URL.Path, r.Method, versionPaths, RequestTracked)
if foundTracked {
context.Set(r, TrackThisEndpoint, metaTrack.(*apidef.TrackEndpointMeta).Path)
ctxSetTrackedPath(r, metaTrack.(*apidef.TrackEndpointMeta).Path)
}

foundDnTrack, meta_dnTrack := a.TykMiddleware.Spec.CheckSpecMatchesStatus(r.URL.Path, r.Method, versionPaths, RequestNotTracked)
foundDnTrack, _ := a.TykMiddleware.Spec.CheckSpecMatchesStatus(r.URL.Path, r.Method, versionPaths, RequestNotTracked)
if foundDnTrack {
context.Set(r, DoNotTrackThisEndpoint, meta_dnTrack.(*apidef.TrackEndpointMeta).Path)
ctxSetDoNotTrack(r, true)
}

return nil, 200
Expand Down

0 comments on commit da6b55c

Please sign in to comment.