77#include <ngx_http.h>
88
99#include <tp_transcode.h>
10-
10+ #include <debug.h>
1111
1212#define log_crit (log , ...) \
1313 ngx_log_error_core(NGX_LOG_CRIT, (log), 0, __VA_ARGS__)
14-
1514#define crit (...) log_crit(r->connection->log, __VA_ARGS__)
1615
17- #define log_dd (log , ...) \
18- ngx_log_debug(NGX_LOG_NOTICE, (log), 0, __VA_ARGS__)
1916
20- #define dd (...) log_dd(r->connection->log, 0, __VA_ARGS__)
17+ enum ctx_state {
18+ OK = 0 ,
19+ INPUT_JSON_PARSE_FAILED ,
20+ INPUT_TO_LARGE
21+ };
2122
2223
2324typedef struct {
@@ -29,13 +30,6 @@ typedef struct {
2930} ngx_http_tnt_loc_conf_t ;
3031
3132
32- enum ctx_state {
33- OK = 0 ,
34- INPUT_JSON_PARSE_FAILED ,
35- INPUT_TO_LARGE
36- };
37-
38-
3933typedef struct {
4034 tp_transcode_t in_t , out_t ;
4135
@@ -55,6 +49,7 @@ static inline ngx_int_t ngx_http_tnt_read_greetings(ngx_http_request_t *r,
5549 ngx_http_tnt_ctx_t * ctx , ngx_buf_t * b );
5650static inline ngx_int_t ngx_http_tnt_say_error (ngx_http_request_t * r ,
5751 ngx_http_tnt_ctx_t * ctx , ngx_int_t code );
52+ static inline void ngx_http_tnt_cleanup (ngx_http_request_t * r );
5853
5954static ngx_int_t ngx_http_tnt_create_request (ngx_http_request_t * r );
6055static ngx_int_t ngx_http_tnt_reinit_request (ngx_http_request_t * r );
@@ -246,8 +241,7 @@ ngx_http_tnt_create_request(ngx_http_request_t *r)
246241 rc = tp_transcode_init (& ctx -> in_t , (char * )ctx -> out_chain -> buf -> start ,
247242 output_size , YAJL_JSON_TO_TP );
248243 if (rc == TP_TRANSCODE_ERROR ) {
249- crit ("input transcode init failed, transcode type: %d" ,
250- YAJL_JSON_TO_TP );
244+ crit ("line:%d tp_transcode_init() failed" , __LINE__ );
251245 return NGX_ERROR ;
252246 }
253247
@@ -371,14 +365,14 @@ ngx_http_tnt_create_request(ngx_http_request_t *r)
371365static ngx_int_t
372366ngx_http_tnt_reinit_request (ngx_http_request_t * r )
373367{
368+ dd ("reinit connection with Tarantool..." );
369+
374370 ngx_http_tnt_ctx_t * ctx = ngx_http_get_module_ctx (r , ngx_http_tnt_module );
375371 if (ctx != NULL ) {
376372 ngx_pfree (r -> pool , ctx );
377373 ngx_http_set_ctx (r , NULL , ngx_http_tnt_module );
378374 }
379375
380- dd ("reinit connection with Tarantool..." );
381-
382376 return NGX_OK ;
383377}
384378
@@ -429,8 +423,9 @@ ngx_http_tnt_process_header(ngx_http_request_t *r)
429423 * for the JSON message.
430424 */
431425 u -> headers_in .content_length_n = JSON_RPC_MAGIC + ctx -> payload * 2 ;
432- crit ("got 'payload' expected input len:%i, _magic_ output len:%i" ,
433- ctx -> payload , u -> headers_in .content_length_n );
426+
427+ dd ("got 'payload' expected input len:%i, _magic_ output len:%i" ,
428+ (int )ctx -> payload , (int )u -> headers_in .content_length_n );
434429
435430 ctx -> in_cache = ngx_create_temp_buf (r -> pool , ctx -> payload );
436431 if (ctx -> in_cache == NULL ) {
@@ -488,14 +483,14 @@ ngx_http_tnt_filter(void *data, ssize_t bytes)
488483 return NGX_ERROR ;
489484 }
490485
491- dd ("bytes recv:%d , upstream length:%d " , bytes , ctx -> payload );
486+ dd ("bytes recv:%i , upstream length:%i " , ( int ) bytes , ( int ) ctx -> payload );
492487
493488 b -> last = b -> last + (bytes > ctx -> payload ? bytes : ctx -> payload );
494489 ctx -> payload -= bytes ;
495490
496491
497492 if (ctx -> in_cache -> pos == ctx -> in_cache -> end ) {
498- crit ("in_cache overflow" );
493+ crit ("[BUG] ctx-> in_cache overflow" );
499494 return NGX_ERROR ;
500495 }
501496
@@ -508,6 +503,8 @@ ngx_http_tnt_filter(void *data, ssize_t bytes)
508503 }
509504
510505 if (ctx -> payload < 0 ) {
506+ /** We still have a change to parse tnt message ^_^
507+ */
511508 crit ("[BUG] payload < 0:%i" , ctx -> payload );
512509 }
513510
@@ -560,29 +557,33 @@ ngx_http_tnt_filter(void *data, ssize_t bytes)
560557 break ;
561558 }
562559
563- size_t complete_msg_size = 0 ;
564- rc = tp_transcode_complete (& ctx -> out_t , & complete_msg_size );
565- if (rc != TP_TRANSCODE_OK || result == NGX_ERROR ) {
566- crit ("'output coding' failed to complete" );
567- result = NGX_ERROR ;
568- } else {
560+ if (result == NGX_OK ) {
561+ size_t complete_msg_size = 0 ;
562+ rc = tp_transcode_complete (& ctx -> out_t , & complete_msg_size );
563+ if (rc != TP_TRANSCODE_OK || result == NGX_ERROR ) {
564+ crit ("'output coding' failed to complete" );
565+ result = NGX_ERROR ;
566+ } else {
569567
570- /** 'erase' trailer
571- */
572- u_char * p = NULL ;
573- for (p = cl -> buf -> start + complete_msg_size ;
574- p < cl -> buf -> end ;
575- ++ p )
576- {
577- * p = ' ' ;
578- }
568+ /** 'erase' trailer
569+ */
570+ u_char * p = NULL ;
571+ for (p = cl -> buf -> start + complete_msg_size ;
572+ p < cl -> buf -> end ;
573+ ++ p )
574+ {
575+ * p = ' ' ;
576+ }
579577
578+ }
580579 }
581580
582581 tp_transcode_free (& ctx -> out_t );
583582
584583 ctx -> payload = 0 ;
585- ctx -> in_cache -> pos = ctx -> in_cache -> start ;
584+ ngx_pfree (r -> pool , ctx -> in_cache );
585+ ctx -> in_cache = NULL ;
586+ ctx -> state = OK ;
586587
587588 return result ;
588589}
@@ -592,22 +593,15 @@ static void
592593ngx_http_tnt_abort_request (ngx_http_request_t * r )
593594{
594595 dd ("abort http tnt request" );
595-
596- ngx_http_tnt_ctx_t * ctx = ngx_http_get_module_ctx (r , ngx_http_tnt_module );
597- if (ctx != NULL ) {
598- ngx_pfree (r -> pool , ctx );
599- ngx_http_set_ctx (r , NULL , ngx_http_tnt_module );
600- }
601-
602- return ;
596+ ngx_http_tnt_cleanup (r );
603597}
604598
605599
606600static void
607601ngx_http_tnt_finalize_request (ngx_http_request_t * r , ngx_int_t rc )
608602{
609603 dd ("finalize http tnt request" );
610- return ;
604+ ngx_http_tnt_cleanup ( r ) ;
611605}
612606
613607
@@ -819,3 +813,19 @@ static inline ngx_int_t ngx_http_tnt_say_error(ngx_http_request_t *r,
819813 return NGX_OK ;
820814}
821815
816+
817+ static inline void
818+ ngx_http_tnt_cleanup (ngx_http_request_t * r )
819+ {
820+ ngx_http_tnt_ctx_t * ctx = ngx_http_get_module_ctx (r , ngx_http_tnt_module );
821+ if (ctx != NULL ) {
822+ ngx_pfree (r -> pool , ctx );
823+
824+ if (ctx -> in_cache != NULL ) {
825+ ngx_pfree (r -> pool , ctx -> in_cache );
826+ }
827+
828+ ngx_http_set_ctx (r , NULL , ngx_http_tnt_module );
829+ }
830+ }
831+
0 commit comments