Skip to content

Commit 48b6bf9

Browse files
committed
1 parent fbcd74a commit 48b6bf9

File tree

6 files changed

+73
-50
lines changed

6 files changed

+73
-50
lines changed

misc/nginx.dev.conf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#daemon off;
2-
#master_process off;
1+
daemon off;
2+
master_process off;
33

44
worker_rlimit_core 500M;
55

66
pid logs/nginx.pid;
77

8-
error_log logs/error.log;
8+
error_log stderr;
99
error_log logs/notice.log notice;
1010
error_log logs/info.log info;
1111
error_log logs/crit.log crit;

ngx_http_tnt_module.c

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
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

2324
typedef 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-
3933
typedef 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);
5650
static 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

5954
static ngx_int_t ngx_http_tnt_create_request(ngx_http_request_t *r);
6055
static 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)
371365
static ngx_int_t
372366
ngx_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
592593
ngx_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

606600
static void
607601
ngx_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+

test/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import urllib2
55
import traceback
6+
import os
67

78
URL = 'http://127.0.0.1:8081/tnt'
89
VERBOSE = False

test/echo.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ function echo(a)
77
return {a}
88
end
99

10+
function big_echo()
11+
local out = {}
12+
for i = 0, 100000 do
13+
out[i] = "string";
14+
end
15+
return {{out}};
16+
end
17+
1018
box.cfg {
1119
log_level = 5;
1220
listen = 9999;

test/post.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
wget --tries 1 --post-data $1 127.0.0.1:8081/tnt
2+
wget --tries 1 --post-data "$1" 127.0.0.1:8081/tnt

tp_transcode.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,17 @@ tp_dump(char *output, size_t output_size, char *input, size_t input_size)
141141
== TP_TRANSCODE_ERROR)
142142
return false;
143143

144-
if (tp_transcode(&t, input, input_size) == TP_TRANSCODE_ERROR)
144+
if (tp_transcode(&t, input, input_size) == TP_TRANSCODE_ERROR) {
145+
tp_transcode_free(&t);
145146
return false;
147+
}
146148

147149
size_t complete_msg_size = 0;
148150
tp_transcode_complete(&t, &complete_msg_size);
149151
output[complete_msg_size] = '0';
150152

153+
tp_transcode_free(&t);
154+
151155
return complete_msg_size > 0;
152156
}
153157

0 commit comments

Comments
 (0)