@@ -15,6 +15,11 @@ typedef struct {
15
15
ngx_frame_counter_t frame_counter ;
16
16
} ngx_http_websocket_stat_ctx ;
17
17
18
+ typedef struct {
19
+ ngx_http_websocket_stat_ctx recv_ctx ;
20
+ ngx_http_websocket_stat_ctx send_ctx ;
21
+ } ngx_http_websocket_stat_request_ctx ;
22
+
18
23
typedef struct {
19
24
char from_client : 1 ;
20
25
ngx_http_websocket_stat_ctx * ws_ctx ;
@@ -145,15 +150,15 @@ my_send(ngx_connection_t *c, u_char *buf, size_t size)
145
150
{
146
151
ngx_http_request_t * r = c -> data ;
147
152
ngx_http_websocket_srv_conf_t * srvcf = ngx_http_get_module_srv_conf (r , ngx_http_websocket_stat_module );
148
- ngx_http_websocket_stat_ctx * ctx = ngx_http_get_module_ctx (r , ngx_http_websocket_stat_module );
153
+ ngx_http_websocket_stat_request_ctx * request_ctx = ngx_http_get_module_ctx (r , ngx_http_websocket_stat_module );
149
154
150
- if (check_ws_age (ctx -> ws_conn_start_time , r ) != NGX_OK ) {
155
+ if (check_ws_age (request_ctx -> send_ctx . ws_conn_start_time , r ) != NGX_OK ) {
151
156
return NGX_ERROR ;
152
157
}
153
158
154
159
template_ctx_s template_ctx ;
155
160
template_ctx .from_client = 0 ;
156
- template_ctx .ws_ctx = ctx ;
161
+ template_ctx .ws_ctx = & request_ctx -> send_ctx ;
157
162
158
163
int n = orig_send (c , buf , size );
159
164
if (n <= 0 ) {
@@ -164,7 +169,7 @@ my_send(ngx_connection_t *c, u_char *buf, size_t size)
164
169
ssize_t sz = n ;
165
170
166
171
while (sz > 0 ) {
167
- if (frame_counter_process_message (& buf , & sz , & ( ctx -> frame_counter ) )) {
172
+ if (frame_counter_process_message (& buf , & sz , & request_ctx -> send_ctx . frame_counter )) {
168
173
ws_do_log (get_ws_log_template (& template_ctx , srvcf ), r , & template_ctx );
169
174
}
170
175
}
@@ -178,7 +183,7 @@ my_recv(ngx_connection_t *c, u_char *buf, size_t size)
178
183
{
179
184
ngx_http_request_t * r = c -> data ;
180
185
ngx_http_websocket_srv_conf_t * srvcf = ngx_http_get_module_srv_conf (r , ngx_http_websocket_stat_module );
181
- ngx_http_websocket_stat_ctx * ctx = ngx_http_get_module_ctx (r , ngx_http_websocket_stat_module );
186
+ ngx_http_websocket_stat_request_ctx * request_ctx = ngx_http_get_module_ctx (r , ngx_http_websocket_stat_module );
182
187
183
188
int n = orig_recv (c , buf , size );
184
189
if (n <= 0 ) {
@@ -188,14 +193,14 @@ my_recv(ngx_connection_t *c, u_char *buf, size_t size)
188
193
ssize_t sz = n ;
189
194
template_ctx_s template_ctx ;
190
195
template_ctx .from_client = 1 ;
191
- template_ctx .ws_ctx = ctx ;
196
+ template_ctx .ws_ctx = & request_ctx -> recv_ctx ;
192
197
193
- if (check_ws_age (ctx -> ws_conn_start_time , r ) != NGX_OK ) {
198
+ if (check_ws_age (request_ctx -> recv_ctx . ws_conn_start_time , r ) != NGX_OK ) {
194
199
return NGX_ERROR ;
195
200
}
196
201
197
202
while (sz > 0 ) {
198
- if (frame_counter_process_message (& buf , & sz , & ctx -> frame_counter )) {
203
+ if (frame_counter_process_message (& buf , & sz , & request_ctx -> recv_ctx . frame_counter )) {
199
204
ws_do_log (get_ws_log_template (& template_ctx , srvcf ), r , & template_ctx );
200
205
}
201
206
}
@@ -224,23 +229,24 @@ ngx_http_websocket_stat_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
224
229
if (r -> headers_in .upgrade ) {
225
230
if (r -> upstream -> peer .connection ) {
226
231
// connection opened
227
- ngx_http_websocket_stat_ctx * ctx = ngx_pcalloc (r -> pool , sizeof (ngx_http_websocket_stat_ctx ));
232
+ ngx_http_websocket_stat_request_ctx * ctx = ngx_pcalloc (r -> pool , sizeof (ngx_http_websocket_stat_request_ctx ));
228
233
if (!ctx ) {
229
234
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
230
235
}
231
236
ngx_http_set_ctx (r , ctx , ngx_http_websocket_stat_module );
232
237
template_ctx_s template_ctx ;
233
- template_ctx .ws_ctx = ctx ;
238
+ template_ctx .ws_ctx = & ctx -> recv_ctx ;
234
239
ws_do_log (srvcf -> log_open_template , r , & template_ctx );
235
240
orig_recv = r -> connection -> recv ;
236
241
r -> connection -> recv = my_recv ;
237
242
orig_send = r -> connection -> send ;
238
243
r -> connection -> send = my_send ;
239
- ctx -> ws_conn_start_time = ngx_time ();
244
+ ctx -> recv_ctx .ws_conn_start_time = ngx_time ();
245
+ ctx -> send_ctx .ws_conn_start_time = ctx -> recv_ctx .ws_conn_start_time ;
240
246
} else {
241
- ngx_http_websocket_stat_ctx * ctx = ngx_http_get_module_ctx (r , ngx_http_websocket_stat_module );
247
+ ngx_http_websocket_stat_request_ctx * ctx = ngx_http_get_module_ctx (r , ngx_http_websocket_stat_module );
242
248
template_ctx_s template_ctx ;
243
- template_ctx .ws_ctx = ctx ;
249
+ template_ctx .ws_ctx = & ctx -> recv_ctx ;
244
250
ws_do_log (srvcf -> log_close_template , r , & template_ctx );
245
251
}
246
252
}
0 commit comments