@@ -45,14 +45,6 @@ func NewHAProxySPOA(config AppsecHAProxyConfig) *HAProxySPOA {
45
45
func (s * HAProxySPOA ) Handler (req * request.Request ) {
46
46
instr .Logger ().Debug ("haproxy_spoa: handle request EngineID: '%s', StreamID: '%d', FrameID: '%d' with %d messages" , req .EngineID , req .StreamID , req .FrameID , req .Messages .Len ())
47
47
48
- mp := message_processor .NewMessageProcessor (
49
- message_processor.MessageProcessorConfig {
50
- BlockingUnavailable : false ,
51
- BodyParsingSizeLimit : 1024 ,
52
- },
53
- instr ,
54
- )
55
-
56
48
// Process each message
57
49
for i := 0 ; i < req .Messages .Len (); i ++ {
58
50
msg , err := req .Messages .GetByIndex (i )
@@ -61,74 +53,65 @@ func (s *HAProxySPOA) Handler(req *request.Request) {
61
53
continue
62
54
}
63
55
56
+ // Get current request state from cache or if nil it will be created by the request headers message
57
+ reqState , _ := getCurrentRequest (msg )
58
+
64
59
ctx := context .Background ()
65
- mpAction , reqState , err := processMessage (mp , ctx , req , msg )
60
+ reqState , mpAction , err := processMessage (s . mp , ctx , req , msg , reqState )
66
61
if err != nil {
67
62
instr .Logger ().Error ("haproxy_spoa: error processing message %s: %v" , msg .Name , err )
68
63
return
69
64
}
70
65
71
- err = s .handleAction (mpAction , req , & reqState )
66
+ err = s .handleAction (mpAction , req , msg , reqState )
72
67
if err != nil {
73
68
instr .Logger ().Error ("haproxy_spoa: error processing message %s: %v" , msg .Name , err )
74
69
return
75
70
}
76
71
}
77
72
}
78
73
79
- func processMessage (mp message_processor.MessageProcessor , ctx context.Context , req * request.Request , msg * message.Message ) (message_processor.Action , message_processor.RequestState , error ) {
80
- instr .Logger ().Debug ("haproxy_spoa : handling message: %s" , msg .Name )
74
+ func processMessage (mp message_processor.MessageProcessor , ctx context.Context , req * request.Request , msg * message.Message , currentRequest * message_processor. RequestState ) (* message_processor.RequestState , message_processor.Action , error ) {
75
+ instr .Logger ().Debug ("f : handling message: %s" , msg .Name )
81
76
82
77
switch msg .Name {
83
78
case "http-request-headers-msg" :
84
- var (
85
- mpAction message_processor.Action
86
- err error
87
- currentRequest message_processor.RequestState
88
- )
89
- currentRequest , mpAction , err = mp .OnRequestHeaders (ctx , & requestHeadersHAProxy {req : req , msg : msg })
90
- return mpAction , currentRequest , err
79
+ return mp .OnRequestHeaders (ctx , & requestHeadersHAProxy {req : req , msg : msg })
91
80
case "http-request-body-msg" :
92
- currentRequest , err := getCurrentRequest (msg )
93
- if err != nil {
94
- return message_processor.Action {}, message_processor.RequestState {}, err
81
+ if currentRequest == nil || ! currentRequest .Ongoing {
82
+ return nil , message_processor.Action {}, fmt .Errorf ("received request body without request headers" )
95
83
}
96
84
97
- var action message_processor.Action
98
- action , err = mp .OnRequestBody (& requestBodyHAProxy {msg : msg }, currentRequest )
99
- return action , currentRequest , err
85
+ action , err := mp .OnRequestBody (& requestBodyHAProxy {msg : msg }, currentRequest )
86
+ return currentRequest , action , err
100
87
case "http-response-headers-msg" :
101
- currentRequest , err := getCurrentRequest (msg )
102
- if err != nil {
103
- return message_processor.Action {}, message_processor.RequestState {}, err
88
+ if currentRequest == nil || ! currentRequest .Ongoing {
89
+ return nil , message_processor.Action {}, fmt .Errorf ("received response headers without request context" )
104
90
}
105
91
106
- var action message_processor.Action
107
- action , err = mp .OnResponseHeaders (& responseHeadersHAProxy {msg : msg }, currentRequest )
108
- return action , currentRequest , err
92
+ action , err := mp .OnResponseHeaders (& responseHeadersHAProxy {msg : msg }, currentRequest )
93
+ return currentRequest , action , err
109
94
case "http-response-body-msg" :
110
- currentRequest , err := getCurrentRequest (msg )
111
- if err != nil {
112
- return message_processor.Action {}, message_processor.RequestState {}, err
95
+ if currentRequest == nil || ! currentRequest .Ongoing {
96
+ return nil , message_processor.Action {}, fmt .Errorf ("received response body without request context" )
113
97
}
114
98
115
- var action message_processor.Action
116
- action , err = mp .OnResponseBody (& responseBodyHAProxy {msg : msg }, currentRequest )
117
- return action , currentRequest , err
99
+ action , err := mp .OnResponseBody (& responseBodyHAProxy {msg : msg }, currentRequest )
100
+ return currentRequest , action , err
118
101
default :
119
- return message_processor. Action {} , message_processor.RequestState {}, fmt .Errorf ("unknown message type: %s" , msg .Name )
102
+ return nil , message_processor.Action {}, fmt .Errorf ("unknown message type: %s" , msg .Name )
120
103
}
121
104
}
122
105
123
- func (s * HAProxySPOA ) handleAction (action message_processor.Action , req * request.Request , reqState * message_processor.RequestState ) error {
106
+ func (s * HAProxySPOA ) handleAction (action message_processor.Action , req * request.Request , msg * message. Message , reqState * message_processor.RequestState ) error {
124
107
switch action .Type {
125
108
case message_processor .ActionTypeContinue :
126
109
if action .Response == nil {
127
110
return nil
128
111
}
129
112
130
113
if data := action .Response .(* message_processor.HeadersResponseData ); data != nil {
131
- return setHeadersResponseData (data , req , reqState )
114
+ return setHeadersResponseData (data , req , msg , reqState )
132
115
}
133
116
134
117
// Could happen if a new response type with data is implemented, and we forget to handle it here.
0 commit comments