github.com/Tyktechnologies/tyk@v2.9.5+incompatible/gateway/mw_context_vars.go (about)

     1  package gateway
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	uuid "github.com/satori/go.uuid"
     8  
     9  	"github.com/TykTechnologies/tyk/request"
    10  )
    11  
    12  type MiddlewareContextVars struct {
    13  	BaseMiddleware
    14  }
    15  
    16  func (m *MiddlewareContextVars) Name() string {
    17  	return "MiddlewareContextVars"
    18  }
    19  
    20  func (m *MiddlewareContextVars) EnabledForSpec() bool {
    21  	return m.Spec.EnableContextVars
    22  }
    23  
    24  // ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
    25  func (m *MiddlewareContextVars) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
    26  
    27  	parseForm(r)
    28  
    29  	contextDataObject := map[string]interface{}{
    30  		"request_data": r.Form, // Form params (map[string][]string)
    31  		"headers":      map[string][]string(r.Header),
    32  		"headers_Host": r.Host,
    33  		"path_parts":   strings.Split(r.URL.Path, "/"), // Path parts
    34  		"path":         r.URL.Path,                     // path data
    35  		"remote_addr":  request.RealIP(r),              // IP
    36  		"request_id":   uuid.NewV4().String(),          //Correlation ID
    37  	}
    38  
    39  	for hname, vals := range r.Header {
    40  		n := "headers_" + strings.Replace(hname, "-", "_", -1)
    41  		contextDataObject[n] = vals[0]
    42  	}
    43  
    44  	for _, c := range r.Cookies() {
    45  		name := "cookies_" + strings.Replace(c.Name, "-", "_", -1)
    46  		contextDataObject[name] = c.Value
    47  	}
    48  
    49  	ctxSetData(r, contextDataObject)
    50  
    51  	return nil, http.StatusOK
    52  }