From 37801ce3c103f59b7e27afbce56e85ff43531861 Mon Sep 17 00:00:00 2001 From: Ahmet Soormally Date: Thu, 8 Feb 2018 14:32:47 +0000 Subject: [PATCH 01/20] Fixing panic by only calling initialiseSystem once fixes #1463 --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index da79ce958c1..80cc4223b32 100644 --- a/main.go +++ b/main.go @@ -693,8 +693,10 @@ var reloadDoneChan = make(chan struct{}, 1) func reloadLoop(tick <-chan time.Time) { <-tick for range startReloadChan { - log.Info("Initiating reload") + log.Info("reload: initiating") doReload() + log.Info("reload: complete") + log.Info("Initiating coprocess reload") doCoprocessReload() @@ -1087,8 +1089,6 @@ func main() { } } - initialiseSystem() - start() if goAgainErr != nil { From 99c1585a595d04c5b611ef333aadb78c06673d86 Mon Sep 17 00:00:00 2001 From: Ahmet Soormally Date: Fri, 9 Feb 2018 10:45:26 +0000 Subject: [PATCH 02/20] variable collision with builtin function real --- reverse_proxy.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reverse_proxy.go b/reverse_proxy.go index 3d98d6242f8..e6c0a687523 100644 --- a/reverse_proxy.go +++ b/reverse_proxy.go @@ -783,8 +783,8 @@ func (m *maxLatencyWriter) flushLoop() { func (m *maxLatencyWriter) stop() { m.done <- true } func requestIP(r *http.Request) string { - if real := r.Header.Get("X-Real-IP"); real != "" { - return real + if realIP := r.Header.Get("X-Real-IP"); realIP != "" { + return realIP } if fw := r.Header.Get("X-Forwarded-For"); fw != "" { // X-Forwarded-For has no port From 5ee40dbf13ad4959d165ae3bf99fa7a72f806322 Mon Sep 17 00:00:00 2001 From: Ahmet Soormally Date: Fri, 9 Feb 2018 10:50:34 +0000 Subject: [PATCH 03/20] lint: removing redundant alias --- reverse_proxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reverse_proxy.go b/reverse_proxy.go index e6c0a687523..ea56214a931 100644 --- a/reverse_proxy.go +++ b/reverse_proxy.go @@ -28,7 +28,7 @@ import ( "time" "github.com/Sirupsen/logrus" - cache "github.com/pmylund/go-cache" + "github.com/pmylund/go-cache" "github.com/TykTechnologies/tyk/apidef" "github.com/TykTechnologies/tyk/config" From 11b44c2640a0e88afcc1bcd9592e708f4161f277 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Fri, 9 Feb 2018 13:44:32 +0200 Subject: [PATCH 04/20] Fix org monitor panic Request object was passed to separate goroutine and was causing panics because of concurrent request header accesss --- mw_organisation_activity.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mw_organisation_activity.go b/mw_organisation_activity.go index 5d0e171d96b..bd113fa73b0 100644 --- a/mw_organisation_activity.go +++ b/mw_organisation_activity.go @@ -117,7 +117,8 @@ func (k *OrganizationMonitor) ProcessRequestOffThread(r *http.Request) (error, i go k.SetOrgSentinel(orgChan, k.Spec.OrgID) } - go k.AllowAccessNext(orgChan, r) + requestCopy := copyRequest(r) + go k.AllowAccessNext(orgChan, requestCopy) orgActiveMap.RLock() active, found := orgActiveMap.OrgMap[k.Spec.OrgID] From 90e0986c5f9536d8b067279324e46c059cd5d817 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Fri, 9 Feb 2018 13:51:42 +0200 Subject: [PATCH 05/20] Extend mutex to orgChanMap --- mw_organisation_activity.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mw_organisation_activity.go b/mw_organisation_activity.go index bd113fa73b0..cb2ba462ed9 100644 --- a/mw_organisation_activity.go +++ b/mw_organisation_activity.go @@ -110,19 +110,19 @@ func (k *OrganizationMonitor) SetOrgSentinel(orgChan chan bool, orgId string) { func (k *OrganizationMonitor) ProcessRequestOffThread(r *http.Request) (error, int) { + orgActiveMap.Lock() orgChan, ok := orgChanMap[k.Spec.OrgID] if !ok { orgChanMap[k.Spec.OrgID] = make(chan bool) orgChan = orgChanMap[k.Spec.OrgID] go k.SetOrgSentinel(orgChan, k.Spec.OrgID) } + active, found := orgActiveMap.OrgMap[k.Spec.OrgID] + orgActiveMap.Unlock() requestCopy := copyRequest(r) go k.AllowAccessNext(orgChan, requestCopy) - orgActiveMap.RLock() - active, found := orgActiveMap.OrgMap[k.Spec.OrgID] - orgActiveMap.RUnlock() if found && !active { log.Debug("Is not active") return errors.New("This organisation access has been disabled or quota is exceeded, please contact your API administrator"), 403 From 06d448fe0b4c254eb90a868344215581bab24dbe Mon Sep 17 00:00:00 2001 From: Martin Buhr Date: Sat, 10 Feb 2018 14:13:07 +1300 Subject: [PATCH 06/20] Changed to a sync.Map instead of a mutex --- mw_organisation_activity.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/mw_organisation_activity.go b/mw_organisation_activity.go index cb2ba462ed9..58abbdfcc75 100644 --- a/mw_organisation_activity.go +++ b/mw_organisation_activity.go @@ -11,14 +11,7 @@ import ( var orgChanMap = make(map[string]chan bool) -type orgActiveMapMu struct { - sync.RWMutex - OrgMap map[string]bool -} - -var orgActiveMap = orgActiveMapMu{ - OrgMap: map[string]bool{}, -} +var orgActiveMap sync.Map // RateLimitAndQuotaCheck will check the incomming request and key whether it is within it's quota and // within it's rate limit, it makes use of the SessionLimiter object to do this @@ -102,28 +95,28 @@ func (k *OrganizationMonitor) ProcessRequestLive(r *http.Request) (error, int) { func (k *OrganizationMonitor) SetOrgSentinel(orgChan chan bool, orgId string) { for isActive := range orgChan { log.Debug("Chan got:", isActive) - orgActiveMap.Lock() - orgActiveMap.OrgMap[orgId] = isActive - orgActiveMap.Unlock() + o, ok := orgActiveMap.Load(orgId) + if ok { + o = isActive + orgActiveMap.Store(orgId, o) + } } } func (k *OrganizationMonitor) ProcessRequestOffThread(r *http.Request) (error, int) { - orgActiveMap.Lock() orgChan, ok := orgChanMap[k.Spec.OrgID] if !ok { orgChanMap[k.Spec.OrgID] = make(chan bool) orgChan = orgChanMap[k.Spec.OrgID] go k.SetOrgSentinel(orgChan, k.Spec.OrgID) } - active, found := orgActiveMap.OrgMap[k.Spec.OrgID] - orgActiveMap.Unlock() + active, found := orgActiveMap.Load(k.Spec.OrgID) requestCopy := copyRequest(r) go k.AllowAccessNext(orgChan, requestCopy) - if found && !active { + if found && !active.(bool) { log.Debug("Is not active") return errors.New("This organisation access has been disabled or quota is exceeded, please contact your API administrator"), 403 } From 9a841f62702e707e63cf8f5f56d75417ce594bbc Mon Sep 17 00:00:00 2001 From: Martin Buhr Date: Sat, 10 Feb 2018 14:55:03 +1300 Subject: [PATCH 07/20] Reduced read dependency on request object, removed copy operation --- log_helpers.go | 20 ++++++++++++++++++++ mw_organisation_activity.go | 16 ++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/log_helpers.go b/log_helpers.go index 2a4bf7a0b44..55cf4331609 100644 --- a/log_helpers.go +++ b/log_helpers.go @@ -30,3 +30,23 @@ func getLogEntryForRequest(r *http.Request, key string, data map[string]interfac } return log.WithFields(fields) } + +func getExplicitLogEntryForRequest(path string, IP string, key string, data map[string]interface{}) *logrus.Entry { + // populate http request fields + fields := logrus.Fields{ + "path": path, + "origin": IP, + } + // add key to log if configured to do so + if key != "" { + fields["key"] = key + if !config.Global.EnableKeyLogging { + fields["key"] = logHiddenValue + } + } + // add to log additional fields if any passed + for key, val := range data { + fields[key] = val + } + return log.WithFields(fields) +} diff --git a/mw_organisation_activity.go b/mw_organisation_activity.go index 58abbdfcc75..7c8601c53dd 100644 --- a/mw_organisation_activity.go +++ b/mw_organisation_activity.go @@ -113,8 +113,7 @@ func (k *OrganizationMonitor) ProcessRequestOffThread(r *http.Request) (error, i } active, found := orgActiveMap.Load(k.Spec.OrgID) - requestCopy := copyRequest(r) - go k.AllowAccessNext(orgChan, requestCopy) + go k.AllowAccessNext(orgChan, r.URL.Path, requestIP(r), r) if found && !active.(bool) { log.Debug("Is not active") @@ -127,7 +126,7 @@ func (k *OrganizationMonitor) ProcessRequestOffThread(r *http.Request) (error, i return nil, 200 } -func (k *OrganizationMonitor) AllowAccessNext(orgChan chan bool, r *http.Request) { +func (k *OrganizationMonitor) AllowAccessNext(orgChan chan bool, path string, IP string, r *http.Request) { session, found := k.OrgSession(k.Spec.OrgID) @@ -138,7 +137,7 @@ func (k *OrganizationMonitor) AllowAccessNext(orgChan chan bool, r *http.Request } // Is it active? - logEntry := getLogEntryForRequest(r, k.Spec.OrgID, nil) + logEntry := getExplicitLogEntryForRequest(path, IP, k.Spec.OrgID, nil) if session.IsInactive { logEntry.Warning("Organisation access is disabled.") @@ -157,10 +156,11 @@ func (k *OrganizationMonitor) AllowAccessNext(orgChan chan bool, r *http.Request // Fire a quota exceeded event k.FireEvent(EventOrgQuotaExceeded, EventKeyFailureMeta{ - EventMetaDefault: EventMetaDefault{Message: "Organisation quota has been exceeded", OriginatingRequest: EncodeRequestToEvent(r)}, - Path: r.URL.Path, - Origin: requestIP(r), - Key: k.Spec.OrgID, + EventMetaDefault: EventMetaDefault{ + Message: "Organisation quota has been exceeded"}, + Path: path, + Origin: IP, + Key: k.Spec.OrgID, }) //return errors.New("This organisation quota has been exceeded, please contact your API administrator"), 403 From e0a0edb6c90816afb518fd0ce2954face0d7367b Mon Sep 17 00:00:00 2001 From: dencoded <33698537+dencoded@users.noreply.github.com> Date: Tue, 13 Feb 2018 22:10:41 -0500 Subject: [PATCH 08/20] createKeyHandler and apiHandler (POST/PUT) changed --- api.go | 25 +++++++++++++++------- api_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/api.go b/api.go index 098894b606a..de0cfd64456 100644 --- a/api.go +++ b/api.go @@ -107,6 +107,14 @@ func checkAndApplyTrialPeriod(keyName, apiId string, newSession *user.SessionSta } } +func applyPoliciesAndSave(keyName string, session *user.SessionState, spec *APISpec) error { + // use basic middleware to apply policies to key/session (it also saves it) + mw := BaseMiddleware{ + Spec: spec, + } + return mw.ApplyPolicies(keyName, session) +} + func doAddOrUpdate(keyName string, newSession *user.SessionState, dontReset bool) error { newSession.LastUpdated = strconv.Itoa(int(time.Now().Unix())) @@ -137,8 +145,8 @@ func doAddOrUpdate(keyName string, newSession *user.SessionState, dontReset bool newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate } - err := apiSpec.SessionManager.UpdateSession(keyName, newSession, newSession.Lifetime(apiSpec.SessionLifetime)) - if err != nil { + // apply polices (if any) and save key + if err := applyPoliciesAndSave(keyName, newSession, apiSpec); err != nil { return err } } @@ -158,8 +166,9 @@ func doAddOrUpdate(keyName string, newSession *user.SessionState, dontReset bool newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate } checkAndApplyTrialPeriod(keyName, spec.APIID, newSession) - err := spec.SessionManager.UpdateSession(keyName, newSession, newSession.Lifetime(spec.SessionLifetime)) - if err != nil { + + // apply polices (if any) and save key + if err := applyPoliciesAndSave(keyName, newSession, spec); err != nil { return err } } @@ -901,8 +910,8 @@ func createKeyHandler(w http.ResponseWriter, r *http.Request) { apiSpec.SessionManager.ResetQuota(newKey, newSession) newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate } - err := apiSpec.SessionManager.UpdateSession(newKey, newSession, newSession.Lifetime(apiSpec.SessionLifetime)) - if err != nil { + // apply polices (if any) and save key + if err := applyPoliciesAndSave(newKey, newSession, apiSpec); err != nil { doJSONWrite(w, 500, apiError("Failed to create key - "+err.Error())) return } @@ -941,8 +950,8 @@ func createKeyHandler(w http.ResponseWriter, r *http.Request) { spec.SessionManager.ResetQuota(newKey, newSession) newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate } - err := spec.SessionManager.UpdateSession(newKey, newSession, newSession.Lifetime(spec.SessionLifetime)) - if err != nil { + // apply polices (if any) and save key + if err := applyPoliciesAndSave(newKey, newSession, spec); err != nil { doJSONWrite(w, 500, apiError("Failed to create key - "+err.Error())) return } diff --git a/api_test.go b/api_test.go index a135e441f6a..89423edfb4c 100644 --- a/api_test.go +++ b/api_test.go @@ -135,12 +135,39 @@ func TestKeyHandler(t *testing.T) { masterKey := createStandardSession() masterKeyJSON, _ := json.Marshal(masterKey) + // with access withAccess := createStandardSession() withAccess.AccessRights = map[string]user.AccessDefinition{"test": { APIID: "test", Versions: []string{"v1"}, }} withAccessJSON, _ := json.Marshal(withAccess) + // with policy + policiesMu.Lock() + policiesByID["abc_policy"] = user.Policy{ + Active: true, + QuotaMax: 1234567890, + } + policiesMu.Unlock() + withPolicy := createStandardSession() + withPolicy.AccessRights = map[string]user.AccessDefinition{"test": { + APIID: "test", Versions: []string{"v1"}, + }} + withPolicy.ApplyPolicies = []string{ + "abc_policy", + } + withPolicyJSON, _ := json.Marshal(withPolicy) + + // with invalid policy + withBadPolicy := createStandardSession() + withBadPolicy.AccessRights = map[string]user.AccessDefinition{"test": { + APIID: "test", Versions: []string{"v1"}, + }} + withBadPolicy.ApplyPolicies = []string{ + "xyz_policy", + } + withBadPolicyJSON, _ := json.Marshal(withBadPolicy) + knownKey := createSession() t.Run("Create key", func(t *testing.T) { @@ -151,6 +178,39 @@ func TestKeyHandler(t *testing.T) { }...) }) + t.Run("Create key with policy", func(t *testing.T) { + ts.Run(t, []test.TestCase{ + { + Method: "POST", + Path: "/tyk/keys/create", + Data: string(withPolicyJSON), + AdminAuth: true, + Code: 200, + }, + { + Method: "POST", + Path: "/tyk/keys/create", + Data: string(withBadPolicyJSON), + AdminAuth: true, + Code: 500, + }, + { + Method: "POST", + Path: "/tyk/keys/my_key_id", + Data: string(withPolicyJSON), + AdminAuth: true, + Code: 200, + }, + { + Method: "GET", + Path: "/tyk/keys/my_key_id" + "?api_id=test", + AdminAuth: true, + Code: 200, + BodyMatch: `"quota_max":1234567890`, + }, + }...) + }) + t.Run("Get key", func(t *testing.T) { ts.Run(t, []test.TestCase{ {Method: "GET", Path: "/tyk/keys/unknown", AdminAuth: true, Code: 404}, From d94e7df58d41a00668d9db68b2c06c13ba51c050 Mon Sep 17 00:00:00 2001 From: Martin Buhr Date: Thu, 28 Sep 2017 16:31:45 -0700 Subject: [PATCH 09/20] Adds JSON Schema validation to the gateway Fixes #1163 Based on https://github.com/TykTechnologies/tyk/pull/1180 Adds a new JSON Validation middleware that can be configured as follows: ``` "version_data": { "not_versioned": true, "versions": { "default": { "name": "default", "use_extended_paths": true, "extended_paths": { "validate_json": [{ "method": "POST", "path": "me", "validate_with": { "title": "Person", "type": "object", "properties": { "firstName": { "type": "string" }, "lastName": { "type": "string" }, "age": { "description": "Age in years", "type": "integer", "minimum": 0 } }, "required": ["firstName", "lastName"] } }] } } } }, ``` The schema must be a draft v4 JSON Schema spec. The gateway will attempt to validate the inbound request against it, if fields are failing the validation process, a detailed error response is provided for the user to fix their payload. This will require a new Dashboard UI to handle input. make Base64 Schema readable in tests removing base64 as not necessary using make for known length slice --- api_definition.go | 26 +++ api_loader.go | 2 + apidef/api_definitions.go | 9 + mw_validate_json.go | 85 +++++++ mw_validate_json_test.go | 208 ++++++++++++++++++ .../github.com/xeipuuv/gojsonschema/schema.go | 5 - vendor/vendor.json | 6 +- 7 files changed, 333 insertions(+), 8 deletions(-) create mode 100644 mw_validate_json.go create mode 100644 mw_validate_json_test.go diff --git a/api_definition.go b/api_definition.go index 6aaaadea6b7..39a10ab66a4 100644 --- a/api_definition.go +++ b/api_definition.go @@ -52,6 +52,7 @@ const ( MethodTransformed RequestTracked RequestNotTracked + ValidateJSONRequest ) // RequestStatus is a custom type to avoid collisions @@ -80,6 +81,7 @@ const ( StatusRequestSizeControlled RequestStatus = "Request Size Limited" StatusRequesTracked RequestStatus = "Request Tracked" StatusRequestNotTracked RequestStatus = "Request Not Tracked" + StatusValidateJSON RequestStatus = "Validate JSON" ) // URLSpec represents a flattened specification for URLs, used to check if a proxy URL @@ -101,6 +103,7 @@ type URLSpec struct { MethodTransform apidef.MethodTransformMeta TrackEndpoint apidef.TrackEndpointMeta DoNotTrackEndpoint apidef.TrackEndpointMeta + ValidatePathMeta apidef.ValidatePathMeta } type TransformSpec struct { @@ -701,6 +704,20 @@ func (a APIDefinitionLoader) compileTrackedEndpointPathspathSpec(paths []apidef. return urlSpec } +func (a APIDefinitionLoader) compileValidateJSONPathspathSpec(paths []apidef.ValidatePathMeta, stat URLStatus) []URLSpec { + urlSpec := make([]URLSpec, len(paths)) + + for i, stringSpec := range paths { + newSpec := URLSpec{} + a.generateRegex(stringSpec.Path, &newSpec, stat) + // Extend with method actions + newSpec.ValidatePathMeta = stringSpec + urlSpec[i] = newSpec + } + + return urlSpec +} + func (a APIDefinitionLoader) compileUnTrackedEndpointPathspathSpec(paths []apidef.TrackEndpointMeta, stat URLStatus) []URLSpec { urlSpec := []URLSpec{} @@ -734,6 +751,7 @@ func (a APIDefinitionLoader) getExtendedPathSpecs(apiVersionDef apidef.VersionIn methodTransforms := a.compileMethodTransformSpec(apiVersionDef.ExtendedPaths.MethodTransforms, MethodTransformed) trackedPaths := a.compileTrackedEndpointPathspathSpec(apiVersionDef.ExtendedPaths.TrackEndpoints, RequestTracked) unTrackedPaths := a.compileUnTrackedEndpointPathspathSpec(apiVersionDef.ExtendedPaths.DoNotTrackEndpoints, RequestNotTracked) + validateJSON := a.compileValidateJSONPathspathSpec(apiVersionDef.ExtendedPaths.ValidateJSON, ValidateJSONRequest) combinedPath := []URLSpec{} combinedPath = append(combinedPath, ignoredPaths...) @@ -752,6 +770,7 @@ func (a APIDefinitionLoader) getExtendedPathSpecs(apiVersionDef apidef.VersionIn combinedPath = append(combinedPath, methodTransforms...) combinedPath = append(combinedPath, trackedPaths...) combinedPath = append(combinedPath, unTrackedPaths...) + combinedPath = append(combinedPath, validateJSON...) return combinedPath, len(whiteListPaths) > 0 } @@ -797,6 +816,9 @@ func (a *APISpec) getURLStatus(stat URLStatus) RequestStatus { return StatusRequesTracked case RequestNotTracked: return StatusRequestNotTracked + case ValidateJSONRequest: + return StatusValidateJSON + default: log.Error("URL Status was not one of Ignored, Blacklist or WhiteList! Blocking.") return EndPointNotAllowed @@ -930,6 +952,10 @@ func (a *APISpec) CheckSpecMatchesStatus(r *http.Request, rxPaths []URLSpec, mod if r.Method == v.DoNotTrackEndpoint.Method { return true, &v.DoNotTrackEndpoint } + case ValidateJSONRequest: + if r.Method == v.ValidatePathMeta.Method { + return true, &v.ValidatePathMeta + } } } return false, nil diff --git a/api_loader.go b/api_loader.go index 081fd7bc87c..f1aba6564d1 100644 --- a/api_loader.go +++ b/api_loader.go @@ -304,6 +304,7 @@ func processSpec(spec *APISpec, apisByListen map[string]int, mwAppendEnabled(&chainArray, &CertificateCheckMW{BaseMiddleware: baseMid}) mwAppendEnabled(&chainArray, &OrganizationMonitor{BaseMiddleware: baseMid}) mwAppendEnabled(&chainArray, &RateLimitForAPI{BaseMiddleware: baseMid}) + mwAppendEnabled(&chainArray, &ValidateJSON{BaseMiddleware: baseMid}) mwAppendEnabled(&chainArray, &MiddlewareContextVars{BaseMiddleware: baseMid}) mwAppendEnabled(&chainArray, &VersionCheck{BaseMiddleware: baseMid}) mwAppendEnabled(&chainArray, &RequestSizeLimitMiddleware{baseMid}) @@ -441,6 +442,7 @@ func processSpec(spec *APISpec, apisByListen map[string]int, mwAppendEnabled(&chainArray, &RateLimitForAPI{BaseMiddleware: baseMid}) mwAppendEnabled(&chainArray, &RateLimitAndQuotaCheck{baseMid}) mwAppendEnabled(&chainArray, &GranularAccessMiddleware{baseMid}) + mwAppendEnabled(&chainArray, &ValidateJSON{BaseMiddleware: baseMid}) mwAppendEnabled(&chainArray, &TransformMiddleware{baseMid}) mwAppendEnabled(&chainArray, &TransformHeaders{BaseMiddleware: baseMid}) mwAppendEnabled(&chainArray, &URLRewriteMiddleware{BaseMiddleware: baseMid}) diff --git a/apidef/api_definitions.go b/apidef/api_definitions.go index e10f57f3af8..99e3ab71497 100644 --- a/apidef/api_definitions.go +++ b/apidef/api_definitions.go @@ -165,6 +165,14 @@ type MethodTransformMeta struct { ToMethod string `bson:"to_method" json:"to_method"` } +type ValidatePathMeta struct { + Path string `bson:"path" json:"path"` + Method string `bson:"method" json:"method"` + ValidateWith map[string]interface{} `json:"validate_with"` + // Allows override of default 422 Unprocessible Entity response code for validation errors. + ValidationErrorResponseCode int `bson:"validation_error_response_code" json:"validation_error_response_code"` +} + type ExtendedPathsSet struct { Ignored []EndPointMeta `bson:"ignored" json:"ignored,omitempty"` WhiteList []EndPointMeta `bson:"white_list" json:"white_list,omitempty"` @@ -182,6 +190,7 @@ type ExtendedPathsSet struct { MethodTransforms []MethodTransformMeta `bson:"method_transforms" json:"method_transforms,omitempty"` TrackEndpoints []TrackEndpointMeta `bson:"track_endpoints" json:"track_endpoints,omitempty"` DoNotTrackEndpoints []TrackEndpointMeta `bson:"do_not_track_endpoints" json:"do_not_track_endpoints,omitempty"` + ValidateJSON []ValidatePathMeta `bson:"validate_json" json:"validate_json,omitempty"` } type VersionInfo struct { diff --git a/mw_validate_json.go b/mw_validate_json.go new file mode 100644 index 00000000000..6484aba8acf --- /dev/null +++ b/mw_validate_json.go @@ -0,0 +1,85 @@ +package main + +import ( + "errors" + "fmt" + "io/ioutil" + "net/http" + + "github.com/xeipuuv/gojsonschema" + + "github.com/TykTechnologies/tyk/apidef" +) + +type ValidateJSON struct { + BaseMiddleware + schemaLoader gojsonschema.JSONLoader +} + +func (k *ValidateJSON) Name() string { + return "ValidateJSON" +} + +func (k *ValidateJSON) EnabledForSpec() bool { + for _, v := range k.Spec.VersionData.Versions { + if len(v.ExtendedPaths.ValidateJSON) > 0 { + return true + } + } + + return false +} + +// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail +func (k *ValidateJSON) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) { + + _, versionPaths, _, _ := k.Spec.Version(r) + found, meta := k.Spec.CheckSpecMatchesStatus(r, versionPaths, ValidateJSONRequest) + if !found { + return nil, http.StatusOK + } + + vPathMeta := meta.(*apidef.ValidatePathMeta) + if vPathMeta.ValidateWith == nil { + return errors.New("no schemas to validate against"), http.StatusInternalServerError + } + + rCopy := copyRequest(r) + bodyBytes, err := ioutil.ReadAll(rCopy.Body) + if err != nil { + return err, http.StatusInternalServerError + } + defer rCopy.Body.Close() + + schema := vPathMeta.ValidateWith + + result, err := k.validate(bodyBytes, schema) + if err != nil { + return err, http.StatusInternalServerError + } + + if !result.Valid() { + errStr := "payload validation failed" + for _, desc := range result.Errors() { + errStr = fmt.Sprintf("%s, %s", errStr, desc) + } + + if vPathMeta.ValidationErrorResponseCode == 0 { + vPathMeta.ValidationErrorResponseCode = http.StatusUnprocessableEntity + } + + return errors.New(errStr), vPathMeta.ValidationErrorResponseCode + } + + return nil, http.StatusOK +} + +func (k *ValidateJSON) validate(input []byte, schema map[string]interface{}) (*gojsonschema.Result, error) { + inputLoader := gojsonschema.NewBytesLoader(input) + + if k.schemaLoader == nil { + k.schemaLoader = gojsonschema.NewGoLoader(schema) + } + + return gojsonschema.Validate(k.schemaLoader, inputLoader) +} diff --git a/mw_validate_json_test.go b/mw_validate_json_test.go new file mode 100644 index 00000000000..052da17eeb5 --- /dev/null +++ b/mw_validate_json_test.go @@ -0,0 +1,208 @@ +package main + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" + "time" + + "github.com/justinas/alice" + + "github.com/TykTechnologies/tyk/user" +) + +type res struct { + Error string + Code int +} + +type fixture struct { + in string + out res + name string +} + +var schema = `{ + "title": "Person", + "type": "object", + "properties": { + "firstName": { + "type": "string" + }, + "lastName": { + "type": "string" + }, + "age": { + "description": "Age in years", + "type": "integer", + "minimum": 0 + } + }, + "required": ["firstName", "lastName"] +}` + +var fixtures = []fixture{ + { + in: `{"age":23, "firstName": "Harry"}`, + out: res{Error: `lastName: lastName is required`, Code: http.StatusUnprocessableEntity}, + name: "missing field", + }, + { + in: `{"age":23}`, + out: res{Error: `firstName: firstName is required, lastName: lastName is required`, Code: http.StatusUnprocessableEntity}, + name: "missing two fields", + }, + { + in: `{}`, + out: res{Error: `firstName: firstName is required, lastName: lastName is required`, Code: http.StatusUnprocessableEntity}, + name: "empty object", + }, + { + in: `[]`, + out: res{Error: `(root): Invalid type. Expected: object, given: array`, Code: http.StatusUnprocessableEntity}, + name: "array", + }, + { + in: `{"age":"23", "firstName": "Harry", "lastName": "Potter"}`, + out: res{Error: `age: Invalid type. Expected: integer, given: string`, Code: http.StatusUnprocessableEntity}, + name: "wrong type", + }, + { + in: `{"age":23, "firstName": "Harry", "lastName": "Potter"}`, + out: res{Error: `null`, Code: http.StatusOK}, + name: "valid", + }, +} + +func getJsonPathGatewaySetup() string { + + validateJSONPathGatewaySetup := `{ + "api_id": "jsontest", + "definition": { + "location": "header", + "key": "version" + }, + "auth": {"auth_header_name": "authorization"}, + "version_data": { + "not_versioned": true, + "versions": { + "default": { + "name": "default", + "use_extended_paths": true, + "extended_paths": { + "validate_json": [{ + "method": "POST", + "path": "me", + "validate_with": REPLACE_SCHEMA + }] + } + } + } + }, + "proxy": { + "listen_path": "/validate/", + "target_url": "` + testHttpAny + `" + } +}` + validateJSONPathGatewaySetup = strings.Replace(validateJSONPathGatewaySetup, "REPLACE_SCHEMA", schema, 1) + + return validateJSONPathGatewaySetup +} + +func TestValidateJSON_validate(t *testing.T) { + + for _, f := range fixtures { + + t.Run(f.name, func(st *testing.T) { + vj := ValidateJSON{} + + dat := map[string]interface{}{} + + if err := json.Unmarshal([]byte(schema), &dat); err != nil { + t.Fatal(err) + } + + res, err := vj.validate([]byte(f.in), dat) + if err != nil { + t.Fatal(err) + } + + if !res.Valid() && f.out.Code != http.StatusUnprocessableEntity { + st.Fatal("Expected invalid") + } + + if res.Valid() && f.out.Code != http.StatusOK { + t.Log(res.Errors()) + st.Fatal("expected valid", res.Valid(), f.out.Code) + } + }) + } +} + +func TestValidateJSON_ProcessRequest(t *testing.T) { + + for _, f := range fixtures { + + t.Run(f.name, func(st *testing.T) { + + spec := createSpecTest(st, getJsonPathGatewaySetup()) + recorder := httptest.NewRecorder() + req := testReq(t, "POST", "/validate/me", f.in) + + session := createJSONVersionedSession() + spec.SessionManager.UpdateSession("986968696869688869696999", session, 60) + req.Header.Set("Authorization", "986968696869688869696999") + + chain := getJSONValidChain(spec) + chain.ServeHTTP(recorder, req) + + if recorder.Code != f.out.Code { + st.Errorf("failed: %v, code: %v (body: %v)", req.URL.String(), recorder.Code, recorder.Body) + } + + if f.out.Code == http.StatusUnprocessableEntity { + recorderBody := recorder.Body.String() + if !strings.Contains(recorderBody, f.out.Error) { + st.Errorf("Incorrect error msg:\nwant: %v\ngot: %v", f.out.Error, recorderBody) + } + } + }) + } +} + +func createJSONVersionedSession() *user.SessionState { + session := new(user.SessionState) + session.Rate = 10000 + session.Allowance = session.Rate + session.LastCheck = time.Now().Unix() + session.Per = 60 + session.Expires = -1 + session.QuotaRenewalRate = 300 // 5 minutes + session.QuotaRenews = time.Now().Unix() + session.QuotaRemaining = 10 + session.QuotaMax = -1 + session.AccessRights = map[string]user.AccessDefinition{"jsontest": {APIName: "Tyk Test API", APIID: "jsontest", Versions: []string{"default"}}} + return session +} + +func getJSONValidChain(spec *APISpec) http.Handler { + remote, _ := url.Parse(spec.Proxy.TargetURL) + proxy := TykNewSingleHostReverseProxy(remote, spec) + proxyHandler := ProxyHandler(proxy, spec) + baseMid := BaseMiddleware{spec, proxy} + chain := alice.New(mwList( + &IPWhiteListMiddleware{baseMid}, + &MiddlewareContextVars{BaseMiddleware: baseMid}, + &AuthKey{baseMid}, + &VersionCheck{BaseMiddleware: baseMid}, + &KeyExpired{baseMid}, + &AccessRightsCheck{baseMid}, + &RateLimitAndQuotaCheck{baseMid}, + &ValidateJSON{BaseMiddleware: baseMid}, + &TransformHeaders{baseMid}, + )...).Then(proxyHandler) + return chain +} diff --git a/vendor/github.com/xeipuuv/gojsonschema/schema.go b/vendor/github.com/xeipuuv/gojsonschema/schema.go index cc6cdbc0e34..2cac71e9b86 100644 --- a/vendor/github.com/xeipuuv/gojsonschema/schema.go +++ b/vendor/github.com/xeipuuv/gojsonschema/schema.go @@ -586,11 +586,6 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) formatString, ok := m[KEY_FORMAT].(string) if ok && FormatCheckers.Has(formatString) { currentSchema.format = formatString - } else { - return errors.New(formatErrorDescription( - Locale.MustBeValidFormat(), - ErrorDetails{"key": KEY_FORMAT, "given": m[KEY_FORMAT]}, - )) } } diff --git a/vendor/vendor.json b/vendor/vendor.json index a6534d67c1a..cf6f0d97888 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -511,10 +511,10 @@ "revisionTime": "2015-08-08T06:50:54Z" }, { - "checksumSHA1": "7f3isFdJvljMc0lgQbl+zaaVeBw=", + "checksumSHA1": "wrBUTFFexM4Kz27RheBx34WhNwk=", "path": "github.com/xeipuuv/gojsonschema", - "revision": "3f523f4c14b6e925da10475eb0447c2f28614aac", - "revisionTime": "2017-09-14T03:15:16Z" + "revision": "212d8a0df7acfab8bdd190a7a69f0ab7376edcc8", + "revisionTime": "2017-10-25T06:06:43Z" }, { "checksumSHA1": "vE43s37+4CJ2CDU6TlOUOYE0K9c=", From e694b666300c360c76cb83b57fc35d6d5022f2ce Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Mon, 22 Jan 2018 17:25:59 +0200 Subject: [PATCH 10/20] Refactor tests to use new framework In addition updates error code and message for not valid json payloads Require schema_version --- apidef/api_definitions.go | 10 +- handler_error.go | 2 +- middleware.go | 3 + mw_validate_json.go | 28 ++++-- mw_validate_json_test.go | 200 +++++--------------------------------- 5 files changed, 52 insertions(+), 191 deletions(-) diff --git a/apidef/api_definitions.go b/apidef/api_definitions.go index 99e3ab71497..ad7ad0550b6 100644 --- a/apidef/api_definitions.go +++ b/apidef/api_definitions.go @@ -166,11 +166,13 @@ type MethodTransformMeta struct { } type ValidatePathMeta struct { - Path string `bson:"path" json:"path"` - Method string `bson:"method" json:"method"` - ValidateWith map[string]interface{} `json:"validate_with"` + Path string `bson:"path" json:"path"` + Method string `bson:"method" json:"method"` + Schema map[string]interface{} `bson:"schema" json:"schema"` + // TODO: Implement multi schema support + SchemaVersion string `bson:"schema_version" json:"schema_version"` // Allows override of default 422 Unprocessible Entity response code for validation errors. - ValidationErrorResponseCode int `bson:"validation_error_response_code" json:"validation_error_response_code"` + ErrorResponseCode int `bson:"error_response_code" json:"error_response_code"` } type ExtendedPathsSet struct { diff --git a/handler_error.go b/handler_error.go index a8b11aba930..ceaaaf1a0ef 100644 --- a/handler_error.go +++ b/handler_error.go @@ -32,6 +32,7 @@ type ErrorHandler struct { // HandleError is the actual error handler and will store the error details in analytics if analytics processing is enabled. func (e *ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, errMsg string, errCode int) { + var templateExtension string var contentType string @@ -66,7 +67,6 @@ func (e *ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, errMs // Need to return the correct error code! w.WriteHeader(errCode) - apiError := APIError{errMsg} tmpl.Execute(w, &apiError) diff --git a/middleware.go b/middleware.go index 01b90d61a38..f401090e44c 100644 --- a/middleware.go +++ b/middleware.go @@ -80,9 +80,12 @@ func createMiddleware(mw TykMiddleware) func(http.Handler) http.Handler { } err, errCode := mw.ProcessRequest(w, r, mwConf) if err != nil { + handler := ErrorHandler{mw.Base()} handler.HandleError(w, r, err.Error(), errCode) + meta["error"] = err.Error() + job.TimingKv("exec_time", time.Since(startTime).Nanoseconds(), meta) job.TimingKv(eventName+".exec_time", time.Since(startTime).Nanoseconds(), meta) return diff --git a/mw_validate_json.go b/mw_validate_json.go index 6484aba8acf..3bdaace7d68 100644 --- a/mw_validate_json.go +++ b/mw_validate_json.go @@ -40,35 +40,43 @@ func (k *ValidateJSON) ProcessRequest(w http.ResponseWriter, r *http.Request, _ } vPathMeta := meta.(*apidef.ValidatePathMeta) - if vPathMeta.ValidateWith == nil { + if vPathMeta.Schema == nil { return errors.New("no schemas to validate against"), http.StatusInternalServerError } + if vPathMeta.SchemaVersion != "" && vPathMeta.SchemaVersion != "draft-v4" { + return errors.New("unsupported schema version"), http.StatusInternalServerError + } + rCopy := copyRequest(r) bodyBytes, err := ioutil.ReadAll(rCopy.Body) if err != nil { - return err, http.StatusInternalServerError + return err, http.StatusBadRequest } defer rCopy.Body.Close() - schema := vPathMeta.ValidateWith + schema := vPathMeta.Schema result, err := k.validate(bodyBytes, schema) if err != nil { - return err, http.StatusInternalServerError + return fmt.Errorf("JSON parsing error: %v", err), http.StatusBadRequest } if !result.Valid() { - errStr := "payload validation failed" - for _, desc := range result.Errors() { - errStr = fmt.Sprintf("%s, %s", errStr, desc) + errStr := "" + for i, desc := range result.Errors() { + if i == 0 { + errStr = desc.String() + } else { + errStr = fmt.Sprintf("%s; %s", errStr, desc) + } } - if vPathMeta.ValidationErrorResponseCode == 0 { - vPathMeta.ValidationErrorResponseCode = http.StatusUnprocessableEntity + if vPathMeta.ErrorResponseCode == 0 { + vPathMeta.ErrorResponseCode = http.StatusUnprocessableEntity } - return errors.New(errStr), vPathMeta.ValidationErrorResponseCode + return errors.New(errStr), vPathMeta.ErrorResponseCode } return nil, http.StatusOK diff --git a/mw_validate_json_test.go b/mw_validate_json_test.go index 052da17eeb5..fd2dcf580dd 100644 --- a/mw_validate_json_test.go +++ b/mw_validate_json_test.go @@ -3,29 +3,13 @@ package main import ( "encoding/json" "net/http" - "net/http/httptest" - "net/url" - "strings" "testing" - "time" - "github.com/justinas/alice" - - "github.com/TykTechnologies/tyk/user" + "github.com/TykTechnologies/tyk/apidef" + "github.com/TykTechnologies/tyk/test" ) -type res struct { - Error string - Code int -} - -type fixture struct { - in string - out res - name string -} - -var schema = `{ +var testJsonSchema = `{ "title": "Person", "type": "object", "properties": { @@ -44,165 +28,29 @@ var schema = `{ "required": ["firstName", "lastName"] }` -var fixtures = []fixture{ - { - in: `{"age":23, "firstName": "Harry"}`, - out: res{Error: `lastName: lastName is required`, Code: http.StatusUnprocessableEntity}, - name: "missing field", - }, - { - in: `{"age":23}`, - out: res{Error: `firstName: firstName is required, lastName: lastName is required`, Code: http.StatusUnprocessableEntity}, - name: "missing two fields", - }, - { - in: `{}`, - out: res{Error: `firstName: firstName is required, lastName: lastName is required`, Code: http.StatusUnprocessableEntity}, - name: "empty object", - }, - { - in: `[]`, - out: res{Error: `(root): Invalid type. Expected: object, given: array`, Code: http.StatusUnprocessableEntity}, - name: "array", - }, - { - in: `{"age":"23", "firstName": "Harry", "lastName": "Potter"}`, - out: res{Error: `age: Invalid type. Expected: integer, given: string`, Code: http.StatusUnprocessableEntity}, - name: "wrong type", - }, - { - in: `{"age":23, "firstName": "Harry", "lastName": "Potter"}`, - out: res{Error: `null`, Code: http.StatusOK}, - name: "valid", - }, -} - -func getJsonPathGatewaySetup() string { - - validateJSONPathGatewaySetup := `{ - "api_id": "jsontest", - "definition": { - "location": "header", - "key": "version" - }, - "auth": {"auth_header_name": "authorization"}, - "version_data": { - "not_versioned": true, - "versions": { - "default": { - "name": "default", - "use_extended_paths": true, - "extended_paths": { - "validate_json": [{ - "method": "POST", - "path": "me", - "validate_with": REPLACE_SCHEMA - }] - } - } - } - }, - "proxy": { - "listen_path": "/validate/", - "target_url": "` + testHttpAny + `" - } -}` - validateJSONPathGatewaySetup = strings.Replace(validateJSONPathGatewaySetup, "REPLACE_SCHEMA", schema, 1) - - return validateJSONPathGatewaySetup -} - -func TestValidateJSON_validate(t *testing.T) { - - for _, f := range fixtures { - - t.Run(f.name, func(st *testing.T) { - vj := ValidateJSON{} - - dat := map[string]interface{}{} - - if err := json.Unmarshal([]byte(schema), &dat); err != nil { - t.Fatal(err) - } - - res, err := vj.validate([]byte(f.in), dat) - if err != nil { - t.Fatal(err) - } - - if !res.Valid() && f.out.Code != http.StatusUnprocessableEntity { - st.Fatal("Expected invalid") - } - - if res.Valid() && f.out.Code != http.StatusOK { - t.Log(res.Errors()) - st.Fatal("expected valid", res.Valid(), f.out.Code) - } - }) - } -} - -func TestValidateJSON_ProcessRequest(t *testing.T) { - - for _, f := range fixtures { - - t.Run(f.name, func(st *testing.T) { - - spec := createSpecTest(st, getJsonPathGatewaySetup()) - recorder := httptest.NewRecorder() - req := testReq(t, "POST", "/validate/me", f.in) - - session := createJSONVersionedSession() - spec.SessionManager.UpdateSession("986968696869688869696999", session, 60) - req.Header.Set("Authorization", "986968696869688869696999") - - chain := getJSONValidChain(spec) - chain.ServeHTTP(recorder, req) - - if recorder.Code != f.out.Code { - st.Errorf("failed: %v, code: %v (body: %v)", req.URL.String(), recorder.Code, recorder.Body) - } - - if f.out.Code == http.StatusUnprocessableEntity { - recorderBody := recorder.Body.String() - if !strings.Contains(recorderBody, f.out.Error) { - st.Errorf("Incorrect error msg:\nwant: %v\ngot: %v", f.out.Error, recorderBody) +func TestValidateJSONSchema(t *testing.T) { + ts := newTykTestServer() + defer ts.Close() + + buildAndLoadAPI(func(spec *APISpec) { + updateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) { + json.Unmarshal([]byte(`[ + { + "path": "/v", + "method": "POST", + "schema": `+testJsonSchema+` } - } + ]`), &v.ExtendedPaths.ValidateJSON) }) - } -} -func createJSONVersionedSession() *user.SessionState { - session := new(user.SessionState) - session.Rate = 10000 - session.Allowance = session.Rate - session.LastCheck = time.Now().Unix() - session.Per = 60 - session.Expires = -1 - session.QuotaRenewalRate = 300 // 5 minutes - session.QuotaRenews = time.Now().Unix() - session.QuotaRemaining = 10 - session.QuotaMax = -1 - session.AccessRights = map[string]user.AccessDefinition{"jsontest": {APIName: "Tyk Test API", APIID: "jsontest", Versions: []string{"default"}}} - return session -} + spec.Proxy.ListenPath = "/" + }) -func getJSONValidChain(spec *APISpec) http.Handler { - remote, _ := url.Parse(spec.Proxy.TargetURL) - proxy := TykNewSingleHostReverseProxy(remote, spec) - proxyHandler := ProxyHandler(proxy, spec) - baseMid := BaseMiddleware{spec, proxy} - chain := alice.New(mwList( - &IPWhiteListMiddleware{baseMid}, - &MiddlewareContextVars{BaseMiddleware: baseMid}, - &AuthKey{baseMid}, - &VersionCheck{BaseMiddleware: baseMid}, - &KeyExpired{baseMid}, - &AccessRightsCheck{baseMid}, - &RateLimitAndQuotaCheck{baseMid}, - &ValidateJSON{BaseMiddleware: baseMid}, - &TransformHeaders{baseMid}, - )...).Then(proxyHandler) - return chain + ts.Run(t, []test.TestCase{ + {Method: "POST", Path: "/without_validation", Data: "{not_valid}", Code: http.StatusOK}, + {Method: "POST", Path: "/v", Data: `{"age":23}`, BodyMatch: `firstName: firstName is required; lastName: lastName is required`, Code: http.StatusUnprocessableEntity}, + {Method: "POST", Path: "/v", Data: `[]`, BodyMatch: `Expected: object, given: array`, Code: http.StatusUnprocessableEntity}, + {Method: "POST", Path: "/v", Data: `not_json`, Code: http.StatusBadRequest}, + {Method: "POST", Path: "/v", Data: `{"age":23, "firstName": "Harry", "lastName": "Potter"}`, Code: http.StatusOK}, + }...) } From 91cfab9a921da9164d32753ca60bf79f1a803872 Mon Sep 17 00:00:00 2001 From: Luan van Pletsen Date: Wed, 14 Feb 2018 14:08:56 +0000 Subject: [PATCH 11/20] Configurable jsvm timeout (#1470) * Add `jsvm_timeout` config option for JSVM timeout --- config/config.go | 1 + lint/schema.go | 3 +++ mw_js_plugin.go | 9 ++++++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 23bc7b99881..ba9d709ce76 100644 --- a/config/config.go +++ b/config/config.go @@ -247,6 +247,7 @@ type Config struct { ControlAPIPort int `json:"control_api_port"` EnableCustomDomains bool `json:"enable_custom_domains"` EnableJSVM bool `json:"enable_jsvm"` + JSVMTimeout int `json:"jsvm_timeout"` CoProcessOptions CoProcessConfig `json:"coprocess_options"` HideGeneratorHeader bool `json:"hide_generator_header"` EventHandlers apidef.EventHandlerMetaConfig `json:"event_handlers"` diff --git a/lint/schema.go b/lint/schema.go index 87f97d5f980..7fd19f7f0e9 100644 --- a/lint/schema.go +++ b/lint/schema.go @@ -221,6 +221,9 @@ const confSchema = `{ "enable_jsvm": { "type": "boolean" }, + "jsvm_timeout": { + "type": "integer" + }, "enable_non_transactional_rate_limiter": { "type": "boolean" }, diff --git a/mw_js_plugin.go b/mw_js_plugin.go index ddf48bc3e86..d5c3adafca9 100644 --- a/mw_js_plugin.go +++ b/mw_js_plugin.go @@ -303,6 +303,8 @@ type JSVM struct { RawLog *logrus.Logger // logger used by `rawlog` func to avoid formatting } +const defaultJSVMTimeout = 5 + // Init creates the JSVM with the core library and sets up a default // timeout. func (j *JSVM) Init(spec *APISpec) { @@ -337,7 +339,12 @@ func (j *JSVM) Init(spec *APISpec) { // Add environment API j.LoadTykJSApi() - j.Timeout = 5 * time.Second + if config.Global.JSVMTimeout <= 0 { + j.Timeout = time.Duration(defaultJSVMTimeout) * time.Second + } else { + j.Timeout = time.Duration(config.Global.JSVMTimeout) * time.Second + } + j.Log = log // use the global logger by default j.RawLog = rawLog } From a446600dcc5cebe5c25da7784edb5032b5214b87 Mon Sep 17 00:00:00 2001 From: Artem Hluvchynskyi Date: Wed, 14 Feb 2018 16:11:40 +0200 Subject: [PATCH 12/20] [WIP] Converts Python coprocess glue module to handcrafted C (#1338) * Converts Python coprocess glue module to handcrafted C This replaces Cython-generated code in Python coprocess with handcrafted C using only limited Python C API subset so that Py_LIMITED_API preprocessor macro could be set and the resulting binary dynamically linked with whatever version of Python as long as it's 3.2+. At the same time plugin interface is left compatible by providing a gateway.py module that actually calls the C wrapper functions. In addition to this, Python code has been partly made PEP-8 conformant. --- coprocess/python/cythonize | 12 - coprocess/python/dispatcher.py | 29 +- coprocess/python/gateway.py | 33 + coprocess/python/tyk/__init__.py | 0 coprocess/python/tyk/decorators.py | 16 +- coprocess/python/tyk/event.py | 3 +- coprocess/python/tyk/gateway.c | 4085 ------------------------ coprocess/python/tyk/gateway.h | 29 - coprocess/python/tyk/gateway.pyx | 27 - coprocess/python/tyk/gateway_wrapper.c | 78 + coprocess/python/tyk/gateway_wrapper.h | 8 + coprocess/python/tyk/middleware.py | 16 +- coprocess/python/tyk/object.py | 3 +- coprocess/python/tyk/request.py | 5 + coprocess/python/tyk/tyk.py | 1 - coprocess_python.go | 8 +- coprocess_python_api.c | 2 +- 17 files changed, 165 insertions(+), 4190 deletions(-) delete mode 100755 coprocess/python/cythonize create mode 100644 coprocess/python/gateway.py create mode 100644 coprocess/python/tyk/__init__.py delete mode 100644 coprocess/python/tyk/gateway.c delete mode 100644 coprocess/python/tyk/gateway.h delete mode 100644 coprocess/python/tyk/gateway.pyx create mode 100644 coprocess/python/tyk/gateway_wrapper.c create mode 100644 coprocess/python/tyk/gateway_wrapper.h delete mode 100644 coprocess/python/tyk/tyk.py diff --git a/coprocess/python/cythonize b/coprocess/python/cythonize deleted file mode 100755 index 2456b5b0fcc..00000000000 --- a/coprocess/python/cythonize +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -# This script compiles a Python file using Cython (.pyx) -# and patches the resulting binding file to include custom build tags (coprocess, python) -INPUT=$1 -echo "cython: $INPUT" -cd tyk/ -rm $INPUT.c -echo "Cython compilation..." -cythonize $INPUT.pyx -echo "Appending build tags!" -echo -e "// +build coprocess\n// +build python\n\n$(cat $INPUT.c)" > $INPUT.c -echo "Done" diff --git a/coprocess/python/dispatcher.py b/coprocess/python/dispatcher.py index 78c1441a840..9f6fbb93b7a 100644 --- a/coprocess/python/dispatcher.py +++ b/coprocess/python/dispatcher.py @@ -1,21 +1,19 @@ from glob import glob -from os import getcwd, chdir, path +from os import path import sys, traceback -import tyk from tyk.middleware import TykMiddleware from tyk.object import TykCoProcessObject from tyk.event import TykEvent, TykEventHandler from gateway import TykGateway as tyk -from importlib.machinery import SourceFileLoader class TykDispatcher: '''A simple dispatcher''' def __init__(self, middleware_path, event_handler_path, bundle_paths): - tyk.log( "Initializing dispatcher", "info" ) + tyk.log("Initializing dispatcher", "info") self.event_handler_path = path.join(event_handler_path, '*.py') self.event_handlers = {} self.load_event_handlers() @@ -29,7 +27,7 @@ def __init__(self, middleware_path, event_handler_path, bundle_paths): def get_modules(self, the_path): files = glob(the_path) - files = [ path.basename( f.replace('.py', '') ) for f in files ] + files = [path.basename(f.replace('.py', '')) for f in files] return files def find_middleware(self, path): @@ -56,9 +54,8 @@ def load_bundle(self, base_bundle_path): self.middlewares.append(middleware) self.update_hook_table() - def load_middlewares(self): - tyk.log( "Loading middlewares.", "debug" ) + tyk.log("Loading middlewares.", "debug") available_modules = self.get_modules(self.middleware_path) for module_name in available_modules: middleware = self.find_middleware(module_name) @@ -70,11 +67,11 @@ def load_middlewares(self): self.update_hook_table() def purge_middlewares(self): - tyk.log( "Purging middlewares.", "debug" ) + tyk.log("Purging middlewares.", "debug") available_modules = self.get_modules(self.middleware_path) for middleware in self.middlewares: - if not middleware.filepath in available_modules: - tyk.log( "Purging middleware: '{0}'".format(middleware.filepath), "warning" ) + if middleware.filepath not in available_modules: + tyk.log("Purging middleware: '{0}'".format(middleware.filepath), "warning") self.middlewares.remove(middleware) def update_hook_table(self): @@ -110,21 +107,21 @@ def dispatch_hook(self, object_msg): if hook_handler: object = middleware.process(hook_handler, object) else: - tyk.log( "Can't dispatch '{0}', hook is not defined.".format(object.hook_name), "error") + tyk.log("Can't dispatch '{0}', hook is not defined.".format(object.hook_name), "error") return object.dump() except: exc_trace = traceback.format_exc() print(exc_trace) - tyk.log_error( "Can't dispatch, error:" ) + tyk.log_error("Can't dispatch, error:") return object_msg def purge_event_handlers(self): - tyk.log( "Purging event handlers.", "debug" ) + tyk.log("Purging event handlers.", "debug") self.event_handlers = {} def load_event_handlers(self): - tyk.log( "Loading event handlers.", "debug" ) + tyk.log("Loading event handlers.", "debug") for module_name in self.get_modules(self.event_handler_path): event_handlers = TykEventHandler.from_module(module_name) for event_handler in event_handlers: @@ -143,10 +140,10 @@ def dispatch_event(self, event_json): if event_handler: event_handler.process(event) except: - tyk.log_error( "Can't dispatch, error:") + tyk.log_error("Can't dispatch, error:") def reload(self): - tyk.log( "Reloading event handlers and middlewares.", "info" ) + tyk.log("Reloading event handlers and middlewares.", "info") self.purge_event_handlers() self.load_event_handlers() self.load_middlewares() diff --git a/coprocess/python/gateway.py b/coprocess/python/gateway.py new file mode 100644 index 00000000000..9c694722422 --- /dev/null +++ b/coprocess/python/gateway.py @@ -0,0 +1,33 @@ +""" +This module provides interface compatibility and flexibility for the C glue code in tyk/gateway_wrapper.c +""" +from sys import exc_info + +import gateway_wrapper as gw + + +class TykGateway: + + @classmethod + def store_data(cls, key, value, ttl): + gw.store_data(key, value, ttl) + + @classmethod + def get_data(cls, key): + return gw.get_data(key) + + @classmethod + def trigger_event(cls, event_name, payload): + gw.trigger_event(event_name, payload) + + @classmethod + def log(cls, msg, level): + gw.log(msg, level) + + @classmethod + def log_error(cls, *args): + excp = exc_info() + if len(args) == 0: + cls.log("{0} {1}".format(excp[0], excp[1]), "error") + else: + cls.log("{0} {1} {2}".format(args[0], excp[0], excp[1]), "error") diff --git a/coprocess/python/tyk/__init__.py b/coprocess/python/tyk/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/coprocess/python/tyk/decorators.py b/coprocess/python/tyk/decorators.py index 062f58da621..4f7c3a2ff49 100644 --- a/coprocess/python/tyk/decorators.py +++ b/coprocess/python/tyk/decorators.py @@ -1,52 +1,60 @@ from inspect import getargspec + class HandlerDecorator(object): def __init__(self, f): self.name = f.__name__ self.f = f - return + def __call__(self, req, sess, spec): self.f + class Hook(object): def __init__(self, f): self.name = f.__name__ self.f = f self.arg_count = len(getargspec(f)[0]) - return + def __call__(self, *args, **kwargs): if self.arg_count == 3: return self.f(args[0], args[1], args[2]) if self.arg_count == 4: return self.f(args[0], args[1], args[2], args[3]) + class Pre(HandlerDecorator): def __call__(self, req, sess, spec): return self.f(req, sess, spec) + class Post(HandlerDecorator): def __call__(self, req, sess, spec): return self.f(req, sess, spec) + class PostKeyAuth(HandlerDecorator): def __call__(self, req, sess, spec): return self.f(req, sess, spec) + class CustomKeyCheck(): def __init__(self, f): self.f = f self.name = f.__name__ - return + def __call__(self, req, sess, metadata, spec): return self.f(req, sess, metadata, spec) + class Event(object): def __init__(self, f): self.name = f.__name__ self.f = f - return + def __call__(self, event, spec): self.f(event, spec) + def ThisIsNotADecorator(): pass diff --git a/coprocess/python/tyk/event.py b/coprocess/python/tyk/event.py index 9a3a04c963b..ad6a4f187af 100644 --- a/coprocess/python/tyk/event.py +++ b/coprocess/python/tyk/event.py @@ -3,12 +3,12 @@ import tyk.decorators as decorators + class TykEventHandler: def __init__(self, name, callback): self.name = name self.callback = callback - def process(self, event): print("process", event) self.callback(event.message, event.spec) @@ -25,6 +25,7 @@ def from_module(module_name): event_handlers.append(event_handler) return event_handlers + class TykEvent: def __init__(self, event_json): self.__dict__ = json.loads(event_json) diff --git a/coprocess/python/tyk/gateway.c b/coprocess/python/tyk/gateway.c deleted file mode 100644 index 0e1208c4ce0..00000000000 --- a/coprocess/python/tyk/gateway.c +++ /dev/null @@ -1,4085 +0,0 @@ -// +build coprocess -// +build python - -/* Generated by Cython 0.24.1 */ - -/* BEGIN: Cython Metadata -{ - "distutils": { - "depends": [] - }, - "module_name": "gateway" -} -END: Cython Metadata */ - -#define PY_SSIZE_T_CLEAN -#include "Python.h" -#ifndef Py_PYTHON_H - #error Python headers needed to compile C extensions, please install development version of Python. -#elif PY_VERSION_HEX < 0x02060000 || (0x03000000 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x03020000) - #error Cython requires Python 2.6+ or Python 3.2+. -#else -#define CYTHON_ABI "0_24_1" -#include -#ifndef offsetof - #define offsetof(type, member) ( (size_t) & ((type*)0) -> member ) -#endif -#if !defined(WIN32) && !defined(MS_WINDOWS) - #ifndef __stdcall - #define __stdcall - #endif - #ifndef __cdecl - #define __cdecl - #endif - #ifndef __fastcall - #define __fastcall - #endif -#endif -#ifndef DL_IMPORT - #define DL_IMPORT(t) t -#endif -#ifndef DL_EXPORT - #define DL_EXPORT(t) t -#endif -#ifndef PY_LONG_LONG - #define PY_LONG_LONG LONG_LONG -#endif -#ifndef Py_HUGE_VAL - #define Py_HUGE_VAL HUGE_VAL -#endif -#ifdef PYPY_VERSION - #define CYTHON_COMPILING_IN_PYPY 1 - #define CYTHON_COMPILING_IN_CPYTHON 0 -#else - #define CYTHON_COMPILING_IN_PYPY 0 - #define CYTHON_COMPILING_IN_CPYTHON 1 -#endif -#if !defined(CYTHON_USE_PYLONG_INTERNALS) && CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x02070000 - #define CYTHON_USE_PYLONG_INTERNALS 1 -#endif -#if CYTHON_USE_PYLONG_INTERNALS - #include "longintrepr.h" - #undef SHIFT - #undef BASE - #undef MASK -#endif -#if CYTHON_COMPILING_IN_PYPY && PY_VERSION_HEX < 0x02070600 && !defined(Py_OptimizeFlag) - #define Py_OptimizeFlag 0 -#endif -#define __PYX_BUILD_PY_SSIZE_T "n" -#define CYTHON_FORMAT_SSIZE_T "z" -#if PY_MAJOR_VERSION < 3 - #define __Pyx_BUILTIN_MODULE_NAME "__builtin__" - #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\ - PyCode_New(a+k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) - #define __Pyx_DefaultClassType PyClass_Type -#else - #define __Pyx_BUILTIN_MODULE_NAME "builtins" - #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\ - PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) - #define __Pyx_DefaultClassType PyType_Type -#endif -#ifndef Py_TPFLAGS_CHECKTYPES - #define Py_TPFLAGS_CHECKTYPES 0 -#endif -#ifndef Py_TPFLAGS_HAVE_INDEX - #define Py_TPFLAGS_HAVE_INDEX 0 -#endif -#ifndef Py_TPFLAGS_HAVE_NEWBUFFER - #define Py_TPFLAGS_HAVE_NEWBUFFER 0 -#endif -#ifndef Py_TPFLAGS_HAVE_FINALIZE - #define Py_TPFLAGS_HAVE_FINALIZE 0 -#endif -#if PY_VERSION_HEX > 0x03030000 && defined(PyUnicode_KIND) - #define CYTHON_PEP393_ENABLED 1 - #define __Pyx_PyUnicode_READY(op) (likely(PyUnicode_IS_READY(op)) ?\ - 0 : _PyUnicode_Ready((PyObject *)(op))) - #define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_LENGTH(u) - #define __Pyx_PyUnicode_READ_CHAR(u, i) PyUnicode_READ_CHAR(u, i) - #define __Pyx_PyUnicode_KIND(u) PyUnicode_KIND(u) - #define __Pyx_PyUnicode_DATA(u) PyUnicode_DATA(u) - #define __Pyx_PyUnicode_READ(k, d, i) PyUnicode_READ(k, d, i) - #define __Pyx_PyUnicode_IS_TRUE(u) (0 != (likely(PyUnicode_IS_READY(u)) ? PyUnicode_GET_LENGTH(u) : PyUnicode_GET_SIZE(u))) -#else - #define CYTHON_PEP393_ENABLED 0 - #define __Pyx_PyUnicode_READY(op) (0) - #define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_SIZE(u) - #define __Pyx_PyUnicode_READ_CHAR(u, i) ((Py_UCS4)(PyUnicode_AS_UNICODE(u)[i])) - #define __Pyx_PyUnicode_KIND(u) (sizeof(Py_UNICODE)) - #define __Pyx_PyUnicode_DATA(u) ((void*)PyUnicode_AS_UNICODE(u)) - #define __Pyx_PyUnicode_READ(k, d, i) ((void)(k), (Py_UCS4)(((Py_UNICODE*)d)[i])) - #define __Pyx_PyUnicode_IS_TRUE(u) (0 != PyUnicode_GET_SIZE(u)) -#endif -#if CYTHON_COMPILING_IN_PYPY - #define __Pyx_PyUnicode_Concat(a, b) PyNumber_Add(a, b) - #define __Pyx_PyUnicode_ConcatSafe(a, b) PyNumber_Add(a, b) -#else - #define __Pyx_PyUnicode_Concat(a, b) PyUnicode_Concat(a, b) - #define __Pyx_PyUnicode_ConcatSafe(a, b) ((unlikely((a) == Py_None) || unlikely((b) == Py_None)) ?\ - PyNumber_Add(a, b) : __Pyx_PyUnicode_Concat(a, b)) -#endif -#if CYTHON_COMPILING_IN_PYPY && !defined(PyUnicode_Contains) - #define PyUnicode_Contains(u, s) PySequence_Contains(u, s) -#endif -#if CYTHON_COMPILING_IN_PYPY && !defined(PyByteArray_Check) - #define PyByteArray_Check(obj) PyObject_TypeCheck(obj, &PyByteArray_Type) -#endif -#if CYTHON_COMPILING_IN_PYPY && !defined(PyObject_Format) - #define PyObject_Format(obj, fmt) PyObject_CallMethod(obj, "__format__", "O", fmt) -#endif -#if CYTHON_COMPILING_IN_PYPY && !defined(PyObject_Malloc) - #define PyObject_Malloc(s) PyMem_Malloc(s) - #define PyObject_Free(p) PyMem_Free(p) - #define PyObject_Realloc(p) PyMem_Realloc(p) -#endif -#define __Pyx_PyString_FormatSafe(a, b) ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : __Pyx_PyString_Format(a, b)) -#define __Pyx_PyUnicode_FormatSafe(a, b) ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : PyUnicode_Format(a, b)) -#if PY_MAJOR_VERSION >= 3 - #define __Pyx_PyString_Format(a, b) PyUnicode_Format(a, b) -#else - #define __Pyx_PyString_Format(a, b) PyString_Format(a, b) -#endif -#if PY_MAJOR_VERSION < 3 && !defined(PyObject_ASCII) - #define PyObject_ASCII(o) PyObject_Repr(o) -#endif -#if PY_MAJOR_VERSION >= 3 - #define PyBaseString_Type PyUnicode_Type - #define PyStringObject PyUnicodeObject - #define PyString_Type PyUnicode_Type - #define PyString_Check PyUnicode_Check - #define PyString_CheckExact PyUnicode_CheckExact -#endif -#if PY_MAJOR_VERSION >= 3 - #define __Pyx_PyBaseString_Check(obj) PyUnicode_Check(obj) - #define __Pyx_PyBaseString_CheckExact(obj) PyUnicode_CheckExact(obj) -#else - #define __Pyx_PyBaseString_Check(obj) (PyString_Check(obj) || PyUnicode_Check(obj)) - #define __Pyx_PyBaseString_CheckExact(obj) (PyString_CheckExact(obj) || PyUnicode_CheckExact(obj)) -#endif -#ifndef PySet_CheckExact - #define PySet_CheckExact(obj) (Py_TYPE(obj) == &PySet_Type) -#endif -#define __Pyx_TypeCheck(obj, type) PyObject_TypeCheck(obj, (PyTypeObject *)type) -#if PY_MAJOR_VERSION >= 3 - #define PyIntObject PyLongObject - #define PyInt_Type PyLong_Type - #define PyInt_Check(op) PyLong_Check(op) - #define PyInt_CheckExact(op) PyLong_CheckExact(op) - #define PyInt_FromString PyLong_FromString - #define PyInt_FromUnicode PyLong_FromUnicode - #define PyInt_FromLong PyLong_FromLong - #define PyInt_FromSize_t PyLong_FromSize_t - #define PyInt_FromSsize_t PyLong_FromSsize_t - #define PyInt_AsLong PyLong_AsLong - #define PyInt_AS_LONG PyLong_AS_LONG - #define PyInt_AsSsize_t PyLong_AsSsize_t - #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask - #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask - #define PyNumber_Int PyNumber_Long -#endif -#if PY_MAJOR_VERSION >= 3 - #define PyBoolObject PyLongObject -#endif -#if PY_MAJOR_VERSION >= 3 && CYTHON_COMPILING_IN_PYPY - #ifndef PyUnicode_InternFromString - #define PyUnicode_InternFromString(s) PyUnicode_FromString(s) - #endif -#endif -#if PY_VERSION_HEX < 0x030200A4 - typedef long Py_hash_t; - #define __Pyx_PyInt_FromHash_t PyInt_FromLong - #define __Pyx_PyInt_AsHash_t PyInt_AsLong -#else - #define __Pyx_PyInt_FromHash_t PyInt_FromSsize_t - #define __Pyx_PyInt_AsHash_t PyInt_AsSsize_t -#endif -#if PY_MAJOR_VERSION >= 3 - #define __Pyx_PyMethod_New(func, self, klass) ((self) ? PyMethod_New(func, self) : PyInstanceMethod_New(func)) -#else - #define __Pyx_PyMethod_New(func, self, klass) PyMethod_New(func, self, klass) -#endif -#if PY_VERSION_HEX >= 0x030500B1 -#define __Pyx_PyAsyncMethodsStruct PyAsyncMethods -#define __Pyx_PyType_AsAsync(obj) (Py_TYPE(obj)->tp_as_async) -#elif CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 -typedef struct { - unaryfunc am_await; - unaryfunc am_aiter; - unaryfunc am_anext; -} __Pyx_PyAsyncMethodsStruct; -#define __Pyx_PyType_AsAsync(obj) ((__Pyx_PyAsyncMethodsStruct*) (Py_TYPE(obj)->tp_reserved)) -#else -#define __Pyx_PyType_AsAsync(obj) NULL -#endif -#ifndef CYTHON_RESTRICT - #if defined(__GNUC__) - #define CYTHON_RESTRICT __restrict__ - #elif defined(_MSC_VER) && _MSC_VER >= 1400 - #define CYTHON_RESTRICT __restrict - #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L - #define CYTHON_RESTRICT restrict - #else - #define CYTHON_RESTRICT - #endif -#endif -#define __Pyx_void_to_None(void_result) ((void)(void_result), Py_INCREF(Py_None), Py_None) - -#ifndef CYTHON_INLINE - #if defined(__GNUC__) - #define CYTHON_INLINE __inline__ - #elif defined(_MSC_VER) - #define CYTHON_INLINE __inline - #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L - #define CYTHON_INLINE inline - #else - #define CYTHON_INLINE - #endif -#endif - -#if defined(WIN32) || defined(MS_WINDOWS) - #define _USE_MATH_DEFINES -#endif -#include -#ifdef NAN -#define __PYX_NAN() ((float) NAN) -#else -static CYTHON_INLINE float __PYX_NAN() { - float value; - memset(&value, 0xFF, sizeof(value)); - return value; -} -#endif -#if defined(__CYGWIN__) && defined(_LDBL_EQ_DBL) -#define __Pyx_truncl trunc -#else -#define __Pyx_truncl truncl -#endif - - -#define __PYX_ERR(f_index, lineno, Ln_error) \ -{ \ - __pyx_filename = __pyx_f[f_index]; __pyx_lineno = lineno; __pyx_clineno = __LINE__; goto Ln_error; \ -} - -#if PY_MAJOR_VERSION >= 3 - #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y) - #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y) -#else - #define __Pyx_PyNumber_Divide(x,y) PyNumber_Divide(x,y) - #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceDivide(x,y) -#endif - -#ifndef __PYX_EXTERN_C - #ifdef __cplusplus - #define __PYX_EXTERN_C extern "C" - #else - #define __PYX_EXTERN_C extern - #endif -#endif - -#define __PYX_HAVE__gateway -#define __PYX_HAVE_API__gateway -#include "coprocess/api.h" -#ifdef _OPENMP -#include -#endif /* _OPENMP */ - -#ifdef PYREX_WITHOUT_ASSERTIONS -#define CYTHON_WITHOUT_ASSERTIONS -#endif - -#ifndef CYTHON_UNUSED -# if defined(__GNUC__) -# if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) -# define CYTHON_UNUSED __attribute__ ((__unused__)) -# else -# define CYTHON_UNUSED -# endif -# elif defined(__ICC) || (defined(__INTEL_COMPILER) && !defined(_MSC_VER)) -# define CYTHON_UNUSED __attribute__ ((__unused__)) -# else -# define CYTHON_UNUSED -# endif -#endif -#ifndef CYTHON_NCP_UNUSED -# if CYTHON_COMPILING_IN_CPYTHON -# define CYTHON_NCP_UNUSED -# else -# define CYTHON_NCP_UNUSED CYTHON_UNUSED -# endif -#endif -typedef struct {PyObject **p; const char *s; const Py_ssize_t n; const char* encoding; - const char is_unicode; const char is_str; const char intern; } __Pyx_StringTabEntry; - -#define __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 0 -#define __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 0 -#define __PYX_DEFAULT_STRING_ENCODING "" -#define __Pyx_PyObject_FromString __Pyx_PyBytes_FromString -#define __Pyx_PyObject_FromStringAndSize __Pyx_PyBytes_FromStringAndSize -#define __Pyx_uchar_cast(c) ((unsigned char)c) -#define __Pyx_long_cast(x) ((long)x) -#define __Pyx_fits_Py_ssize_t(v, type, is_signed) (\ - (sizeof(type) < sizeof(Py_ssize_t)) ||\ - (sizeof(type) > sizeof(Py_ssize_t) &&\ - likely(v < (type)PY_SSIZE_T_MAX ||\ - v == (type)PY_SSIZE_T_MAX) &&\ - (!is_signed || likely(v > (type)PY_SSIZE_T_MIN ||\ - v == (type)PY_SSIZE_T_MIN))) ||\ - (sizeof(type) == sizeof(Py_ssize_t) &&\ - (is_signed || likely(v < (type)PY_SSIZE_T_MAX ||\ - v == (type)PY_SSIZE_T_MAX))) ) -#if defined (__cplusplus) && __cplusplus >= 201103L - #include - #define __Pyx_sst_abs(value) std::abs(value) -#elif SIZEOF_INT >= SIZEOF_SIZE_T - #define __Pyx_sst_abs(value) abs(value) -#elif SIZEOF_LONG >= SIZEOF_SIZE_T - #define __Pyx_sst_abs(value) labs(value) -#elif defined (_MSC_VER) && defined (_M_X64) - #define __Pyx_sst_abs(value) _abs64(value) -#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L - #define __Pyx_sst_abs(value) llabs(value) -#elif defined (__GNUC__) - #define __Pyx_sst_abs(value) __builtin_llabs(value) -#else - #define __Pyx_sst_abs(value) ((value<0) ? -value : value) -#endif -static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject*); -static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject*, Py_ssize_t* length); -#define __Pyx_PyByteArray_FromString(s) PyByteArray_FromStringAndSize((const char*)s, strlen((const char*)s)) -#define __Pyx_PyByteArray_FromStringAndSize(s, l) PyByteArray_FromStringAndSize((const char*)s, l) -#define __Pyx_PyBytes_FromString PyBytes_FromString -#define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize -static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char*); -#if PY_MAJOR_VERSION < 3 - #define __Pyx_PyStr_FromString __Pyx_PyBytes_FromString - #define __Pyx_PyStr_FromStringAndSize __Pyx_PyBytes_FromStringAndSize -#else - #define __Pyx_PyStr_FromString __Pyx_PyUnicode_FromString - #define __Pyx_PyStr_FromStringAndSize __Pyx_PyUnicode_FromStringAndSize -#endif -#define __Pyx_PyObject_AsSString(s) ((signed char*) __Pyx_PyObject_AsString(s)) -#define __Pyx_PyObject_AsUString(s) ((unsigned char*) __Pyx_PyObject_AsString(s)) -#define __Pyx_PyObject_FromCString(s) __Pyx_PyObject_FromString((const char*)s) -#define __Pyx_PyBytes_FromCString(s) __Pyx_PyBytes_FromString((const char*)s) -#define __Pyx_PyByteArray_FromCString(s) __Pyx_PyByteArray_FromString((const char*)s) -#define __Pyx_PyStr_FromCString(s) __Pyx_PyStr_FromString((const char*)s) -#define __Pyx_PyUnicode_FromCString(s) __Pyx_PyUnicode_FromString((const char*)s) -#if PY_MAJOR_VERSION < 3 -static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u) -{ - const Py_UNICODE *u_end = u; - while (*u_end++) ; - return (size_t)(u_end - u - 1); -} -#else -#define __Pyx_Py_UNICODE_strlen Py_UNICODE_strlen -#endif -#define __Pyx_PyUnicode_FromUnicode(u) PyUnicode_FromUnicode(u, __Pyx_Py_UNICODE_strlen(u)) -#define __Pyx_PyUnicode_FromUnicodeAndLength PyUnicode_FromUnicode -#define __Pyx_PyUnicode_AsUnicode PyUnicode_AsUnicode -#define __Pyx_NewRef(obj) (Py_INCREF(obj), obj) -#define __Pyx_Owned_Py_None(b) __Pyx_NewRef(Py_None) -#define __Pyx_PyBool_FromLong(b) ((b) ? __Pyx_NewRef(Py_True) : __Pyx_NewRef(Py_False)) -static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*); -static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x); -static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*); -static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t); -#if CYTHON_COMPILING_IN_CPYTHON -#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x)) -#else -#define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x) -#endif -#define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x)) -#if PY_MAJOR_VERSION >= 3 -#define __Pyx_PyNumber_Int(x) (PyLong_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Long(x)) -#else -#define __Pyx_PyNumber_Int(x) (PyInt_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Int(x)) -#endif -#define __Pyx_PyNumber_Float(x) (PyFloat_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Float(x)) -#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII -static int __Pyx_sys_getdefaultencoding_not_ascii; -static int __Pyx_init_sys_getdefaultencoding_params(void) { - PyObject* sys; - PyObject* default_encoding = NULL; - PyObject* ascii_chars_u = NULL; - PyObject* ascii_chars_b = NULL; - const char* default_encoding_c; - sys = PyImport_ImportModule("sys"); - if (!sys) goto bad; - default_encoding = PyObject_CallMethod(sys, (char*) "getdefaultencoding", NULL); - Py_DECREF(sys); - if (!default_encoding) goto bad; - default_encoding_c = PyBytes_AsString(default_encoding); - if (!default_encoding_c) goto bad; - if (strcmp(default_encoding_c, "ascii") == 0) { - __Pyx_sys_getdefaultencoding_not_ascii = 0; - } else { - char ascii_chars[128]; - int c; - for (c = 0; c < 128; c++) { - ascii_chars[c] = c; - } - __Pyx_sys_getdefaultencoding_not_ascii = 1; - ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL); - if (!ascii_chars_u) goto bad; - ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL); - if (!ascii_chars_b || !PyBytes_Check(ascii_chars_b) || memcmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) { - PyErr_Format( - PyExc_ValueError, - "This module compiled with c_string_encoding=ascii, but default encoding '%.200s' is not a superset of ascii.", - default_encoding_c); - goto bad; - } - Py_DECREF(ascii_chars_u); - Py_DECREF(ascii_chars_b); - } - Py_DECREF(default_encoding); - return 0; -bad: - Py_XDECREF(default_encoding); - Py_XDECREF(ascii_chars_u); - Py_XDECREF(ascii_chars_b); - return -1; -} -#endif -#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT && PY_MAJOR_VERSION >= 3 -#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL) -#else -#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL) -#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT -static char* __PYX_DEFAULT_STRING_ENCODING; -static int __Pyx_init_sys_getdefaultencoding_params(void) { - PyObject* sys; - PyObject* default_encoding = NULL; - char* default_encoding_c; - sys = PyImport_ImportModule("sys"); - if (!sys) goto bad; - default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL); - Py_DECREF(sys); - if (!default_encoding) goto bad; - default_encoding_c = PyBytes_AsString(default_encoding); - if (!default_encoding_c) goto bad; - __PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c)); - if (!__PYX_DEFAULT_STRING_ENCODING) goto bad; - strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c); - Py_DECREF(default_encoding); - return 0; -bad: - Py_XDECREF(default_encoding); - return -1; -} -#endif -#endif - - -/* Test for GCC > 2.95 */ -#if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))) - #define likely(x) __builtin_expect(!!(x), 1) - #define unlikely(x) __builtin_expect(!!(x), 0) -#else /* !__GNUC__ or GCC < 2.95 */ - #define likely(x) (x) - #define unlikely(x) (x) -#endif /* __GNUC__ */ - -static PyObject *__pyx_m; -static PyObject *__pyx_d; -static PyObject *__pyx_b; -static PyObject *__pyx_empty_tuple; -static PyObject *__pyx_empty_bytes; -static PyObject *__pyx_empty_unicode; -static int __pyx_lineno; -static int __pyx_clineno = 0; -static const char * __pyx_cfilenm= __FILE__; -static const char *__pyx_filename; - - -static const char *__pyx_f[] = { - "gateway.pyx", -}; - -/*--- Type declarations ---*/ - -/* --- Runtime support code (head) --- */ -/* Refnanny.proto */ -#ifndef CYTHON_REFNANNY - #define CYTHON_REFNANNY 0 -#endif -#if CYTHON_REFNANNY - typedef struct { - void (*INCREF)(void*, PyObject*, int); - void (*DECREF)(void*, PyObject*, int); - void (*GOTREF)(void*, PyObject*, int); - void (*GIVEREF)(void*, PyObject*, int); - void* (*SetupContext)(const char*, int, const char*); - void (*FinishContext)(void**); - } __Pyx_RefNannyAPIStruct; - static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL; - static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname); - #define __Pyx_RefNannyDeclarations void *__pyx_refnanny = NULL; -#ifdef WITH_THREAD - #define __Pyx_RefNannySetupContext(name, acquire_gil)\ - if (acquire_gil) {\ - PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();\ - __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__);\ - PyGILState_Release(__pyx_gilstate_save);\ - } else {\ - __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__);\ - } -#else - #define __Pyx_RefNannySetupContext(name, acquire_gil)\ - __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__) -#endif - #define __Pyx_RefNannyFinishContext()\ - __Pyx_RefNanny->FinishContext(&__pyx_refnanny) - #define __Pyx_INCREF(r) __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__) - #define __Pyx_DECREF(r) __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__) - #define __Pyx_GOTREF(r) __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__) - #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__) - #define __Pyx_XINCREF(r) do { if((r) != NULL) {__Pyx_INCREF(r); }} while(0) - #define __Pyx_XDECREF(r) do { if((r) != NULL) {__Pyx_DECREF(r); }} while(0) - #define __Pyx_XGOTREF(r) do { if((r) != NULL) {__Pyx_GOTREF(r); }} while(0) - #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);}} while(0) -#else - #define __Pyx_RefNannyDeclarations - #define __Pyx_RefNannySetupContext(name, acquire_gil) - #define __Pyx_RefNannyFinishContext() - #define __Pyx_INCREF(r) Py_INCREF(r) - #define __Pyx_DECREF(r) Py_DECREF(r) - #define __Pyx_GOTREF(r) - #define __Pyx_GIVEREF(r) - #define __Pyx_XINCREF(r) Py_XINCREF(r) - #define __Pyx_XDECREF(r) Py_XDECREF(r) - #define __Pyx_XGOTREF(r) - #define __Pyx_XGIVEREF(r) -#endif -#define __Pyx_XDECREF_SET(r, v) do {\ - PyObject *tmp = (PyObject *) r;\ - r = v; __Pyx_XDECREF(tmp);\ - } while (0) -#define __Pyx_DECREF_SET(r, v) do {\ - PyObject *tmp = (PyObject *) r;\ - r = v; __Pyx_DECREF(tmp);\ - } while (0) -#define __Pyx_CLEAR(r) do { PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);} while(0) -#define __Pyx_XCLEAR(r) do { if((r) != NULL) {PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);}} while(0) - -/* RaiseArgTupleInvalid.proto */ -static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact, - Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); - -/* RaiseDoubleKeywords.proto */ -static void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); - -/* ParseKeywords.proto */ -static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[],\ - PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args,\ - const char* function_name); - -/* PyObjectGetAttrStr.proto */ -#if CYTHON_COMPILING_IN_CPYTHON -static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name) { - PyTypeObject* tp = Py_TYPE(obj); - if (likely(tp->tp_getattro)) - return tp->tp_getattro(obj, attr_name); -#if PY_MAJOR_VERSION < 3 - if (likely(tp->tp_getattr)) - return tp->tp_getattr(obj, PyString_AS_STRING(attr_name)); -#endif - return PyObject_GetAttr(obj, attr_name); -} -#else -#define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n) -#endif - -/* PyObjectCall.proto */ -#if CYTHON_COMPILING_IN_CPYTHON -static CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw); -#else -#define __Pyx_PyObject_Call(func, arg, kw) PyObject_Call(func, arg, kw) -#endif - -/* IncludeStringH.proto */ -#include - -/* decode_c_string.proto */ -static CYTHON_INLINE PyObject* __Pyx_decode_c_string( - const char* cstring, Py_ssize_t start, Py_ssize_t stop, - const char* encoding, const char* errors, - PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors)); - -/* KeywordStringCheck.proto */ -static CYTHON_INLINE int __Pyx_CheckKeywordStrings(PyObject *kwdict, const char* function_name, int kw_allowed); - -/* GetBuiltinName.proto */ -static PyObject *__Pyx_GetBuiltinName(PyObject *name); - -/* GetModuleGlobalName.proto */ -static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name); - -/* PyObjectCallMethO.proto */ -#if CYTHON_COMPILING_IN_CPYTHON -static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject *arg); -#endif - -/* PyObjectCallOneArg.proto */ -static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg); - -/* PyObjectCallNoArg.proto */ -#if CYTHON_COMPILING_IN_CPYTHON -static CYTHON_INLINE PyObject* __Pyx_PyObject_CallNoArg(PyObject *func); -#else -#define __Pyx_PyObject_CallNoArg(func) __Pyx_PyObject_Call(func, __pyx_empty_tuple, NULL) -#endif - -/* GetItemInt.proto */ -#define __Pyx_GetItemInt(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck)\ - (__Pyx_fits_Py_ssize_t(i, type, is_signed) ?\ - __Pyx_GetItemInt_Fast(o, (Py_ssize_t)i, is_list, wraparound, boundscheck) :\ - (is_list ? (PyErr_SetString(PyExc_IndexError, "list index out of range"), (PyObject*)NULL) :\ - __Pyx_GetItemInt_Generic(o, to_py_func(i)))) -#define __Pyx_GetItemInt_List(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck)\ - (__Pyx_fits_Py_ssize_t(i, type, is_signed) ?\ - __Pyx_GetItemInt_List_Fast(o, (Py_ssize_t)i, wraparound, boundscheck) :\ - (PyErr_SetString(PyExc_IndexError, "list index out of range"), (PyObject*)NULL)) -static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i, - int wraparound, int boundscheck); -#define __Pyx_GetItemInt_Tuple(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck)\ - (__Pyx_fits_Py_ssize_t(i, type, is_signed) ?\ - __Pyx_GetItemInt_Tuple_Fast(o, (Py_ssize_t)i, wraparound, boundscheck) :\ - (PyErr_SetString(PyExc_IndexError, "tuple index out of range"), (PyObject*)NULL)) -static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i, - int wraparound, int boundscheck); -static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j); -static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i, - int is_list, int wraparound, int boundscheck); - -/* Import.proto */ -static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level); - -/* ImportFrom.proto */ -static PyObject* __Pyx_ImportFrom(PyObject* module, PyObject* name); - -/* FetchCommonType.proto */ -static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type); - -/* CythonFunction.proto */ -#define __Pyx_CyFunction_USED 1 -#include -#define __Pyx_CYFUNCTION_STATICMETHOD 0x01 -#define __Pyx_CYFUNCTION_CLASSMETHOD 0x02 -#define __Pyx_CYFUNCTION_CCLASS 0x04 -#define __Pyx_CyFunction_GetClosure(f)\ - (((__pyx_CyFunctionObject *) (f))->func_closure) -#define __Pyx_CyFunction_GetClassObj(f)\ - (((__pyx_CyFunctionObject *) (f))->func_classobj) -#define __Pyx_CyFunction_Defaults(type, f)\ - ((type *)(((__pyx_CyFunctionObject *) (f))->defaults)) -#define __Pyx_CyFunction_SetDefaultsGetter(f, g)\ - ((__pyx_CyFunctionObject *) (f))->defaults_getter = (g) -typedef struct { - PyCFunctionObject func; -#if PY_VERSION_HEX < 0x030500A0 - PyObject *func_weakreflist; -#endif - PyObject *func_dict; - PyObject *func_name; - PyObject *func_qualname; - PyObject *func_doc; - PyObject *func_globals; - PyObject *func_code; - PyObject *func_closure; - PyObject *func_classobj; - void *defaults; - int defaults_pyobjects; - int flags; - PyObject *defaults_tuple; - PyObject *defaults_kwdict; - PyObject *(*defaults_getter)(PyObject *); - PyObject *func_annotations; -} __pyx_CyFunctionObject; -static PyTypeObject *__pyx_CyFunctionType = 0; -#define __Pyx_CyFunction_NewEx(ml, flags, qualname, self, module, globals, code)\ - __Pyx_CyFunction_New(__pyx_CyFunctionType, ml, flags, qualname, self, module, globals, code) -static PyObject *__Pyx_CyFunction_New(PyTypeObject *, PyMethodDef *ml, - int flags, PyObject* qualname, - PyObject *self, - PyObject *module, PyObject *globals, - PyObject* code); -static CYTHON_INLINE void *__Pyx_CyFunction_InitDefaults(PyObject *m, - size_t size, - int pyobjects); -static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsTuple(PyObject *m, - PyObject *tuple); -static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsKwDict(PyObject *m, - PyObject *dict); -static CYTHON_INLINE void __Pyx_CyFunction_SetAnnotationsDict(PyObject *m, - PyObject *dict); -static int __pyx_CyFunction_init(void); - -/* CalculateMetaclass.proto */ -static PyObject *__Pyx_CalculateMetaclass(PyTypeObject *metaclass, PyObject *bases); - -/* Py3ClassCreate.proto */ -static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name, PyObject *qualname, - PyObject *mkw, PyObject *modname, PyObject *doc); -static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases, PyObject *dict, - PyObject *mkw, int calculate_metaclass, int allow_py2_metaclass); - -/* CodeObjectCache.proto */ -typedef struct { - PyCodeObject* code_object; - int code_line; -} __Pyx_CodeObjectCacheEntry; -struct __Pyx_CodeObjectCache { - int count; - int max_count; - __Pyx_CodeObjectCacheEntry* entries; -}; -static struct __Pyx_CodeObjectCache __pyx_code_cache = {0,0,NULL}; -static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line); -static PyCodeObject *__pyx_find_code_object(int code_line); -static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object); - -/* AddTraceback.proto */ -static void __Pyx_AddTraceback(const char *funcname, int c_line, - int py_line, const char *filename); - -/* CIntToPy.proto */ -static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value); - -/* CIntFromPy.proto */ -static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *); - -/* CIntFromPy.proto */ -static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *); - -/* CheckBinaryVersion.proto */ -static int __Pyx_check_binary_version(void); - -/* InitStrings.proto */ -static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); - - -/* Module declarations from 'gateway' */ -#define __Pyx_MODULE_NAME "gateway" -int __pyx_module_is_main_gateway = 0; - -/* Implementation of 'gateway' */ -static const char __pyx_k_0_1[] = "{0} {1}"; -static const char __pyx_k_doc[] = "__doc__"; -static const char __pyx_k_key[] = "key"; -static const char __pyx_k_log[] = "log"; -static const char __pyx_k_msg[] = "msg"; -static const char __pyx_k_sys[] = "sys"; -static const char __pyx_k_ttl[] = "ttl"; -static const char __pyx_k_args[] = "args"; -static const char __pyx_k_excp[] = "excp"; -static const char __pyx_k_main[] = "__main__"; -static const char __pyx_k_test[] = "__test__"; -static const char __pyx_k_0_1_2[] = "{0} {1} {2}"; -static const char __pyx_k_error[] = "error"; -static const char __pyx_k_level[] = "level"; -static const char __pyx_k_utf_8[] = "utf-8"; -static const char __pyx_k_value[] = "value"; -static const char __pyx_k_encode[] = "encode"; -static const char __pyx_k_format[] = "format"; -static const char __pyx_k_import[] = "__import__"; -static const char __pyx_k_module[] = "__module__"; -static const char __pyx_k_output[] = "output"; -static const char __pyx_k_gateway[] = "gateway"; -static const char __pyx_k_payload[] = "payload"; -static const char __pyx_k_prepare[] = "__prepare__"; -static const char __pyx_k_exc_info[] = "exc_info"; -static const char __pyx_k_get_data[] = "get_data"; -static const char __pyx_k_qualname[] = "__qualname__"; -static const char __pyx_k_log_error[] = "log_error"; -static const char __pyx_k_metaclass[] = "__metaclass__"; -static const char __pyx_k_TykGateway[] = "TykGateway"; -static const char __pyx_k_event_name[] = "event_name"; -static const char __pyx_k_store_data[] = "store_data"; -static const char __pyx_k_trigger_event[] = "trigger_event"; -static const char __pyx_k_TykGateway_log[] = "TykGateway.log"; -static const char __pyx_k_TykGateway_get_data[] = "TykGateway.get_data"; -static const char __pyx_k_TykGateway_log_error[] = "TykGateway.log_error"; -static const char __pyx_k_TykGateway_store_data[] = "TykGateway.store_data"; -static const char __pyx_k_TykGateway_trigger_event[] = "TykGateway.trigger_event"; -static const char __pyx_k_Users_matias_dev_tyk_coprocess[] = "/Users/matias/dev/tyk/coprocess/python/tyk/gateway.pyx"; -static PyObject *__pyx_kp_s_0_1; -static PyObject *__pyx_kp_s_0_1_2; -static PyObject *__pyx_n_s_TykGateway; -static PyObject *__pyx_n_s_TykGateway_get_data; -static PyObject *__pyx_n_s_TykGateway_log; -static PyObject *__pyx_n_s_TykGateway_log_error; -static PyObject *__pyx_n_s_TykGateway_store_data; -static PyObject *__pyx_n_s_TykGateway_trigger_event; -static PyObject *__pyx_kp_s_Users_matias_dev_tyk_coprocess; -static PyObject *__pyx_n_s_args; -static PyObject *__pyx_n_s_doc; -static PyObject *__pyx_n_s_encode; -static PyObject *__pyx_n_s_error; -static PyObject *__pyx_n_s_event_name; -static PyObject *__pyx_n_s_exc_info; -static PyObject *__pyx_n_s_excp; -static PyObject *__pyx_n_s_format; -static PyObject *__pyx_n_s_gateway; -static PyObject *__pyx_n_s_get_data; -static PyObject *__pyx_n_s_import; -static PyObject *__pyx_n_s_key; -static PyObject *__pyx_n_s_level; -static PyObject *__pyx_n_s_log; -static PyObject *__pyx_n_s_log_error; -static PyObject *__pyx_n_s_main; -static PyObject *__pyx_n_s_metaclass; -static PyObject *__pyx_n_s_module; -static PyObject *__pyx_n_s_msg; -static PyObject *__pyx_n_s_output; -static PyObject *__pyx_n_s_payload; -static PyObject *__pyx_n_s_prepare; -static PyObject *__pyx_n_s_qualname; -static PyObject *__pyx_n_s_store_data; -static PyObject *__pyx_n_s_sys; -static PyObject *__pyx_n_s_test; -static PyObject *__pyx_n_s_trigger_event; -static PyObject *__pyx_n_s_ttl; -static PyObject *__pyx_kp_s_utf_8; -static PyObject *__pyx_n_s_value; -static PyObject *__pyx_pf_7gateway_10TykGateway_store_data(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_key, PyObject *__pyx_v_value, PyObject *__pyx_v_ttl); /* proto */ -static PyObject *__pyx_pf_7gateway_10TykGateway_2get_data(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_key); /* proto */ -static PyObject *__pyx_pf_7gateway_10TykGateway_4trigger_event(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_event_name, PyObject *__pyx_v_payload); /* proto */ -static PyObject *__pyx_pf_7gateway_10TykGateway_6log(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_msg, PyObject *__pyx_v_level); /* proto */ -static PyObject *__pyx_pf_7gateway_10TykGateway_8log_error(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_args); /* proto */ -static PyObject *__pyx_tuple_; -static PyObject *__pyx_tuple__2; -static PyObject *__pyx_tuple__3; -static PyObject *__pyx_tuple__4; -static PyObject *__pyx_tuple__5; -static PyObject *__pyx_tuple__6; -static PyObject *__pyx_tuple__7; -static PyObject *__pyx_tuple__8; -static PyObject *__pyx_tuple__10; -static PyObject *__pyx_tuple__12; -static PyObject *__pyx_tuple__14; -static PyObject *__pyx_tuple__16; -static PyObject *__pyx_codeobj__9; -static PyObject *__pyx_codeobj__11; -static PyObject *__pyx_codeobj__13; -static PyObject *__pyx_codeobj__15; -static PyObject *__pyx_codeobj__17; - -/* "gateway.pyx":13 - * - * class TykGateway: - * def store_data(key, value, ttl): # <<<<<<<<<<<<<< - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - * def get_data(key): - */ - -/* Python wrapper */ -static PyObject *__pyx_pw_7gateway_10TykGateway_1store_data(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ -static PyMethodDef __pyx_mdef_7gateway_10TykGateway_1store_data = {"store_data", (PyCFunction)__pyx_pw_7gateway_10TykGateway_1store_data, METH_VARARGS|METH_KEYWORDS, 0}; -static PyObject *__pyx_pw_7gateway_10TykGateway_1store_data(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { - PyObject *__pyx_v_key = 0; - PyObject *__pyx_v_value = 0; - PyObject *__pyx_v_ttl = 0; - PyObject *__pyx_r = 0; - __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("store_data (wrapper)", 0); - { - static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_key,&__pyx_n_s_value,&__pyx_n_s_ttl,0}; - PyObject* values[3] = {0,0,0}; - if (unlikely(__pyx_kwds)) { - Py_ssize_t kw_args; - const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args); - switch (pos_args) { - case 3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2); - case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); - case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); - case 0: break; - default: goto __pyx_L5_argtuple_error; - } - kw_args = PyDict_Size(__pyx_kwds); - switch (pos_args) { - case 0: - if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_key)) != 0)) kw_args--; - else goto __pyx_L5_argtuple_error; - case 1: - if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_value)) != 0)) kw_args--; - else { - __Pyx_RaiseArgtupleInvalid("store_data", 1, 3, 3, 1); __PYX_ERR(0, 13, __pyx_L3_error) - } - case 2: - if (likely((values[2] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_ttl)) != 0)) kw_args--; - else { - __Pyx_RaiseArgtupleInvalid("store_data", 1, 3, 3, 2); __PYX_ERR(0, 13, __pyx_L3_error) - } - } - if (unlikely(kw_args > 0)) { - if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "store_data") < 0)) __PYX_ERR(0, 13, __pyx_L3_error) - } - } else if (PyTuple_GET_SIZE(__pyx_args) != 3) { - goto __pyx_L5_argtuple_error; - } else { - values[0] = PyTuple_GET_ITEM(__pyx_args, 0); - values[1] = PyTuple_GET_ITEM(__pyx_args, 1); - values[2] = PyTuple_GET_ITEM(__pyx_args, 2); - } - __pyx_v_key = values[0]; - __pyx_v_value = values[1]; - __pyx_v_ttl = values[2]; - } - goto __pyx_L4_argument_unpacking_done; - __pyx_L5_argtuple_error:; - __Pyx_RaiseArgtupleInvalid("store_data", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 13, __pyx_L3_error) - __pyx_L3_error:; - __Pyx_AddTraceback("gateway.TykGateway.store_data", __pyx_clineno, __pyx_lineno, __pyx_filename); - __Pyx_RefNannyFinishContext(); - return NULL; - __pyx_L4_argument_unpacking_done:; - __pyx_r = __pyx_pf_7gateway_10TykGateway_store_data(__pyx_self, __pyx_v_key, __pyx_v_value, __pyx_v_ttl); - - /* function exit code */ - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -static PyObject *__pyx_pf_7gateway_10TykGateway_store_data(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_key, PyObject *__pyx_v_value, PyObject *__pyx_v_ttl) { - PyObject *__pyx_r = NULL; - __Pyx_RefNannyDeclarations - PyObject *__pyx_t_1 = NULL; - PyObject *__pyx_t_2 = NULL; - char *__pyx_t_3; - PyObject *__pyx_t_4 = NULL; - char *__pyx_t_5; - int __pyx_t_6; - __Pyx_RefNannySetupContext("store_data", 0); - - /* "gateway.pyx":14 - * class TykGateway: - * def store_data(key, value, ttl): - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) # <<<<<<<<<<<<<< - * def get_data(key): - * output = TykGetData(key.encode('utf-8')) - */ - __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_key, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 14, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple_, NULL); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 14, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_2); - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __pyx_t_3 = __Pyx_PyObject_AsString(__pyx_t_2); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) __PYX_ERR(0, 14, __pyx_L1_error) - __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_value, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 14, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__2, NULL); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 14, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_4); - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_t_4); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) __PYX_ERR(0, 14, __pyx_L1_error) - __pyx_t_6 = __Pyx_PyInt_As_int(__pyx_v_ttl); if (unlikely((__pyx_t_6 == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 14, __pyx_L1_error) - TykStoreData(__pyx_t_3, __pyx_t_5, __pyx_t_6); - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; - - /* "gateway.pyx":13 - * - * class TykGateway: - * def store_data(key, value, ttl): # <<<<<<<<<<<<<< - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - * def get_data(key): - */ - - /* function exit code */ - __pyx_r = Py_None; __Pyx_INCREF(Py_None); - goto __pyx_L0; - __pyx_L1_error:; - __Pyx_XDECREF(__pyx_t_1); - __Pyx_XDECREF(__pyx_t_2); - __Pyx_XDECREF(__pyx_t_4); - __Pyx_AddTraceback("gateway.TykGateway.store_data", __pyx_clineno, __pyx_lineno, __pyx_filename); - __pyx_r = NULL; - __pyx_L0:; - __Pyx_XGIVEREF(__pyx_r); - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -/* "gateway.pyx":15 - * def store_data(key, value, ttl): - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - * def get_data(key): # <<<<<<<<<<<<<< - * output = TykGetData(key.encode('utf-8')) - * return output.decode('utf-8') - */ - -/* Python wrapper */ -static PyObject *__pyx_pw_7gateway_10TykGateway_3get_data(PyObject *__pyx_self, PyObject *__pyx_v_key); /*proto*/ -static PyMethodDef __pyx_mdef_7gateway_10TykGateway_3get_data = {"get_data", (PyCFunction)__pyx_pw_7gateway_10TykGateway_3get_data, METH_O, 0}; -static PyObject *__pyx_pw_7gateway_10TykGateway_3get_data(PyObject *__pyx_self, PyObject *__pyx_v_key) { - PyObject *__pyx_r = 0; - __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("get_data (wrapper)", 0); - __pyx_r = __pyx_pf_7gateway_10TykGateway_2get_data(__pyx_self, ((PyObject *)__pyx_v_key)); - - /* function exit code */ - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -static PyObject *__pyx_pf_7gateway_10TykGateway_2get_data(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_key) { - char *__pyx_v_output; - PyObject *__pyx_r = NULL; - __Pyx_RefNannyDeclarations - PyObject *__pyx_t_1 = NULL; - PyObject *__pyx_t_2 = NULL; - char *__pyx_t_3; - __Pyx_RefNannySetupContext("get_data", 0); - - /* "gateway.pyx":16 - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - * def get_data(key): - * output = TykGetData(key.encode('utf-8')) # <<<<<<<<<<<<<< - * return output.decode('utf-8') - * def trigger_event(event_name, payload): - */ - __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_key, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 16, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__3, NULL); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 16, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_2); - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __pyx_t_3 = __Pyx_PyObject_AsString(__pyx_t_2); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) __PYX_ERR(0, 16, __pyx_L1_error) - __pyx_v_output = TykGetData(__pyx_t_3); - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - - /* "gateway.pyx":17 - * def get_data(key): - * output = TykGetData(key.encode('utf-8')) - * return output.decode('utf-8') # <<<<<<<<<<<<<< - * def trigger_event(event_name, payload): - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) - */ - __Pyx_XDECREF(__pyx_r); - __pyx_t_2 = __Pyx_decode_c_string(__pyx_v_output, 0, strlen(__pyx_v_output), NULL, NULL, PyUnicode_DecodeUTF8); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 17, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_2); - __pyx_r = __pyx_t_2; - __pyx_t_2 = 0; - goto __pyx_L0; - - /* "gateway.pyx":15 - * def store_data(key, value, ttl): - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - * def get_data(key): # <<<<<<<<<<<<<< - * output = TykGetData(key.encode('utf-8')) - * return output.decode('utf-8') - */ - - /* function exit code */ - __pyx_L1_error:; - __Pyx_XDECREF(__pyx_t_1); - __Pyx_XDECREF(__pyx_t_2); - __Pyx_AddTraceback("gateway.TykGateway.get_data", __pyx_clineno, __pyx_lineno, __pyx_filename); - __pyx_r = NULL; - __pyx_L0:; - __Pyx_XGIVEREF(__pyx_r); - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -/* "gateway.pyx":18 - * output = TykGetData(key.encode('utf-8')) - * return output.decode('utf-8') - * def trigger_event(event_name, payload): # <<<<<<<<<<<<<< - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) - * def log(msg, level): - */ - -/* Python wrapper */ -static PyObject *__pyx_pw_7gateway_10TykGateway_5trigger_event(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ -static PyMethodDef __pyx_mdef_7gateway_10TykGateway_5trigger_event = {"trigger_event", (PyCFunction)__pyx_pw_7gateway_10TykGateway_5trigger_event, METH_VARARGS|METH_KEYWORDS, 0}; -static PyObject *__pyx_pw_7gateway_10TykGateway_5trigger_event(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { - PyObject *__pyx_v_event_name = 0; - PyObject *__pyx_v_payload = 0; - PyObject *__pyx_r = 0; - __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("trigger_event (wrapper)", 0); - { - static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_event_name,&__pyx_n_s_payload,0}; - PyObject* values[2] = {0,0}; - if (unlikely(__pyx_kwds)) { - Py_ssize_t kw_args; - const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args); - switch (pos_args) { - case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); - case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); - case 0: break; - default: goto __pyx_L5_argtuple_error; - } - kw_args = PyDict_Size(__pyx_kwds); - switch (pos_args) { - case 0: - if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_event_name)) != 0)) kw_args--; - else goto __pyx_L5_argtuple_error; - case 1: - if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_payload)) != 0)) kw_args--; - else { - __Pyx_RaiseArgtupleInvalid("trigger_event", 1, 2, 2, 1); __PYX_ERR(0, 18, __pyx_L3_error) - } - } - if (unlikely(kw_args > 0)) { - if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "trigger_event") < 0)) __PYX_ERR(0, 18, __pyx_L3_error) - } - } else if (PyTuple_GET_SIZE(__pyx_args) != 2) { - goto __pyx_L5_argtuple_error; - } else { - values[0] = PyTuple_GET_ITEM(__pyx_args, 0); - values[1] = PyTuple_GET_ITEM(__pyx_args, 1); - } - __pyx_v_event_name = values[0]; - __pyx_v_payload = values[1]; - } - goto __pyx_L4_argument_unpacking_done; - __pyx_L5_argtuple_error:; - __Pyx_RaiseArgtupleInvalid("trigger_event", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 18, __pyx_L3_error) - __pyx_L3_error:; - __Pyx_AddTraceback("gateway.TykGateway.trigger_event", __pyx_clineno, __pyx_lineno, __pyx_filename); - __Pyx_RefNannyFinishContext(); - return NULL; - __pyx_L4_argument_unpacking_done:; - __pyx_r = __pyx_pf_7gateway_10TykGateway_4trigger_event(__pyx_self, __pyx_v_event_name, __pyx_v_payload); - - /* function exit code */ - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -static PyObject *__pyx_pf_7gateway_10TykGateway_4trigger_event(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_event_name, PyObject *__pyx_v_payload) { - PyObject *__pyx_r = NULL; - __Pyx_RefNannyDeclarations - PyObject *__pyx_t_1 = NULL; - PyObject *__pyx_t_2 = NULL; - char *__pyx_t_3; - PyObject *__pyx_t_4 = NULL; - char *__pyx_t_5; - __Pyx_RefNannySetupContext("trigger_event", 0); - - /* "gateway.pyx":19 - * return output.decode('utf-8') - * def trigger_event(event_name, payload): - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) # <<<<<<<<<<<<<< - * def log(msg, level): - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) - */ - __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_event_name, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 19, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__4, NULL); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 19, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_2); - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __pyx_t_3 = __Pyx_PyObject_AsString(__pyx_t_2); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) __PYX_ERR(0, 19, __pyx_L1_error) - __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_payload, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 19, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 19, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_4); - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_t_4); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) __PYX_ERR(0, 19, __pyx_L1_error) - TykTriggerEvent(__pyx_t_3, __pyx_t_5); - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; - - /* "gateway.pyx":18 - * output = TykGetData(key.encode('utf-8')) - * return output.decode('utf-8') - * def trigger_event(event_name, payload): # <<<<<<<<<<<<<< - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) - * def log(msg, level): - */ - - /* function exit code */ - __pyx_r = Py_None; __Pyx_INCREF(Py_None); - goto __pyx_L0; - __pyx_L1_error:; - __Pyx_XDECREF(__pyx_t_1); - __Pyx_XDECREF(__pyx_t_2); - __Pyx_XDECREF(__pyx_t_4); - __Pyx_AddTraceback("gateway.TykGateway.trigger_event", __pyx_clineno, __pyx_lineno, __pyx_filename); - __pyx_r = NULL; - __pyx_L0:; - __Pyx_XGIVEREF(__pyx_r); - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -/* "gateway.pyx":20 - * def trigger_event(event_name, payload): - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) - * def log(msg, level): # <<<<<<<<<<<<<< - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) - * def log_error(*args): - */ - -/* Python wrapper */ -static PyObject *__pyx_pw_7gateway_10TykGateway_7log(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ -static PyMethodDef __pyx_mdef_7gateway_10TykGateway_7log = {"log", (PyCFunction)__pyx_pw_7gateway_10TykGateway_7log, METH_VARARGS|METH_KEYWORDS, 0}; -static PyObject *__pyx_pw_7gateway_10TykGateway_7log(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { - PyObject *__pyx_v_msg = 0; - PyObject *__pyx_v_level = 0; - PyObject *__pyx_r = 0; - __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("log (wrapper)", 0); - { - static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_msg,&__pyx_n_s_level,0}; - PyObject* values[2] = {0,0}; - if (unlikely(__pyx_kwds)) { - Py_ssize_t kw_args; - const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args); - switch (pos_args) { - case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); - case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); - case 0: break; - default: goto __pyx_L5_argtuple_error; - } - kw_args = PyDict_Size(__pyx_kwds); - switch (pos_args) { - case 0: - if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_msg)) != 0)) kw_args--; - else goto __pyx_L5_argtuple_error; - case 1: - if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_level)) != 0)) kw_args--; - else { - __Pyx_RaiseArgtupleInvalid("log", 1, 2, 2, 1); __PYX_ERR(0, 20, __pyx_L3_error) - } - } - if (unlikely(kw_args > 0)) { - if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "log") < 0)) __PYX_ERR(0, 20, __pyx_L3_error) - } - } else if (PyTuple_GET_SIZE(__pyx_args) != 2) { - goto __pyx_L5_argtuple_error; - } else { - values[0] = PyTuple_GET_ITEM(__pyx_args, 0); - values[1] = PyTuple_GET_ITEM(__pyx_args, 1); - } - __pyx_v_msg = values[0]; - __pyx_v_level = values[1]; - } - goto __pyx_L4_argument_unpacking_done; - __pyx_L5_argtuple_error:; - __Pyx_RaiseArgtupleInvalid("log", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 20, __pyx_L3_error) - __pyx_L3_error:; - __Pyx_AddTraceback("gateway.TykGateway.log", __pyx_clineno, __pyx_lineno, __pyx_filename); - __Pyx_RefNannyFinishContext(); - return NULL; - __pyx_L4_argument_unpacking_done:; - __pyx_r = __pyx_pf_7gateway_10TykGateway_6log(__pyx_self, __pyx_v_msg, __pyx_v_level); - - /* function exit code */ - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -static PyObject *__pyx_pf_7gateway_10TykGateway_6log(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_msg, PyObject *__pyx_v_level) { - PyObject *__pyx_r = NULL; - __Pyx_RefNannyDeclarations - PyObject *__pyx_t_1 = NULL; - PyObject *__pyx_t_2 = NULL; - char *__pyx_t_3; - PyObject *__pyx_t_4 = NULL; - char *__pyx_t_5; - __Pyx_RefNannySetupContext("log", 0); - - /* "gateway.pyx":21 - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) - * def log(msg, level): - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) # <<<<<<<<<<<<<< - * def log_error(*args): - * excp = exc_info() - */ - __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_msg, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 21, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__6, NULL); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 21, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_2); - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __pyx_t_3 = __Pyx_PyObject_AsString(__pyx_t_2); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) __PYX_ERR(0, 21, __pyx_L1_error) - __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_v_level, __pyx_n_s_encode); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 21, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - __pyx_t_4 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_tuple__7, NULL); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 21, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_4); - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_t_4); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) __PYX_ERR(0, 21, __pyx_L1_error) - CoProcessLog(__pyx_t_3, __pyx_t_5); - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; - - /* "gateway.pyx":20 - * def trigger_event(event_name, payload): - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) - * def log(msg, level): # <<<<<<<<<<<<<< - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) - * def log_error(*args): - */ - - /* function exit code */ - __pyx_r = Py_None; __Pyx_INCREF(Py_None); - goto __pyx_L0; - __pyx_L1_error:; - __Pyx_XDECREF(__pyx_t_1); - __Pyx_XDECREF(__pyx_t_2); - __Pyx_XDECREF(__pyx_t_4); - __Pyx_AddTraceback("gateway.TykGateway.log", __pyx_clineno, __pyx_lineno, __pyx_filename); - __pyx_r = NULL; - __pyx_L0:; - __Pyx_XGIVEREF(__pyx_r); - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -/* "gateway.pyx":22 - * def log(msg, level): - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) - * def log_error(*args): # <<<<<<<<<<<<<< - * excp = exc_info() - * if len(args) == 0: - */ - -/* Python wrapper */ -static PyObject *__pyx_pw_7gateway_10TykGateway_9log_error(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ -static PyMethodDef __pyx_mdef_7gateway_10TykGateway_9log_error = {"log_error", (PyCFunction)__pyx_pw_7gateway_10TykGateway_9log_error, METH_VARARGS|METH_KEYWORDS, 0}; -static PyObject *__pyx_pw_7gateway_10TykGateway_9log_error(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { - PyObject *__pyx_v_args = 0; - PyObject *__pyx_r = 0; - __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("log_error (wrapper)", 0); - if (unlikely(__pyx_kwds) && unlikely(PyDict_Size(__pyx_kwds) > 0) && unlikely(!__Pyx_CheckKeywordStrings(__pyx_kwds, "log_error", 0))) return NULL; - __Pyx_INCREF(__pyx_args); - __pyx_v_args = __pyx_args; - __pyx_r = __pyx_pf_7gateway_10TykGateway_8log_error(__pyx_self, __pyx_v_args); - - /* function exit code */ - __Pyx_XDECREF(__pyx_v_args); - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -static PyObject *__pyx_pf_7gateway_10TykGateway_8log_error(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_args) { - PyObject *__pyx_v_excp = NULL; - PyObject *__pyx_r = NULL; - __Pyx_RefNannyDeclarations - PyObject *__pyx_t_1 = NULL; - PyObject *__pyx_t_2 = NULL; - PyObject *__pyx_t_3 = NULL; - Py_ssize_t __pyx_t_4; - int __pyx_t_5; - PyObject *__pyx_t_6 = NULL; - PyObject *__pyx_t_7 = NULL; - PyObject *__pyx_t_8 = NULL; - PyObject *__pyx_t_9 = NULL; - PyObject *__pyx_t_10 = NULL; - PyObject *__pyx_t_11 = NULL; - __Pyx_RefNannySetupContext("log_error", 0); - - /* "gateway.pyx":23 - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) - * def log_error(*args): - * excp = exc_info() # <<<<<<<<<<<<<< - * if len(args) == 0: - * TykGateway.log( "{0} {1}".format( excp[0], excp[1] ), "error" ) - */ - __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_exc_info); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 23, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_2); - __pyx_t_3 = NULL; - if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_2))) { - __pyx_t_3 = PyMethod_GET_SELF(__pyx_t_2); - if (likely(__pyx_t_3)) { - PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_2); - __Pyx_INCREF(__pyx_t_3); - __Pyx_INCREF(function); - __Pyx_DECREF_SET(__pyx_t_2, function); - } - } - if (__pyx_t_3) { - __pyx_t_1 = __Pyx_PyObject_CallOneArg(__pyx_t_2, __pyx_t_3); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 23, __pyx_L1_error) - __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; - } else { - __pyx_t_1 = __Pyx_PyObject_CallNoArg(__pyx_t_2); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 23, __pyx_L1_error) - } - __Pyx_GOTREF(__pyx_t_1); - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - __pyx_v_excp = __pyx_t_1; - __pyx_t_1 = 0; - - /* "gateway.pyx":24 - * def log_error(*args): - * excp = exc_info() - * if len(args) == 0: # <<<<<<<<<<<<<< - * TykGateway.log( "{0} {1}".format( excp[0], excp[1] ), "error" ) - * else: - */ - __pyx_t_4 = PyTuple_GET_SIZE(__pyx_v_args); if (unlikely(__pyx_t_4 == -1)) __PYX_ERR(0, 24, __pyx_L1_error) - __pyx_t_5 = ((__pyx_t_4 == 0) != 0); - if (__pyx_t_5) { - - /* "gateway.pyx":25 - * excp = exc_info() - * if len(args) == 0: - * TykGateway.log( "{0} {1}".format( excp[0], excp[1] ), "error" ) # <<<<<<<<<<<<<< - * else: - * TykGateway.log( "{0} {1} {2}".format( args[0], excp[0], excp[1] ), "error" ) - */ - __pyx_t_2 = __Pyx_GetModuleGlobalName(__pyx_n_s_TykGateway); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 25, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_2); - __pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_log); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 25, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_3); - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_kp_s_0_1, __pyx_n_s_format); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 25, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_6); - __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_excp, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 25, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_7); - __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_excp, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 25, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_8); - __pyx_t_9 = NULL; - __pyx_t_4 = 0; - if (CYTHON_COMPILING_IN_CPYTHON && likely(PyMethod_Check(__pyx_t_6))) { - __pyx_t_9 = PyMethod_GET_SELF(__pyx_t_6); - if (likely(__pyx_t_9)) { - PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_6); - __Pyx_INCREF(__pyx_t_9); - __Pyx_INCREF(function); - __Pyx_DECREF_SET(__pyx_t_6, function); - __pyx_t_4 = 1; - } - } - __pyx_t_10 = PyTuple_New(2+__pyx_t_4); if (unlikely(!__pyx_t_10)) __PYX_ERR(0, 25, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_10); - if (__pyx_t_9) { - __Pyx_GIVEREF(__pyx_t_9); PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_9); __pyx_t_9 = NULL; - } - __Pyx_GIVEREF(__pyx_t_7); - PyTuple_SET_ITEM(__pyx_t_10, 0+__pyx_t_4, __pyx_t_7); - __Pyx_GIVEREF(__pyx_t_8); - PyTuple_SET_ITEM(__pyx_t_10, 1+__pyx_t_4, __pyx_t_8); - __pyx_t_7 = 0; - __pyx_t_8 = 0; - __pyx_t_2 = __Pyx_PyObject_Call(__pyx_t_6, __pyx_t_10, NULL); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 25, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_2); - __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0; - __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; - __pyx_t_6 = NULL; - __pyx_t_4 = 0; - if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_3))) { - __pyx_t_6 = PyMethod_GET_SELF(__pyx_t_3); - if (likely(__pyx_t_6)) { - PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_3); - __Pyx_INCREF(__pyx_t_6); - __Pyx_INCREF(function); - __Pyx_DECREF_SET(__pyx_t_3, function); - __pyx_t_4 = 1; - } - } - __pyx_t_10 = PyTuple_New(2+__pyx_t_4); if (unlikely(!__pyx_t_10)) __PYX_ERR(0, 25, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_10); - if (__pyx_t_6) { - __Pyx_GIVEREF(__pyx_t_6); PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_6); __pyx_t_6 = NULL; - } - __Pyx_GIVEREF(__pyx_t_2); - PyTuple_SET_ITEM(__pyx_t_10, 0+__pyx_t_4, __pyx_t_2); - __Pyx_INCREF(__pyx_n_s_error); - __Pyx_GIVEREF(__pyx_n_s_error); - PyTuple_SET_ITEM(__pyx_t_10, 1+__pyx_t_4, __pyx_n_s_error); - __pyx_t_2 = 0; - __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_3, __pyx_t_10, NULL); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 25, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0; - __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - - /* "gateway.pyx":24 - * def log_error(*args): - * excp = exc_info() - * if len(args) == 0: # <<<<<<<<<<<<<< - * TykGateway.log( "{0} {1}".format( excp[0], excp[1] ), "error" ) - * else: - */ - goto __pyx_L3; - } - - /* "gateway.pyx":27 - * TykGateway.log( "{0} {1}".format( excp[0], excp[1] ), "error" ) - * else: - * TykGateway.log( "{0} {1} {2}".format( args[0], excp[0], excp[1] ), "error" ) # <<<<<<<<<<<<<< - */ - /*else*/ { - __pyx_t_3 = __Pyx_GetModuleGlobalName(__pyx_n_s_TykGateway); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 27, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_3); - __pyx_t_10 = __Pyx_PyObject_GetAttrStr(__pyx_t_3, __pyx_n_s_log); if (unlikely(!__pyx_t_10)) __PYX_ERR(0, 27, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_10); - __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; - __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_kp_s_0_1_2, __pyx_n_s_format); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 27, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_2); - __pyx_t_6 = __Pyx_GetItemInt_Tuple(__pyx_v_args, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 27, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_6); - __pyx_t_8 = __Pyx_GetItemInt(__pyx_v_excp, 0, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 27, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_8); - __pyx_t_7 = __Pyx_GetItemInt(__pyx_v_excp, 1, long, 1, __Pyx_PyInt_From_long, 0, 0, 1); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 27, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_7); - __pyx_t_9 = NULL; - __pyx_t_4 = 0; - if (CYTHON_COMPILING_IN_CPYTHON && likely(PyMethod_Check(__pyx_t_2))) { - __pyx_t_9 = PyMethod_GET_SELF(__pyx_t_2); - if (likely(__pyx_t_9)) { - PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_2); - __Pyx_INCREF(__pyx_t_9); - __Pyx_INCREF(function); - __Pyx_DECREF_SET(__pyx_t_2, function); - __pyx_t_4 = 1; - } - } - __pyx_t_11 = PyTuple_New(3+__pyx_t_4); if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 27, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_11); - if (__pyx_t_9) { - __Pyx_GIVEREF(__pyx_t_9); PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_t_9); __pyx_t_9 = NULL; - } - __Pyx_GIVEREF(__pyx_t_6); - PyTuple_SET_ITEM(__pyx_t_11, 0+__pyx_t_4, __pyx_t_6); - __Pyx_GIVEREF(__pyx_t_8); - PyTuple_SET_ITEM(__pyx_t_11, 1+__pyx_t_4, __pyx_t_8); - __Pyx_GIVEREF(__pyx_t_7); - PyTuple_SET_ITEM(__pyx_t_11, 2+__pyx_t_4, __pyx_t_7); - __pyx_t_6 = 0; - __pyx_t_8 = 0; - __pyx_t_7 = 0; - __pyx_t_3 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_11, NULL); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 27, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_3); - __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - __pyx_t_2 = NULL; - __pyx_t_4 = 0; - if (CYTHON_COMPILING_IN_CPYTHON && unlikely(PyMethod_Check(__pyx_t_10))) { - __pyx_t_2 = PyMethod_GET_SELF(__pyx_t_10); - if (likely(__pyx_t_2)) { - PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_10); - __Pyx_INCREF(__pyx_t_2); - __Pyx_INCREF(function); - __Pyx_DECREF_SET(__pyx_t_10, function); - __pyx_t_4 = 1; - } - } - __pyx_t_11 = PyTuple_New(2+__pyx_t_4); if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 27, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_11); - if (__pyx_t_2) { - __Pyx_GIVEREF(__pyx_t_2); PyTuple_SET_ITEM(__pyx_t_11, 0, __pyx_t_2); __pyx_t_2 = NULL; - } - __Pyx_GIVEREF(__pyx_t_3); - PyTuple_SET_ITEM(__pyx_t_11, 0+__pyx_t_4, __pyx_t_3); - __Pyx_INCREF(__pyx_n_s_error); - __Pyx_GIVEREF(__pyx_n_s_error); - PyTuple_SET_ITEM(__pyx_t_11, 1+__pyx_t_4, __pyx_n_s_error); - __pyx_t_3 = 0; - __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_10, __pyx_t_11, NULL); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 27, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; - __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0; - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - } - __pyx_L3:; - - /* "gateway.pyx":22 - * def log(msg, level): - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) - * def log_error(*args): # <<<<<<<<<<<<<< - * excp = exc_info() - * if len(args) == 0: - */ - - /* function exit code */ - __pyx_r = Py_None; __Pyx_INCREF(Py_None); - goto __pyx_L0; - __pyx_L1_error:; - __Pyx_XDECREF(__pyx_t_1); - __Pyx_XDECREF(__pyx_t_2); - __Pyx_XDECREF(__pyx_t_3); - __Pyx_XDECREF(__pyx_t_6); - __Pyx_XDECREF(__pyx_t_7); - __Pyx_XDECREF(__pyx_t_8); - __Pyx_XDECREF(__pyx_t_9); - __Pyx_XDECREF(__pyx_t_10); - __Pyx_XDECREF(__pyx_t_11); - __Pyx_AddTraceback("gateway.TykGateway.log_error", __pyx_clineno, __pyx_lineno, __pyx_filename); - __pyx_r = NULL; - __pyx_L0:; - __Pyx_XDECREF(__pyx_v_excp); - __Pyx_XGIVEREF(__pyx_r); - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -static PyMethodDef __pyx_methods[] = { - {0, 0, 0, 0} -}; - -#if PY_MAJOR_VERSION >= 3 -static struct PyModuleDef __pyx_moduledef = { - #if PY_VERSION_HEX < 0x03020000 - { PyObject_HEAD_INIT(NULL) NULL, 0, NULL }, - #else - PyModuleDef_HEAD_INIT, - #endif - "gateway", - 0, /* m_doc */ - -1, /* m_size */ - __pyx_methods /* m_methods */, - NULL, /* m_reload */ - NULL, /* m_traverse */ - NULL, /* m_clear */ - NULL /* m_free */ -}; -#endif - -static __Pyx_StringTabEntry __pyx_string_tab[] = { - {&__pyx_kp_s_0_1, __pyx_k_0_1, sizeof(__pyx_k_0_1), 0, 0, 1, 0}, - {&__pyx_kp_s_0_1_2, __pyx_k_0_1_2, sizeof(__pyx_k_0_1_2), 0, 0, 1, 0}, - {&__pyx_n_s_TykGateway, __pyx_k_TykGateway, sizeof(__pyx_k_TykGateway), 0, 0, 1, 1}, - {&__pyx_n_s_TykGateway_get_data, __pyx_k_TykGateway_get_data, sizeof(__pyx_k_TykGateway_get_data), 0, 0, 1, 1}, - {&__pyx_n_s_TykGateway_log, __pyx_k_TykGateway_log, sizeof(__pyx_k_TykGateway_log), 0, 0, 1, 1}, - {&__pyx_n_s_TykGateway_log_error, __pyx_k_TykGateway_log_error, sizeof(__pyx_k_TykGateway_log_error), 0, 0, 1, 1}, - {&__pyx_n_s_TykGateway_store_data, __pyx_k_TykGateway_store_data, sizeof(__pyx_k_TykGateway_store_data), 0, 0, 1, 1}, - {&__pyx_n_s_TykGateway_trigger_event, __pyx_k_TykGateway_trigger_event, sizeof(__pyx_k_TykGateway_trigger_event), 0, 0, 1, 1}, - {&__pyx_kp_s_Users_matias_dev_tyk_coprocess, __pyx_k_Users_matias_dev_tyk_coprocess, sizeof(__pyx_k_Users_matias_dev_tyk_coprocess), 0, 0, 1, 0}, - {&__pyx_n_s_args, __pyx_k_args, sizeof(__pyx_k_args), 0, 0, 1, 1}, - {&__pyx_n_s_doc, __pyx_k_doc, sizeof(__pyx_k_doc), 0, 0, 1, 1}, - {&__pyx_n_s_encode, __pyx_k_encode, sizeof(__pyx_k_encode), 0, 0, 1, 1}, - {&__pyx_n_s_error, __pyx_k_error, sizeof(__pyx_k_error), 0, 0, 1, 1}, - {&__pyx_n_s_event_name, __pyx_k_event_name, sizeof(__pyx_k_event_name), 0, 0, 1, 1}, - {&__pyx_n_s_exc_info, __pyx_k_exc_info, sizeof(__pyx_k_exc_info), 0, 0, 1, 1}, - {&__pyx_n_s_excp, __pyx_k_excp, sizeof(__pyx_k_excp), 0, 0, 1, 1}, - {&__pyx_n_s_format, __pyx_k_format, sizeof(__pyx_k_format), 0, 0, 1, 1}, - {&__pyx_n_s_gateway, __pyx_k_gateway, sizeof(__pyx_k_gateway), 0, 0, 1, 1}, - {&__pyx_n_s_get_data, __pyx_k_get_data, sizeof(__pyx_k_get_data), 0, 0, 1, 1}, - {&__pyx_n_s_import, __pyx_k_import, sizeof(__pyx_k_import), 0, 0, 1, 1}, - {&__pyx_n_s_key, __pyx_k_key, sizeof(__pyx_k_key), 0, 0, 1, 1}, - {&__pyx_n_s_level, __pyx_k_level, sizeof(__pyx_k_level), 0, 0, 1, 1}, - {&__pyx_n_s_log, __pyx_k_log, sizeof(__pyx_k_log), 0, 0, 1, 1}, - {&__pyx_n_s_log_error, __pyx_k_log_error, sizeof(__pyx_k_log_error), 0, 0, 1, 1}, - {&__pyx_n_s_main, __pyx_k_main, sizeof(__pyx_k_main), 0, 0, 1, 1}, - {&__pyx_n_s_metaclass, __pyx_k_metaclass, sizeof(__pyx_k_metaclass), 0, 0, 1, 1}, - {&__pyx_n_s_module, __pyx_k_module, sizeof(__pyx_k_module), 0, 0, 1, 1}, - {&__pyx_n_s_msg, __pyx_k_msg, sizeof(__pyx_k_msg), 0, 0, 1, 1}, - {&__pyx_n_s_output, __pyx_k_output, sizeof(__pyx_k_output), 0, 0, 1, 1}, - {&__pyx_n_s_payload, __pyx_k_payload, sizeof(__pyx_k_payload), 0, 0, 1, 1}, - {&__pyx_n_s_prepare, __pyx_k_prepare, sizeof(__pyx_k_prepare), 0, 0, 1, 1}, - {&__pyx_n_s_qualname, __pyx_k_qualname, sizeof(__pyx_k_qualname), 0, 0, 1, 1}, - {&__pyx_n_s_store_data, __pyx_k_store_data, sizeof(__pyx_k_store_data), 0, 0, 1, 1}, - {&__pyx_n_s_sys, __pyx_k_sys, sizeof(__pyx_k_sys), 0, 0, 1, 1}, - {&__pyx_n_s_test, __pyx_k_test, sizeof(__pyx_k_test), 0, 0, 1, 1}, - {&__pyx_n_s_trigger_event, __pyx_k_trigger_event, sizeof(__pyx_k_trigger_event), 0, 0, 1, 1}, - {&__pyx_n_s_ttl, __pyx_k_ttl, sizeof(__pyx_k_ttl), 0, 0, 1, 1}, - {&__pyx_kp_s_utf_8, __pyx_k_utf_8, sizeof(__pyx_k_utf_8), 0, 0, 1, 0}, - {&__pyx_n_s_value, __pyx_k_value, sizeof(__pyx_k_value), 0, 0, 1, 1}, - {0, 0, 0, 0, 0, 0, 0} -}; -static int __Pyx_InitCachedBuiltins(void) { - return 0; -} - -static int __Pyx_InitCachedConstants(void) { - __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0); - - /* "gateway.pyx":14 - * class TykGateway: - * def store_data(key, value, ttl): - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) # <<<<<<<<<<<<<< - * def get_data(key): - * output = TykGetData(key.encode('utf-8')) - */ - __pyx_tuple_ = PyTuple_Pack(1, __pyx_kp_s_utf_8); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 14, __pyx_L1_error) - __Pyx_GOTREF(__pyx_tuple_); - __Pyx_GIVEREF(__pyx_tuple_); - __pyx_tuple__2 = PyTuple_Pack(1, __pyx_kp_s_utf_8); if (unlikely(!__pyx_tuple__2)) __PYX_ERR(0, 14, __pyx_L1_error) - __Pyx_GOTREF(__pyx_tuple__2); - __Pyx_GIVEREF(__pyx_tuple__2); - - /* "gateway.pyx":16 - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - * def get_data(key): - * output = TykGetData(key.encode('utf-8')) # <<<<<<<<<<<<<< - * return output.decode('utf-8') - * def trigger_event(event_name, payload): - */ - __pyx_tuple__3 = PyTuple_Pack(1, __pyx_kp_s_utf_8); if (unlikely(!__pyx_tuple__3)) __PYX_ERR(0, 16, __pyx_L1_error) - __Pyx_GOTREF(__pyx_tuple__3); - __Pyx_GIVEREF(__pyx_tuple__3); - - /* "gateway.pyx":19 - * return output.decode('utf-8') - * def trigger_event(event_name, payload): - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) # <<<<<<<<<<<<<< - * def log(msg, level): - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) - */ - __pyx_tuple__4 = PyTuple_Pack(1, __pyx_kp_s_utf_8); if (unlikely(!__pyx_tuple__4)) __PYX_ERR(0, 19, __pyx_L1_error) - __Pyx_GOTREF(__pyx_tuple__4); - __Pyx_GIVEREF(__pyx_tuple__4); - __pyx_tuple__5 = PyTuple_Pack(1, __pyx_kp_s_utf_8); if (unlikely(!__pyx_tuple__5)) __PYX_ERR(0, 19, __pyx_L1_error) - __Pyx_GOTREF(__pyx_tuple__5); - __Pyx_GIVEREF(__pyx_tuple__5); - - /* "gateway.pyx":21 - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) - * def log(msg, level): - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) # <<<<<<<<<<<<<< - * def log_error(*args): - * excp = exc_info() - */ - __pyx_tuple__6 = PyTuple_Pack(1, __pyx_kp_s_utf_8); if (unlikely(!__pyx_tuple__6)) __PYX_ERR(0, 21, __pyx_L1_error) - __Pyx_GOTREF(__pyx_tuple__6); - __Pyx_GIVEREF(__pyx_tuple__6); - __pyx_tuple__7 = PyTuple_Pack(1, __pyx_kp_s_utf_8); if (unlikely(!__pyx_tuple__7)) __PYX_ERR(0, 21, __pyx_L1_error) - __Pyx_GOTREF(__pyx_tuple__7); - __Pyx_GIVEREF(__pyx_tuple__7); - - /* "gateway.pyx":13 - * - * class TykGateway: - * def store_data(key, value, ttl): # <<<<<<<<<<<<<< - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - * def get_data(key): - */ - __pyx_tuple__8 = PyTuple_Pack(3, __pyx_n_s_key, __pyx_n_s_value, __pyx_n_s_ttl); if (unlikely(!__pyx_tuple__8)) __PYX_ERR(0, 13, __pyx_L1_error) - __Pyx_GOTREF(__pyx_tuple__8); - __Pyx_GIVEREF(__pyx_tuple__8); - __pyx_codeobj__9 = (PyObject*)__Pyx_PyCode_New(3, 0, 3, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__8, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_Users_matias_dev_tyk_coprocess, __pyx_n_s_store_data, 13, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__9)) __PYX_ERR(0, 13, __pyx_L1_error) - - /* "gateway.pyx":15 - * def store_data(key, value, ttl): - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - * def get_data(key): # <<<<<<<<<<<<<< - * output = TykGetData(key.encode('utf-8')) - * return output.decode('utf-8') - */ - __pyx_tuple__10 = PyTuple_Pack(2, __pyx_n_s_key, __pyx_n_s_output); if (unlikely(!__pyx_tuple__10)) __PYX_ERR(0, 15, __pyx_L1_error) - __Pyx_GOTREF(__pyx_tuple__10); - __Pyx_GIVEREF(__pyx_tuple__10); - __pyx_codeobj__11 = (PyObject*)__Pyx_PyCode_New(1, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__10, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_Users_matias_dev_tyk_coprocess, __pyx_n_s_get_data, 15, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__11)) __PYX_ERR(0, 15, __pyx_L1_error) - - /* "gateway.pyx":18 - * output = TykGetData(key.encode('utf-8')) - * return output.decode('utf-8') - * def trigger_event(event_name, payload): # <<<<<<<<<<<<<< - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) - * def log(msg, level): - */ - __pyx_tuple__12 = PyTuple_Pack(2, __pyx_n_s_event_name, __pyx_n_s_payload); if (unlikely(!__pyx_tuple__12)) __PYX_ERR(0, 18, __pyx_L1_error) - __Pyx_GOTREF(__pyx_tuple__12); - __Pyx_GIVEREF(__pyx_tuple__12); - __pyx_codeobj__13 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__12, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_Users_matias_dev_tyk_coprocess, __pyx_n_s_trigger_event, 18, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__13)) __PYX_ERR(0, 18, __pyx_L1_error) - - /* "gateway.pyx":20 - * def trigger_event(event_name, payload): - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) - * def log(msg, level): # <<<<<<<<<<<<<< - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) - * def log_error(*args): - */ - __pyx_tuple__14 = PyTuple_Pack(2, __pyx_n_s_msg, __pyx_n_s_level); if (unlikely(!__pyx_tuple__14)) __PYX_ERR(0, 20, __pyx_L1_error) - __Pyx_GOTREF(__pyx_tuple__14); - __Pyx_GIVEREF(__pyx_tuple__14); - __pyx_codeobj__15 = (PyObject*)__Pyx_PyCode_New(2, 0, 2, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__14, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_Users_matias_dev_tyk_coprocess, __pyx_n_s_log, 20, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__15)) __PYX_ERR(0, 20, __pyx_L1_error) - - /* "gateway.pyx":22 - * def log(msg, level): - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) - * def log_error(*args): # <<<<<<<<<<<<<< - * excp = exc_info() - * if len(args) == 0: - */ - __pyx_tuple__16 = PyTuple_Pack(2, __pyx_n_s_args, __pyx_n_s_excp); if (unlikely(!__pyx_tuple__16)) __PYX_ERR(0, 22, __pyx_L1_error) - __Pyx_GOTREF(__pyx_tuple__16); - __Pyx_GIVEREF(__pyx_tuple__16); - __pyx_codeobj__17 = (PyObject*)__Pyx_PyCode_New(0, 0, 2, 0, CO_VARARGS, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__16, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_Users_matias_dev_tyk_coprocess, __pyx_n_s_log_error, 22, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__17)) __PYX_ERR(0, 22, __pyx_L1_error) - __Pyx_RefNannyFinishContext(); - return 0; - __pyx_L1_error:; - __Pyx_RefNannyFinishContext(); - return -1; -} - -static int __Pyx_InitGlobals(void) { - if (__Pyx_InitStrings(__pyx_string_tab) < 0) __PYX_ERR(0, 1, __pyx_L1_error); - return 0; - __pyx_L1_error:; - return -1; -} - -#if PY_MAJOR_VERSION < 3 -PyMODINIT_FUNC initgateway(void); /*proto*/ -PyMODINIT_FUNC initgateway(void) -#else -PyMODINIT_FUNC PyInit_gateway(void); /*proto*/ -PyMODINIT_FUNC PyInit_gateway(void) -#endif -{ - PyObject *__pyx_t_1 = NULL; - PyObject *__pyx_t_2 = NULL; - __Pyx_RefNannyDeclarations - #if CYTHON_REFNANNY - __Pyx_RefNanny = __Pyx_RefNannyImportAPI("refnanny"); - if (!__Pyx_RefNanny) { - PyErr_Clear(); - __Pyx_RefNanny = __Pyx_RefNannyImportAPI("Cython.Runtime.refnanny"); - if (!__Pyx_RefNanny) - Py_FatalError("failed to import 'refnanny' module"); - } - #endif - __Pyx_RefNannySetupContext("PyMODINIT_FUNC PyInit_gateway(void)", 0); - if (__Pyx_check_binary_version() < 0) __PYX_ERR(0, 1, __pyx_L1_error) - __pyx_empty_tuple = PyTuple_New(0); if (unlikely(!__pyx_empty_tuple)) __PYX_ERR(0, 1, __pyx_L1_error) - __pyx_empty_bytes = PyBytes_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_bytes)) __PYX_ERR(0, 1, __pyx_L1_error) - __pyx_empty_unicode = PyUnicode_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_unicode)) __PYX_ERR(0, 1, __pyx_L1_error) - #ifdef __Pyx_CyFunction_USED - if (__pyx_CyFunction_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) - #endif - #ifdef __Pyx_FusedFunction_USED - if (__pyx_FusedFunction_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) - #endif - #ifdef __Pyx_Coroutine_USED - if (__pyx_Coroutine_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) - #endif - #ifdef __Pyx_Generator_USED - if (__pyx_Generator_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) - #endif - #ifdef __Pyx_StopAsyncIteration_USED - if (__pyx_StopAsyncIteration_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) - #endif - /*--- Library function declarations ---*/ - /*--- Threads initialization code ---*/ - #if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS - #ifdef WITH_THREAD /* Python build with threading support? */ - PyEval_InitThreads(); - #endif - #endif - /*--- Module creation code ---*/ - #if PY_MAJOR_VERSION < 3 - __pyx_m = Py_InitModule4("gateway", __pyx_methods, 0, 0, PYTHON_API_VERSION); Py_XINCREF(__pyx_m); - #else - __pyx_m = PyModule_Create(&__pyx_moduledef); - #endif - if (unlikely(!__pyx_m)) __PYX_ERR(0, 1, __pyx_L1_error) - __pyx_d = PyModule_GetDict(__pyx_m); if (unlikely(!__pyx_d)) __PYX_ERR(0, 1, __pyx_L1_error) - Py_INCREF(__pyx_d); - __pyx_b = PyImport_AddModule(__Pyx_BUILTIN_MODULE_NAME); if (unlikely(!__pyx_b)) __PYX_ERR(0, 1, __pyx_L1_error) - #if CYTHON_COMPILING_IN_PYPY - Py_INCREF(__pyx_b); - #endif - if (PyObject_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) __PYX_ERR(0, 1, __pyx_L1_error); - /*--- Initialize various global constants etc. ---*/ - if (__Pyx_InitGlobals() < 0) __PYX_ERR(0, 1, __pyx_L1_error) - #if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT) - if (__Pyx_init_sys_getdefaultencoding_params() < 0) __PYX_ERR(0, 1, __pyx_L1_error) - #endif - if (__pyx_module_is_main_gateway) { - if (PyObject_SetAttrString(__pyx_m, "__name__", __pyx_n_s_main) < 0) __PYX_ERR(0, 1, __pyx_L1_error) - } - #if PY_MAJOR_VERSION >= 3 - { - PyObject *modules = PyImport_GetModuleDict(); if (unlikely(!modules)) __PYX_ERR(0, 1, __pyx_L1_error) - if (!PyDict_GetItemString(modules, "gateway")) { - if (unlikely(PyDict_SetItemString(modules, "gateway", __pyx_m) < 0)) __PYX_ERR(0, 1, __pyx_L1_error) - } - } - #endif - /*--- Builtin init code ---*/ - if (__Pyx_InitCachedBuiltins() < 0) __PYX_ERR(0, 1, __pyx_L1_error) - /*--- Constants init code ---*/ - if (__Pyx_InitCachedConstants() < 0) __PYX_ERR(0, 1, __pyx_L1_error) - /*--- Global init code ---*/ - /*--- Variable export code ---*/ - /*--- Function export code ---*/ - /*--- Type init code ---*/ - /*--- Type import code ---*/ - /*--- Variable import code ---*/ - /*--- Function import code ---*/ - /*--- Execution code ---*/ - #if defined(__Pyx_Generator_USED) || defined(__Pyx_Coroutine_USED) - if (__Pyx_patch_abc() < 0) __PYX_ERR(0, 1, __pyx_L1_error) - #endif - - /* "gateway.pyx":4 - * # Recompile with: cythonize gateway.pyx - * - * from sys import exc_info # <<<<<<<<<<<<<< - * - * cdef extern from "coprocess/api.h": - */ - __pyx_t_1 = PyList_New(1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 4, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - __Pyx_INCREF(__pyx_n_s_exc_info); - __Pyx_GIVEREF(__pyx_n_s_exc_info); - PyList_SET_ITEM(__pyx_t_1, 0, __pyx_n_s_exc_info); - __pyx_t_2 = __Pyx_Import(__pyx_n_s_sys, __pyx_t_1, -1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 4, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_2); - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __pyx_t_1 = __Pyx_ImportFrom(__pyx_t_2, __pyx_n_s_exc_info); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 4, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - if (PyDict_SetItem(__pyx_d, __pyx_n_s_exc_info, __pyx_t_1) < 0) __PYX_ERR(0, 4, __pyx_L1_error) - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - - /* "gateway.pyx":12 - * void CoProcessLog(char *msg, char *level); - * - * class TykGateway: # <<<<<<<<<<<<<< - * def store_data(key, value, ttl): - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - */ - __pyx_t_2 = __Pyx_Py3MetaclassPrepare((PyObject *) NULL, __pyx_empty_tuple, __pyx_n_s_TykGateway, __pyx_n_s_TykGateway, (PyObject *) NULL, __pyx_n_s_gateway, (PyObject *) NULL); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 12, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_2); - - /* "gateway.pyx":13 - * - * class TykGateway: - * def store_data(key, value, ttl): # <<<<<<<<<<<<<< - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - * def get_data(key): - */ - __pyx_t_1 = __Pyx_CyFunction_NewEx(&__pyx_mdef_7gateway_10TykGateway_1store_data, 0, __pyx_n_s_TykGateway_store_data, NULL, __pyx_n_s_gateway, __pyx_d, ((PyObject *)__pyx_codeobj__9)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 13, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - if (PyObject_SetItem(__pyx_t_2, __pyx_n_s_store_data, __pyx_t_1) < 0) __PYX_ERR(0, 13, __pyx_L1_error) - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - - /* "gateway.pyx":15 - * def store_data(key, value, ttl): - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - * def get_data(key): # <<<<<<<<<<<<<< - * output = TykGetData(key.encode('utf-8')) - * return output.decode('utf-8') - */ - __pyx_t_1 = __Pyx_CyFunction_NewEx(&__pyx_mdef_7gateway_10TykGateway_3get_data, 0, __pyx_n_s_TykGateway_get_data, NULL, __pyx_n_s_gateway, __pyx_d, ((PyObject *)__pyx_codeobj__11)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 15, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - if (PyObject_SetItem(__pyx_t_2, __pyx_n_s_get_data, __pyx_t_1) < 0) __PYX_ERR(0, 15, __pyx_L1_error) - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - - /* "gateway.pyx":18 - * output = TykGetData(key.encode('utf-8')) - * return output.decode('utf-8') - * def trigger_event(event_name, payload): # <<<<<<<<<<<<<< - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) - * def log(msg, level): - */ - __pyx_t_1 = __Pyx_CyFunction_NewEx(&__pyx_mdef_7gateway_10TykGateway_5trigger_event, 0, __pyx_n_s_TykGateway_trigger_event, NULL, __pyx_n_s_gateway, __pyx_d, ((PyObject *)__pyx_codeobj__13)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 18, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - if (PyObject_SetItem(__pyx_t_2, __pyx_n_s_trigger_event, __pyx_t_1) < 0) __PYX_ERR(0, 18, __pyx_L1_error) - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - - /* "gateway.pyx":20 - * def trigger_event(event_name, payload): - * TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) - * def log(msg, level): # <<<<<<<<<<<<<< - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) - * def log_error(*args): - */ - __pyx_t_1 = __Pyx_CyFunction_NewEx(&__pyx_mdef_7gateway_10TykGateway_7log, 0, __pyx_n_s_TykGateway_log, NULL, __pyx_n_s_gateway, __pyx_d, ((PyObject *)__pyx_codeobj__15)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 20, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - if (PyObject_SetItem(__pyx_t_2, __pyx_n_s_log, __pyx_t_1) < 0) __PYX_ERR(0, 20, __pyx_L1_error) - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - - /* "gateway.pyx":22 - * def log(msg, level): - * CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) - * def log_error(*args): # <<<<<<<<<<<<<< - * excp = exc_info() - * if len(args) == 0: - */ - __pyx_t_1 = __Pyx_CyFunction_NewEx(&__pyx_mdef_7gateway_10TykGateway_9log_error, 0, __pyx_n_s_TykGateway_log_error, NULL, __pyx_n_s_gateway, __pyx_d, ((PyObject *)__pyx_codeobj__17)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 22, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - if (PyObject_SetItem(__pyx_t_2, __pyx_n_s_log_error, __pyx_t_1) < 0) __PYX_ERR(0, 22, __pyx_L1_error) - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - - /* "gateway.pyx":12 - * void CoProcessLog(char *msg, char *level); - * - * class TykGateway: # <<<<<<<<<<<<<< - * def store_data(key, value, ttl): - * TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - */ - __pyx_t_1 = __Pyx_Py3ClassCreate(((PyObject*)&__Pyx_DefaultClassType), __pyx_n_s_TykGateway, __pyx_empty_tuple, __pyx_t_2, NULL, 0, 1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 12, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - if (PyDict_SetItem(__pyx_d, __pyx_n_s_TykGateway, __pyx_t_1) < 0) __PYX_ERR(0, 12, __pyx_L1_error) - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - - /* "gateway.pyx":1 - * # Gateway API Cython binding # <<<<<<<<<<<<<< - * # Recompile with: cythonize gateway.pyx - * - */ - __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 1, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_2); - if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_2) < 0) __PYX_ERR(0, 1, __pyx_L1_error) - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - - /*--- Wrapped vars code ---*/ - - goto __pyx_L0; - __pyx_L1_error:; - __Pyx_XDECREF(__pyx_t_1); - __Pyx_XDECREF(__pyx_t_2); - if (__pyx_m) { - if (__pyx_d) { - __Pyx_AddTraceback("init gateway", __pyx_clineno, __pyx_lineno, __pyx_filename); - } - Py_DECREF(__pyx_m); __pyx_m = 0; - } else if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_ImportError, "init gateway"); - } - __pyx_L0:; - __Pyx_RefNannyFinishContext(); - #if PY_MAJOR_VERSION < 3 - return; - #else - return __pyx_m; - #endif -} - -/* --- Runtime support code --- */ -/* Refnanny */ -#if CYTHON_REFNANNY -static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname) { - PyObject *m = NULL, *p = NULL; - void *r = NULL; - m = PyImport_ImportModule((char *)modname); - if (!m) goto end; - p = PyObject_GetAttrString(m, (char *)"RefNannyAPI"); - if (!p) goto end; - r = PyLong_AsVoidPtr(p); -end: - Py_XDECREF(p); - Py_XDECREF(m); - return (__Pyx_RefNannyAPIStruct *)r; -} -#endif - -/* RaiseArgTupleInvalid */ -static void __Pyx_RaiseArgtupleInvalid( - const char* func_name, - int exact, - Py_ssize_t num_min, - Py_ssize_t num_max, - Py_ssize_t num_found) -{ - Py_ssize_t num_expected; - const char *more_or_less; - if (num_found < num_min) { - num_expected = num_min; - more_or_less = "at least"; - } else { - num_expected = num_max; - more_or_less = "at most"; - } - if (exact) { - more_or_less = "exactly"; - } - PyErr_Format(PyExc_TypeError, - "%.200s() takes %.8s %" CYTHON_FORMAT_SSIZE_T "d positional argument%.1s (%" CYTHON_FORMAT_SSIZE_T "d given)", - func_name, more_or_less, num_expected, - (num_expected == 1) ? "" : "s", num_found); -} - -/* RaiseDoubleKeywords */ -static void __Pyx_RaiseDoubleKeywordsError( - const char* func_name, - PyObject* kw_name) -{ - PyErr_Format(PyExc_TypeError, - #if PY_MAJOR_VERSION >= 3 - "%s() got multiple values for keyword argument '%U'", func_name, kw_name); - #else - "%s() got multiple values for keyword argument '%s'", func_name, - PyString_AsString(kw_name)); - #endif -} - -/* ParseKeywords */ -static int __Pyx_ParseOptionalKeywords( - PyObject *kwds, - PyObject **argnames[], - PyObject *kwds2, - PyObject *values[], - Py_ssize_t num_pos_args, - const char* function_name) -{ - PyObject *key = 0, *value = 0; - Py_ssize_t pos = 0; - PyObject*** name; - PyObject*** first_kw_arg = argnames + num_pos_args; - while (PyDict_Next(kwds, &pos, &key, &value)) { - name = first_kw_arg; - while (*name && (**name != key)) name++; - if (*name) { - values[name-argnames] = value; - continue; - } - name = first_kw_arg; - #if PY_MAJOR_VERSION < 3 - if (likely(PyString_CheckExact(key)) || likely(PyString_Check(key))) { - while (*name) { - if ((CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**name) == PyString_GET_SIZE(key)) - && _PyString_Eq(**name, key)) { - values[name-argnames] = value; - break; - } - name++; - } - if (*name) continue; - else { - PyObject*** argname = argnames; - while (argname != first_kw_arg) { - if ((**argname == key) || ( - (CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**argname) == PyString_GET_SIZE(key)) - && _PyString_Eq(**argname, key))) { - goto arg_passed_twice; - } - argname++; - } - } - } else - #endif - if (likely(PyUnicode_Check(key))) { - while (*name) { - int cmp = (**name == key) ? 0 : - #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3 - (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 : - #endif - PyUnicode_Compare(**name, key); - if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad; - if (cmp == 0) { - values[name-argnames] = value; - break; - } - name++; - } - if (*name) continue; - else { - PyObject*** argname = argnames; - while (argname != first_kw_arg) { - int cmp = (**argname == key) ? 0 : - #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3 - (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 : - #endif - PyUnicode_Compare(**argname, key); - if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad; - if (cmp == 0) goto arg_passed_twice; - argname++; - } - } - } else - goto invalid_keyword_type; - if (kwds2) { - if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad; - } else { - goto invalid_keyword; - } - } - return 0; -arg_passed_twice: - __Pyx_RaiseDoubleKeywordsError(function_name, key); - goto bad; -invalid_keyword_type: - PyErr_Format(PyExc_TypeError, - "%.200s() keywords must be strings", function_name); - goto bad; -invalid_keyword: - PyErr_Format(PyExc_TypeError, - #if PY_MAJOR_VERSION < 3 - "%.200s() got an unexpected keyword argument '%.200s'", - function_name, PyString_AsString(key)); - #else - "%s() got an unexpected keyword argument '%U'", - function_name, key); - #endif -bad: - return -1; -} - -/* PyObjectCall */ -#if CYTHON_COMPILING_IN_CPYTHON -static CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) { - PyObject *result; - ternaryfunc call = func->ob_type->tp_call; - if (unlikely(!call)) - return PyObject_Call(func, arg, kw); - if (unlikely(Py_EnterRecursiveCall((char*)" while calling a Python object"))) - return NULL; - result = (*call)(func, arg, kw); - Py_LeaveRecursiveCall(); - if (unlikely(!result) && unlikely(!PyErr_Occurred())) { - PyErr_SetString( - PyExc_SystemError, - "NULL result without error in PyObject_Call"); - } - return result; -} -#endif - -/* decode_c_string */ -static CYTHON_INLINE PyObject* __Pyx_decode_c_string( - const char* cstring, Py_ssize_t start, Py_ssize_t stop, - const char* encoding, const char* errors, - PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors)) { - Py_ssize_t length; - if (unlikely((start < 0) | (stop < 0))) { - size_t slen = strlen(cstring); - if (unlikely(slen > (size_t) PY_SSIZE_T_MAX)) { - PyErr_SetString(PyExc_OverflowError, - "c-string too long to convert to Python"); - return NULL; - } - length = (Py_ssize_t) slen; - if (start < 0) { - start += length; - if (start < 0) - start = 0; - } - if (stop < 0) - stop += length; - } - length = stop - start; - if (unlikely(length <= 0)) - return PyUnicode_FromUnicode(NULL, 0); - cstring += start; - if (decode_func) { - return decode_func(cstring, length, errors); - } else { - return PyUnicode_Decode(cstring, length, encoding, errors); - } -} - -/* KeywordStringCheck */ -static CYTHON_INLINE int __Pyx_CheckKeywordStrings( - PyObject *kwdict, - const char* function_name, - int kw_allowed) -{ - PyObject* key = 0; - Py_ssize_t pos = 0; -#if CYTHON_COMPILING_IN_PYPY - if (!kw_allowed && PyDict_Next(kwdict, &pos, &key, 0)) - goto invalid_keyword; - return 1; -#else - while (PyDict_Next(kwdict, &pos, &key, 0)) { - #if PY_MAJOR_VERSION < 3 - if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) - #endif - if (unlikely(!PyUnicode_Check(key))) - goto invalid_keyword_type; - } - if ((!kw_allowed) && unlikely(key)) - goto invalid_keyword; - return 1; -invalid_keyword_type: - PyErr_Format(PyExc_TypeError, - "%.200s() keywords must be strings", function_name); - return 0; -#endif -invalid_keyword: - PyErr_Format(PyExc_TypeError, - #if PY_MAJOR_VERSION < 3 - "%.200s() got an unexpected keyword argument '%.200s'", - function_name, PyString_AsString(key)); - #else - "%s() got an unexpected keyword argument '%U'", - function_name, key); - #endif - return 0; -} - -/* GetBuiltinName */ -static PyObject *__Pyx_GetBuiltinName(PyObject *name) { - PyObject* result = __Pyx_PyObject_GetAttrStr(__pyx_b, name); - if (unlikely(!result)) { - PyErr_Format(PyExc_NameError, -#if PY_MAJOR_VERSION >= 3 - "name '%U' is not defined", name); -#else - "name '%.200s' is not defined", PyString_AS_STRING(name)); -#endif - } - return result; -} - -/* GetModuleGlobalName */ -static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) { - PyObject *result; -#if CYTHON_COMPILING_IN_CPYTHON - result = PyDict_GetItem(__pyx_d, name); - if (likely(result)) { - Py_INCREF(result); - } else { -#else - result = PyObject_GetItem(__pyx_d, name); - if (!result) { - PyErr_Clear(); -#endif - result = __Pyx_GetBuiltinName(name); - } - return result; -} - -/* PyObjectCallMethO */ - #if CYTHON_COMPILING_IN_CPYTHON -static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject *arg) { - PyObject *self, *result; - PyCFunction cfunc; - cfunc = PyCFunction_GET_FUNCTION(func); - self = PyCFunction_GET_SELF(func); - if (unlikely(Py_EnterRecursiveCall((char*)" while calling a Python object"))) - return NULL; - result = cfunc(self, arg); - Py_LeaveRecursiveCall(); - if (unlikely(!result) && unlikely(!PyErr_Occurred())) { - PyErr_SetString( - PyExc_SystemError, - "NULL result without error in PyObject_Call"); - } - return result; -} -#endif - -/* PyObjectCallOneArg */ - #if CYTHON_COMPILING_IN_CPYTHON -static PyObject* __Pyx__PyObject_CallOneArg(PyObject *func, PyObject *arg) { - PyObject *result; - PyObject *args = PyTuple_New(1); - if (unlikely(!args)) return NULL; - Py_INCREF(arg); - PyTuple_SET_ITEM(args, 0, arg); - result = __Pyx_PyObject_Call(func, args, NULL); - Py_DECREF(args); - return result; -} -static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg) { -#ifdef __Pyx_CyFunction_USED - if (likely(PyCFunction_Check(func) || PyObject_TypeCheck(func, __pyx_CyFunctionType))) { -#else - if (likely(PyCFunction_Check(func))) { -#endif - if (likely(PyCFunction_GET_FLAGS(func) & METH_O)) { - return __Pyx_PyObject_CallMethO(func, arg); - } - } - return __Pyx__PyObject_CallOneArg(func, arg); -} -#else -static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg) { - PyObject *result; - PyObject *args = PyTuple_Pack(1, arg); - if (unlikely(!args)) return NULL; - result = __Pyx_PyObject_Call(func, args, NULL); - Py_DECREF(args); - return result; -} -#endif - -/* PyObjectCallNoArg */ - #if CYTHON_COMPILING_IN_CPYTHON -static CYTHON_INLINE PyObject* __Pyx_PyObject_CallNoArg(PyObject *func) { -#ifdef __Pyx_CyFunction_USED - if (likely(PyCFunction_Check(func) || PyObject_TypeCheck(func, __pyx_CyFunctionType))) { -#else - if (likely(PyCFunction_Check(func))) { -#endif - if (likely(PyCFunction_GET_FLAGS(func) & METH_NOARGS)) { - return __Pyx_PyObject_CallMethO(func, NULL); - } - } - return __Pyx_PyObject_Call(func, __pyx_empty_tuple, NULL); -} -#endif - -/* GetItemInt */ - static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) { - PyObject *r; - if (!j) return NULL; - r = PyObject_GetItem(o, j); - Py_DECREF(j); - return r; -} -static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i, - CYTHON_NCP_UNUSED int wraparound, - CYTHON_NCP_UNUSED int boundscheck) { -#if CYTHON_COMPILING_IN_CPYTHON - if (wraparound & unlikely(i < 0)) i += PyList_GET_SIZE(o); - if ((!boundscheck) || likely((0 <= i) & (i < PyList_GET_SIZE(o)))) { - PyObject *r = PyList_GET_ITEM(o, i); - Py_INCREF(r); - return r; - } - return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i)); -#else - return PySequence_GetItem(o, i); -#endif -} -static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i, - CYTHON_NCP_UNUSED int wraparound, - CYTHON_NCP_UNUSED int boundscheck) { -#if CYTHON_COMPILING_IN_CPYTHON - if (wraparound & unlikely(i < 0)) i += PyTuple_GET_SIZE(o); - if ((!boundscheck) || likely((0 <= i) & (i < PyTuple_GET_SIZE(o)))) { - PyObject *r = PyTuple_GET_ITEM(o, i); - Py_INCREF(r); - return r; - } - return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i)); -#else - return PySequence_GetItem(o, i); -#endif -} -static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i, int is_list, - CYTHON_NCP_UNUSED int wraparound, - CYTHON_NCP_UNUSED int boundscheck) { -#if CYTHON_COMPILING_IN_CPYTHON - if (is_list || PyList_CheckExact(o)) { - Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyList_GET_SIZE(o); - if ((!boundscheck) || (likely((n >= 0) & (n < PyList_GET_SIZE(o))))) { - PyObject *r = PyList_GET_ITEM(o, n); - Py_INCREF(r); - return r; - } - } - else if (PyTuple_CheckExact(o)) { - Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyTuple_GET_SIZE(o); - if ((!boundscheck) || likely((n >= 0) & (n < PyTuple_GET_SIZE(o)))) { - PyObject *r = PyTuple_GET_ITEM(o, n); - Py_INCREF(r); - return r; - } - } else { - PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence; - if (likely(m && m->sq_item)) { - if (wraparound && unlikely(i < 0) && likely(m->sq_length)) { - Py_ssize_t l = m->sq_length(o); - if (likely(l >= 0)) { - i += l; - } else { - if (!PyErr_ExceptionMatches(PyExc_OverflowError)) - return NULL; - PyErr_Clear(); - } - } - return m->sq_item(o, i); - } - } -#else - if (is_list || PySequence_Check(o)) { - return PySequence_GetItem(o, i); - } -#endif - return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i)); -} - -/* Import */ - static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level) { - PyObject *empty_list = 0; - PyObject *module = 0; - PyObject *global_dict = 0; - PyObject *empty_dict = 0; - PyObject *list; - #if PY_VERSION_HEX < 0x03030000 - PyObject *py_import; - py_import = __Pyx_PyObject_GetAttrStr(__pyx_b, __pyx_n_s_import); - if (!py_import) - goto bad; - #endif - if (from_list) - list = from_list; - else { - empty_list = PyList_New(0); - if (!empty_list) - goto bad; - list = empty_list; - } - global_dict = PyModule_GetDict(__pyx_m); - if (!global_dict) - goto bad; - empty_dict = PyDict_New(); - if (!empty_dict) - goto bad; - { - #if PY_MAJOR_VERSION >= 3 - if (level == -1) { - if (strchr(__Pyx_MODULE_NAME, '.')) { - #if PY_VERSION_HEX < 0x03030000 - PyObject *py_level = PyInt_FromLong(1); - if (!py_level) - goto bad; - module = PyObject_CallFunctionObjArgs(py_import, - name, global_dict, empty_dict, list, py_level, NULL); - Py_DECREF(py_level); - #else - module = PyImport_ImportModuleLevelObject( - name, global_dict, empty_dict, list, 1); - #endif - if (!module) { - if (!PyErr_ExceptionMatches(PyExc_ImportError)) - goto bad; - PyErr_Clear(); - } - } - level = 0; - } - #endif - if (!module) { - #if PY_VERSION_HEX < 0x03030000 - PyObject *py_level = PyInt_FromLong(level); - if (!py_level) - goto bad; - module = PyObject_CallFunctionObjArgs(py_import, - name, global_dict, empty_dict, list, py_level, NULL); - Py_DECREF(py_level); - #else - module = PyImport_ImportModuleLevelObject( - name, global_dict, empty_dict, list, level); - #endif - } - } -bad: - #if PY_VERSION_HEX < 0x03030000 - Py_XDECREF(py_import); - #endif - Py_XDECREF(empty_list); - Py_XDECREF(empty_dict); - return module; -} - -/* ImportFrom */ - static PyObject* __Pyx_ImportFrom(PyObject* module, PyObject* name) { - PyObject* value = __Pyx_PyObject_GetAttrStr(module, name); - if (unlikely(!value) && PyErr_ExceptionMatches(PyExc_AttributeError)) { - PyErr_Format(PyExc_ImportError, - #if PY_MAJOR_VERSION < 3 - "cannot import name %.230s", PyString_AS_STRING(name)); - #else - "cannot import name %S", name); - #endif - } - return value; -} - -/* FetchCommonType */ - static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type) { - PyObject* fake_module; - PyTypeObject* cached_type = NULL; - fake_module = PyImport_AddModule((char*) "_cython_" CYTHON_ABI); - if (!fake_module) return NULL; - Py_INCREF(fake_module); - cached_type = (PyTypeObject*) PyObject_GetAttrString(fake_module, type->tp_name); - if (cached_type) { - if (!PyType_Check((PyObject*)cached_type)) { - PyErr_Format(PyExc_TypeError, - "Shared Cython type %.200s is not a type object", - type->tp_name); - goto bad; - } - if (cached_type->tp_basicsize != type->tp_basicsize) { - PyErr_Format(PyExc_TypeError, - "Shared Cython type %.200s has the wrong size, try recompiling", - type->tp_name); - goto bad; - } - } else { - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto bad; - PyErr_Clear(); - if (PyType_Ready(type) < 0) goto bad; - if (PyObject_SetAttrString(fake_module, type->tp_name, (PyObject*) type) < 0) - goto bad; - Py_INCREF(type); - cached_type = type; - } -done: - Py_DECREF(fake_module); - return cached_type; -bad: - Py_XDECREF(cached_type); - cached_type = NULL; - goto done; -} - -/* CythonFunction */ - static PyObject * -__Pyx_CyFunction_get_doc(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *closure) -{ - if (unlikely(op->func_doc == NULL)) { - if (op->func.m_ml->ml_doc) { -#if PY_MAJOR_VERSION >= 3 - op->func_doc = PyUnicode_FromString(op->func.m_ml->ml_doc); -#else - op->func_doc = PyString_FromString(op->func.m_ml->ml_doc); -#endif - if (unlikely(op->func_doc == NULL)) - return NULL; - } else { - Py_INCREF(Py_None); - return Py_None; - } - } - Py_INCREF(op->func_doc); - return op->func_doc; -} -static int -__Pyx_CyFunction_set_doc(__pyx_CyFunctionObject *op, PyObject *value) -{ - PyObject *tmp = op->func_doc; - if (value == NULL) { - value = Py_None; - } - Py_INCREF(value); - op->func_doc = value; - Py_XDECREF(tmp); - return 0; -} -static PyObject * -__Pyx_CyFunction_get_name(__pyx_CyFunctionObject *op) -{ - if (unlikely(op->func_name == NULL)) { -#if PY_MAJOR_VERSION >= 3 - op->func_name = PyUnicode_InternFromString(op->func.m_ml->ml_name); -#else - op->func_name = PyString_InternFromString(op->func.m_ml->ml_name); -#endif - if (unlikely(op->func_name == NULL)) - return NULL; - } - Py_INCREF(op->func_name); - return op->func_name; -} -static int -__Pyx_CyFunction_set_name(__pyx_CyFunctionObject *op, PyObject *value) -{ - PyObject *tmp; -#if PY_MAJOR_VERSION >= 3 - if (unlikely(value == NULL || !PyUnicode_Check(value))) { -#else - if (unlikely(value == NULL || !PyString_Check(value))) { -#endif - PyErr_SetString(PyExc_TypeError, - "__name__ must be set to a string object"); - return -1; - } - tmp = op->func_name; - Py_INCREF(value); - op->func_name = value; - Py_XDECREF(tmp); - return 0; -} -static PyObject * -__Pyx_CyFunction_get_qualname(__pyx_CyFunctionObject *op) -{ - Py_INCREF(op->func_qualname); - return op->func_qualname; -} -static int -__Pyx_CyFunction_set_qualname(__pyx_CyFunctionObject *op, PyObject *value) -{ - PyObject *tmp; -#if PY_MAJOR_VERSION >= 3 - if (unlikely(value == NULL || !PyUnicode_Check(value))) { -#else - if (unlikely(value == NULL || !PyString_Check(value))) { -#endif - PyErr_SetString(PyExc_TypeError, - "__qualname__ must be set to a string object"); - return -1; - } - tmp = op->func_qualname; - Py_INCREF(value); - op->func_qualname = value; - Py_XDECREF(tmp); - return 0; -} -static PyObject * -__Pyx_CyFunction_get_self(__pyx_CyFunctionObject *m, CYTHON_UNUSED void *closure) -{ - PyObject *self; - self = m->func_closure; - if (self == NULL) - self = Py_None; - Py_INCREF(self); - return self; -} -static PyObject * -__Pyx_CyFunction_get_dict(__pyx_CyFunctionObject *op) -{ - if (unlikely(op->func_dict == NULL)) { - op->func_dict = PyDict_New(); - if (unlikely(op->func_dict == NULL)) - return NULL; - } - Py_INCREF(op->func_dict); - return op->func_dict; -} -static int -__Pyx_CyFunction_set_dict(__pyx_CyFunctionObject *op, PyObject *value) -{ - PyObject *tmp; - if (unlikely(value == NULL)) { - PyErr_SetString(PyExc_TypeError, - "function's dictionary may not be deleted"); - return -1; - } - if (unlikely(!PyDict_Check(value))) { - PyErr_SetString(PyExc_TypeError, - "setting function's dictionary to a non-dict"); - return -1; - } - tmp = op->func_dict; - Py_INCREF(value); - op->func_dict = value; - Py_XDECREF(tmp); - return 0; -} -static PyObject * -__Pyx_CyFunction_get_globals(__pyx_CyFunctionObject *op) -{ - Py_INCREF(op->func_globals); - return op->func_globals; -} -static PyObject * -__Pyx_CyFunction_get_closure(CYTHON_UNUSED __pyx_CyFunctionObject *op) -{ - Py_INCREF(Py_None); - return Py_None; -} -static PyObject * -__Pyx_CyFunction_get_code(__pyx_CyFunctionObject *op) -{ - PyObject* result = (op->func_code) ? op->func_code : Py_None; - Py_INCREF(result); - return result; -} -static int -__Pyx_CyFunction_init_defaults(__pyx_CyFunctionObject *op) { - int result = 0; - PyObject *res = op->defaults_getter((PyObject *) op); - if (unlikely(!res)) - return -1; - #if CYTHON_COMPILING_IN_CPYTHON - op->defaults_tuple = PyTuple_GET_ITEM(res, 0); - Py_INCREF(op->defaults_tuple); - op->defaults_kwdict = PyTuple_GET_ITEM(res, 1); - Py_INCREF(op->defaults_kwdict); - #else - op->defaults_tuple = PySequence_ITEM(res, 0); - if (unlikely(!op->defaults_tuple)) result = -1; - else { - op->defaults_kwdict = PySequence_ITEM(res, 1); - if (unlikely(!op->defaults_kwdict)) result = -1; - } - #endif - Py_DECREF(res); - return result; -} -static int -__Pyx_CyFunction_set_defaults(__pyx_CyFunctionObject *op, PyObject* value) { - PyObject* tmp; - if (!value) { - value = Py_None; - } else if (value != Py_None && !PyTuple_Check(value)) { - PyErr_SetString(PyExc_TypeError, - "__defaults__ must be set to a tuple object"); - return -1; - } - Py_INCREF(value); - tmp = op->defaults_tuple; - op->defaults_tuple = value; - Py_XDECREF(tmp); - return 0; -} -static PyObject * -__Pyx_CyFunction_get_defaults(__pyx_CyFunctionObject *op) { - PyObject* result = op->defaults_tuple; - if (unlikely(!result)) { - if (op->defaults_getter) { - if (__Pyx_CyFunction_init_defaults(op) < 0) return NULL; - result = op->defaults_tuple; - } else { - result = Py_None; - } - } - Py_INCREF(result); - return result; -} -static int -__Pyx_CyFunction_set_kwdefaults(__pyx_CyFunctionObject *op, PyObject* value) { - PyObject* tmp; - if (!value) { - value = Py_None; - } else if (value != Py_None && !PyDict_Check(value)) { - PyErr_SetString(PyExc_TypeError, - "__kwdefaults__ must be set to a dict object"); - return -1; - } - Py_INCREF(value); - tmp = op->defaults_kwdict; - op->defaults_kwdict = value; - Py_XDECREF(tmp); - return 0; -} -static PyObject * -__Pyx_CyFunction_get_kwdefaults(__pyx_CyFunctionObject *op) { - PyObject* result = op->defaults_kwdict; - if (unlikely(!result)) { - if (op->defaults_getter) { - if (__Pyx_CyFunction_init_defaults(op) < 0) return NULL; - result = op->defaults_kwdict; - } else { - result = Py_None; - } - } - Py_INCREF(result); - return result; -} -static int -__Pyx_CyFunction_set_annotations(__pyx_CyFunctionObject *op, PyObject* value) { - PyObject* tmp; - if (!value || value == Py_None) { - value = NULL; - } else if (!PyDict_Check(value)) { - PyErr_SetString(PyExc_TypeError, - "__annotations__ must be set to a dict object"); - return -1; - } - Py_XINCREF(value); - tmp = op->func_annotations; - op->func_annotations = value; - Py_XDECREF(tmp); - return 0; -} -static PyObject * -__Pyx_CyFunction_get_annotations(__pyx_CyFunctionObject *op) { - PyObject* result = op->func_annotations; - if (unlikely(!result)) { - result = PyDict_New(); - if (unlikely(!result)) return NULL; - op->func_annotations = result; - } - Py_INCREF(result); - return result; -} -static PyGetSetDef __pyx_CyFunction_getsets[] = { - {(char *) "func_doc", (getter)__Pyx_CyFunction_get_doc, (setter)__Pyx_CyFunction_set_doc, 0, 0}, - {(char *) "__doc__", (getter)__Pyx_CyFunction_get_doc, (setter)__Pyx_CyFunction_set_doc, 0, 0}, - {(char *) "func_name", (getter)__Pyx_CyFunction_get_name, (setter)__Pyx_CyFunction_set_name, 0, 0}, - {(char *) "__name__", (getter)__Pyx_CyFunction_get_name, (setter)__Pyx_CyFunction_set_name, 0, 0}, - {(char *) "__qualname__", (getter)__Pyx_CyFunction_get_qualname, (setter)__Pyx_CyFunction_set_qualname, 0, 0}, - {(char *) "__self__", (getter)__Pyx_CyFunction_get_self, 0, 0, 0}, - {(char *) "func_dict", (getter)__Pyx_CyFunction_get_dict, (setter)__Pyx_CyFunction_set_dict, 0, 0}, - {(char *) "__dict__", (getter)__Pyx_CyFunction_get_dict, (setter)__Pyx_CyFunction_set_dict, 0, 0}, - {(char *) "func_globals", (getter)__Pyx_CyFunction_get_globals, 0, 0, 0}, - {(char *) "__globals__", (getter)__Pyx_CyFunction_get_globals, 0, 0, 0}, - {(char *) "func_closure", (getter)__Pyx_CyFunction_get_closure, 0, 0, 0}, - {(char *) "__closure__", (getter)__Pyx_CyFunction_get_closure, 0, 0, 0}, - {(char *) "func_code", (getter)__Pyx_CyFunction_get_code, 0, 0, 0}, - {(char *) "__code__", (getter)__Pyx_CyFunction_get_code, 0, 0, 0}, - {(char *) "func_defaults", (getter)__Pyx_CyFunction_get_defaults, (setter)__Pyx_CyFunction_set_defaults, 0, 0}, - {(char *) "__defaults__", (getter)__Pyx_CyFunction_get_defaults, (setter)__Pyx_CyFunction_set_defaults, 0, 0}, - {(char *) "__kwdefaults__", (getter)__Pyx_CyFunction_get_kwdefaults, (setter)__Pyx_CyFunction_set_kwdefaults, 0, 0}, - {(char *) "__annotations__", (getter)__Pyx_CyFunction_get_annotations, (setter)__Pyx_CyFunction_set_annotations, 0, 0}, - {0, 0, 0, 0, 0} -}; -static PyMemberDef __pyx_CyFunction_members[] = { - {(char *) "__module__", T_OBJECT, offsetof(__pyx_CyFunctionObject, func.m_module), PY_WRITE_RESTRICTED, 0}, - {0, 0, 0, 0, 0} -}; -static PyObject * -__Pyx_CyFunction_reduce(__pyx_CyFunctionObject *m, CYTHON_UNUSED PyObject *args) -{ -#if PY_MAJOR_VERSION >= 3 - return PyUnicode_FromString(m->func.m_ml->ml_name); -#else - return PyString_FromString(m->func.m_ml->ml_name); -#endif -} -static PyMethodDef __pyx_CyFunction_methods[] = { - {"__reduce__", (PyCFunction)__Pyx_CyFunction_reduce, METH_VARARGS, 0}, - {0, 0, 0, 0} -}; -#if PY_VERSION_HEX < 0x030500A0 -#define __Pyx_CyFunction_weakreflist(cyfunc) ((cyfunc)->func_weakreflist) -#else -#define __Pyx_CyFunction_weakreflist(cyfunc) ((cyfunc)->func.m_weakreflist) -#endif -static PyObject *__Pyx_CyFunction_New(PyTypeObject *type, PyMethodDef *ml, int flags, PyObject* qualname, - PyObject *closure, PyObject *module, PyObject* globals, PyObject* code) { - __pyx_CyFunctionObject *op = PyObject_GC_New(__pyx_CyFunctionObject, type); - if (op == NULL) - return NULL; - op->flags = flags; - __Pyx_CyFunction_weakreflist(op) = NULL; - op->func.m_ml = ml; - op->func.m_self = (PyObject *) op; - Py_XINCREF(closure); - op->func_closure = closure; - Py_XINCREF(module); - op->func.m_module = module; - op->func_dict = NULL; - op->func_name = NULL; - Py_INCREF(qualname); - op->func_qualname = qualname; - op->func_doc = NULL; - op->func_classobj = NULL; - op->func_globals = globals; - Py_INCREF(op->func_globals); - Py_XINCREF(code); - op->func_code = code; - op->defaults_pyobjects = 0; - op->defaults = NULL; - op->defaults_tuple = NULL; - op->defaults_kwdict = NULL; - op->defaults_getter = NULL; - op->func_annotations = NULL; - PyObject_GC_Track(op); - return (PyObject *) op; -} -static int -__Pyx_CyFunction_clear(__pyx_CyFunctionObject *m) -{ - Py_CLEAR(m->func_closure); - Py_CLEAR(m->func.m_module); - Py_CLEAR(m->func_dict); - Py_CLEAR(m->func_name); - Py_CLEAR(m->func_qualname); - Py_CLEAR(m->func_doc); - Py_CLEAR(m->func_globals); - Py_CLEAR(m->func_code); - Py_CLEAR(m->func_classobj); - Py_CLEAR(m->defaults_tuple); - Py_CLEAR(m->defaults_kwdict); - Py_CLEAR(m->func_annotations); - if (m->defaults) { - PyObject **pydefaults = __Pyx_CyFunction_Defaults(PyObject *, m); - int i; - for (i = 0; i < m->defaults_pyobjects; i++) - Py_XDECREF(pydefaults[i]); - PyObject_Free(m->defaults); - m->defaults = NULL; - } - return 0; -} -static void __Pyx_CyFunction_dealloc(__pyx_CyFunctionObject *m) -{ - PyObject_GC_UnTrack(m); - if (__Pyx_CyFunction_weakreflist(m) != NULL) - PyObject_ClearWeakRefs((PyObject *) m); - __Pyx_CyFunction_clear(m); - PyObject_GC_Del(m); -} -static int __Pyx_CyFunction_traverse(__pyx_CyFunctionObject *m, visitproc visit, void *arg) -{ - Py_VISIT(m->func_closure); - Py_VISIT(m->func.m_module); - Py_VISIT(m->func_dict); - Py_VISIT(m->func_name); - Py_VISIT(m->func_qualname); - Py_VISIT(m->func_doc); - Py_VISIT(m->func_globals); - Py_VISIT(m->func_code); - Py_VISIT(m->func_classobj); - Py_VISIT(m->defaults_tuple); - Py_VISIT(m->defaults_kwdict); - if (m->defaults) { - PyObject **pydefaults = __Pyx_CyFunction_Defaults(PyObject *, m); - int i; - for (i = 0; i < m->defaults_pyobjects; i++) - Py_VISIT(pydefaults[i]); - } - return 0; -} -static PyObject *__Pyx_CyFunction_descr_get(PyObject *func, PyObject *obj, PyObject *type) -{ - __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func; - if (m->flags & __Pyx_CYFUNCTION_STATICMETHOD) { - Py_INCREF(func); - return func; - } - if (m->flags & __Pyx_CYFUNCTION_CLASSMETHOD) { - if (type == NULL) - type = (PyObject *)(Py_TYPE(obj)); - return __Pyx_PyMethod_New(func, type, (PyObject *)(Py_TYPE(type))); - } - if (obj == Py_None) - obj = NULL; - return __Pyx_PyMethod_New(func, obj, type); -} -static PyObject* -__Pyx_CyFunction_repr(__pyx_CyFunctionObject *op) -{ -#if PY_MAJOR_VERSION >= 3 - return PyUnicode_FromFormat("", - op->func_qualname, (void *)op); -#else - return PyString_FromFormat("", - PyString_AsString(op->func_qualname), (void *)op); -#endif -} -#if CYTHON_COMPILING_IN_PYPY -static PyObject * __Pyx_CyFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) { - PyCFunctionObject* f = (PyCFunctionObject*)func; - PyCFunction meth = f->m_ml->ml_meth; - PyObject *self = f->m_self; - Py_ssize_t size; - switch (f->m_ml->ml_flags & (METH_VARARGS | METH_KEYWORDS | METH_NOARGS | METH_O)) { - case METH_VARARGS: - if (likely(kw == NULL || PyDict_Size(kw) == 0)) - return (*meth)(self, arg); - break; - case METH_VARARGS | METH_KEYWORDS: - return (*(PyCFunctionWithKeywords)meth)(self, arg, kw); - case METH_NOARGS: - if (likely(kw == NULL || PyDict_Size(kw) == 0)) { - size = PyTuple_GET_SIZE(arg); - if (likely(size == 0)) - return (*meth)(self, NULL); - PyErr_Format(PyExc_TypeError, - "%.200s() takes no arguments (%" CYTHON_FORMAT_SSIZE_T "d given)", - f->m_ml->ml_name, size); - return NULL; - } - break; - case METH_O: - if (likely(kw == NULL || PyDict_Size(kw) == 0)) { - size = PyTuple_GET_SIZE(arg); - if (likely(size == 1)) { - PyObject *result, *arg0 = PySequence_ITEM(arg, 0); - if (unlikely(!arg0)) return NULL; - result = (*meth)(self, arg0); - Py_DECREF(arg0); - return result; - } - PyErr_Format(PyExc_TypeError, - "%.200s() takes exactly one argument (%" CYTHON_FORMAT_SSIZE_T "d given)", - f->m_ml->ml_name, size); - return NULL; - } - break; - default: - PyErr_SetString(PyExc_SystemError, "Bad call flags in " - "__Pyx_CyFunction_Call. METH_OLDARGS is no " - "longer supported!"); - return NULL; - } - PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", - f->m_ml->ml_name); - return NULL; -} -#else -static PyObject * __Pyx_CyFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) { - return PyCFunction_Call(func, arg, kw); -} -#endif -static PyTypeObject __pyx_CyFunctionType_type = { - PyVarObject_HEAD_INIT(0, 0) - "cython_function_or_method", - sizeof(__pyx_CyFunctionObject), - 0, - (destructor) __Pyx_CyFunction_dealloc, - 0, - 0, - 0, -#if PY_MAJOR_VERSION < 3 - 0, -#else - 0, -#endif - (reprfunc) __Pyx_CyFunction_repr, - 0, - 0, - 0, - 0, - __Pyx_CyFunction_Call, - 0, - 0, - 0, - 0, - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, - 0, - (traverseproc) __Pyx_CyFunction_traverse, - (inquiry) __Pyx_CyFunction_clear, - 0, -#if PY_VERSION_HEX < 0x030500A0 - offsetof(__pyx_CyFunctionObject, func_weakreflist), -#else - offsetof(PyCFunctionObject, m_weakreflist), -#endif - 0, - 0, - __pyx_CyFunction_methods, - __pyx_CyFunction_members, - __pyx_CyFunction_getsets, - 0, - 0, - __Pyx_CyFunction_descr_get, - 0, - offsetof(__pyx_CyFunctionObject, func_dict), - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, -#if PY_VERSION_HEX >= 0x030400a1 - 0, -#endif -}; -static int __pyx_CyFunction_init(void) { -#if !CYTHON_COMPILING_IN_PYPY - __pyx_CyFunctionType_type.tp_call = PyCFunction_Call; -#endif - __pyx_CyFunctionType = __Pyx_FetchCommonType(&__pyx_CyFunctionType_type); - if (__pyx_CyFunctionType == NULL) { - return -1; - } - return 0; -} -static CYTHON_INLINE void *__Pyx_CyFunction_InitDefaults(PyObject *func, size_t size, int pyobjects) { - __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func; - m->defaults = PyObject_Malloc(size); - if (!m->defaults) - return PyErr_NoMemory(); - memset(m->defaults, 0, size); - m->defaults_pyobjects = pyobjects; - return m->defaults; -} -static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsTuple(PyObject *func, PyObject *tuple) { - __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func; - m->defaults_tuple = tuple; - Py_INCREF(tuple); -} -static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsKwDict(PyObject *func, PyObject *dict) { - __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func; - m->defaults_kwdict = dict; - Py_INCREF(dict); -} -static CYTHON_INLINE void __Pyx_CyFunction_SetAnnotationsDict(PyObject *func, PyObject *dict) { - __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func; - m->func_annotations = dict; - Py_INCREF(dict); -} - -/* CalculateMetaclass */ - static PyObject *__Pyx_CalculateMetaclass(PyTypeObject *metaclass, PyObject *bases) { - Py_ssize_t i, nbases = PyTuple_GET_SIZE(bases); - for (i=0; i < nbases; i++) { - PyTypeObject *tmptype; - PyObject *tmp = PyTuple_GET_ITEM(bases, i); - tmptype = Py_TYPE(tmp); -#if PY_MAJOR_VERSION < 3 - if (tmptype == &PyClass_Type) - continue; -#endif - if (!metaclass) { - metaclass = tmptype; - continue; - } - if (PyType_IsSubtype(metaclass, tmptype)) - continue; - if (PyType_IsSubtype(tmptype, metaclass)) { - metaclass = tmptype; - continue; - } - PyErr_SetString(PyExc_TypeError, - "metaclass conflict: " - "the metaclass of a derived class " - "must be a (non-strict) subclass " - "of the metaclasses of all its bases"); - return NULL; - } - if (!metaclass) { -#if PY_MAJOR_VERSION < 3 - metaclass = &PyClass_Type; -#else - metaclass = &PyType_Type; -#endif - } - Py_INCREF((PyObject*) metaclass); - return (PyObject*) metaclass; -} - -/* Py3ClassCreate */ - static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name, - PyObject *qualname, PyObject *mkw, PyObject *modname, PyObject *doc) { - PyObject *ns; - if (metaclass) { - PyObject *prep = __Pyx_PyObject_GetAttrStr(metaclass, __pyx_n_s_prepare); - if (prep) { - PyObject *pargs = PyTuple_Pack(2, name, bases); - if (unlikely(!pargs)) { - Py_DECREF(prep); - return NULL; - } - ns = PyObject_Call(prep, pargs, mkw); - Py_DECREF(prep); - Py_DECREF(pargs); - } else { - if (unlikely(!PyErr_ExceptionMatches(PyExc_AttributeError))) - return NULL; - PyErr_Clear(); - ns = PyDict_New(); - } - } else { - ns = PyDict_New(); - } - if (unlikely(!ns)) - return NULL; - if (unlikely(PyObject_SetItem(ns, __pyx_n_s_module, modname) < 0)) goto bad; - if (unlikely(PyObject_SetItem(ns, __pyx_n_s_qualname, qualname) < 0)) goto bad; - if (unlikely(doc && PyObject_SetItem(ns, __pyx_n_s_doc, doc) < 0)) goto bad; - return ns; -bad: - Py_DECREF(ns); - return NULL; -} -static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases, - PyObject *dict, PyObject *mkw, - int calculate_metaclass, int allow_py2_metaclass) { - PyObject *result, *margs; - PyObject *owned_metaclass = NULL; - if (allow_py2_metaclass) { - owned_metaclass = PyObject_GetItem(dict, __pyx_n_s_metaclass); - if (owned_metaclass) { - metaclass = owned_metaclass; - } else if (likely(PyErr_ExceptionMatches(PyExc_KeyError))) { - PyErr_Clear(); - } else { - return NULL; - } - } - if (calculate_metaclass && (!metaclass || PyType_Check(metaclass))) { - metaclass = __Pyx_CalculateMetaclass((PyTypeObject*) metaclass, bases); - Py_XDECREF(owned_metaclass); - if (unlikely(!metaclass)) - return NULL; - owned_metaclass = metaclass; - } - margs = PyTuple_Pack(3, name, bases, dict); - if (unlikely(!margs)) { - result = NULL; - } else { - result = PyObject_Call(metaclass, margs, mkw); - Py_DECREF(margs); - } - Py_XDECREF(owned_metaclass); - return result; -} - -/* CodeObjectCache */ - static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line) { - int start = 0, mid = 0, end = count - 1; - if (end >= 0 && code_line > entries[end].code_line) { - return count; - } - while (start < end) { - mid = start + (end - start) / 2; - if (code_line < entries[mid].code_line) { - end = mid; - } else if (code_line > entries[mid].code_line) { - start = mid + 1; - } else { - return mid; - } - } - if (code_line <= entries[mid].code_line) { - return mid; - } else { - return mid + 1; - } -} -static PyCodeObject *__pyx_find_code_object(int code_line) { - PyCodeObject* code_object; - int pos; - if (unlikely(!code_line) || unlikely(!__pyx_code_cache.entries)) { - return NULL; - } - pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line); - if (unlikely(pos >= __pyx_code_cache.count) || unlikely(__pyx_code_cache.entries[pos].code_line != code_line)) { - return NULL; - } - code_object = __pyx_code_cache.entries[pos].code_object; - Py_INCREF(code_object); - return code_object; -} -static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object) { - int pos, i; - __Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries; - if (unlikely(!code_line)) { - return; - } - if (unlikely(!entries)) { - entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Malloc(64*sizeof(__Pyx_CodeObjectCacheEntry)); - if (likely(entries)) { - __pyx_code_cache.entries = entries; - __pyx_code_cache.max_count = 64; - __pyx_code_cache.count = 1; - entries[0].code_line = code_line; - entries[0].code_object = code_object; - Py_INCREF(code_object); - } - return; - } - pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line); - if ((pos < __pyx_code_cache.count) && unlikely(__pyx_code_cache.entries[pos].code_line == code_line)) { - PyCodeObject* tmp = entries[pos].code_object; - entries[pos].code_object = code_object; - Py_DECREF(tmp); - return; - } - if (__pyx_code_cache.count == __pyx_code_cache.max_count) { - int new_max = __pyx_code_cache.max_count + 64; - entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Realloc( - __pyx_code_cache.entries, (size_t)new_max*sizeof(__Pyx_CodeObjectCacheEntry)); - if (unlikely(!entries)) { - return; - } - __pyx_code_cache.entries = entries; - __pyx_code_cache.max_count = new_max; - } - for (i=__pyx_code_cache.count; i>pos; i--) { - entries[i] = entries[i-1]; - } - entries[pos].code_line = code_line; - entries[pos].code_object = code_object; - __pyx_code_cache.count++; - Py_INCREF(code_object); -} - -/* AddTraceback */ - #include "compile.h" -#include "frameobject.h" -#include "traceback.h" -static PyCodeObject* __Pyx_CreateCodeObjectForTraceback( - const char *funcname, int c_line, - int py_line, const char *filename) { - PyCodeObject *py_code = 0; - PyObject *py_srcfile = 0; - PyObject *py_funcname = 0; - #if PY_MAJOR_VERSION < 3 - py_srcfile = PyString_FromString(filename); - #else - py_srcfile = PyUnicode_FromString(filename); - #endif - if (!py_srcfile) goto bad; - if (c_line) { - #if PY_MAJOR_VERSION < 3 - py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, c_line); - #else - py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, c_line); - #endif - } - else { - #if PY_MAJOR_VERSION < 3 - py_funcname = PyString_FromString(funcname); - #else - py_funcname = PyUnicode_FromString(funcname); - #endif - } - if (!py_funcname) goto bad; - py_code = __Pyx_PyCode_New( - 0, - 0, - 0, - 0, - 0, - __pyx_empty_bytes, /*PyObject *code,*/ - __pyx_empty_tuple, /*PyObject *consts,*/ - __pyx_empty_tuple, /*PyObject *names,*/ - __pyx_empty_tuple, /*PyObject *varnames,*/ - __pyx_empty_tuple, /*PyObject *freevars,*/ - __pyx_empty_tuple, /*PyObject *cellvars,*/ - py_srcfile, /*PyObject *filename,*/ - py_funcname, /*PyObject *name,*/ - py_line, - __pyx_empty_bytes /*PyObject *lnotab*/ - ); - Py_DECREF(py_srcfile); - Py_DECREF(py_funcname); - return py_code; -bad: - Py_XDECREF(py_srcfile); - Py_XDECREF(py_funcname); - return NULL; -} -static void __Pyx_AddTraceback(const char *funcname, int c_line, - int py_line, const char *filename) { - PyCodeObject *py_code = 0; - PyFrameObject *py_frame = 0; - py_code = __pyx_find_code_object(c_line ? c_line : py_line); - if (!py_code) { - py_code = __Pyx_CreateCodeObjectForTraceback( - funcname, c_line, py_line, filename); - if (!py_code) goto bad; - __pyx_insert_code_object(c_line ? c_line : py_line, py_code); - } - py_frame = PyFrame_New( - PyThreadState_GET(), /*PyThreadState *tstate,*/ - py_code, /*PyCodeObject *code,*/ - __pyx_d, /*PyObject *globals,*/ - 0 /*PyObject *locals*/ - ); - if (!py_frame) goto bad; - py_frame->f_lineno = py_line; - PyTraceBack_Here(py_frame); -bad: - Py_XDECREF(py_code); - Py_XDECREF(py_frame); -} - -/* CIntFromPyVerify */ - #define __PYX_VERIFY_RETURN_INT(target_type, func_type, func_value)\ - __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, 0) -#define __PYX_VERIFY_RETURN_INT_EXC(target_type, func_type, func_value)\ - __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, 1) -#define __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, exc)\ - {\ - func_type value = func_value;\ - if (sizeof(target_type) < sizeof(func_type)) {\ - if (unlikely(value != (func_type) (target_type) value)) {\ - func_type zero = 0;\ - if (exc && unlikely(value == (func_type)-1 && PyErr_Occurred()))\ - return (target_type) -1;\ - if (is_unsigned && unlikely(value < zero))\ - goto raise_neg_overflow;\ - else\ - goto raise_overflow;\ - }\ - }\ - return (target_type) value;\ - } - -/* CIntToPy */ - static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) { - const long neg_one = (long) -1, const_zero = (long) 0; - const int is_unsigned = neg_one > const_zero; - if (is_unsigned) { - if (sizeof(long) < sizeof(long)) { - return PyInt_FromLong((long) value); - } else if (sizeof(long) <= sizeof(unsigned long)) { - return PyLong_FromUnsignedLong((unsigned long) value); - } else if (sizeof(long) <= sizeof(unsigned PY_LONG_LONG)) { - return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG) value); - } - } else { - if (sizeof(long) <= sizeof(long)) { - return PyInt_FromLong((long) value); - } else if (sizeof(long) <= sizeof(PY_LONG_LONG)) { - return PyLong_FromLongLong((PY_LONG_LONG) value); - } - } - { - int one = 1; int little = (int)*(unsigned char *)&one; - unsigned char *bytes = (unsigned char *)&value; - return _PyLong_FromByteArray(bytes, sizeof(long), - little, !is_unsigned); - } -} - -/* CIntFromPy */ - static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) { - const int neg_one = (int) -1, const_zero = (int) 0; - const int is_unsigned = neg_one > const_zero; -#if PY_MAJOR_VERSION < 3 - if (likely(PyInt_Check(x))) { - if (sizeof(int) < sizeof(long)) { - __PYX_VERIFY_RETURN_INT(int, long, PyInt_AS_LONG(x)) - } else { - long val = PyInt_AS_LONG(x); - if (is_unsigned && unlikely(val < 0)) { - goto raise_neg_overflow; - } - return (int) val; - } - } else -#endif - if (likely(PyLong_Check(x))) { - if (is_unsigned) { -#if CYTHON_USE_PYLONG_INTERNALS - const digit* digits = ((PyLongObject*)x)->ob_digit; - switch (Py_SIZE(x)) { - case 0: return (int) 0; - case 1: __PYX_VERIFY_RETURN_INT(int, digit, digits[0]) - case 2: - if (8 * sizeof(int) > 1 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(int) >= 2 * PyLong_SHIFT) { - return (int) (((((int)digits[1]) << PyLong_SHIFT) | (int)digits[0])); - } - } - break; - case 3: - if (8 * sizeof(int) > 2 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(int) >= 3 * PyLong_SHIFT) { - return (int) (((((((int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0])); - } - } - break; - case 4: - if (8 * sizeof(int) > 3 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(int) >= 4 * PyLong_SHIFT) { - return (int) (((((((((int)digits[3]) << PyLong_SHIFT) | (int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0])); - } - } - break; - } -#endif -#if CYTHON_COMPILING_IN_CPYTHON - if (unlikely(Py_SIZE(x) < 0)) { - goto raise_neg_overflow; - } -#else - { - int result = PyObject_RichCompareBool(x, Py_False, Py_LT); - if (unlikely(result < 0)) - return (int) -1; - if (unlikely(result == 1)) - goto raise_neg_overflow; - } -#endif - if (sizeof(int) <= sizeof(unsigned long)) { - __PYX_VERIFY_RETURN_INT_EXC(int, unsigned long, PyLong_AsUnsignedLong(x)) - } else if (sizeof(int) <= sizeof(unsigned PY_LONG_LONG)) { - __PYX_VERIFY_RETURN_INT_EXC(int, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x)) - } - } else { -#if CYTHON_USE_PYLONG_INTERNALS - const digit* digits = ((PyLongObject*)x)->ob_digit; - switch (Py_SIZE(x)) { - case 0: return (int) 0; - case -1: __PYX_VERIFY_RETURN_INT(int, sdigit, (sdigit) (-(sdigit)digits[0])) - case 1: __PYX_VERIFY_RETURN_INT(int, digit, +digits[0]) - case -2: - if (8 * sizeof(int) - 1 > 1 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) { - return (int) (((int)-1)*(((((int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); - } - } - break; - case 2: - if (8 * sizeof(int) > 1 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) { - return (int) ((((((int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); - } - } - break; - case -3: - if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) { - return (int) (((int)-1)*(((((((int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); - } - } - break; - case 3: - if (8 * sizeof(int) > 2 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) { - return (int) ((((((((int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); - } - } - break; - case -4: - if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(int) - 1 > 4 * PyLong_SHIFT) { - return (int) (((int)-1)*(((((((((int)digits[3]) << PyLong_SHIFT) | (int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); - } - } - break; - case 4: - if (8 * sizeof(int) > 3 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(int) - 1 > 4 * PyLong_SHIFT) { - return (int) ((((((((((int)digits[3]) << PyLong_SHIFT) | (int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); - } - } - break; - } -#endif - if (sizeof(int) <= sizeof(long)) { - __PYX_VERIFY_RETURN_INT_EXC(int, long, PyLong_AsLong(x)) - } else if (sizeof(int) <= sizeof(PY_LONG_LONG)) { - __PYX_VERIFY_RETURN_INT_EXC(int, PY_LONG_LONG, PyLong_AsLongLong(x)) - } - } - { -#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray) - PyErr_SetString(PyExc_RuntimeError, - "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers"); -#else - int val; - PyObject *v = __Pyx_PyNumber_IntOrLong(x); - #if PY_MAJOR_VERSION < 3 - if (likely(v) && !PyLong_Check(v)) { - PyObject *tmp = v; - v = PyNumber_Long(tmp); - Py_DECREF(tmp); - } - #endif - if (likely(v)) { - int one = 1; int is_little = (int)*(unsigned char *)&one; - unsigned char *bytes = (unsigned char *)&val; - int ret = _PyLong_AsByteArray((PyLongObject *)v, - bytes, sizeof(val), - is_little, !is_unsigned); - Py_DECREF(v); - if (likely(!ret)) - return val; - } -#endif - return (int) -1; - } - } else { - int val; - PyObject *tmp = __Pyx_PyNumber_IntOrLong(x); - if (!tmp) return (int) -1; - val = __Pyx_PyInt_As_int(tmp); - Py_DECREF(tmp); - return val; - } -raise_overflow: - PyErr_SetString(PyExc_OverflowError, - "value too large to convert to int"); - return (int) -1; -raise_neg_overflow: - PyErr_SetString(PyExc_OverflowError, - "can't convert negative value to int"); - return (int) -1; -} - -/* CIntFromPy */ - static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) { - const long neg_one = (long) -1, const_zero = (long) 0; - const int is_unsigned = neg_one > const_zero; -#if PY_MAJOR_VERSION < 3 - if (likely(PyInt_Check(x))) { - if (sizeof(long) < sizeof(long)) { - __PYX_VERIFY_RETURN_INT(long, long, PyInt_AS_LONG(x)) - } else { - long val = PyInt_AS_LONG(x); - if (is_unsigned && unlikely(val < 0)) { - goto raise_neg_overflow; - } - return (long) val; - } - } else -#endif - if (likely(PyLong_Check(x))) { - if (is_unsigned) { -#if CYTHON_USE_PYLONG_INTERNALS - const digit* digits = ((PyLongObject*)x)->ob_digit; - switch (Py_SIZE(x)) { - case 0: return (long) 0; - case 1: __PYX_VERIFY_RETURN_INT(long, digit, digits[0]) - case 2: - if (8 * sizeof(long) > 1 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(long) >= 2 * PyLong_SHIFT) { - return (long) (((((long)digits[1]) << PyLong_SHIFT) | (long)digits[0])); - } - } - break; - case 3: - if (8 * sizeof(long) > 2 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(long) >= 3 * PyLong_SHIFT) { - return (long) (((((((long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0])); - } - } - break; - case 4: - if (8 * sizeof(long) > 3 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(long) >= 4 * PyLong_SHIFT) { - return (long) (((((((((long)digits[3]) << PyLong_SHIFT) | (long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0])); - } - } - break; - } -#endif -#if CYTHON_COMPILING_IN_CPYTHON - if (unlikely(Py_SIZE(x) < 0)) { - goto raise_neg_overflow; - } -#else - { - int result = PyObject_RichCompareBool(x, Py_False, Py_LT); - if (unlikely(result < 0)) - return (long) -1; - if (unlikely(result == 1)) - goto raise_neg_overflow; - } -#endif - if (sizeof(long) <= sizeof(unsigned long)) { - __PYX_VERIFY_RETURN_INT_EXC(long, unsigned long, PyLong_AsUnsignedLong(x)) - } else if (sizeof(long) <= sizeof(unsigned PY_LONG_LONG)) { - __PYX_VERIFY_RETURN_INT_EXC(long, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x)) - } - } else { -#if CYTHON_USE_PYLONG_INTERNALS - const digit* digits = ((PyLongObject*)x)->ob_digit; - switch (Py_SIZE(x)) { - case 0: return (long) 0; - case -1: __PYX_VERIFY_RETURN_INT(long, sdigit, (sdigit) (-(sdigit)digits[0])) - case 1: __PYX_VERIFY_RETURN_INT(long, digit, +digits[0]) - case -2: - if (8 * sizeof(long) - 1 > 1 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(long, long, -(long) (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { - return (long) (((long)-1)*(((((long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); - } - } - break; - case 2: - if (8 * sizeof(long) > 1 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { - return (long) ((((((long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); - } - } - break; - case -3: - if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(long, long, -(long) (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { - return (long) (((long)-1)*(((((((long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); - } - } - break; - case 3: - if (8 * sizeof(long) > 2 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { - return (long) ((((((((long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); - } - } - break; - case -4: - if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(long, long, -(long) (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(long) - 1 > 4 * PyLong_SHIFT) { - return (long) (((long)-1)*(((((((((long)digits[3]) << PyLong_SHIFT) | (long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); - } - } - break; - case 4: - if (8 * sizeof(long) > 3 * PyLong_SHIFT) { - if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { - __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) - } else if (8 * sizeof(long) - 1 > 4 * PyLong_SHIFT) { - return (long) ((((((((((long)digits[3]) << PyLong_SHIFT) | (long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); - } - } - break; - } -#endif - if (sizeof(long) <= sizeof(long)) { - __PYX_VERIFY_RETURN_INT_EXC(long, long, PyLong_AsLong(x)) - } else if (sizeof(long) <= sizeof(PY_LONG_LONG)) { - __PYX_VERIFY_RETURN_INT_EXC(long, PY_LONG_LONG, PyLong_AsLongLong(x)) - } - } - { -#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray) - PyErr_SetString(PyExc_RuntimeError, - "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers"); -#else - long val; - PyObject *v = __Pyx_PyNumber_IntOrLong(x); - #if PY_MAJOR_VERSION < 3 - if (likely(v) && !PyLong_Check(v)) { - PyObject *tmp = v; - v = PyNumber_Long(tmp); - Py_DECREF(tmp); - } - #endif - if (likely(v)) { - int one = 1; int is_little = (int)*(unsigned char *)&one; - unsigned char *bytes = (unsigned char *)&val; - int ret = _PyLong_AsByteArray((PyLongObject *)v, - bytes, sizeof(val), - is_little, !is_unsigned); - Py_DECREF(v); - if (likely(!ret)) - return val; - } -#endif - return (long) -1; - } - } else { - long val; - PyObject *tmp = __Pyx_PyNumber_IntOrLong(x); - if (!tmp) return (long) -1; - val = __Pyx_PyInt_As_long(tmp); - Py_DECREF(tmp); - return val; - } -raise_overflow: - PyErr_SetString(PyExc_OverflowError, - "value too large to convert to long"); - return (long) -1; -raise_neg_overflow: - PyErr_SetString(PyExc_OverflowError, - "can't convert negative value to long"); - return (long) -1; -} - -/* CheckBinaryVersion */ - static int __Pyx_check_binary_version(void) { - char ctversion[4], rtversion[4]; - PyOS_snprintf(ctversion, 4, "%d.%d", PY_MAJOR_VERSION, PY_MINOR_VERSION); - PyOS_snprintf(rtversion, 4, "%s", Py_GetVersion()); - if (ctversion[0] != rtversion[0] || ctversion[2] != rtversion[2]) { - char message[200]; - PyOS_snprintf(message, sizeof(message), - "compiletime version %s of module '%.100s' " - "does not match runtime version %s", - ctversion, __Pyx_MODULE_NAME, rtversion); - return PyErr_WarnEx(NULL, message, 1); - } - return 0; -} - -/* InitStrings */ - static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) { - while (t->p) { - #if PY_MAJOR_VERSION < 3 - if (t->is_unicode) { - *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL); - } else if (t->intern) { - *t->p = PyString_InternFromString(t->s); - } else { - *t->p = PyString_FromStringAndSize(t->s, t->n - 1); - } - #else - if (t->is_unicode | t->is_str) { - if (t->intern) { - *t->p = PyUnicode_InternFromString(t->s); - } else if (t->encoding) { - *t->p = PyUnicode_Decode(t->s, t->n - 1, t->encoding, NULL); - } else { - *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1); - } - } else { - *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1); - } - #endif - if (!*t->p) - return -1; - ++t; - } - return 0; -} - -static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char* c_str) { - return __Pyx_PyUnicode_FromStringAndSize(c_str, (Py_ssize_t)strlen(c_str)); -} -static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject* o) { - Py_ssize_t ignore; - return __Pyx_PyObject_AsStringAndSize(o, &ignore); -} -static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) { -#if CYTHON_COMPILING_IN_CPYTHON && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT) - if ( -#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII - __Pyx_sys_getdefaultencoding_not_ascii && -#endif - PyUnicode_Check(o)) { -#if PY_VERSION_HEX < 0x03030000 - char* defenc_c; - PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL); - if (!defenc) return NULL; - defenc_c = PyBytes_AS_STRING(defenc); -#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII - { - char* end = defenc_c + PyBytes_GET_SIZE(defenc); - char* c; - for (c = defenc_c; c < end; c++) { - if ((unsigned char) (*c) >= 128) { - PyUnicode_AsASCIIString(o); - return NULL; - } - } - } -#endif - *length = PyBytes_GET_SIZE(defenc); - return defenc_c; -#else - if (__Pyx_PyUnicode_READY(o) == -1) return NULL; -#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII - if (PyUnicode_IS_ASCII(o)) { - *length = PyUnicode_GET_LENGTH(o); - return PyUnicode_AsUTF8(o); - } else { - PyUnicode_AsASCIIString(o); - return NULL; - } -#else - return PyUnicode_AsUTF8AndSize(o, length); -#endif -#endif - } else -#endif -#if (!CYTHON_COMPILING_IN_PYPY) || (defined(PyByteArray_AS_STRING) && defined(PyByteArray_GET_SIZE)) - if (PyByteArray_Check(o)) { - *length = PyByteArray_GET_SIZE(o); - return PyByteArray_AS_STRING(o); - } else -#endif - { - char* result; - int r = PyBytes_AsStringAndSize(o, &result, length); - if (unlikely(r < 0)) { - return NULL; - } else { - return result; - } - } -} -static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) { - int is_true = x == Py_True; - if (is_true | (x == Py_False) | (x == Py_None)) return is_true; - else return PyObject_IsTrue(x); -} -static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x) { - PyNumberMethods *m; - const char *name = NULL; - PyObject *res = NULL; -#if PY_MAJOR_VERSION < 3 - if (PyInt_Check(x) || PyLong_Check(x)) -#else - if (PyLong_Check(x)) -#endif - return __Pyx_NewRef(x); - m = Py_TYPE(x)->tp_as_number; -#if PY_MAJOR_VERSION < 3 - if (m && m->nb_int) { - name = "int"; - res = PyNumber_Int(x); - } - else if (m && m->nb_long) { - name = "long"; - res = PyNumber_Long(x); - } -#else - if (m && m->nb_int) { - name = "int"; - res = PyNumber_Long(x); - } -#endif - if (res) { -#if PY_MAJOR_VERSION < 3 - if (!PyInt_Check(res) && !PyLong_Check(res)) { -#else - if (!PyLong_Check(res)) { -#endif - PyErr_Format(PyExc_TypeError, - "__%.4s__ returned non-%.4s (type %.200s)", - name, name, Py_TYPE(res)->tp_name); - Py_DECREF(res); - return NULL; - } - } - else if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, - "an integer is required"); - } - return res; -} -static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) { - Py_ssize_t ival; - PyObject *x; -#if PY_MAJOR_VERSION < 3 - if (likely(PyInt_CheckExact(b))) { - if (sizeof(Py_ssize_t) >= sizeof(long)) - return PyInt_AS_LONG(b); - else - return PyInt_AsSsize_t(x); - } -#endif - if (likely(PyLong_CheckExact(b))) { - #if CYTHON_USE_PYLONG_INTERNALS - const digit* digits = ((PyLongObject*)b)->ob_digit; - const Py_ssize_t size = Py_SIZE(b); - if (likely(__Pyx_sst_abs(size) <= 1)) { - ival = likely(size) ? digits[0] : 0; - if (size == -1) ival = -ival; - return ival; - } else { - switch (size) { - case 2: - if (8 * sizeof(Py_ssize_t) > 2 * PyLong_SHIFT) { - return (Py_ssize_t) (((((size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); - } - break; - case -2: - if (8 * sizeof(Py_ssize_t) > 2 * PyLong_SHIFT) { - return -(Py_ssize_t) (((((size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); - } - break; - case 3: - if (8 * sizeof(Py_ssize_t) > 3 * PyLong_SHIFT) { - return (Py_ssize_t) (((((((size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); - } - break; - case -3: - if (8 * sizeof(Py_ssize_t) > 3 * PyLong_SHIFT) { - return -(Py_ssize_t) (((((((size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); - } - break; - case 4: - if (8 * sizeof(Py_ssize_t) > 4 * PyLong_SHIFT) { - return (Py_ssize_t) (((((((((size_t)digits[3]) << PyLong_SHIFT) | (size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); - } - break; - case -4: - if (8 * sizeof(Py_ssize_t) > 4 * PyLong_SHIFT) { - return -(Py_ssize_t) (((((((((size_t)digits[3]) << PyLong_SHIFT) | (size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); - } - break; - } - } - #endif - return PyLong_AsSsize_t(b); - } - x = PyNumber_Index(b); - if (!x) return -1; - ival = PyInt_AsSsize_t(x); - Py_DECREF(x); - return ival; -} -static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) { - return PyInt_FromSize_t(ival); -} - - -#endif /* Py_PYTHON_H */ diff --git a/coprocess/python/tyk/gateway.h b/coprocess/python/tyk/gateway.h deleted file mode 100644 index 2ba76426e0e..00000000000 --- a/coprocess/python/tyk/gateway.h +++ /dev/null @@ -1,29 +0,0 @@ -/* Generated by Cython 0.24.1 */ - -#ifndef __PYX_HAVE__gateway -#define __PYX_HAVE__gateway - - -#ifndef __PYX_HAVE_API__gateway - -#ifndef __PYX_EXTERN_C - #ifdef __cplusplus - #define __PYX_EXTERN_C extern "C" - #else - #define __PYX_EXTERN_C extern - #endif -#endif - -#ifndef DL_IMPORT - #define DL_IMPORT(_T) _T -#endif - -#endif /* !__PYX_HAVE_API__gateway */ - -#if PY_MAJOR_VERSION < 3 -PyMODINIT_FUNC initgateway(void); -#else -PyMODINIT_FUNC PyInit_gateway(void); -#endif - -#endif /* !__PYX_HAVE__gateway */ diff --git a/coprocess/python/tyk/gateway.pyx b/coprocess/python/tyk/gateway.pyx deleted file mode 100644 index 4b278b2817b..00000000000 --- a/coprocess/python/tyk/gateway.pyx +++ /dev/null @@ -1,27 +0,0 @@ -# Gateway API Cython binding -# Recompile with: cythonize gateway.pyx - -from sys import exc_info - -cdef extern from "coprocess/api.h": - void TykStoreData(char* key, char* value, int ttl); - char* TykGetData(char* key); - void TykTriggerEvent(char* event_name, char* payload); - void CoProcessLog(char *msg, char *level); - -class TykGateway: - def store_data(key, value, ttl): - TykStoreData( key.encode('utf-8'), value.encode('utf-8'), ttl) - def get_data(key): - output = TykGetData(key.encode('utf-8')) - return output.decode('utf-8') - def trigger_event(event_name, payload): - TykTriggerEvent( event_name.encode('utf-8'), payload.encode('utf-8')) - def log(msg, level): - CoProcessLog( msg.encode('utf-8'), level.encode('utf-8') ) - def log_error(*args): - excp = exc_info() - if len(args) == 0: - TykGateway.log( "{0} {1}".format( excp[0], excp[1] ), "error" ) - else: - TykGateway.log( "{0} {1} {2}".format( args[0], excp[0], excp[1] ), "error" ) diff --git a/coprocess/python/tyk/gateway_wrapper.c b/coprocess/python/tyk/gateway_wrapper.c new file mode 100644 index 00000000000..73780930b27 --- /dev/null +++ b/coprocess/python/tyk/gateway_wrapper.c @@ -0,0 +1,78 @@ +// +build coprocess +// +build python + +#include +#include "coprocess/api.h" + + +static PyObject *store_data(PyObject *self, PyObject *args) { + char *key, *value; + int ttl; + + if (!PyArg_ParseTuple(args, "ssi", &key, &value, &ttl)) + return NULL; + + TykStoreData(key, value, ttl); + + Py_RETURN_NONE; +} + +static PyObject *get_data(PyObject *self, PyObject *args) { + char *key, *value; + PyObject *ret; + + if (!PyArg_ParseTuple(args, "s", &key)) + return NULL; + + value = TykGetData(key); + // TykGetData doesn't currently handle storage errors so let's at least safeguard against null pointer + if (value == NULL) { + PyErr_SetString(PyExc_ValueError, "Null pointer from TykGetData"); + return NULL; + } + ret = Py_BuildValue("s", value); + // CGO mallocs it in TykGetData and Py_BuildValue just copies strings, hence it's our responsibility to free it now + free(value); + + return ret; +} + +static PyObject *trigger_event(PyObject *self, PyObject *args) { + char *name, *payload; + + if (!PyArg_ParseTuple(args, "ss", &name, &payload)) + return NULL; + + TykTriggerEvent(name, payload); + + Py_RETURN_NONE; +} + +static PyObject *coprocess_log(PyObject *self, PyObject *args) { + char *message, *level; + + if (!PyArg_ParseTuple(args, "ss", &message, &level)) + return NULL; + + CoProcessLog(message, level); + + Py_RETURN_NONE; +} + + +static PyMethodDef module_methods[] = { + {"store_data", store_data, METH_VARARGS, "Stores the data in gateway storage by given key and TTL"}, + {"get_data", get_data, METH_VARARGS, "Retrieves the data from gateway storage by given key"}, + {"trigger_event", trigger_event, METH_VARARGS, "Triggers a named gateway event with given payload"}, + {"log", coprocess_log, METH_VARARGS, "Logs a message with given level"}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +static PyModuleDef module = { + PyModuleDef_HEAD_INIT, "gateway_wrapper", NULL, -1, module_methods, + NULL, NULL, NULL, NULL +}; + +PyMODINIT_FUNC PyInit_gateway_wrapper(void) { + return PyModule_Create(&module); +} diff --git a/coprocess/python/tyk/gateway_wrapper.h b/coprocess/python/tyk/gateway_wrapper.h new file mode 100644 index 00000000000..bffd6bf87e3 --- /dev/null +++ b/coprocess/python/tyk/gateway_wrapper.h @@ -0,0 +1,8 @@ +#ifndef GATEWAY_WRAPPER_H +#define GATEWAY_WRAPPER_H + +#include + +PyMODINIT_FUNC PyInit_gateway_wrapper(void); + +#endif diff --git a/coprocess/python/tyk/middleware.py b/coprocess/python/tyk/middleware.py index c0863e4a27e..4152160448a 100644 --- a/coprocess/python/tyk/middleware.py +++ b/coprocess/python/tyk/middleware.py @@ -3,16 +3,17 @@ from importlib.machinery import SourceFileLoader -import inspect, sys +import inspect import tyk.decorators as decorators from gateway import TykGateway as tyk -HandlerDecorators = list( map( lambda m: m[1], inspect.getmembers(decorators, inspect.isclass) ) ) +HandlerDecorators = list(map(lambda m: m[1], inspect.getmembers(decorators, inspect.isclass))) + class TykMiddleware: def __init__(self, module_path, module_name): - tyk.log( "Loading module: '{0}'".format(module_name), "info") + tyk.log("Loading module: '{0}'".format(module_name), "info") self.module_path = module_path self.handlers = {} try: @@ -20,7 +21,7 @@ def __init__(self, module_path, module_name): self.module = source.load_module() self.register_handlers() except: - tyk.log_error( "Middleware initialization error:" ) + tyk.log_error("Middleware initialization error:") def register_handlers(self): new_handlers = {} @@ -41,13 +42,12 @@ def reload(self): reload_module(self.module) self.register_handlers() except: - tyk.log_error( "Reload error:" ) + tyk.log_error("Reload error:") def process(self, handler, object): - handlerType = type(handler) - if handler.arg_count == 4: - object.request, object.session, object.metadata = handler(object.request, object.session, object.metadata, object.spec) + object.request, object.session, object.metadata = handler( + object.request, object.session, object.metadata, object.spec) elif handler.arg_count == 3: object.request, object.session = handler(object.request, object.session, object.spec) return object diff --git a/coprocess/python/tyk/object.py b/coprocess/python/tyk/object.py index cd6fdc17aa8..c57d42928bb 100644 --- a/coprocess/python/tyk/object.py +++ b/coprocess/python/tyk/object.py @@ -1,5 +1,3 @@ -import sys - from tyk.session import TykSession from tyk.request import TykCoProcessRequest @@ -10,6 +8,7 @@ from coprocess_return_overrides_pb2 import ReturnOverrides from coprocess_session_state_pb2 import SessionState + class TykCoProcessObject: def __init__(self, object_msg): self.object = Object() diff --git a/coprocess/python/tyk/request.py b/coprocess/python/tyk/request.py index 01d76053f7e..b3c70ee57b4 100644 --- a/coprocess/python/tyk/request.py +++ b/coprocess/python/tyk/request.py @@ -1,14 +1,19 @@ class TykCoProcessRequest(): def __init__(self, request): self.object = request + def add_header(self, key, value): self.object.set_headers[key] = value + def delete_header(self, key): self.object.delete_headers.append(key) + def add_param(self, key, value): self.object.add_params[key] = value + def delete_param(self, key): self.object.delete_params.append(key) + def get_header(self, key): if key in self.object.headers: return self.object.headers[key] diff --git a/coprocess/python/tyk/tyk.py b/coprocess/python/tyk/tyk.py deleted file mode 100644 index 8b137891791..00000000000 --- a/coprocess/python/tyk/tyk.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/coprocess_python.go b/coprocess_python.go index 9daaafd061c..7afae9b1e0a 100644 --- a/coprocess_python.go +++ b/coprocess_python.go @@ -5,7 +5,7 @@ package main /* #cgo pkg-config: python3 -#cgo python CFLAGS: -DENABLE_PYTHON +#cgo python CFLAGS: -DENABLE_PYTHON -DPy_LIMITED_API #include @@ -20,17 +20,17 @@ package main #include "coprocess/python/binding.h" #include "coprocess/python/dispatcher.h" -#include "coprocess/python/tyk/gateway.h" +#include "coprocess/python/tyk/gateway_wrapper.h" PyGILState_STATE gilState; static int Python_Init() { CoProcessLog( sdsnew("Initializing interpreter, Py_Initialize()"), "info"); + // This exposes the glue module as "gateway_wrapper" + PyImport_AppendInittab("gateway_wrapper", &PyInit_gateway_wrapper); Py_Initialize(); gilState = PyGILState_Ensure(); PyEval_InitThreads(); - // This exposes the Cython interface as "gateway" - PyInit_gateway(); return Py_IsInitialized(); } diff --git a/coprocess_python_api.c b/coprocess_python_api.c index 6830646ee78..843480a5d81 120000 --- a/coprocess_python_api.c +++ b/coprocess_python_api.c @@ -1 +1 @@ -coprocess/python/tyk/gateway.c \ No newline at end of file +coprocess/python/tyk/gateway_wrapper.c \ No newline at end of file From 8f6bd5bd7fc92d4620521248c6e0f8548a4bd188 Mon Sep 17 00:00:00 2001 From: dencoded <33698537+dencoded@users.noreply.github.com> Date: Tue, 13 Feb 2018 23:54:43 -0500 Subject: [PATCH 13/20] compile tracked endpoints logic change to set empty path --- api_definition.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api_definition.go b/api_definition.go index 39a10ab66a4..c75d04650cc 100644 --- a/api_definition.go +++ b/api_definition.go @@ -696,6 +696,13 @@ func (a APIDefinitionLoader) compileTrackedEndpointPathspathSpec(paths []apidef. for _, stringSpec := range paths { newSpec := URLSpec{} a.generateRegex(stringSpec.Path, &newSpec, stat) + + // set Path if it wasn't set + if stringSpec.Path == "" { + // even if it is empty (and regex matches everything) some middlewares expect to be value here + stringSpec.Path = "/" + } + // Extend with method actions newSpec.TrackEndpoint = stringSpec urlSpec = append(urlSpec, newSpec) From acfd0998dafef1b2c8d1a1b379123f08884b6839 Mon Sep 17 00:00:00 2001 From: Luan van Pletsen Date: Mon, 19 Feb 2018 17:20:40 +0000 Subject: [PATCH 14/20] Update tyk.conf.example (#1480) Remove mongo fields from install files --- install/data/tyk.self_contained.conf | 8 +------- install/data/tyk.with_dash.conf | 8 +------- tyk.conf.example | 7 +------ 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/install/data/tyk.self_contained.conf b/install/data/tyk.self_contained.conf index c65d8c7a5b1..e5658799425 100644 --- a/install/data/tyk.self_contained.conf +++ b/install/data/tyk.self_contained.conf @@ -17,13 +17,7 @@ }, "enable_analytics": false, "analytics_config": { - "type": "csv", - "pool_size": 100, - "csv_dir": "/tmp", - "mongo_url": "", - "mongo_db_name": "", - "mongo_collection": "", - "purge_delay": -1, + "type": "", "ignored_ips": [], "normalise_urls": { "enabled": true, diff --git a/install/data/tyk.with_dash.conf b/install/data/tyk.with_dash.conf index c6390b54fad..b22ed3fd898 100644 --- a/install/data/tyk.with_dash.conf +++ b/install/data/tyk.with_dash.conf @@ -24,13 +24,7 @@ }, "enable_analytics": true, "analytics_config": { - "type": "mongo", - "pool_size": 100, - "csv_dir": "/tmp", - "mongo_url": "", - "mongo_db_name": "", - "mongo_collection": "", - "purge_delay": 100, + "type": "", "ignored_ips": [], "enable_detailed_recording": true, "enable_geo_ip": false, diff --git a/tyk.conf.example b/tyk.conf.example index e2ddd18c53f..b88e6adb1cc 100644 --- a/tyk.conf.example +++ b/tyk.conf.example @@ -18,12 +18,7 @@ }, "enable_analytics": false, "analytics_config": { - "type": "csv", - "csv_dir": "/tmp", - "mongo_url": "", - "mongo_db_name": "", - "mongo_collection": "", - "purge_delay": -1, + "type": "", "ignored_ips": [] }, "optimisations_use_async_session_write": true, From f8e0db2d6bb2910fdaacc0f9e4a26ad3f93dcbc4 Mon Sep 17 00:00:00 2001 From: Ahmet Soormally Date: Mon, 19 Feb 2018 22:49:57 +0000 Subject: [PATCH 15/20] bug: cpuProfile & memProfile dont populate Current scenario, `start()` is called, but immediately returns. As such, `defer memProfFile.Close()` and `defer pprof.StopCPUProfile()` get called too quickly. This change moves the profiling logic just after `start()`. --- main.go | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 80cc4223b32..5fc715ec517 100644 --- a/main.go +++ b/main.go @@ -1091,6 +1091,28 @@ func main() { start() + if *memProfile { + log.WithFields(logrus.Fields{ + "prefix": "main", + }).Debug("Memory profiling active") + var err error + if memProfFile, err = os.Create("tyk.mprof"); err != nil { + panic(err) + } + defer memProfFile.Close() + } + if *cpuProfile { + log.WithFields(logrus.Fields{ + "prefix": "main", + }).Info("Cpu profiling active") + cpuProfFile, err := os.Create("tyk.prof") + if err != nil { + panic(err) + } + pprof.StartCPUProfile(cpuProfFile) + defer pprof.StopCPUProfile() + } + if goAgainErr != nil { var err error if l, err = generateListener(config.Global.ListenPort); err != nil { @@ -1146,28 +1168,6 @@ func main() { } func start() { - if *memProfile { - log.WithFields(logrus.Fields{ - "prefix": "main", - }).Debug("Memory profiling active") - var err error - if memProfFile, err = os.Create("tyk.mprof"); err != nil { - panic(err) - } - defer memProfFile.Close() - } - if *cpuProfile { - log.WithFields(logrus.Fields{ - "prefix": "main", - }).Info("Cpu profiling active") - cpuProfFile, err := os.Create("tyk.prof") - if err != nil { - panic(err) - } - pprof.StartCPUProfile(cpuProfFile) - defer pprof.StopCPUProfile() - } - // Set up a default org manager so we can traverse non-live paths if !config.Global.SupressDefaultOrgStore { log.WithFields(logrus.Fields{ From 4f89433ee4d2eea8baf55c0d2383dc0635f2c7b1 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Thu, 15 Feb 2018 22:16:11 +0300 Subject: [PATCH 16/20] Improve RPC stability in case of master failures tbd --- api_definition.go | 8 +- api_definition_test.go | 75 ---------------- api_loader.go | 2 - config/config.go | 1 + gateway_test.go | 3 + ldap_auth_handler.go | 6 ++ lint/schema.go | 3 + main.go | 46 ++++++---- policy.go | 38 +++++--- rpc_analytics_purger.go | 37 +++++++- rpc_backup_handlers.go | 114 ++++++++++++------------ rpc_storage_handler.go | 73 ++++++++++------ rpc_test.go | 184 +++++++++++++++++++++++++++++++++++++++ storage/redis_cluster.go | 11 ++- storage/storage.go | 1 + 15 files changed, 410 insertions(+), 192 deletions(-) create mode 100644 rpc_test.go diff --git a/api_definition.go b/api_definition.go index c75d04650cc..9bc6928fc7a 100644 --- a/api_definition.go +++ b/api_definition.go @@ -301,8 +301,14 @@ func (a APIDefinitionLoader) FromDashboardService(endpoint, secret string) []*AP // FromCloud will connect and download ApiDefintions from a Mongo DB instance. func (a APIDefinitionLoader) FromRPC(orgId string) []*APISpec { + if rpcEmergencyMode { + return LoadDefinitionsFromRPCBackup() + } + store := RPCStorageHandler{UserKey: config.Global.SlaveOptions.APIKey, Address: config.Global.SlaveOptions.ConnectionString} - store.Connect() + if !store.Connect() { + return nil + } // enable segments var tags []string diff --git a/api_definition_test.go b/api_definition_test.go index d95a2e1d180..4f3d72e33c7 100644 --- a/api_definition_test.go +++ b/api_definition_test.go @@ -12,7 +12,6 @@ import ( "time" "github.com/garyburd/redigo/redis" - "github.com/lonelycode/gorpc" "github.com/TykTechnologies/tyk/apidef" "github.com/TykTechnologies/tyk/config" @@ -191,80 +190,6 @@ func TestIgnored(t *testing.T) { }) } -func startRPCMock(dispatcher *gorpc.Dispatcher) *gorpc.Server { - configMu.Lock() - defer configMu.Unlock() - - config.Global.SlaveOptions.UseRPC = true - config.Global.SlaveOptions.RPCKey = "test_org" - config.Global.SlaveOptions.APIKey = "test" - - server := gorpc.NewTCPServer("127.0.0.1:0", dispatcher.NewHandlerFunc()) - list := &customListener{} - server.Listener = list - server.LogError = gorpc.NilErrorLogger - - if err := server.Start(); err != nil { - panic(err) - } - config.Global.SlaveOptions.ConnectionString = list.L.Addr().String() - - return server -} - -func stopRPCMock(server *gorpc.Server) { - config.Global.SlaveOptions.ConnectionString = "" - config.Global.SlaveOptions.RPCKey = "" - config.Global.SlaveOptions.APIKey = "" - config.Global.SlaveOptions.UseRPC = false - - server.Listener.Close() - server.Stop() - - RPCCLientSingleton.Stop() - RPCClientIsConnected = false - RPCCLientSingleton = nil - RPCFuncClientSingleton = nil -} - -func TestSyncAPISpecsRPCFailure(t *testing.T) { - // Mock RPC - dispatcher := gorpc.NewDispatcher() - dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) { - return "malformed json", nil - }) - dispatcher.AddFunc("Login", func(clientAddr, userKey string) bool { - return true - }) - - rpc := startRPCMock(dispatcher) - defer stopRPCMock(rpc) - - count := syncAPISpecs() - if count != 0 { - t.Error("Should return empty value for malformed rpc response", apiSpecs) - } -} - -func TestSyncAPISpecsRPCSuccess(t *testing.T) { - // Mock RPC - dispatcher := gorpc.NewDispatcher() - dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) { - return "[{}]", nil - }) - dispatcher.AddFunc("Login", func(clientAddr, userKey string) bool { - return true - }) - - rpc := startRPCMock(dispatcher) - defer stopRPCMock(rpc) - - count := syncAPISpecs() - if count != 1 { - t.Error("Should return array with one spec", apiSpecs) - } -} - func TestSyncAPISpecsDashboardSuccess(t *testing.T) { // Mock Dashboard ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/api_loader.go b/api_loader.go index f1aba6564d1..7b2bd713b52 100644 --- a/api_loader.go +++ b/api_loader.go @@ -667,8 +667,6 @@ func loadApps(specs []*APISpec, muxer *mux.Router) { }).Info("Initialised API Definitions") if config.Global.SlaveOptions.UseRPC { - //log.Warning("TODO: PUT THE KEEPALIVE WATCHER BACK") - startRPCKeepaliveWatcher(rpcAuthStore) startRPCKeepaliveWatcher(rpcOrgStore) } } diff --git a/config/config.go b/config/config.go index ba9d709ce76..2109b76b8ae 100644 --- a/config/config.go +++ b/config/config.go @@ -66,6 +66,7 @@ type AnalyticsConfigConfig struct { GeoIPDBLocation string `json:"geo_ip_db_path"` NormaliseUrls NormalisedURLConfig `json:"normalise_urls"` PoolSize int `json:"pool_size"` + StorageExpirationTime int `json:"storage_expiration_time"` ignoredIPsCompiled map[string]bool } diff --git a/gateway_test.go b/gateway_test.go index 92c38dd1501..4d6631168df 100644 --- a/gateway_test.go +++ b/gateway_test.go @@ -113,6 +113,9 @@ func TestMain(m *testing.M) { config.Global.BundleBaseURL = testHttpBundles config.Global.MiddlewarePath = testMiddlewarePath + purgeTicker = make(chan time.Time) + rpcPurgeTicker = make(chan time.Time) + // force ipv4 for now, to work around the docker bug affecting // Go 1.8 and ealier config.Global.ListenAddress = "127.0.0.1" diff --git a/ldap_auth_handler.go b/ldap_auth_handler.go index 989b0321082..6a90d7f6a84 100644 --- a/ldap_auth_handler.go +++ b/ldap_auth_handler.go @@ -95,10 +95,16 @@ func (l *LDAPStorageHandler) GetRawKey(filter string) (string, error) { return "", nil } +func (l *LDAPStorageHandler) SetExp(cn string, exp int64) error { + log.Warning("Not implementated") + return nil +} + func (l *LDAPStorageHandler) GetExp(cn string) (int64, error) { log.Warning("Not implementated") return 0, nil } + func (l *LDAPStorageHandler) GetKeys(filter string) []string { log.Warning("Not implementated") s := []string{} diff --git a/lint/schema.go b/lint/schema.go index 7fd19f7f0e9..a3b6bcd57a7 100644 --- a/lint/schema.go +++ b/lint/schema.go @@ -92,6 +92,9 @@ const confSchema = `{ "pool_size": { "type": "integer" }, + "storage_expiration_time": { + "type": "integer" + }, "type": { "type": "string" } diff --git a/main.go b/main.go index 5fc715ec517..e9472fbd7d3 100644 --- a/main.go +++ b/main.go @@ -124,6 +124,11 @@ func apisByIDLen() int { return len(apisByID) } +var redisPurgeOnce sync.Once +var rpcPurgeOnce sync.Once +var purgeTicker <-chan time.Time = time.Tick(time.Second) +var rpcPurgeTicker <-chan time.Time = time.Tick(10 * time.Second) + // Create all globals and init connection handlers func setupGlobals() { reloadMu.Lock() @@ -144,21 +149,29 @@ func setupGlobals() { if config.Global.EnableAnalytics && analytics.Store == nil { config.Global.LoadIgnoredIPs() - analyticsStore := storage.RedisCluster{KeyPrefix: "analytics-"} log.WithFields(logrus.Fields{ "prefix": "main", }).Debug("Setting up analytics DB connection") + analyticsStore := storage.RedisCluster{KeyPrefix: "analytics-"} analytics.Store = &analyticsStore analytics.Init() + redisPurgeOnce.Do(func() { + store := storage.RedisCluster{KeyPrefix: "analytics-"} + redisPurger := RedisPurger{Store: &store} + go redisPurger.PurgeLoop(purgeTicker) + }) + if config.Global.AnalyticsConfig.Type == "rpc" { log.Debug("Using RPC cache purge") - purger := RPCPurger{Store: &analyticsStore} - purger.Connect() - analytics.Clean = &purger - go analytics.Clean.PurgeLoop(10 * time.Second) + rpcPurgeOnce.Do(func() { + store := storage.RedisCluster{KeyPrefix: "analytics-"} + purger := RPCPurger{Store: &store} + purger.Connect() + go purger.PurgeLoop(rpcPurgeTicker) + }) } } @@ -274,7 +287,7 @@ func syncAPISpecs() int { return len(apiSpecs) } -func syncPolicies() { +func syncPolicies() int { var pols map[string]user.Policy log.WithFields(logrus.Fields{ @@ -308,7 +321,7 @@ func syncPolicies() { log.WithFields(logrus.Fields{ "prefix": "main", }).Debug("No policy record name defined, skipping...") - return + return 0 } pols = LoadPoliciesFromFile(config.Global.Policies.PolicyRecordName) } @@ -321,11 +334,13 @@ func syncPolicies() { }).Infof(" - %s", id) } + policiesMu.Lock() + defer policiesMu.Unlock() if len(pols) > 0 { - policiesMu.Lock() policiesByID = pols - policiesMu.Unlock() } + + return len(pols) } // stripSlashes removes any trailing slashes from the request's URL @@ -676,9 +691,9 @@ func doReload() { mainRouter = newRouter - // Unset these - rpcEmergencyModeLoaded = false - rpcEmergencyMode = false + // // Unset these + // rpcEmergencyModeLoaded = false + // rpcEmergencyMode = false } // startReloadChan and reloadDoneChan are used by the two reload loops @@ -1275,6 +1290,8 @@ func handleDashboardRegistration() { go DashService.StartBeating() } +var drlOnce sync.Once + func startDRL() { switch { case config.Global.ManagementNode: @@ -1311,6 +1328,8 @@ func listen(l, controlListener net.Listener, err error) { writeTimeout = config.Global.HttpServerOptions.WriteTimeout } + drlOnce.Do(startDRL) + // Handle reload when SIGUSR2 is received if err != nil { // Listen on a TCP or a UNIX domain socket (TCP here). @@ -1321,8 +1340,6 @@ func listen(l, controlListener net.Listener, err error) { // handle dashboard registration and nonces if available handleDashboardRegistration() - startDRL() - if !rpcEmergencyMode { count := syncAPISpecs() if count > 0 { @@ -1396,7 +1413,6 @@ func listen(l, controlListener net.Listener, err error) { os.Setenv("TYK_SERVICE_NONCE", "") os.Setenv("TYK_SERVICE_NODEID", "") } - startDRL() // Resume accepting connections in a new goroutine. if !rpcEmergencyMode { diff --git a/policy.go b/policy.go index 4e31d157075..da136bd5110 100644 --- a/policy.go +++ b/policy.go @@ -139,29 +139,45 @@ func LoadPoliciesFromDashboard(endpoint, secret string, allowExplicit bool) map[ return policies } -func LoadPoliciesFromRPC(orgId string) map[string]user.Policy { +func parsePoliciesFromRPC(list string) (map[string]user.Policy, error) { var dbPolicyList []user.Policy + if err := json.Unmarshal([]byte(list), &dbPolicyList); err != nil { + return nil, err + } + + policies := make(map[string]user.Policy, len(dbPolicyList)) + + for _, p := range dbPolicyList { + p.ID = p.MID.Hex() + policies[p.MID.Hex()] = p + } + + return policies, nil +} + +func LoadPoliciesFromRPC(orgId string) map[string]user.Policy { + if rpcEmergencyMode { + return LoadPoliciesFromRPCBackup() + } + store := &RPCStorageHandler{UserKey: config.Global.SlaveOptions.APIKey, Address: config.Global.SlaveOptions.ConnectionString} - store.Connect() + if !store.Connect() { + return nil + } rpcPolicies := store.GetPolicies(orgId) - //store.Disconnect() + policies, err := parsePoliciesFromRPC(rpcPolicies) - if err := json.Unmarshal([]byte(rpcPolicies), &dbPolicyList); err != nil { + if err != nil { log.WithFields(logrus.Fields{ "prefix": "policy", - }).Error("Failed decode: ", err) + }).Error("Failed decode: ", err, rpcPolicies) return nil } - policies := make(map[string]user.Policy, len(dbPolicyList)) - - for _, p := range dbPolicyList { - p.ID = p.MID.Hex() - policies[p.MID.Hex()] = p - } + saveRPCPoliciesBackup(rpcPolicies) return policies } diff --git a/rpc_analytics_purger.go b/rpc_analytics_purger.go index 79e02fe6a94..057a6c0efaf 100644 --- a/rpc_analytics_purger.go +++ b/rpc_analytics_purger.go @@ -6,6 +6,7 @@ import ( "gopkg.in/vmihailenco/msgpack.v2" + "github.com/TykTechnologies/tyk/config" "github.com/TykTechnologies/tyk/storage" ) @@ -13,7 +14,7 @@ import ( // of analytics data to prevent it growing too large type Purger interface { PurgeCache() - PurgeLoop(time.Duration) + PurgeLoop(<-chan time.Time) } // RPCPurger will purge analytics data into a Mongo database, requires that the Mongo DB string is specified @@ -32,15 +33,20 @@ func (r *RPCPurger) Connect() { // PurgeLoop starts the loop that will pull data out of the in-memory // store and into RPC. -func (r RPCPurger) PurgeLoop(sleep time.Duration) { +func (r RPCPurger) PurgeLoop(ticker <-chan time.Time) { for { - time.Sleep(sleep) + <-ticker r.PurgeCache() } } // PurgeCache will pull the data from the in-memory store and drop it into the specified MongoDB collection func (r *RPCPurger) PurgeCache() { + if _, err := RPCFuncClientSingleton.Call("Ping", nil); err != nil { + log.Error("Failed to ping RPC: ", err) + return + } + analyticsValues := r.Store.GetAndDeleteSet(analyticsKeyName) if len(analyticsValues) == 0 { return @@ -70,3 +76,28 @@ func (r *RPCPurger) PurgeCache() { } } + +type RedisPurger struct { + Store storage.Handler +} + +func (r RedisPurger) PurgeLoop(ticker <-chan time.Time) { + for { + <-ticker + r.PurgeCache() + } +} + +func (r *RedisPurger) PurgeCache() { + configMu.Lock() + expireAfter := config.Global.AnalyticsConfig.StorageExpirationTime + configMu.Unlock() + if expireAfter == 0 { + expireAfter = 60 // 1 minute + } + + exp, _ := r.Store.GetExp(analyticsKeyName) + if exp <= 0 { + r.Store.SetExp(analyticsKeyName, int64(expireAfter)) + } +} diff --git a/rpc_backup_handlers.go b/rpc_backup_handlers.go index 3edce925382..2a97502b05a 100644 --- a/rpc_backup_handlers.go +++ b/rpc_backup_handlers.go @@ -6,18 +6,18 @@ import ( "crypto/rand" "encoding/base64" "io" - "net/http" "strings" "github.com/Sirupsen/logrus" - "github.com/gorilla/mux" "github.com/TykTechnologies/tyk/config" "github.com/TykTechnologies/tyk/storage" + "github.com/TykTechnologies/tyk/user" ) const RPCKeyPrefix = "rpc:" -const BackupKeyBase = "node-definition-backup:" +const BackupApiKeyBase = "node-definition-backup:" +const BackupPolicyKeyBase = "node-policy-backup:" func getTagListAsString() string { tagList := "" @@ -28,8 +28,34 @@ func getTagListAsString() string { return tagList } +func LoadDefinitionsFromRPCBackup() []*APISpec { + tagList := getTagListAsString() + checkKey := BackupApiKeyBase + tagList + + store := storage.RedisCluster{KeyPrefix: RPCKeyPrefix} + connected := store.Connect() + log.Info("[RPC] --> Loading API definitions from backup") + + if !connected { + log.Error("[RPC] --> RPC Backup recovery failed: redis connection failed") + return nil + } + + secret := rightPad2Len(config.Global.Secret, "=", 32) + cryptoText, err := store.GetKey(checkKey) + apiListAsString := decrypt([]byte(secret), cryptoText) + + if err != nil { + log.Error("[RPC] --> Failed to get node backup (", checkKey, "): ", err) + return nil + } + + a := APIDefinitionLoader{} + return a.processRPCDefinitions(apiListAsString) +} + func saveRPCDefinitionsBackup(list string) { - log.Info("Storing RPC backup") + log.Info("Storing RPC Definitions backup") tagList := getTagListAsString() log.Info("--> Connecting to DB") @@ -46,91 +72,67 @@ func saveRPCDefinitionsBackup(list string) { secret := rightPad2Len(config.Global.Secret, "=", 32) cryptoText := encrypt([]byte(secret), list) - err := store.SetKey(BackupKeyBase+tagList, cryptoText, -1) + err := store.SetKey(BackupApiKeyBase+tagList, cryptoText, -1) if err != nil { log.Error("Failed to store node backup: ", err) } } -func LoadDefinitionsFromRPCBackup() []*APISpec { +func LoadPoliciesFromRPCBackup() map[string]user.Policy { tagList := getTagListAsString() - checkKey := BackupKeyBase + tagList + checkKey := BackupPolicyKeyBase + tagList store := storage.RedisCluster{KeyPrefix: RPCKeyPrefix} connected := store.Connect() - log.Info("[RPC] --> Connected to DB") + log.Info("[RPC] Loading Policies from backup") if !connected { - log.Error("[RPC] --> RPC Backup recovery failed: redis connection failed") + log.Error("[RPC] --> RPC Policy Backup recovery failed: redis connection failed") return nil } secret := rightPad2Len(config.Global.Secret, "=", 32) cryptoText, err := store.GetKey(checkKey) - apiListAsString := decrypt([]byte(secret), cryptoText) + listAsString := decrypt([]byte(secret), cryptoText) if err != nil { - log.Error("[RPC] --> Failed to get node backup (", checkKey, "): ", err) + log.Error("[RPC] --> Failed to get node policy backup (", checkKey, "): ", err) return nil } - a := APIDefinitionLoader{} - return a.processRPCDefinitions(apiListAsString) -} - -func doLoadWithBackup(specs []*APISpec) { - - log.Warning("[RPC Backup] --> Load Policies too!") - - if len(specs) == 0 { + if policies, err := parsePoliciesFromRPC(listAsString); err != nil { log.WithFields(logrus.Fields{ - "prefix": "main", - }).Warning("No API Definitions found, not loading backup") - return + "prefix": "policy", + }).Error("Failed decode: ", err) + return nil + } else { + return policies } +} - // Reset the JSVM - GlobalEventsJSVM.Init(nil) - log.Warning("[RPC Backup] --> Initialised JSVM") - - newRouter := mux.NewRouter() +func saveRPCPoliciesBackup(list string) { + log.Info("Storing RPC policies backup") + tagList := getTagListAsString() - log.Warning("[RPC Backup] --> Set up routers") - log.Warning("[RPC Backup] --> Loading endpoints") + log.Info("--> Connecting to DB") - loadAPIEndpoints(newRouter) + store := storage.RedisCluster{KeyPrefix: RPCKeyPrefix} + connected := store.Connect() - log.Warning("[RPC Backup] --> Loading APIs") - loadApps(specs, newRouter) - log.Warning("[RPC Backup] --> API Load Done") + log.Info("--> Connected to DB") - if config.Global.NewRelic.AppName != "" { - log.Warning("[RPC Backup] --> Adding NewRelic instrumentation") - AddNewRelicInstrumentation(NewRelicApplication, newRouter) - log.Warning("[RPC Backup] --> NewRelic instrumentation added") + if !connected { + log.Error("--> RPC Backup save failed: redis connection failed") + return } - newServeMux := http.NewServeMux() - newServeMux.Handle("/", newRouter) - - mainRouter = newRouter - - http.DefaultServeMux = newServeMux - log.Warning("[RPC Backup] --> Replaced muxer") - - log.WithFields(logrus.Fields{ - "prefix": "main", - }).Info("API backup load complete") - - log.Warning("[RPC Backup] --> Ready to listen") - rpcEmergencyModeLoaded = true - - l, err := generateListener(0) + secret := rightPad2Len(config.Global.Secret, "=", 32) + cryptoText := encrypt([]byte(secret), list) + err := store.SetKey(BackupPolicyKeyBase+tagList, cryptoText, -1) if err != nil { - log.Error("Failed to generate listener:", err) + log.Error("Failed to store node backup: ", err) } - listen(l, nil, nil) } // encrypt string to base64 crypto using AES diff --git a/rpc_storage_handler.go b/rpc_storage_handler.go index 4a469888a6e..30b085c08b3 100644 --- a/rpc_storage_handler.go +++ b/rpc_storage_handler.go @@ -61,10 +61,10 @@ var ( ) func rpcKeepAliveCheck(r *RPCStorageHandler) { - // Only run when connected if !RPCClientIsConnected { return } + // Make sure the auth back end is still alive c1 := make(chan string, 1) @@ -236,7 +236,9 @@ func (r *RPCStorageHandler) Connect() bool { RPCFuncClientSingleton = d.NewFuncClient(RPCCLientSingleton) } - r.Login() + if !r.Login() { + return false + } if !r.SuppressRegister { r.Register() @@ -280,31 +282,25 @@ func (r *RPCStorageHandler) cleanKey(keyName string) string { return setKeyName } -func (r *RPCStorageHandler) ReAttemptLogin(err error) { +func (r *RPCStorageHandler) ReAttemptLogin(err error) bool { log.Warning("[RPC Store] Login failed, waiting 3s to re-attempt") if rpcLoadCount == 0 && !rpcEmergencyModeLoaded { log.Warning("[RPC Store] --> Detected cold start, attempting to load from cache") - apiList := LoadDefinitionsFromRPCBackup() - log.Warning("[RPC Store] --> Done") - if apiList != nil { - rpcEmergencyMode = true - log.Warning("[RPC Store] ----> Found APIs... beginning emergency load") - doLoadWithBackup(apiList) - } - - //LoadPoliciesFromRPCBackup() + log.Warning("[RPC Store] ----> Found APIs... beginning emergency load") + doReload() + rpcEmergencyModeLoaded = true } time.Sleep(time.Second * 3) if strings.Contains(err.Error(), "Cannot obtain response during timeout") { r.ReConnect() - return + return false } - r.Login() + return r.Login() } -func (r *RPCStorageHandler) GroupLogin() { +func (r *RPCStorageHandler) GroupLogin() bool { groupLoginData := GroupLoginRequest{ UserKey: r.UserKey, GroupID: config.Global.SlaveOptions.GroupID, @@ -320,20 +316,31 @@ func (r *RPCStorageHandler) GroupLogin() { "GroupID": groupLoginData.GroupID, }, ) - r.ReAttemptLogin(err) - return + rpcEmergencyMode = true + go r.ReAttemptLogin(err) + return false } if ok == false { log.Error("RPC Login incorrect") - r.ReAttemptLogin(errors.New("Login incorrect")) - return + rpcEmergencyMode = true + go r.ReAttemptLogin(errors.New("Login incorrect")) + return false } log.Debug("[RPC Store] Group Login complete") rpcLoadCount++ + + // Recovery + if rpcEmergencyMode { + doReload() + } + + rpcEmergencyMode = false + rpcEmergencyModeLoaded = false + return true } -func (r *RPCStorageHandler) Login() { +func (r *RPCStorageHandler) Login() bool { log.Debug("[RPC Store] Login initiated") if len(r.UserKey) == 0 { @@ -342,25 +349,34 @@ func (r *RPCStorageHandler) Login() { // If we have a group ID, lets login as a group if config.Global.SlaveOptions.GroupID != "" { - r.GroupLogin() - return + return r.GroupLogin() } ok, err := RPCFuncClientSingleton.CallTimeout("Login", r.UserKey, GlobalRPCCallTimeout) if err != nil { log.Error("RPC Login failed: ", err) emitRPCErrorEvent(rpcFuncClientSingletonCall, "Login", err) - r.ReAttemptLogin(err) - return + rpcEmergencyMode = true + go r.ReAttemptLogin(err) + return false } if ok == false { log.Error("RPC Login incorrect") - r.ReAttemptLogin(errors.New("Login incorrect")) - return + rpcEmergencyMode = true + go r.ReAttemptLogin(errors.New("Login incorrect")) + return false } log.Debug("[RPC Store] Login complete") rpcLoadCount++ + + if rpcEmergencyMode { + doReload() + } + + rpcEmergencyMode = false + rpcEmergencyModeLoaded = false + return true } // GetKey will retrieve a key from the database @@ -441,6 +457,11 @@ func (r *RPCStorageHandler) GetExp(keyName string) (int64, error) { return value.(int64), nil } +func (r *RPCStorageHandler) SetExp(keyName string, timeout int64) error { + log.Error("SetExp Not Implemented") + return nil +} + // SetKey will create (or update) a key value in the store func (r *RPCStorageHandler) SetKey(keyName, session string, timeout int64) error { start := time.Now() // get current time diff --git a/rpc_test.go b/rpc_test.go new file mode 100644 index 00000000000..037800892b9 --- /dev/null +++ b/rpc_test.go @@ -0,0 +1,184 @@ +// +build !race + +package main + +import ( + "fmt" + "testing" + "time" + + "github.com/lonelycode/gorpc" + + "github.com/TykTechnologies/tyk/config" + "github.com/TykTechnologies/tyk/test" +) + +func startRPCMock(dispatcher *gorpc.Dispatcher) *gorpc.Server { + configMu.Lock() + defer configMu.Unlock() + + GlobalRPCCallTimeout = 100 * time.Millisecond + config.Global.SlaveOptions.UseRPC = true + config.Global.SlaveOptions.RPCKey = "test_org" + config.Global.SlaveOptions.APIKey = "test" + config.Global.Policies.PolicySource = "rpc" + config.Global.SlaveOptions.CallTimeout = 1 + config.Global.SlaveOptions.RPCPoolSize = 2 + + server := gorpc.NewTCPServer("127.0.0.1:0", dispatcher.NewHandlerFunc()) + list := &customListener{} + server.Listener = list + server.LogError = gorpc.NilErrorLogger + + if err := server.Start(); err != nil { + panic(err) + } + config.Global.SlaveOptions.ConnectionString = list.L.Addr().String() + + return server +} + +func stopRPCMock(server *gorpc.Server) { + config.Global.SlaveOptions.ConnectionString = "" + config.Global.SlaveOptions.RPCKey = "" + config.Global.SlaveOptions.APIKey = "" + config.Global.SlaveOptions.UseRPC = false + config.Global.Policies.PolicySource = "" + + if server != nil { + server.Listener.Close() + server.Stop() + } + + RPCCLientSingleton.Stop() + RPCClientIsConnected = false + RPCCLientSingleton = nil + RPCFuncClientSingleton = nil + rpcLoadCount = 0 + rpcEmergencyMode = false + rpcEmergencyModeLoaded = false +} + +// Our RPC layer too racy, but not harmul, mostly global variables like RPCIsClientConnected +func TestSyncAPISpecsRPCFailure(t *testing.T) { + // Mock RPC + dispatcher := gorpc.NewDispatcher() + dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) { + return "malformed json", nil + }) + dispatcher.AddFunc("Login", func(clientAddr, userKey string) bool { + return true + }) + + rpc := startRPCMock(dispatcher) + defer stopRPCMock(rpc) + + count := syncAPISpecs() + if count != 0 { + t.Error("Should return empty value for malformed rpc response", apiSpecs) + } +} + +func TestSyncAPISpecsRPCSuccess(t *testing.T) { + // Mock RPC + dispatcher := gorpc.NewDispatcher() + dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) { + return "[" + sampleAPI + "]", nil + }) + dispatcher.AddFunc("GetPolicies", func(clientAddr string, orgid string) (string, error) { + return `[{"_id":"507f191e810c19729de860ea", "rate":1, "per":1}]`, nil + }) + dispatcher.AddFunc("Login", func(clientAddr, userKey string) bool { + return true + }) + dispatcher.AddFunc("GetKey", func(clientAddr, key string) (string, error) { + return "", fmt.Errorf("Not found") + }) + + t.Run("RPC is live", func(t *testing.T) { + rpc := startRPCMock(dispatcher) + defer stopRPCMock(rpc) + ts := newTykTestServer() + defer ts.Close() + + apiBackup := LoadDefinitionsFromRPCBackup() + if len(apiBackup) != 1 { + t.Fatal("Should have APIs in backup") + } + + policyBackup := LoadPoliciesFromRPCBackup() + if len(policyBackup) != 1 { + t.Fatal("Should have Policies in backup") + } + + ts.Run(t, []test.TestCase{ + {Path: "/sample", Code: 200}, + }...) + + count := syncAPISpecs() + if count != 1 { + t.Error("Should return array with one spec", apiSpecs) + } + }) + + t.Run("RPC down, load backup", func(t *testing.T) { + // Point rpc to non existent address + config.Global.SlaveOptions.ConnectionString = testHttpFailure + config.Global.SlaveOptions.UseRPC = true + config.Global.SlaveOptions.RPCKey = "test_org" + config.Global.SlaveOptions.APIKey = "test" + config.Global.Policies.PolicySource = "rpc" + + // RPC layer is down + ts := newTykTestServer() + defer ts.Close() + + // Wait for backup to load + time.Sleep(200 * time.Millisecond) + + // Still should work! + ts.Run(t, []test.TestCase{ + {Path: "/sample", Code: 200}, + }...) + + stopRPCMock(nil) + }) + + t.Run("RPC is back", func(t *testing.T) { + rpcEmergencyModeLoaded = false + rpcEmergencyMode = false + + dispatcher := gorpc.NewDispatcher() + dispatcher.AddFunc("GetApiDefinitions", func(clientAddr string, dr *DefRequest) (string, error) { + return "[" + sampleAPI + "," + sampleAPI + "]", nil + }) + dispatcher.AddFunc("GetPolicies", func(clientAddr string, orgid string) (string, error) { + return `[{"_id":"507f191e810c19729de860ea", "rate":1, "per":1}, {"_id":"507f191e810c19729de860eb", "rate":1, "per":1}]`, nil + }) + dispatcher.AddFunc("Login", func(clientAddr, userKey string) bool { + return true + }) + dispatcher.AddFunc("GetKey", func(clientAddr, key string) (string, error) { + return "", fmt.Errorf("Not found") + }) + // Back to live + rpc := startRPCMock(dispatcher) + defer stopRPCMock(rpc) + ts := newTykTestServer() + defer ts.Close() + + time.Sleep(100 * time.Millisecond) + + ts.Run(t, []test.TestCase{ + {Path: "/sample", Code: 200}, + }...) + + if count := syncAPISpecs(); count != 2 { + t.Error("Should fetch latest specs", count) + } + + if count := syncPolicies(); count != 2 { + t.Error("Should fetch latest policies", count) + } + }) +} diff --git a/storage/redis_cluster.go b/storage/redis_cluster.go index 1ce7ae03680..3d642c20f4d 100644 --- a/storage/redis_cluster.go +++ b/storage/redis_cluster.go @@ -176,7 +176,14 @@ func (r RedisCluster) GetExp(keyName string) (int64, error) { return 0, ErrKeyNotFound } return value, nil +} +func (r RedisCluster) SetExp(keyName string, timeout int64) error { + _, err := r.singleton().Do("EXPIRE", r.fixKey(keyName), timeout) + if err != nil { + log.Error("Could not EXPIRE key: ", err) + } + return err } // SetKey will create (or update) a key value in the store @@ -187,9 +194,7 @@ func (r RedisCluster) SetKey(keyName, session string, timeout int64) error { r.ensureConnection() _, err := r.singleton().Do("SET", r.fixKey(keyName), session) if timeout > 0 { - _, err := r.singleton().Do("EXPIRE", r.fixKey(keyName), timeout) - if err != nil { - log.Error("Could not EXPIRE key: ", err) + if err := r.SetExp(keyName, timeout); err != nil { return err } } diff --git a/storage/storage.go b/storage/storage.go index 70da534b655..cf5db9b3641 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -22,6 +22,7 @@ type Handler interface { GetRawKey(string) (string, error) SetKey(string, string, int64) error // Second input string is expected to be a JSON object (user.SessionState) SetRawKey(string, string, int64) error + SetExp(string, int64) error // Set key expiration GetExp(string) (int64, error) // Returns expiry of a key GetKeys(string) []string DeleteKey(string) bool From 291b493097ccabd15082923b84e3e956bdb6c0be Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Tue, 20 Feb 2018 15:10:06 +0300 Subject: [PATCH 17/20] Tyk cache should respect etags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit if upstream and client both provide Etags, and Tyk cached response, it now should properly response with “304” status code without body, if client etag match cached etag. --- gateway_test.go | 43 ++++++++++++++++++++++++++++++++++++++----- helpers_test.go | 9 +++++++-- mw_redis_cache.go | 13 ++++++++++++- test/http.go | 2 ++ 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/gateway_test.go b/gateway_test.go index 4d6631168df..05736ad6ebe 100644 --- a/gateway_test.go +++ b/gateway_test.go @@ -859,11 +859,11 @@ func TestHelloHealthcheck(t *testing.T) { }) } -func TestWithCacheAllSafeRequests(t *testing.T) { - ts := newTykTestServer(tykTestServerConfig{ - delay: 10 * time.Millisecond, - }) +func TestCacheAllSafeRequests(t *testing.T) { + ts := newTykTestServer() defer ts.Close() + cache := storage.RedisCluster{KeyPrefix: "cache-"} + defer cache.DeleteScanMatch("*") buildAndLoadAPI(func(spec *APISpec) { spec.CacheOptions = apidef.CacheOptions{ @@ -877,7 +877,7 @@ func TestWithCacheAllSafeRequests(t *testing.T) { headerCache := map[string]string{"x-tyk-cached-response": "1"} ts.Run(t, []test.TestCase{ - {Method: "GET", Path: "/", HeadersNotMatch: headerCache}, + {Method: "GET", Path: "/", HeadersNotMatch: headerCache, Delay: 10 * time.Millisecond}, {Method: "GET", Path: "/", HeadersMatch: headerCache}, {Method: "POST", Path: "/", HeadersNotMatch: headerCache}, {Method: "POST", Path: "/", HeadersNotMatch: headerCache}, @@ -885,6 +885,39 @@ func TestWithCacheAllSafeRequests(t *testing.T) { }...) } +func TestCacheEtag(t *testing.T) { + ts := newTykTestServer() + defer ts.Close() + cache := storage.RedisCluster{KeyPrefix: "cache-"} + defer cache.DeleteScanMatch("*") + + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Etag", "12345") + w.Write([]byte("body")) + })) + + buildAndLoadAPI(func(spec *APISpec) { + spec.CacheOptions = apidef.CacheOptions{ + CacheTimeout: 120, + EnableCache: true, + CacheAllSafeRequests: true, + } + spec.Proxy.ListenPath = "/" + spec.Proxy.TargetURL = upstream.URL + }) + + headerCache := map[string]string{"x-tyk-cached-response": "1"} + invalidEtag := map[string]string{"If-None-Match": "invalid"} + validEtag := map[string]string{"If-None-Match": "12345"} + + ts.Run(t, []test.TestCase{ + {Method: "GET", Path: "/", HeadersNotMatch: headerCache, Delay: 100 * time.Millisecond}, + {Method: "GET", Path: "/", HeadersMatch: headerCache, BodyMatch: "body"}, + {Method: "GET", Path: "/", Headers: invalidEtag, HeadersMatch: headerCache, BodyMatch: "body"}, + {Method: "GET", Path: "/", Headers: validEtag, HeadersMatch: headerCache, BodyNotMatch: "body"}, + }...) +} + func TestWebsocketsUpstreamUpgradeRequest(t *testing.T) { // setup spec and do test HTTP upgrade-request config.Global.HttpServerOptions.EnableWebSockets = true diff --git a/helpers_test.go b/helpers_test.go index 72caee1636a..6c292f3ec2a 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -368,8 +368,13 @@ func (s *tykTestServer) Run(t *testing.T, testCases ...test.TestCase) (*http.Res t.Errorf("[%d] %s. %s", ti, lastError.Error(), string(tcJSON)) } - if s.config.delay > 0 { - time.Sleep(s.config.delay) + delay := tc.Delay + if delay == 0 { + delay = s.config.delay + } + + if delay > 0 { + time.Sleep(delay) } } diff --git a/mw_redis_cache.go b/mw_redis_cache.go index ce088ff411e..7187f59aaf6 100644 --- a/mw_redis_cache.go +++ b/mw_redis_cache.go @@ -251,8 +251,19 @@ func (m *RedisCacheMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req w.Header().Set("X-RateLimit-Reset", strconv.Itoa(int(session.QuotaRenews))) } w.Header().Set("x-tyk-cached-response", "1") + + if reqEtag := r.Header.Get("If-None-Match"); reqEtag != "" { + if respEtag := newRes.Header.Get("Etag"); respEtag != "" { + if strings.Contains(reqEtag, respEtag) { + newRes.StatusCode = http.StatusNotModified + } + } + } + w.WriteHeader(newRes.StatusCode) - m.Proxy.CopyResponse(w, newRes.Body) + if newRes.StatusCode != http.StatusNotModified { + m.Proxy.CopyResponse(w, newRes.Body) + } // Record analytics if !m.Spec.DoNotTrack { diff --git a/test/http.go b/test/http.go index f557916b552..ee0cc63be89 100644 --- a/test/http.go +++ b/test/http.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "net/http" "strings" + "time" ) type TestCase struct { @@ -18,6 +19,7 @@ type TestCase struct { Headers map[string]string `json:",omitempty"` PathParams map[string]string `json:",omitempty"` Cookies []*http.Cookie `json:",omitempty"` + Delay time.Duration `json:",omitempty"` BodyMatch string `json:",omitempty"` BodyNotMatch string `json:",omitempty"` HeadersMatch map[string]string `json:",omitempty"` From 949b43498e8f95760bb24afaed410be5101f2af9 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Tue, 20 Feb 2018 16:51:31 +0300 Subject: [PATCH 18/20] Add way to control maximum connection time Added new `max_conn_time` option which will re-create API HTTPTransport after specified time. Added to force DNS cache flush. Fix #1485 --- api_definition.go | 1 + config/config.go | 1 + lint/schema.go | 3 +++ reverse_proxy.go | 9 ++++++++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/api_definition.go b/api_definition.go index 9bc6928fc7a..6fd8d3a593a 100644 --- a/api_definition.go +++ b/api_definition.go @@ -141,6 +141,7 @@ type APISpec struct { HasRun bool ServiceRefreshInProgress bool HTTPTransport http.RoundTripper + HTTPTransportCreated time.Time } // APIDefinitionLoader will load an Api definition from a storage diff --git a/config/config.go b/config/config.go index 2109b76b8ae..38090607bf3 100644 --- a/config/config.go +++ b/config/config.go @@ -265,6 +265,7 @@ type Config struct { AllowRemoteConfig bool `bson:"allow_remote_config" json:"allow_remote_config"` LegacyEnableAllowanceCountdown bool `bson:"legacy_enable_allowance_countdown" json:"legacy_enable_allowance_countdown"` MaxIdleConnsPerHost int `bson:"max_idle_connections_per_host" json:"max_idle_connections_per_host"` + MaxConnTime int64 `json:"max_conn_time"` ReloadWaitTime int `bson:"reload_wait_time" json:"reload_wait_time"` ProxySSLInsecureSkipVerify bool `json:"proxy_ssl_insecure_skip_verify"` ProxyDefaultTimeout int `json:"proxy_default_timeout"` diff --git a/lint/schema.go b/lint/schema.go index a3b6bcd57a7..8795b1aedb0 100644 --- a/lint/schema.go +++ b/lint/schema.go @@ -407,6 +407,9 @@ const confSchema = `{ "max_idle_connections_per_host": { "type": "integer" }, + "max_conn_time": { + "type": "integer" + }, "middleware_path": { "type": "string", "format": "path" diff --git a/reverse_proxy.go b/reverse_proxy.go index ea56214a931..3c6b7f79e09 100644 --- a/reverse_proxy.go +++ b/reverse_proxy.go @@ -454,12 +454,19 @@ func httpTransport(timeOut int, rw http.ResponseWriter, req *http.Request, p *Re func (p *ReverseProxy) WrappedServeHTTP(rw http.ResponseWriter, req *http.Request, withCache bool) *http.Response { // 1. Check if timeouts are set for this endpoint p.TykAPISpec.Lock() - if p.TykAPISpec.HTTPTransport == nil { + + createTransport := p.TykAPISpec.HTTPTransport == nil + if !createTransport && config.Global.MaxConnTime != 0 { + createTransport = time.Since(p.TykAPISpec.HTTPTransportCreated) > time.Duration(config.Global.MaxConnTime)*time.Second + } + + if createTransport { _, timeout := p.CheckHardTimeoutEnforced(p.TykAPISpec, req) p.TykAPISpec.HTTPTransport = httpTransport(timeout, rw, req, p) } else if IsWebsocket(req) { // check if it is an upgrade request to NEW WS-connection // overwrite transport's ResponseWriter from previous upgrade request // as it was already hijacked and now is being used for other connection + p.TykAPISpec.HTTPTransportCreated = time.Now() p.TykAPISpec.HTTPTransport.(*WSDialer).RW = rw } p.TykAPISpec.Unlock() From 68d484baa0043724c91970ad6db319d356db1dc4 Mon Sep 17 00:00:00 2001 From: joshblakeley <31618778+joshblakeley@users.noreply.github.com> Date: Tue, 20 Feb 2018 14:17:23 +0000 Subject: [PATCH 19/20] Batch requests client checks for ssl config and add test (#1471) Now batch requests obey both `proxy_ssl_insecure_skip_verify` and upstream mutual tls certificates --- batch_requests.go | 12 ++++- batch_requests_test.go | 111 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) diff --git a/batch_requests.go b/batch_requests.go index fc3b82cab11..8a4da004e5e 100644 --- a/batch_requests.go +++ b/batch_requests.go @@ -1,6 +1,7 @@ package main import ( + "crypto/tls" "encoding/json" "fmt" "io/ioutil" @@ -40,7 +41,16 @@ type BatchRequestHandler struct { // doRequest will make the same request but return a BatchReplyUnit func (b *BatchRequestHandler) doRequest(req *http.Request, relURL string) BatchReplyUnit { - resp, err := http.DefaultClient.Do(req) + tr := &http.Transport{TLSClientConfig: &tls.Config{}} + + if cert := getUpstreamCertificate(req.Host, b.API); cert != nil { + tr.TLSClientConfig.Certificates = []tls.Certificate{*cert} + } + + tr.TLSClientConfig.InsecureSkipVerify = config.Global.ProxySSLInsecureSkipVerify + client := &http.Client{Transport: tr} + + resp, err := client.Do(req) if err != nil { log.Error("Webhook request failed: ", err) return BatchReplyUnit{} diff --git a/batch_requests_test.go b/batch_requests_test.go index 35f7129ec91..300c33f5b45 100644 --- a/batch_requests_test.go +++ b/batch_requests_test.go @@ -1,10 +1,18 @@ package main import ( + "crypto/tls" + "crypto/x509" + "encoding/base64" "encoding/json" "io/ioutil" + "net/http" + "net/http/httptest" + "strings" "testing" + "github.com/TykTechnologies/tyk/apidef" + "github.com/TykTechnologies/tyk/config" "github.com/TykTechnologies/tyk/test" ) @@ -65,3 +73,106 @@ func TestBatch(t *testing.T) { } } } + +var virtBatchTest = `function batchTest (request, session, config) { + // Set up a response object + var response = { + Body: "", + Headers: { + "content-type": "application/json" + }, + Code: 202 + } + + // Batch request + var batch = { + "requests": [ + { + "method": "GET", + "headers": {}, + "body": "", + "relative_url": "{upstream_URL}" + }, + { + "method": "GET", + "headers": {}, + "body": "", + "relative_url": "{upstream_URL}" + } + ], + "suppress_parallel_execution": false + } + + var newBody = TykBatchRequest(JSON.stringify(batch)) + var asJS = JSON.parse(newBody) + for (var i in asJS) { + if (asJS[i].code == 0){ + response.Code = 500 + } + } + return TykJsResponse(response, session.meta_data) +}` + +func TestVirtualEndpointBatch(t *testing.T) { + _, _, combinedClientPEM, clientCert := genCertificate(&x509.Certificate{}) + clientCert.Leaf, _ = x509.ParseCertificate(clientCert.Certificate[0]) + + upstream := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + })) + + // Mutual TLS protected upstream + pool := x509.NewCertPool() + pool.AddCert(clientCert.Leaf) + upstream.TLS = &tls.Config{ + ClientAuth: tls.RequireAndVerifyClientCert, + ClientCAs: pool, + InsecureSkipVerify: true, + } + + upstream.StartTLS() + defer upstream.Close() + + clientCertID, _ := CertificateManager.Add(combinedClientPEM, "") + defer CertificateManager.Delete(clientCertID) + + virtBatchTest = strings.Replace(virtBatchTest, "{upstream_URL}", upstream.URL, 2) + defer upstream.Close() + + upstreamHost := strings.TrimPrefix(upstream.URL, "https://") + + config.Global.Security.Certificates.Upstream = map[string]string{upstreamHost: clientCertID} + defer resetTestConfig() + + ts := newTykTestServer() + defer ts.Close() + + buildAndLoadAPI(func(spec *APISpec) { + spec.Proxy.ListenPath = "/" + virtualMeta := apidef.VirtualMeta{ + ResponseFunctionName: "batchTest", + FunctionSourceType: "blob", + FunctionSourceURI: base64.StdEncoding.EncodeToString([]byte(virtBatchTest)), + Path: "/virt", + Method: "GET", + } + updateAPIVersion(spec, "v1", func(v *apidef.VersionInfo) { + v.UseExtendedPaths = true + v.ExtendedPaths = apidef.ExtendedPathsSet{ + Virtual: []apidef.VirtualMeta{virtualMeta}, + } + }) + }) + + t.Run("Skip verification", func(t *testing.T) { + config.Global.ProxySSLInsecureSkipVerify = true + + ts.Run(t, test.TestCase{Path: "/virt", Code: 202}) + }) + + t.Run("Verification required", func(t *testing.T) { + config.Global.ProxySSLInsecureSkipVerify = false + + ts.Run(t, test.TestCase{Path: "/virt", Code: 500}) + }) + +} From 94a4397349b1b5e9a89732eb075e889b217b956d Mon Sep 17 00:00:00 2001 From: dencoded <33698537+dencoded@users.noreply.github.com> Date: Tue, 20 Feb 2018 09:42:46 -0500 Subject: [PATCH 20/20] mw_jwt updated (#1436) Validate Policy ID in stored token against that in JWT in case the jWT is varying a modified policy ID. fix #1379 --- helpers_test.go | 17 + mw_jwt.go | 65 +-- mw_jwt_test.go | 1039 ++++++++++++++++++++--------------------------- 3 files changed, 491 insertions(+), 630 deletions(-) diff --git a/helpers_test.go b/helpers_test.go index 6c292f3ec2a..95da235fa39 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -246,6 +246,23 @@ func createJWKToken(jGen ...func(*jwt.Token)) string { return tokenString } +func createJWKTokenHMAC(jGen ...func(*jwt.Token)) string { + // Create the token + token := jwt.New(jwt.SigningMethodHS256) + // Set the token ID + + if len(jGen) > 0 { + jGen[0](token) + } + + tokenString, err := token.SignedString([]byte(jwtSecret)) + if err != nil { + panic("Couldn't create JWT token: " + err.Error()) + } + + return tokenString +} + func firstVals(vals map[string][]string) map[string]string { m := make(map[string]string, len(vals)) for k, vs := range vals { diff --git a/mw_jwt.go b/mw_jwt.go index 3ba0f5ddcba..2f753509326 100644 --- a/mw_jwt.go +++ b/mw_jwt.go @@ -244,28 +244,27 @@ func (k *JWTMiddleware) processCentralisedJWT(r *http.Request, token *jwt.Token) newSession, err := generateSessionFromPolicy(basePolicyID, k.Spec.OrgID, true) + if err != nil { + k.reportLoginFailure(baseFieldData, r) + log.Error("Could not find a valid policy to apply to this token!") + return errors.New("Key not authorized: no matching policy"), 403 + } - if err == nil { - session = newSession - session.MetaData = map[string]interface{}{"TykJWTSessionID": sessionID} - session.Alias = baseFieldData + session = newSession + session.MetaData = map[string]interface{}{"TykJWTSessionID": sessionID} + session.Alias = baseFieldData - // Update the session in the session manager in case it gets called again - k.Spec.SessionManager.UpdateSession(sessionID, &session, session.Lifetime(k.Spec.SessionLifetime)) - log.Debug("Policy applied to key") + // Update the session in the session manager in case it gets called again + k.Spec.SessionManager.UpdateSession(sessionID, &session, session.Lifetime(k.Spec.SessionLifetime)) + log.Debug("Policy applied to key") - switch k.Spec.BaseIdentityProvidedBy { - case apidef.JWTClaim, apidef.UnsetAuth: - ctxSetSession(r, &session) - ctxSetAuthToken(r, sessionID) - } - k.setContextVars(r, token) - return nil, 200 + switch k.Spec.BaseIdentityProvidedBy { + case apidef.JWTClaim, apidef.UnsetAuth: + ctxSetSession(r, &session) + ctxSetAuthToken(r, sessionID) } - - k.reportLoginFailure(baseFieldData, r) - log.Error("Could not find a valid policy to apply to this token!") - return errors.New("Key not authorized: no matching policy"), 403 + k.setContextVars(r, token) + return nil, 200 } else if k.Spec.JWTPolicyFieldName != "" { // extract policy ID from JWT token policyID, foundPolicy := k.getPolicyIDFromToken(token) @@ -273,17 +272,35 @@ func (k *JWTMiddleware) processCentralisedJWT(r *http.Request, token *jwt.Token) k.reportLoginFailure(baseFieldData, r) return errors.New("Key not authorized: no matching policy found"), 403 } - // check if policy in session is the same as policy set in the given token + // check if we received a valid policy ID in claim + policiesMu.RLock() + policy, ok := policiesByID[policyID] + policiesMu.RUnlock() + if !ok { + k.reportLoginFailure(baseFieldData, r) + log.Error("Policy ID found in token is invalid!") + return errors.New("Key not authorized: no matching policy"), 403 + } + // check if token for this session was switched to another valid policy pols := session.PolicyIDs() - if len(pols) < 1 { + if len(pols) == 0 { k.reportLoginFailure(baseFieldData, r) log.Error("No policies for the found session. Failing Request.") return errors.New("Key not authorized: no matching policy found"), 403 } - if pols[0] != policyID { - k.reportLoginFailure(baseFieldData, r) - log.Error("Policy ID found in active session is not the same as policy in token!") - return errors.New("Key not authorized: no matching policy"), 403 + if pols[0] != policyID { // switch session to new policy and update session storage and cache + // check ownership before updating session + if policy.OrgID != k.Spec.OrgID { + k.reportLoginFailure(baseFieldData, r) + log.Error("Policy ID found in token is invalid (wrong ownership)!") + return errors.New("Key not authorized: no matching policy"), 403 + } + // update session storage + session.SetPolicies(policyID) + session.LastUpdated = time.Now().String() + k.Spec.SessionManager.UpdateSession(sessionID, &session, session.Lifetime(k.Spec.SessionLifetime)) + // update session in cache + go SessionCache.Set(sessionID, session, cache.DefaultExpiration) } } diff --git a/mw_jwt_test.go b/mw_jwt_test.go index 1db69254a2e..151f65a099a 100644 --- a/mw_jwt_test.go +++ b/mw_jwt_test.go @@ -2,99 +2,15 @@ package main import ( "encoding/base64" - "net/http" - "net/http/httptest" - "net/url" "testing" "time" "github.com/dgrijalva/jwt-go" - "github.com/justinas/alice" "github.com/TykTechnologies/tyk/test" "github.com/TykTechnologies/tyk/user" ) -const jwtDef = `{ - "api_id": "76", - "org_id": "default", - "enable_jwt": true, - "auth": {"auth_header_name": "authorization"}, - "version_data": { - "not_versioned": true, - "versions": { - "v1": {"name": "v1"} - } - }, - "proxy": { - "listen_path": "/jwt_test", - "target_url": "` + testHttpAny + `" - } -}` - -const jwtWithJWKDef = `{ - "api_id": "76", - "org_id": "default", - "enable_jwt": true, - "jwt_source": "` + testHttpJWK + `", - "jwt_signing_method": "RSA", - "jwt_identity_base_field": "user_id", - "jwt_policy_field_name": "policy_id", - "auth": {"auth_header_name": "authorization"}, - "version_data": { - "not_versioned": true, - "versions": { - "v1": {"name": "v1"} - } - }, - "proxy": { - "listen_path": "/jwt_test", - "target_url": "` + testHttpAny + `" - } -}` - -const jwtWithCentralDef = `{ - "api_id": "76", - "org_id": "default", - "enable_jwt": true, - "jwt_source": "Ci0tLS0tQkVHSU4gUFVCTElDIEtFWS0tLS0tCk1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBeXFaNHJ3S0Y4cUNFeFM3a3BZNGMKbkphLzM3Rk1rSk5rYWxaM091c2xMQjBvUkw4VDRjOTRrZEY0YWVOelNGa1NlMm45OUlCSTZTc2w3OXZiZk1aYgordDA2TDBROTRrKy9QMzd4NysvUkpaaWZmNHkxVkdqcm5ybk1JMml1OWw0aUJCUll6Tm1HNmVibHJvRU1NV2xnCms1dHlzSGd4QjU5Q1NOSWNEOWdxazFoeDRuL0ZnT212S3NmUWdXSE5sUFNEVFJjV0dXR2hCMi9YZ05WWUcycE8KbFF4QVBxTGhCSGVxR1RYQmJQZkdGOWNIeml4cHNQcjZHdGJ6UHdoc1EvOGJQeG9KN2hkZm4rcnp6dGtzM2Q2KwpIV1VSY3lOVExSZTBtalhqamVlOVo2K2daK0grZlM0cG5QOXRxVDdJZ1U2ZVBVV1Rwam9pUHRMZXhnc0FhL2N0CmpRSURBUUFCCi0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQo=", - "jwt_signing_method": "RSA", - "jwt_identity_base_field": "user_id", - "jwt_policy_field_name": "policy_id", - "auth": {"auth_header_name": "authorization"}, - "version_data": { - "not_versioned": true, - "versions": { - "v1": {"name": "v1"} - } - }, - "proxy": { - "listen_path": "/jwt_test", - "target_url": "` + testHttpAny + `" - } -}` - -const jwtWithCentralDefNoPolicyBaseField = `{ - "api_id": "76", - "org_id": "default", - "enable_jwt": true, - "jwt_source": "Ci0tLS0tQkVHSU4gUFVCTElDIEtFWS0tLS0tCk1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBeXFaNHJ3S0Y4cUNFeFM3a3BZNGMKbkphLzM3Rk1rSk5rYWxaM091c2xMQjBvUkw4VDRjOTRrZEY0YWVOelNGa1NlMm45OUlCSTZTc2w3OXZiZk1aYgordDA2TDBROTRrKy9QMzd4NysvUkpaaWZmNHkxVkdqcm5ybk1JMml1OWw0aUJCUll6Tm1HNmVibHJvRU1NV2xnCms1dHlzSGd4QjU5Q1NOSWNEOWdxazFoeDRuL0ZnT212S3NmUWdXSE5sUFNEVFJjV0dXR2hCMi9YZ05WWUcycE8KbFF4QVBxTGhCSGVxR1RYQmJQZkdGOWNIeml4cHNQcjZHdGJ6UHdoc1EvOGJQeG9KN2hkZm4rcnp6dGtzM2Q2KwpIV1VSY3lOVExSZTBtalhqamVlOVo2K2daK0grZlM0cG5QOXRxVDdJZ1U2ZVBVV1Rwam9pUHRMZXhnc0FhL2N0CmpRSURBUUFCCi0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQo=", - "jwt_signing_method": "RSA", - "jwt_identity_base_field": "user_id", - "jwt_client_base_field": "azp", - "auth": {"auth_header_name": "authorization"}, - "version_data": { - "not_versioned": true, - "versions": { - "v1": {"name": "v1"} - } - }, - "proxy": { - "listen_path": "/jwt_test", - "target_url": "` + testHttpAny + `" - } -}` - const jwtSecret = "9879879878787878" // openssl genrsa -out app.rsa @@ -161,605 +77,516 @@ func createJWTSessionWithRSA() *user.SessionState { return session } -func createJWTSessionWithRSAWithPolicy() *user.SessionState { +func createJWTSessionWithRSAWithPolicy(policyID string) *user.SessionState { session := createJWTSessionWithRSA() - session.SetPolicies("987654321") + session.SetPolicies(policyID) return session } -func getJWTChain(spec *APISpec) http.Handler { - remote, _ := url.Parse(testHttpAny) - proxy := TykNewSingleHostReverseProxy(remote, spec) - proxyHandler := ProxyHandler(proxy, spec) - baseMid := BaseMiddleware{spec, proxy} - chain := alice.New(mwList( - &IPWhiteListMiddleware{baseMid}, - &JWTMiddleware{baseMid}, - &VersionCheck{BaseMiddleware: baseMid}, - &KeyExpired{baseMid}, - &AccessRightsCheck{baseMid}, - &RateLimitAndQuotaCheck{baseMid}, - )...).Then(proxyHandler) - return chain -} - func TestJWTSessionHMAC(t *testing.T) { - tokenKID := testKey(t, "token") - spec := createSpecTest(t, jwtDef) - spec.JWTSigningMethod = "hmac" + ts := newTykTestServer() + defer ts.Close() + + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.JWTSigningMethod = "hmac" + spec.EnableJWT = true + spec.Proxy.ListenPath = "/" + })[0] + + spec = loadAPI(spec)[0] session := createJWTSession() + tokenKID := testKey(t, "token") spec.SessionManager.UpdateSession(tokenKID, session, 60) - // Create the token - token := jwt.New(jwt.SigningMethodHS256) - // Set the token ID - token.Header["kid"] = tokenKID - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() - // Sign and get the complete encoded token as a string - tokenString, err := token.SignedString([]byte(jwtSecret)) - if err != nil { - t.Fatal("Couldn't create JWT token: ", err) - } - - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) - req.Header.Set("authorization", tokenString) - - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) - - if recorder.Code != 200 { - t.Error("Initial request failed with non-200 code, should have gone through!: \n", recorder.Code) - } + jwtToken := createJWKTokenHMAC(func(t *jwt.Token) { + t.Header["kid"] = tokenKID + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) + + authHeaders := map[string]string{"authorization": jwtToken} + t.Run("Request with valid JWT signed with HMAC", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 200, + }) + }) } func TestJWTSessionRSA(t *testing.T) { - tokenKID := testKey(t, "token") - spec := createSpecTest(t, jwtDef) - spec.JWTSigningMethod = "rsa" - session := createJWTSessionWithRSA() - spec.SessionManager.UpdateSession(tokenKID, session, 60) + ts := newTykTestServer() + defer ts.Close() - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set the token ID - token.Header["kid"] = tokenKID - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() - // Sign and get the complete encoded token as a string - signKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(jwtRSAPrivKey)) - if err != nil { - t.Fatal("Couldn't extract private key: ", err) - } - tokenString, err := token.SignedString(signKey) - if err != nil { - t.Fatal("Couldn't create JWT token: ", err) - } - - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) - req.Header.Set("authorization", tokenString) - - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) - - if recorder.Code != 200 { - t.Error("Initial request failed with non-200 code, should have gone through!: \n", recorder.Code) - } -} + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.JWTSigningMethod = "rsa" + spec.EnableJWT = true + spec.Proxy.ListenPath = "/" + })[0] -func TestJWTSessionFailRSA_EmptyJWT(t *testing.T) { - tokenKID := testKey(t, "token") - spec := createSpecTest(t, jwtDef) - spec.JWTSigningMethod = "rsa" + spec = loadAPI(spec)[0] session := createJWTSessionWithRSA() + tokenKID := testKey(t, "token") spec.SessionManager.UpdateSession(tokenKID, session, 60) - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set the token ID - token.Header["kid"] = tokenKID - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() - - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) + jwtToken := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = tokenKID + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) - // Make it empty - req.Header.Set("authorization", "") + authHeaders := map[string]string{"authorization": jwtToken} + t.Run("Request with valid JWT", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 200, + }) + }) +} - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) +func TestJWTSessionFailRSA_EmptyJWT(t *testing.T) { + ts := newTykTestServer() + defer ts.Close() - if recorder.Code != 400 { - t.Error("Initial request failed with non-400 code, was: \n", recorder.Code) - } -} + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.JWTSigningMethod = "rsa" + spec.EnableJWT = true + spec.Proxy.ListenPath = "/" + })[0] -func TestJWTSessionFailRSA_NoAuthHeader(t *testing.T) { - tokenKID := testKey(t, "token") - spec := createSpecTest(t, jwtDef) - spec.JWTSigningMethod = "rsa" + spec = loadAPI(spec)[0] session := createJWTSessionWithRSA() + tokenKID := testKey(t, "token") spec.SessionManager.UpdateSession(tokenKID, session, 60) - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set the token ID - token.Header["kid"] = tokenKID - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + authHeaders := map[string]string{"authorization": ""} + t.Run("Request with empty authorization header", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 400, + }) + }) +} + +func TestJWTSessionFailRSA_NoAuthHeader(t *testing.T) { + ts := newTykTestServer() + defer ts.Close() - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.JWTSigningMethod = "rsa" + spec.EnableJWT = true + spec.Proxy.ListenPath = "/" + })[0] - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) + spec = loadAPI(spec)[0] + session := createJWTSessionWithRSA() + tokenKID := testKey(t, "token") + spec.SessionManager.UpdateSession(tokenKID, session, 60) - if recorder.Code != 400 { - t.Error("Initial request failed with non-400 code, was: \n", recorder.Code) - } + authHeaders := map[string]string{} + t.Run("Request without authorization header", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 400, + }) + }) } func TestJWTSessionFailRSA_MalformedJWT(t *testing.T) { - tokenKID := testKey(t, "token") - spec := createSpecTest(t, jwtDef) - spec.JWTSigningMethod = "rsa" + ts := newTykTestServer() + defer ts.Close() + + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.JWTSigningMethod = "rsa" + spec.EnableJWT = true + spec.Proxy.ListenPath = "/" + })[0] + + spec = loadAPI(spec)[0] session := createJWTSessionWithRSA() + tokenKID := testKey(t, "token") spec.SessionManager.UpdateSession(tokenKID, session, 60) - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set the token ID - token.Header["kid"] = tokenKID - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() - // Sign and get the complete encoded token as a string - signKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(jwtRSAPrivKey)) - if err != nil { - t.Fatal("Couldn't extract private key: ", err) - } - tokenString, err := token.SignedString(signKey) - if err != nil { - t.Fatal("Couldn't create JWT token: ", err) - } - - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) - - // Make it empty - req.Header.Set("authorization", tokenString+"ajhdkjhsdfkjashdkajshdkajhsdkajhsd") - - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) - - if recorder.Code != 403 { - t.Error("Initial request failed with non-403 code, was: \n", recorder.Code) - } + jwtToken := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = tokenKID + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) + + authHeaders := map[string]string{"authorization": jwtToken + "ajhdkjhsdfkjashdkajshdkajhsdkajhsd"} + t.Run("Request with malformed JWT", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 403, + }) + }) } func TestJWTSessionFailRSA_MalformedJWT_NOTRACK(t *testing.T) { - tokenKID := testKey(t, "token") - spec := createSpecTest(t, jwtDef) - spec.DoNotTrack = true - spec.JWTSigningMethod = "rsa" - session := createJWTSessionWithRSA() - spec.SessionManager.UpdateSession(tokenKID, session, 60) + ts := newTykTestServer() + defer ts.Close() - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set the token ID - token.Header["kid"] = tokenKID - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() - // Sign and get the complete encoded token as a string - signKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(jwtRSAPrivKey)) - if err != nil { - t.Fatal("Couldn't extract private key: ", err) - } - tokenString, err := token.SignedString(signKey) - if err != nil { - t.Fatal("Couldn't create JWT token: ", err) - } - - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) - - // Make it empty - req.Header.Set("authorization", tokenString+"ajhdkjhsdfkjashdkajshdkajhsdkajhsd") - - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) - - if recorder.Code != 403 { - t.Error("Initial request failed with non-403 code, was: \n", recorder.Code) - } -} + spec := buildAPI(func(spec *APISpec) { + spec.DoNotTrack = true + spec.UseKeylessAccess = false + spec.JWTSigningMethod = "rsa" + spec.EnableJWT = true + spec.Proxy.ListenPath = "/" + })[0] -func TestJWTSessionFailRSA_WrongJWT(t *testing.T) { - tokenKID := testKey(t, "token") - spec := createSpecTest(t, jwtDef) - spec.JWTSigningMethod = "rsa" + spec = loadAPI(spec)[0] session := createJWTSessionWithRSA() + tokenKID := testKey(t, "token") spec.SessionManager.UpdateSession(tokenKID, session, 60) - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set the token ID - token.Header["kid"] = tokenKID - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + jwtToken := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = tokenKID + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) + authHeaders := map[string]string{"authorization": jwtToken + "ajhdkjhsdfkjashdkajshdkajhsdkajhsd"} + t.Run("Request with malformed JWT no track", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 403, + }) + }) +} + +func TestJWTSessionFailRSA_WrongJWT(t *testing.T) { + ts := newTykTestServer() + defer ts.Close() - // Make it empty - req.Header.Set("authorization", "123") + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.JWTSigningMethod = "rsa" + spec.EnableJWT = true + spec.Proxy.ListenPath = "/" + })[0] - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) + spec = loadAPI(spec)[0] + session := createJWTSessionWithRSA() + tokenKID := testKey(t, "token") + spec.SessionManager.UpdateSession(tokenKID, session, 60) - if recorder.Code != 403 { - t.Error("Initial request failed with non-403 code, was: \n", recorder.Code) - } + authHeaders := map[string]string{"authorization": "123"} + t.Run("Request with invalid JWT", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 403, + }) + }) } func TestJWTSessionRSABearer(t *testing.T) { - tokenKID := testKey(t, "token") - spec := createSpecTest(t, jwtDef) - spec.JWTSigningMethod = "rsa" + ts := newTykTestServer() + defer ts.Close() + + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.JWTSigningMethod = "rsa" + spec.EnableJWT = true + spec.Proxy.ListenPath = "/" + })[0] + + spec = loadAPI(spec)[0] session := createJWTSessionWithRSA() - spec.SessionManager.ResetQuota(tokenKID, session) + tokenKID := testKey(t, "token") spec.SessionManager.UpdateSession(tokenKID, session, 60) - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set the token ID - token.Header["kid"] = tokenKID - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() - // Sign and get the complete encoded token as a string - signKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(jwtRSAPrivKey)) - if err != nil { - t.Fatal("Couldn't extract private key: ", err) - } - tokenString, err := token.SignedString(signKey) - if err != nil { - t.Fatal("Couldn't create JWT token: ", err) - } - - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) - req.Header.Set("authorization", "Bearer "+tokenString) - - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) - - if recorder.Code != 200 { - t.Error("Initial request failed with non-200 code, should have gone through!: \n", recorder.Code) - } + jwtToken := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = tokenKID + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) + + authHeaders := map[string]string{"authorization": "Bearer " + jwtToken} + t.Run("Request with valid Bearer", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 200, + }) + }) } func TestJWTSessionRSABearerInvalid(t *testing.T) { - tokenKID := testKey(t, "token") - spec := createSpecTest(t, jwtDef) - spec.JWTSigningMethod = "rsa" + ts := newTykTestServer() + defer ts.Close() + + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.JWTSigningMethod = "rsa" + spec.EnableJWT = true + spec.Proxy.ListenPath = "/" + })[0] + + spec = loadAPI(spec)[0] session := createJWTSessionWithRSA() + tokenKID := testKey(t, "token") + spec.SessionManager.ResetQuota(tokenKID, session) spec.SessionManager.UpdateSession(tokenKID, session, 60) - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set the token ID - token.Header["kid"] = tokenKID - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() - // Sign and get the complete encoded token as a string - signKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(jwtRSAPrivKey)) - if err != nil { - t.Fatal("Couldn't extract private key: ", err) - } - tokenString, err := token.SignedString(signKey) - if err != nil { - t.Fatal("Couldn't create JWT token: ", err) - } - - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) - // add a colon here - req.Header.Set("authorization", "Bearer: "+tokenString) - - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) - - if recorder.Code != 403 { - t.Error("Initial request failed with !=403 code, should have failed!: \n", recorder.Code) - } + jwtToken := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = tokenKID + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) + + authHeaders := map[string]string{"authorization": "Bearer: " + jwtToken} // extra ":" + t.Run("Request with invalid Bearer", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 403, + }) + }) } func TestJWTSessionRSAWithRawSourceOnWithClientID(t *testing.T) { - spec := createSpecTest(t, jwtWithCentralDefNoPolicyBaseField) - spec.JWTSigningMethod = "rsa" + ts := newTykTestServer() + defer ts.Close() + + spec := buildAndLoadAPI(func(spec *APISpec) { + spec.APIID = "777888" + spec.OrgID = "default" + spec.UseKeylessAccess = false + spec.EnableJWT = true + spec.JWTSigningMethod = "rsa" + spec.JWTSource = base64.StdEncoding.EncodeToString([]byte(jwtRSAPubKey)) + spec.JWTIdentityBaseField = "user_id" + spec.JWTClientIDBaseField = "azp" + spec.Proxy.ListenPath = "/" + })[0] + + policyID := createPolicy(func(p *user.Policy) { + p.OrgID = "default" + p.AccessRights = map[string]user.AccessDefinition{ + spec.APIID: { + APIName: spec.APIDefinition.Name, + APIID: spec.APIID, + Versions: []string{"default"}, + }, + } + }) tokenID := "1234567891010101" - session := createJWTSessionWithRSAWithPolicy() + session := createJWTSessionWithRSAWithPolicy(policyID) + spec.SessionManager.ResetQuota(tokenID, session) spec.SessionManager.UpdateSession(tokenID, session, 60) - policiesMu.Lock() - policiesByID["987654321"] = user.Policy{ - ID: "987654321", - OrgID: "default", - Rate: 1000.0, - Per: 1.0, - QuotaMax: -1, - QuotaRenewalRate: -1, - AccessRights: map[string]user.AccessDefinition{"76": { - APIName: "Test", - APIID: "76", - Versions: []string{"default"}, - }}, - Active: true, - KeyExpiresIn: 60, - } - policiesMu.Unlock() - - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["user_id"] = testKey(t, "token") - token.Claims.(jwt.MapClaims)["azp"] = tokenID - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() - // Sign and get the complete encoded token as a string - signKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(jwtRSAPrivKey)) - if err != nil { - t.Fatal("Couldn't extract private key: ", err) - } - tokenString, err := token.SignedString(signKey) - if err != nil { - t.Fatal("Couldn't create JWT token: ", err) - } - - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) - // add a colon here - req.Header.Set("authorization", "Bearer "+tokenString) - - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) - - if recorder.Code != 200 { - t.Error("Initial request failed with non-200 code, should have passed!: ", recorder.Code) - } + jwtToken := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = "12345" + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["user_id"] = "user" + t.Claims.(jwt.MapClaims)["azp"] = tokenID + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) + + authHeaders := map[string]string{"authorization": jwtToken} + t.Run("Initial request with no policy base field in JWT", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 200, + }) + }) } func TestJWTSessionRSAWithRawSource(t *testing.T) { - spec := createSpecTest(t, jwtWithCentralDef) - spec.JWTSigningMethod = "rsa" - - policiesMu.Lock() - policiesByID["987654321"] = user.Policy{ - ID: "987654321", - OrgID: "default", - Rate: 1000.0, - Per: 1.0, - QuotaMax: -1, - QuotaRenewalRate: -1, - AccessRights: map[string]user.AccessDefinition{}, - Active: true, - KeyExpiresIn: 60, - } - policiesMu.Unlock() - - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set the token ID - token.Header["kid"] = "12345" - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["user_id"] = testKey(t, "token") - token.Claims.(jwt.MapClaims)["policy_id"] = "987654321" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() - // Sign and get the complete encoded token as a string - signKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(jwtRSAPrivKey)) - if err != nil { - t.Fatal("Couldn't extract private key: ", err) - } - tokenString, err := token.SignedString(signKey) - if err != nil { - t.Fatal("Couldn't create JWT token: ", err) - } - - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) - // add a colon here - req.Header.Set("authorization", "Bearer "+tokenString) - - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) - - if recorder.Code != 200 { - t.Error("Initial request failed with non-200 code, should have passed!: ", recorder.Code) - } + ts := newTykTestServer() + defer ts.Close() + + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.EnableJWT = true + spec.JWTSigningMethod = "rsa" + spec.JWTSource = base64.StdEncoding.EncodeToString([]byte(jwtRSAPubKey)) + spec.JWTIdentityBaseField = "user_id" + spec.JWTPolicyFieldName = "policy_id" + spec.Proxy.ListenPath = "/" + })[0] + + loadAPI(spec) + + pID := createPolicy() + + jwtToken := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = "12345" + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["user_id"] = "user" + t.Claims.(jwt.MapClaims)["policy_id"] = pID + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) + + authHeaders := map[string]string{"authorization": jwtToken} + t.Run("Initial request with invalid policy", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 200, + }) + }) } func TestJWTSessionRSAWithRawSourceInvalidPolicyID(t *testing.T) { - spec := createSpecTest(t, jwtWithCentralDef) - spec.JWTSigningMethod = "rsa" - - policiesMu.Lock() - policiesByID["987654321"] = user.Policy{ - ID: "987654321", - OrgID: "default", - Rate: 1000.0, - Per: 1.0, - QuotaMax: -1, - QuotaRenewalRate: -1, - AccessRights: map[string]user.AccessDefinition{}, - Active: true, - KeyExpiresIn: 60, - } - policiesMu.Unlock() - - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set the token ID - token.Header["kid"] = "12345" - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["user_id"] = testKey(t, "token") - token.Claims.(jwt.MapClaims)["policy_id"] = "1234567898978788" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() - // Sign and get the complete encoded token as a string - signKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(jwtRSAPrivKey)) - if err != nil { - t.Fatal("Couldn't extract private key: ", err) - } - tokenString, err := token.SignedString(signKey) - if err != nil { - t.Fatal("Couldn't create JWT token: ", err) - } - - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) - // add a colon here - req.Header.Set("authorization", "Bearer "+tokenString) - - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) - - if recorder.Code != 403 { - t.Error("Initial request failed with non-403 code, should have failed!: ", recorder.Code) - } + ts := newTykTestServer() + defer ts.Close() + + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.EnableJWT = true + spec.JWTSigningMethod = "rsa" + spec.JWTSource = base64.StdEncoding.EncodeToString([]byte(jwtRSAPubKey)) + spec.JWTIdentityBaseField = "user_id" + spec.JWTPolicyFieldName = "policy_id" + spec.Proxy.ListenPath = "/" + })[0] + + loadAPI(spec) + + createPolicy() + + jwtToken := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = "12345" + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["user_id"] = "user" + t.Claims.(jwt.MapClaims)["policy_id"] = "abcxyz" + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) + + authHeaders := map[string]string{"authorization": jwtToken} + t.Run("Initial request with invalid policy", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 403, + }) + }) } func TestJWTExistingSessionRSAWithRawSourceInvalidPolicyID(t *testing.T) { - spec := createSpecTest(t, jwtWithCentralDef) - spec.JWTSigningMethod = "rsa" - - policiesMu.Lock() - policiesByID["987654321"] = user.Policy{ - ID: "987654321", - OrgID: "default", - Rate: 1000.0, - Per: 1.0, - QuotaMax: -1, - QuotaRenewalRate: -1, - AccessRights: map[string]user.AccessDefinition{}, - Active: true, - KeyExpiresIn: 60, - } - policiesMu.Unlock() - - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set the token ID - token.Header["kid"] = "12345" - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["user_id"] = testKey(t, "token") - token.Claims.(jwt.MapClaims)["policy_id"] = "987654321" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() - // Sign and get the complete encoded token as a string - signKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(jwtRSAPrivKey)) - if err != nil { - t.Fatal("Couldn't extract private key: ", err) - } - tokenString, err := token.SignedString(signKey) - if err != nil { - t.Fatal("Couldn't create JWT token: ", err) - } - - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) - // add a colon here - req.Header.Set("authorization", "Bearer "+tokenString) - - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) - - if recorder.Code != 200 { - t.Error("Initial request failed with non-200 code, should have passed!: ", recorder.Code) - } - - // put JWT invalid policy ID and do request again - token.Claims.(jwt.MapClaims)["policy_id"] = "abcdef" - tokenString, err = token.SignedString(signKey) - if err != nil { - t.Fatal("Couldn't create JWT token: ", err) - } - - recorder = httptest.NewRecorder() - req = testReq(t, "GET", "/jwt_test/", nil) - // add a colon here - req.Header.Set("authorization", "Bearer "+tokenString) - - chain.ServeHTTP(recorder, req) - - if recorder.Code != 403 { - t.Error("Initial request failed with non-403 code, should have failed!: ", recorder.Code) - } + ts := newTykTestServer() + defer ts.Close() + + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.EnableJWT = true + spec.JWTSigningMethod = "rsa" + spec.JWTSource = base64.StdEncoding.EncodeToString([]byte(jwtRSAPubKey)) + spec.JWTIdentityBaseField = "user_id" + spec.JWTPolicyFieldName = "policy_id" + spec.Proxy.ListenPath = "/" + })[0] + + loadAPI(spec) + p1ID := createPolicy() + + jwtToken := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = "12345" + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["user_id"] = "user" + t.Claims.(jwt.MapClaims)["policy_id"] = p1ID + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) + + authHeaders := map[string]string{"authorization": jwtToken} + t.Run("Initial request with valid policy", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 200, + }) + }) + + // put in JWT invalid policy ID and do request again + jwtTokenInvalidPolicy := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = "12345" + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["user_id"] = "user" + t.Claims.(jwt.MapClaims)["policy_id"] = "abcdef" + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) + + authHeaders = map[string]string{"authorization": jwtTokenInvalidPolicy} + t.Run("Request with invalid policy in JWT", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 403, + }) + }) +} + +func TestJWTExistingSessionRSAWithRawSourcePolicyIDChanged(t *testing.T) { + ts := newTykTestServer() + defer ts.Close() + + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.EnableJWT = true + spec.JWTSigningMethod = "rsa" + spec.JWTSource = base64.StdEncoding.EncodeToString([]byte(jwtRSAPubKey)) + spec.JWTIdentityBaseField = "user_id" + spec.JWTPolicyFieldName = "policy_id" + spec.Proxy.ListenPath = "/" + })[0] + + loadAPI(spec) + + p1ID := createPolicy() + p2ID := createPolicy() + + jwtToken := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = "12345" + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["user_id"] = "user" + t.Claims.(jwt.MapClaims)["policy_id"] = p1ID + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) + + authHeaders := map[string]string{"authorization": jwtToken} + t.Run("Initial request with 1st policy", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 200, + }) + }) + + // put in JWT another valid policy ID and do request again + jwtTokenAnotherPolicy := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = "12345" + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["user_id"] = "user" + t.Claims.(jwt.MapClaims)["policy_id"] = p2ID + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) + + authHeaders = map[string]string{"authorization": jwtTokenAnotherPolicy} + t.Run("Request with new valid policy in JWT", func(t *testing.T) { + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 200, + }) + }) } func TestJWTSessionRSAWithJWK(t *testing.T) { - spec := createSpecTest(t, jwtWithJWKDef) - spec.JWTSigningMethod = "rsa" - - policiesMu.Lock() - policiesByID["987654321"] = user.Policy{ - ID: "987654321", - OrgID: "default", - Rate: 1000.0, - Per: 1.0, - QuotaMax: -1, - QuotaRenewalRate: -1, - AccessRights: map[string]user.AccessDefinition{}, - Active: true, - KeyExpiresIn: 60, - } - policiesMu.Unlock() - - // Create the token - token := jwt.New(jwt.GetSigningMethod("RS512")) - // Set the token ID - token.Header["kid"] = "12345" - // Set some claims - token.Claims.(jwt.MapClaims)["foo"] = "bar" - token.Claims.(jwt.MapClaims)["user_id"] = testKey(t, "token") - token.Claims.(jwt.MapClaims)["policy_id"] = "987654321" - token.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() - // Sign and get the complete encoded token as a string - signKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(jwtRSAPrivKey)) - if err != nil { - t.Fatal("Couldn't extract private key: ", err) - } - tokenString, err := token.SignedString(signKey) - if err != nil { - t.Fatal("Couldn't create JWT token: ", err) - } - - recorder := httptest.NewRecorder() - req := testReq(t, "GET", "/jwt_test/", nil) - // add a colon here - req.Header.Set("authorization", "Bearer "+tokenString) - - chain := getJWTChain(spec) - chain.ServeHTTP(recorder, req) - - if recorder.Code != 200 { - t.Error("Initial request failed with non-200 code, should have passed!: ", recorder.Code) - } + ts := newTykTestServer() + defer ts.Close() + + spec := buildAPI(func(spec *APISpec) { + spec.UseKeylessAccess = false + spec.EnableJWT = true + spec.JWTSigningMethod = "rsa" + spec.JWTSource = testHttpJWK + spec.JWTIdentityBaseField = "user_id" + spec.JWTPolicyFieldName = "policy_id" + spec.Proxy.ListenPath = "/" + })[0] + + pID := createPolicy() + jwtToken := createJWKToken(func(t *jwt.Token) { + t.Header["kid"] = "12345" + t.Claims.(jwt.MapClaims)["foo"] = "bar" + t.Claims.(jwt.MapClaims)["user_id"] = "user" + t.Claims.(jwt.MapClaims)["policy_id"] = pID + t.Claims.(jwt.MapClaims)["exp"] = time.Now().Add(time.Hour * 72).Unix() + }) + + authHeaders := map[string]string{"authorization": jwtToken} + + t.Run("JWTSessionRSAWithJWK", func(t *testing.T) { + loadAPI(spec) + + ts.Run(t, test.TestCase{ + Headers: authHeaders, Code: 200, + }) + }) } func TestJWTSessionRSAWithEncodedJWK(t *testing.T) {