@@ -275,28 +275,13 @@ func (gw *Gateway) CoProcessInit() {
275
275
276
276
// EnabledForSpec checks if this middleware should be enabled for a given API.
277
277
func (m * CoProcessMiddleware ) EnabledForSpec () bool {
278
-
279
278
if ! m .Gw .GetConfig ().CoProcessOptions .EnableCoProcess {
280
279
log .WithFields (logrus.Fields {
281
280
"prefix" : "coprocess" ,
282
281
}).Error ("Your API specifies a CP custom middleware, either Tyk wasn't build with CP support or CP is not enabled in your Tyk configuration file!" )
283
282
return false
284
283
}
285
284
286
- var supported bool
287
- for _ , driver := range supportedDrivers {
288
- if m .Spec .CustomMiddleware .Driver == driver {
289
- supported = true
290
- }
291
- }
292
-
293
- if ! supported {
294
- log .WithFields (logrus.Fields {
295
- "prefix" : "coprocess" ,
296
- }).Errorf ("Unsupported driver '%s'" , m .Spec .CustomMiddleware .Driver )
297
- return false
298
- }
299
-
300
285
log .WithFields (logrus.Fields {
301
286
"prefix" : "coprocess" ,
302
287
}).Debug ("Enabling CP middleware." )
@@ -306,13 +291,9 @@ func (m *CoProcessMiddleware) EnabledForSpec() bool {
306
291
307
292
// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
308
293
func (m * CoProcessMiddleware ) ProcessRequest (w http.ResponseWriter , r * http.Request , _ interface {}) (error , int ) {
309
- if d := loadedDrivers [m .Spec .CustomMiddleware .Driver ]; d == nil {
310
- log .WithFields (logrus.Fields {
311
- "prefix" : "coprocess" ,
312
- }).Errorf ("Driver '%s' isn't loaded" , m .Spec .CustomMiddleware .Driver )
313
- respCode := http .StatusInternalServerError
314
-
315
- return errors .New (http .StatusText (respCode )), respCode
294
+ errorCode , err := m .validateDriver ()
295
+ if err != nil {
296
+ return err , errorCode
316
297
}
317
298
318
299
if m .HookType == coprocess .HookType_CustomKeyCheck {
@@ -513,6 +494,38 @@ func (m *CoProcessMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Requ
513
494
return nil , http .StatusOK
514
495
}
515
496
497
+ func (m * CoProcessMiddleware ) validateDriver () (int , error ) {
498
+ if ! m .isDriverSupported () {
499
+ log .WithFields (logrus.Fields {
500
+ "prefix" : "coprocess" ,
501
+ }).Errorf ("Unsupported driver '%s'" , m .Spec .CustomMiddleware .Driver )
502
+ respCode := http .StatusInternalServerError
503
+
504
+ return respCode , errors .New (http .StatusText (respCode ))
505
+ }
506
+
507
+ if d := loadedDrivers [m .Spec .CustomMiddleware .Driver ]; d == nil {
508
+ log .WithFields (logrus.Fields {
509
+ "prefix" : "coprocess" ,
510
+ }).Errorf ("Driver '%s' isn't loaded" , m .Spec .CustomMiddleware .Driver )
511
+ respCode := http .StatusInternalServerError
512
+
513
+ return respCode , errors .New (http .StatusText (respCode ))
514
+ }
515
+
516
+ return http .StatusOK , nil
517
+ }
518
+
519
+ func (m * CoProcessMiddleware ) isDriverSupported () bool {
520
+ for _ , driver := range supportedDrivers {
521
+ if m .Spec .CustomMiddleware .Driver == driver {
522
+ return true
523
+ }
524
+ }
525
+
526
+ return false
527
+ }
528
+
516
529
type CustomMiddlewareResponseHook struct {
517
530
BaseTykResponseHandler
518
531
mw * CoProcessMiddleware
0 commit comments