-
Notifications
You must be signed in to change notification settings - Fork 39
/
ngx_http_rds_json_util.c
147 lines (116 loc) · 3.05 KB
/
ngx_http_rds_json_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#define DDEBUG 0
#include "ddebug.h"
#include "resty_dbd_stream.h"
#include "ngx_http_rds_json_util.h"
uintptr_t
ngx_http_rds_json_escape_json_str(u_char *dst, u_char *src, size_t size)
{
ngx_uint_t n;
static u_char hex[] = "0123456789abcdef";
if (dst == NULL) {
/* find the number of characters to be escaped */
n = 0;
while (size) {
/* UTF-8 char has high bit of 1 */
if ((*src & 0x80) == 0) {
switch (*src) {
case '\r':
case '\n':
case '\\':
case '"':
case '\f':
case '\b':
case '\t':
n++;
break;
default:
if (*src < 32) {
n += sizeof("\\u00xx") - 2;
}
break;
}
}
src++;
size--;
}
return (uintptr_t) n;
}
while (size) {
if ((*src & 0x80) == 0) {
switch (*src) {
case '\r':
*dst++ = '\\';
*dst++ = 'r';
break;
case '\n':
*dst++ = '\\';
*dst++ = 'n';
break;
case '\\':
*dst++ = '\\';
*dst++ = '\\';
break;
case '"':
*dst++ = '\\';
*dst++ = '"';
break;
case '\f':
*dst++ = '\\';
*dst++ = 'f';
break;
case '\b':
*dst++ = '\\';
*dst++ = 'b';
break;
case '\t':
*dst++ = '\\';
*dst++ = 't';
break;
default:
if (*src < 32) { /* control chars */
*dst++ = '\\';
*dst++ = 'u';
*dst++ = '0';
*dst++ = '0';
*dst++ = hex[*src >> 4];
*dst++ = hex[*src & 0x0f];
} else {
*dst++ = *src;
}
break;
} /* switch */
src++;
} else {
*dst++ = *src++;
}
size--;
}
return (uintptr_t) dst;
}
ngx_int_t
ngx_http_rds_json_test_content_type(ngx_http_request_t *r)
{
ngx_str_t *type;
type = &r->headers_out.content_type;
if (type->len != rds_content_type_len ||
ngx_strncmp(type->data, rds_content_type,
rds_content_type_len) != 0)
{
return NGX_DECLINED;
}
return NGX_OK;
}
void
ngx_http_rds_json_discard_bufs(ngx_pool_t *pool, ngx_chain_t *in)
{
ngx_chain_t *cl;
for (cl = in; cl; cl = cl->next) {
#if 0
if (cl->buf->temporary
&& ngx_buf_size(cl->buf) > 0) {
ngx_pfree(pool, cl->buf->start);
}
#endif
cl->buf->pos = cl->buf->last;
}
}