Skip to content

Commit 623794c

Browse files
committed
#52 - is fixed. The module has escaping any string(including keys) recv. from the Tarantool. Update (C) and comments. Update tests.
1 parent 1278ee5 commit 623794c

16 files changed

+430
-183
lines changed

Makefile

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,19 @@ configure-as-dynamic-debug:
8686
json2tp:
8787
$(CC) $(CFLAGS) $(DEV_CFLAGS) $(INC_FLAGS) $(LDFLAGS)\
8888
$(CUR_PATH)/misc/json2tp.c \
89+
src/json_encoders.c \
8990
src/tp_transcode.c \
9091
-o misc/json2tp \
9192
-lyajl_s
9293

9394
tp_dump:
9495
$(CC) $(CFLAGS) $(DEV_CFLAGS) $(INC_FLAGS) $(LDFLAGS)\
9596
$(CUR_PATH)/misc/tp_dump.c \
97+
src/json_encoders.c \
9698
src/tp_transcode.c \
9799
-o misc/tp_dump \
98100
-lyajl_s
99101

100-
tp_allowed:
101-
$(CC) $(CFLAGS) $(DEV_CFLAGS) $(INC_FLAGS) $(LDFLAGS)\
102-
$(CUR_PATH)/test/tp_allowed.c\
103-
$(CUR_PATH)/src/tp_allowed_methods.c\
104-
-o test/tp_allowed
105-
$(CUR_PATH)/test/tp_allowed
106-
107102
test-dev-man: utils build
108103
$(CUR_PATH)/test/transcode.sh
109104
$(CUR_PATH)/test/run_all.sh

config

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ __include_paths=" \
1212
"
1313

1414
__sources=" \
15-
$__module_src_dir/ngx_http_tnt_module.c \
15+
$__module_src_dir/json_encoders.c \
1616
$__module_src_dir/tp_transcode.c \
17+
$__module_src_dir/ngx_http_tnt_module.c \
1718
$__module_src_dir/ngx_http_tnt_handlers.c \
1819
"
1920
__headers=" \
2021
$__module_src_dir/debug.h \
21-
$__module_src_dir/ngx_http_tnt_handlers.h \
2222
$__module_src_dir/tp_ext.h \
23+
$__module_src_dir/json_encoders.h \
2324
$__module_src_dir/tp_transcode.h \
25+
$__module_src_dir/ngx_http_tnt_handlers.h \
2426
"
2527

2628
__old_style_build=yes

src/debug.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* Copyright (C) 2015 Tarantool AUTHORS:
29+
* Copyright (C) 2015-2016 Tarantool AUTHORS:
3030
* please see AUTHORS file.
3131
*/
3232

@@ -80,7 +80,7 @@ get_str_safe(const u_char *str) {
8080
# define crit(...) log_crit(r->connection->log, __VA_ARGS__)
8181
# else
8282
/** TODO
83-
* Warn. user here
83+
* Need I add some warn message?
8484
*/
8585
static inline void
8686
crit(...) {}

src/json_encoders.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Redistribution and use in source and binary forms, with or
3+
* without modification, are permitted provided that the following
4+
* conditions are met:
5+
*
6+
* 1. Redistributions of source code must retain the above
7+
* copyright notice, this list of conditions and the
8+
* following disclaimer.
9+
*
10+
* 2. Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following
12+
* disclaimer in the documentation and/or other materials
13+
* provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY AUTHORS ``AS IS'' AND
16+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19+
* AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23+
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26+
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27+
* SUCH DAMAGE.
28+
*
29+
* Copyright (C) 2016 Tarantool AUTHORS:
30+
* please see AUTHORS file.
31+
*/
32+
33+
#include "json_encoders.h"
34+
35+
36+
static inline void
37+
hex_buf_init(char *hex_buf)
38+
{
39+
hex_buf[0] = '\\';
40+
hex_buf[1] = 'u';
41+
hex_buf[2] = '0';
42+
hex_buf[3] = '0';
43+
hex_buf[6] = 0;
44+
}
45+
46+
47+
static inline void
48+
char_to_hex(char *hex_buf, unsigned char c)
49+
{
50+
static const char * hexchar = "0123456789ABCDEF";
51+
hex_buf[0] = hexchar[c >> 4];
52+
hex_buf[1] = hexchar[c & 0x0F];
53+
}
54+
55+
56+
const char * /*error message*/
57+
json_encode_string(
58+
char **buf, size_t buf_len,
59+
const char *str, size_t str_len,
60+
bool escape_solidus)
61+
{
62+
if (!buf || !*buf || !buf_len || !str || !str_len)
63+
return "json_encode_string: invalid arguments";
64+
65+
char hex_buf[7];
66+
67+
hex_buf_init(hex_buf);
68+
69+
if (!append_ch(buf, &buf_len, '\"'))
70+
goto oom;
71+
72+
for (;str_len != 0; --str_len, ++str) {
73+
74+
const char ch = *str;
75+
const char *escaped = NULL;
76+
77+
switch (ch) {
78+
case '\r': escaped = "\\r"; break;
79+
case '\n': escaped = "\\n"; break;
80+
case '\\': escaped = "\\\\"; break;
81+
/* it is not required to escape a solidus in JSON:
82+
* read sec. 2.5: http://www.ietf.org/rfc/rfc4627.txt
83+
* specifically, this production from the grammar:
84+
* unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
85+
*/
86+
case '/': if (escape_solidus) escaped = "\\/"; break;
87+
case '"': escaped = "\\\""; break;
88+
case '\f': escaped = "\\f"; break;
89+
case '\b': escaped = "\\b"; break;
90+
case '\t': escaped = "\\t"; break;
91+
default:
92+
if ((unsigned char) ch < 32) {
93+
char_to_hex(hex_buf + 4, ch);
94+
escaped = hex_buf;
95+
}
96+
break;
97+
} /* switch */
98+
99+
if (escaped) {
100+
if (!append_str(buf, &buf_len, escaped, strlen(escaped)))
101+
goto oom;
102+
} else {
103+
if (!append_ch(buf, &buf_len, ch))
104+
goto oom;
105+
}
106+
} /* for */
107+
108+
if (!append_ch(buf, &buf_len, '\"'))
109+
goto oom;
110+
111+
return NULL;
112+
oom:
113+
return "json_encode_string: out of memory";
114+
}

src/json_encoders.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Redistribution and use in source and binary forms, with or
3+
* without modification, are permitted provided that the following
4+
* conditions are met:
5+
*
6+
* 1. Redistributions of source code must retain the above
7+
* copyright notice, this list of conditions and the
8+
* following disclaimer.
9+
*
10+
* 2. Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following
12+
* disclaimer in the documentation and/or other materials
13+
* provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY AUTHORS ``AS IS'' AND
16+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19+
* AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23+
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26+
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27+
* SUCH DAMAGE.
28+
*
29+
* Copyright (C) 2016 Tarantool AUTHORS:
30+
* please see AUTHORS file.
31+
*/
32+
33+
#ifndef JSON_ENCODER_H_INCLUDED
34+
#define JSON_ENCODER_H_INCLUDED 1
35+
36+
#include <string.h>
37+
#include <stdlib.h>
38+
#include <stdbool.h>
39+
40+
41+
#ifdef __cplusplus
42+
extern "C" {
43+
#endif
44+
45+
/* {{{ API declaration */
46+
47+
/** Utils [
48+
*/
49+
static inline bool
50+
append_str(char **buf, size_t *buf_len, const char *str, size_t str_size)
51+
{
52+
if (*buf_len < str_size)
53+
return false;
54+
memcpy(*buf, str, str_size);
55+
*buf_len -= str_size;
56+
*buf += str_size;
57+
return true;
58+
}
59+
60+
static inline bool
61+
append_ch(char **buf, size_t *buf_len, char ch)
62+
{
63+
return append_str(buf, buf_len, (const char *)&ch, 1);
64+
}
65+
/* ]
66+
*/
67+
68+
/**
69+
* Encode input string to JSON string.
70+
* Plus, this function does "JSON escape" the input string
71+
*
72+
* Returns NULL - if ok, error message - if error occurred
73+
*/
74+
const char * /*error message*/
75+
json_encode_string(
76+
char **buf, size_t buf_len,
77+
const char *str, size_t str_len,
78+
bool escape_solidus);
79+
80+
/* escape_solidus = false
81+
*/
82+
#define json_encode_string_ns(a,b,c,d) \
83+
json_encode_string((a), (b), (c), (d), false)
84+
85+
/* escape_solidus = true
86+
*/
87+
#define json_encode_string_s(a,b,c,d) \
88+
json_encode_string((a), (b), (c), (d), true)
89+
90+
#ifdef __cplusplus
91+
} /* extern "C" */
92+
#endif
93+
94+
/* }}} */
95+
96+
#endif /* JSON_ENCODER_H_INCLUDED */

src/ngx_http_tnt_handlers.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <ngx_http_tnt_handlers.h>
3535
#include <debug.h>
3636

37+
3738
extern ngx_module_t ngx_http_tnt_module;
3839

3940
static inline size_t
@@ -131,9 +132,9 @@ ngx_http_tnt_read_greeting(ngx_http_request_t *r,
131132
{
132133
b->pos = b->pos + 128;
133134
/**
134-
* Sometimes Nginx reads only 'greeting'(i.e. 128 bytes) -- to avoid
135-
* side effects (inside 'init'/'filter') we must return to
136-
* 'process_header'
135+
* Nginx can read only the "greeting"(128 bytes).
136+
* This check is to avoid the "read only the greeting" side effects.
137+
* Fix is - I just telling the nginx read more bytes.
137138
*/
138139
if (b->pos == b->last) {
139140
return NGX_AGAIN;
@@ -250,7 +251,7 @@ ngx_http_tnt_set_method(ngx_http_tnt_ctx_t *ctx,
250251
ngx_memcpy(ctx->preset_method, start, ctx->preset_method_len);
251252
}
252253
}
253-
/* Else -- expect method in the body */
254+
/* Else -- expect the method in the body */
254255

255256
return NGX_OK;
256257

0 commit comments

Comments
 (0)