@@ -100,7 +100,7 @@ websocket_log(char *str)
100
100
void
101
101
ws_do_log (compiled_template * template , ngx_http_request_t * r , void * ctx )
102
102
{
103
- if (ws_log ) {
103
+ if (ws_log && template ) {
104
104
char * log_line = apply_template (template , r , ctx );
105
105
websocket_log (log_line );
106
106
free (log_line );
@@ -330,6 +330,7 @@ my_send(ngx_connection_t *c, u_char *buf, size_t size)
330
330
ngx_atomic_fetch_add (frame_counter -> frames , 1 );
331
331
ngx_atomic_fetch_add (frame_counter -> total_payload_size ,
332
332
ctx -> frame_counter .current_payload_size );
333
+ ctx -> frame_counter .total_payload_size += ctx -> frame_counter .current_payload_size ;
333
334
ws_do_log (log_template , r , & template_ctx );
334
335
}
335
336
}
@@ -371,6 +372,7 @@ my_recv(ngx_connection_t *c, u_char *buf, size_t size)
371
372
ngx_atomic_fetch_add (frame_counter -> frames , 1 );
372
373
ngx_atomic_fetch_add (frame_counter -> total_payload_size ,
373
374
ctx -> frame_counter .current_payload_size );
375
+ ctx -> frame_counter .total_payload_size += ctx -> frame_counter .current_payload_size ;
374
376
ws_do_log (log_template , r , & template_ctx );
375
377
}
376
378
}
@@ -408,6 +410,7 @@ ngx_http_websocket_stat_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
408
410
ctx -> connection_id .data = ngx_pcalloc (r -> pool , UID_LENGTH + 1 );
409
411
ctx -> connection_id .len = UID_LENGTH ;
410
412
memcpy (ctx -> connection_id .data , request_id_str , UID_LENGTH + 1 );
413
+ ctx -> frame_counter .total_payload_size = 0 ;
411
414
412
415
ws_do_log (log_open_template , r , & template_ctx );
413
416
ngx_http_set_ctx (r , ctx , ngx_http_websocket_stat_module );
@@ -428,16 +431,16 @@ ngx_http_websocket_stat_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
428
431
return ngx_http_next_body_filter (r , in );
429
432
}
430
433
431
- char buff [100 ];
432
-
433
434
const char *
434
435
ws_packet_type (ngx_http_request_t * r , void * data )
435
436
{
436
437
template_ctx_s * ctx = data ;
437
438
ngx_frame_counter_t * frame_cntr = & (ctx -> ws_ctx -> frame_counter );
438
439
if (!ctx || !frame_cntr )
439
440
return UNKNOWN_VAR ;
440
- sprintf (buff , "%d" , frame_cntr -> current_frame_type );
441
+
442
+ char * buff = ngx_pcalloc (r -> pool , NGX_ATOMIC_T_LEN );
443
+ snprintf (buff , NGX_ATOMIC_T_LEN , "%d" , frame_cntr -> current_frame_type );
441
444
return buff ;
442
445
}
443
446
@@ -448,7 +451,22 @@ ws_packet_size(ngx_http_request_t *r, void *data)
448
451
ngx_frame_counter_t * frame_cntr = & ctx -> ws_ctx -> frame_counter ;
449
452
if (!ctx || !frame_cntr )
450
453
return UNKNOWN_VAR ;
451
- sprintf (buff , "%lu" , frame_cntr -> current_payload_size );
454
+
455
+ char * buff = ngx_pcalloc (r -> pool , NGX_ATOMIC_T_LEN );
456
+ snprintf (buff , NGX_ATOMIC_T_LEN , "%lu" , frame_cntr -> current_payload_size );
457
+ return (char * )buff ;
458
+ }
459
+
460
+ const char *
461
+ ws_total_payload_size (ngx_http_request_t * r , void * data )
462
+ {
463
+ template_ctx_s * ctx = data ;
464
+ ngx_frame_counter_t * frame_cntr = & ctx -> ws_ctx -> frame_counter ;
465
+ if (!ctx || !frame_cntr )
466
+ return UNKNOWN_VAR ;
467
+
468
+ char * buff = ngx_pcalloc (r -> pool , NGX_ATOMIC_T_LEN );
469
+ snprintf (buff , NGX_ATOMIC_T_LEN , "%lu" , frame_cntr -> total_payload_size );
452
470
return (char * )buff ;
453
471
}
454
472
@@ -475,6 +493,7 @@ get_core_var(ngx_http_request_t *r, const char *variable)
475
493
key = ngx_hash (key , * (variable ++ ));
476
494
477
495
vv = ngx_http_get_variable (r , & var , key );
496
+ char * buff = ngx_pcalloc (r -> pool , vv -> len + 1 );
478
497
memcpy (buff , vv -> data , vv -> len );
479
498
buff [vv -> len ] = '\0' ;
480
499
return buff ;
@@ -486,20 +505,26 @@ ws_connection_age(ngx_http_request_t *r, void *data)
486
505
template_ctx_s * ctx = data ;
487
506
if (!ctx || !ctx -> ws_ctx )
488
507
return UNKNOWN_VAR ;
489
- sprintf (buff , "%lu" , ngx_time () - ctx -> ws_ctx -> ws_conn_start_time );
508
+
509
+ char * buff = ngx_pcalloc (r -> pool , NGX_ATOMIC_T_LEN );
510
+ snprintf (buff , NGX_ATOMIC_T_LEN , "%lu" , ngx_time () - ctx -> ws_ctx -> ws_conn_start_time );
490
511
491
512
return (char * )buff ;
492
513
}
493
514
494
515
const char *
495
516
local_time (ngx_http_request_t * r , void * data )
496
517
{
497
- return memcpy (buff , ngx_cached_http_time .data , ngx_cached_http_time .len );
518
+ char * buff = ngx_pcalloc (r -> pool , ngx_cached_http_time .len + 1 );
519
+ memcpy (buff , ngx_cached_http_time .data , ngx_cached_http_time .len );
520
+ buff [ngx_cached_http_time .len ] = '\0' ;
521
+ return buff ;
498
522
}
499
523
500
524
const char *
501
525
remote_ip (ngx_http_request_t * r , void * data )
502
526
{
527
+ char * buff = ngx_pcalloc (r -> pool , r -> connection -> addr_text .len + 1 );
503
528
memcpy (buff , r -> connection -> addr_text .data , r -> connection -> addr_text .len );
504
529
buff [r -> connection -> addr_text .len ] = '\0' ;
505
530
@@ -545,6 +570,7 @@ GEN_CORE_GET_FUNC(server_port, "server_port")
545
570
const template_variable variables [] = {
546
571
{VAR_NAME ("$ws_opcode" ), sizeof ("ping" ) - 1 , ws_packet_type },
547
572
{VAR_NAME ("$ws_payload_size" ), NGX_SIZE_T_LEN , ws_packet_size },
573
+ {VAR_NAME ("$ws_total_payload_size" ), NGX_SIZE_T_LEN , ws_total_payload_size },
548
574
{VAR_NAME ("$ws_packet_source" ), sizeof ("upstream" ) - 1 , ws_packet_source },
549
575
{VAR_NAME ("$ws_conn_age" ), NGX_SIZE_T_LEN , ws_connection_age },
550
576
{VAR_NAME ("$time_local" ), sizeof ("Mon, 23 Oct 2017 11:27:42 GMT" ) - 1 ,
@@ -585,12 +611,18 @@ ngx_http_ws_log_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
585
611
ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 , "Wrong argument number" );
586
612
return NGX_CONF_ERROR ;
587
613
}
614
+
588
615
if (cf -> args -> nelts == 2 ) {
589
616
log_template =
590
617
compile_template ((char * )args [1 ].data , variables , cf -> pool );
591
618
return NGX_CONF_OK ;
619
+
592
620
}
593
- if (strcmp ((char * )args [1 ].data , "close" ) == 0 ) {
621
+ if (strcmp ((char * )args [1 ].data , "packet" ) == 0 ) {
622
+ log_template =
623
+ compile_template ((char * )args [2 ].data , variables , cf -> pool );
624
+ return NGX_CONF_OK ;
625
+ } else if (strcmp ((char * )args [1 ].data , "close" ) == 0 ) {
594
626
log_close_template =
595
627
compile_template ((char * )args [2 ].data , variables , cf -> pool );
596
628
return NGX_CONF_OK ;
@@ -700,7 +732,7 @@ complete_ws_handshake(ngx_connection_t *connection, const char *ws_key)
700
732
Base64Encode (hash , SHA_DIGEST_LENGTH , access_key , ACCEPT_SIZE );
701
733
access_key [ACCEPT_SIZE ] = '\0' ;
702
734
char resp [256 ];
703
- sprintf (resp , resp_template , access_key );
735
+ snprintf (resp , 256 , resp_template , access_key );
704
736
ngx_log_error (NGX_LOG_ERR , ngx_cycle -> log , 0 ,
705
737
"Websocket connection closed" );
706
738
connection -> send (connection , (unsigned char * )resp , strlen (resp ));
@@ -746,18 +778,18 @@ ngx_http_websocket_stat_init(ngx_conf_t *cf)
746
778
ngx_http_next_body_filter = ngx_http_top_body_filter ;
747
779
ngx_http_top_body_filter = ngx_http_websocket_stat_body_filter ;
748
780
749
- if (!log_template ) {
750
- log_template =
751
- compile_template (default_log_template_str , variables , cf -> pool );
752
- }
753
- if (!log_open_template ) {
754
- log_open_template = compile_template (default_open_log_template_str ,
755
- variables , cf -> pool );
756
- }
757
- if (!log_close_template ) {
758
- log_close_template = compile_template (default_close_log_template_str ,
759
- variables , cf -> pool );
760
- }
781
+ // if (!log_template) {
782
+ // log_template =
783
+ // compile_template(default_log_template_str, variables, cf->pool);
784
+ // }
785
+ // if (!log_open_template) {
786
+ // log_open_template = compile_template(default_open_log_template_str,
787
+ // variables, cf->pool);
788
+ // }
789
+ // if (!log_close_template) {
790
+ // log_close_template = compile_template(default_close_log_template_str,
791
+ // variables, cf->pool);
792
+ // }
761
793
762
794
ngx_http_handler_pt * h ;
763
795
ngx_http_core_main_conf_t * cmcf ;
0 commit comments