bug fix - request body not read properly
This commit is contained in:
@@ -10,88 +10,60 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/beckn/beckn-onix/pkg/log"
|
"github.com/beckn/beckn-onix/pkg/log"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config holds the configuration settings for the application.
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
ContextKeys []string // ContextKeys is a list of context keys used for request processing.
|
ContextKeys []string
|
||||||
Role string // Role specifies the role of the entity (e.g., subscriber, gateway).
|
Role string
|
||||||
}
|
|
||||||
|
|
||||||
type becknRequest struct {
|
|
||||||
Context map[string]any `json:"context"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const contextKey = "context"
|
const contextKey = "context"
|
||||||
const subscriberIDKey = "subscriber_id"
|
const subscriberIDKey = "subscriber_id"
|
||||||
|
|
||||||
// NewPreProcessor creates a middleware that processes incoming HTTP requests by extracting
|
|
||||||
// and modifying the request context based on the provided configuration.
|
|
||||||
func NewPreProcessor(cfg *Config) (func(http.Handler) http.Handler, error) {
|
func NewPreProcessor(cfg *Config) (func(http.Handler) http.Handler, error) {
|
||||||
if err := validateConfig(cfg); err != nil {
|
if err := validateConfig(cfg); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
body, _ := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
var req becknRequest
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to read request body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var req map[string]interface{}
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
if err := json.Unmarshal(body, &req); err != nil {
|
if err := json.Unmarshal(body, &req); err != nil {
|
||||||
http.Error(w, "Failed to decode request body", http.StatusBadRequest)
|
http.Error(w, "Failed to decode request body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.Context == nil {
|
|
||||||
http.Error(w, fmt.Sprintf("%s field not found.", contextKey), http.StatusBadRequest)
|
// Extract context from request
|
||||||
|
reqContext, ok := req["context"].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
http.Error(w, fmt.Sprintf("%s field not found or invalid.", contextKey), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var subID any
|
var subID any
|
||||||
switch cfg.Role {
|
switch cfg.Role {
|
||||||
case "bap":
|
case "bap":
|
||||||
subID = req.Context["bap_id"]
|
subID = reqContext["bap_id"]
|
||||||
case "bpp":
|
case "bpp":
|
||||||
subID = req.Context["bpp_id"]
|
subID = reqContext["bap_id"]
|
||||||
}
|
}
|
||||||
if subID != nil {
|
if subID != nil {
|
||||||
log.Debugf(ctx, "adding subscriberId to request:%s, %v", subscriberIDKey, subID)
|
log.Debugf(ctx, "adding subscriberId to request:%s, %v", subscriberIDKey, subID)
|
||||||
// TODO: Add a ContextKey type in model and use it here instead of raw context key.
|
|
||||||
ctx = context.WithValue(ctx, subscriberIDKey, subID)
|
ctx = context.WithValue(ctx, subscriberIDKey, subID)
|
||||||
}
|
}
|
||||||
for _, key := range cfg.ContextKeys {
|
|
||||||
value := uuid.NewString()
|
r.Body = io.NopCloser(bytes.NewBuffer(body))
|
||||||
updatedValue := update(req.Context, key, value)
|
r.ContentLength = int64(len(body))
|
||||||
ctx = context.WithValue(ctx, key, updatedValue)
|
|
||||||
}
|
|
||||||
reqData := map[string]any{"context": req.Context}
|
|
||||||
updatedBody, _ := json.Marshal(reqData)
|
|
||||||
r.Body = io.NopCloser(bytes.NewBuffer(updatedBody))
|
|
||||||
r.ContentLength = int64(len(updatedBody))
|
|
||||||
r = r.WithContext(ctx)
|
r = r.WithContext(ctx)
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func update(wrapper map[string]any, key string, value any) any {
|
|
||||||
field, exists := wrapper[key]
|
|
||||||
if !exists || isEmpty(field) {
|
|
||||||
wrapper[key] = value
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
return field
|
|
||||||
}
|
|
||||||
func isEmpty(v any) bool {
|
|
||||||
switch v := v.(type) {
|
|
||||||
case string:
|
|
||||||
return v == ""
|
|
||||||
case nil:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateConfig(cfg *Config) error {
|
func validateConfig(cfg *Config) error {
|
||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
return errors.New("config cannot be nil")
|
return errors.New("config cannot be nil")
|
||||||
|
|||||||
Reference in New Issue
Block a user