1- #include "ngx_http_websocket_stat_format.h"
2- #include "ngx_http_websocket_stat_frame_counter.h"
3- #include <assert.h>
41#include <ngx_config.h>
52#include <ngx_core.h>
63#include <ngx_http.h>
74
5+ #include "ngx_http_websocket_stat_format.h"
6+ #include "ngx_http_websocket_stat_frame_counter.h"
7+ #include <assert.h>
8+
89#include <openssl/bio.h>
910#include <openssl/evp.h>
1011#include <openssl/sha.h>
@@ -99,7 +100,7 @@ websocket_log(char *str)
99100void
100101ws_do_log (compiled_template * template , ngx_http_request_t * r , void * ctx )
101102{
102- if (ws_log ) {
103+ if (ws_log && template ) {
103104 char * log_line = apply_template (template , r , ctx );
104105 websocket_log (log_line );
105106 free (log_line );
@@ -329,14 +330,15 @@ my_send(ngx_connection_t *c, u_char *buf, size_t size)
329330 ngx_atomic_fetch_add (frame_counter -> frames , 1 );
330331 ngx_atomic_fetch_add (frame_counter -> total_payload_size ,
331332 ctx -> frame_counter .current_payload_size );
333+ ctx -> frame_counter .total_payload_size += ctx -> frame_counter .current_payload_size ;
332334 ws_do_log (log_template , r , & template_ctx );
333335 }
334336 }
335337 int n = orig_send (c , buf , size );
336338 if (n < 0 ) {
337- if (!ngx_atomic_cmp_set (ngx_websocket_stat_active , 0 , 0 )){
338- ngx_atomic_fetch_add (ngx_websocket_stat_active , -1 );
339- ws_do_log (log_close_template , r , & template_ctx );
339+ if (!ngx_atomic_cmp_set (ngx_websocket_stat_active , 0 , 0 )) {
340+ ngx_atomic_fetch_add (ngx_websocket_stat_active , -1 );
341+ ws_do_log (log_close_template , r , & template_ctx );
340342 }
341343 }
342344 return n ;
@@ -370,6 +372,7 @@ my_recv(ngx_connection_t *c, u_char *buf, size_t size)
370372 ngx_atomic_fetch_add (frame_counter -> frames , 1 );
371373 ngx_atomic_fetch_add (frame_counter -> total_payload_size ,
372374 ctx -> frame_counter .current_payload_size );
375+ ctx -> frame_counter .total_payload_size += ctx -> frame_counter .current_payload_size ;
373376 ws_do_log (log_template , r , & template_ctx );
374377 }
375378 }
@@ -401,10 +404,13 @@ ngx_http_websocket_stat_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
401404 if (ctx == NULL ) {
402405 return NGX_HTTP_INTERNAL_SERVER_ERROR ;
403406 }
407+ template_ctx .ws_ctx = ctx ;
408+
404409 const char * request_id_str = get_core_var (r , "request_id" );
405410 ctx -> connection_id .data = ngx_pcalloc (r -> pool , UID_LENGTH + 1 );
406411 ctx -> connection_id .len = UID_LENGTH ;
407412 memcpy (ctx -> connection_id .data , request_id_str , UID_LENGTH + 1 );
413+ ctx -> frame_counter .total_payload_size = 0 ;
408414
409415 ws_do_log (log_open_template , r , & template_ctx );
410416 ngx_http_set_ctx (r , ctx , ngx_http_websocket_stat_module );
@@ -415,26 +421,26 @@ ngx_http_websocket_stat_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
415421 ngx_atomic_fetch_add (ngx_websocket_stat_active , 1 );
416422 ctx -> ws_conn_start_time = ngx_time ();
417423 } else {
418- if (!ngx_atomic_cmp_set (ngx_websocket_stat_active , 0 , 0 )){
419- ngx_atomic_fetch_add (ngx_websocket_stat_active , -1 );
420- ws_do_log (log_close_template , r , & template_ctx );
424+ if (!ngx_atomic_cmp_set (ngx_websocket_stat_active , 0 , 0 )) {
425+ ngx_atomic_fetch_add (ngx_websocket_stat_active , -1 );
426+ ws_do_log (log_close_template , r , & template_ctx );
421427 }
422428 }
423429 }
424430
425431 return ngx_http_next_body_filter (r , in );
426432}
427433
428- char buff [100 ];
429-
430434const char *
431435ws_packet_type (ngx_http_request_t * r , void * data )
432436{
433437 template_ctx_s * ctx = data ;
434438 ngx_frame_counter_t * frame_cntr = & (ctx -> ws_ctx -> frame_counter );
435439 if (!ctx || !frame_cntr )
436440 return UNKNOWN_VAR ;
437- 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 );
438444 return buff ;
439445}
440446
@@ -445,7 +451,22 @@ ws_packet_size(ngx_http_request_t *r, void *data)
445451 ngx_frame_counter_t * frame_cntr = & ctx -> ws_ctx -> frame_counter ;
446452 if (!ctx || !frame_cntr )
447453 return UNKNOWN_VAR ;
448- 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 );
449470 return (char * )buff ;
450471}
451472
@@ -472,6 +493,7 @@ get_core_var(ngx_http_request_t *r, const char *variable)
472493 key = ngx_hash (key , * (variable ++ ));
473494
474495 vv = ngx_http_get_variable (r , & var , key );
496+ char * buff = ngx_pcalloc (r -> pool , vv -> len + 1 );
475497 memcpy (buff , vv -> data , vv -> len );
476498 buff [vv -> len ] = '\0' ;
477499 return buff ;
@@ -483,20 +505,26 @@ ws_connection_age(ngx_http_request_t *r, void *data)
483505 template_ctx_s * ctx = data ;
484506 if (!ctx || !ctx -> ws_ctx )
485507 return UNKNOWN_VAR ;
486- 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 );
487511
488512 return (char * )buff ;
489513}
490514
491515const char *
492516local_time (ngx_http_request_t * r , void * data )
493517{
494- 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 ;
495522}
496523
497524const char *
498525remote_ip (ngx_http_request_t * r , void * data )
499526{
527+ char * buff = ngx_pcalloc (r -> pool , r -> connection -> addr_text .len + 1 );
500528 memcpy (buff , r -> connection -> addr_text .data , r -> connection -> addr_text .len );
501529 buff [r -> connection -> addr_text .len ] = '\0' ;
502530
@@ -542,13 +570,14 @@ GEN_CORE_GET_FUNC(server_port, "server_port")
542570const template_variable variables [] = {
543571 {VAR_NAME ("$ws_opcode" ), sizeof ("ping" ) - 1 , ws_packet_type },
544572 {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 },
545574 {VAR_NAME ("$ws_packet_source" ), sizeof ("upstream" ) - 1 , ws_packet_source },
546575 {VAR_NAME ("$ws_conn_age" ), NGX_SIZE_T_LEN , ws_connection_age },
547576 {VAR_NAME ("$time_local" ), sizeof ("Mon, 23 Oct 2017 11:27:42 GMT" ) - 1 ,
548577 local_time },
549- {VAR_NAME ("$upstream_addr" ), 60 , upstream_addr },
550- {VAR_NAME ("$request" ), 60 , request },
551- {VAR_NAME ("$uri" ), 60 , uri },
578+ {VAR_NAME ("$upstream_addr" ), 160 , upstream_addr },
579+ {VAR_NAME ("$request" ), 160 , request },
580+ {VAR_NAME ("$uri" ), 160 , uri },
552581 {VAR_NAME ("$request_id" ), UID_LENGTH , request_id },
553582 {VAR_NAME ("$remote_user" ), 60 , remote_user },
554583 {VAR_NAME ("$remote_addr" ), 60 , remote_addr },
@@ -582,12 +611,18 @@ ngx_http_ws_log_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
582611 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 , "Wrong argument number" );
583612 return NGX_CONF_ERROR ;
584613 }
614+
585615 if (cf -> args -> nelts == 2 ) {
586616 log_template =
587617 compile_template ((char * )args [1 ].data , variables , cf -> pool );
588618 return NGX_CONF_OK ;
619+
589620 }
590- 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 ) {
591626 log_close_template =
592627 compile_template ((char * )args [2 ].data , variables , cf -> pool );
593628 return NGX_CONF_OK ;
@@ -697,7 +732,7 @@ complete_ws_handshake(ngx_connection_t *connection, const char *ws_key)
697732 Base64Encode (hash , SHA_DIGEST_LENGTH , access_key , ACCEPT_SIZE );
698733 access_key [ACCEPT_SIZE ] = '\0' ;
699734 char resp [256 ];
700- sprintf (resp , resp_template , access_key );
735+ snprintf (resp , 256 , resp_template , access_key );
701736 ngx_log_error (NGX_LOG_ERR , ngx_cycle -> log , 0 ,
702737 "Websocket connection closed" );
703738 connection -> send (connection , (unsigned char * )resp , strlen (resp ));
@@ -743,18 +778,18 @@ ngx_http_websocket_stat_init(ngx_conf_t *cf)
743778 ngx_http_next_body_filter = ngx_http_top_body_filter ;
744779 ngx_http_top_body_filter = ngx_http_websocket_stat_body_filter ;
745780
746- if (!log_template ) {
747- log_template =
748- compile_template (default_log_template_str , variables , cf -> pool );
749- }
750- if (!log_open_template ) {
751- log_open_template = compile_template (default_open_log_template_str ,
752- variables , cf -> pool );
753- }
754- if (!log_close_template ) {
755- log_close_template = compile_template (default_close_log_template_str ,
756- variables , cf -> pool );
757- }
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+ // }
758793
759794 ngx_http_handler_pt * h ;
760795 ngx_http_core_main_conf_t * cmcf ;
0 commit comments