-
Notifications
You must be signed in to change notification settings - Fork 39
/
ngx_http_rds_json_handler.c
195 lines (148 loc) · 5.01 KB
/
ngx_http_rds_json_handler.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_rds_json_handler.h"
#include "ngx_http_rds_json_util.h"
ngx_int_t
ngx_http_rds_json_ret_handler(ngx_http_request_t *r)
{
ngx_chain_t *cl;
ngx_buf_t *b;
size_t len;
ngx_http_rds_json_conf_t *conf;
ngx_str_t errstr;
ngx_int_t rc;
uintptr_t escape = 0;
ngx_str_t *values = NULL;
uintptr_t *escapes = NULL;
ngx_uint_t i;
ngx_http_rds_json_property_t *prop = NULL;
dd("entered ret handler");
conf = ngx_http_get_module_loc_conf(r,
ngx_http_rds_json_filter_module);
/* evaluate the final value of conf->errstr */
if (ngx_http_complex_value(r, conf->errstr, &errstr) != NGX_OK) {
return NGX_ERROR;
}
/* calculate the buffer size */
len = sizeof("{") - 1
+ conf->errcode_key.len
+ sizeof(":") - 1
+ conf->errcode.len
+ sizeof("}") - 1
;
if (errstr.len) {
escape = ngx_http_rds_json_escape_json_str(NULL,
errstr.data, errstr.len);
len += sizeof(",") - 1
+ conf->errstr_key.len
+ sizeof(":\"") - 1
+ errstr.len + escape
+ sizeof("\"") - 1
;
}
if (conf->success.len) {
len += conf->success.len + sizeof(":,") - 1;
if (ngx_atoi(conf->errcode.data, conf->errcode.len) == 0) {
len += sizeof("true") - 1;
} else {
len += sizeof("false") - 1;
}
}
if (conf->user_props) {
values = ngx_pnalloc(r->pool,
conf->user_props->nelts * (sizeof(ngx_str_t) +
sizeof(uintptr_t)));
if (values == NULL) {
return NGX_ERROR;
}
escapes = (uintptr_t *) ((u_char *) values +
conf->user_props->nelts * sizeof(ngx_str_t));
prop = conf->user_props->elts;
for (i = 0; i < conf->user_props->nelts; i++) {
if (ngx_http_complex_value(r, &prop[i].value, &values[i])
!= NGX_OK)
{
return NGX_ERROR;
}
escapes[i] = ngx_http_rds_json_escape_json_str(NULL, values[i].data,
values[i].len);
len += sizeof(":\"\",") - 1 + prop[i].key.len + values[i].len
+ escapes[i];
}
}
/* create the buffer */
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL) {
return NGX_ERROR;
}
if (r == r->main) {
b->last_buf = 1;
}
cl->buf = b;
cl->next = NULL;
/* copy data over to the buffer */
*b->last++ = '{';
b->last = ngx_copy(b->last, conf->errcode_key.data, conf->errcode_key.len);
*b->last++ = ':';
b->last = ngx_copy(b->last, conf->errcode.data, conf->errcode.len);
if (errstr.len) {
*b->last++ = ',';
b->last = ngx_copy(b->last,
conf->errstr_key.data, conf->errstr_key.len);
*b->last++ = ':';
*b->last++ = '"';
if (escape == 0) {
b->last = ngx_copy(b->last, errstr.data, errstr.len);
} else {
b->last = (u_char *) ngx_http_rds_json_escape_json_str(b->last,
errstr.data, errstr.len);
}
*b->last++ = '"';
}
if (conf->success.len) {
*b->last++ = ',';
b->last = ngx_copy(b->last, conf->success.data, conf->success.len);
if (ngx_atoi(conf->errcode.data, conf->errcode.len) == 0) {
b->last = ngx_copy_literal(b->last, ":true");
} else {
b->last = ngx_copy_literal(b->last, ":false");
}
}
if (conf->user_props) {
for (i = 0; i < conf->user_props->nelts; i++) {
*b->last++ = ',';
b->last = ngx_copy(b->last, prop[i].key.data, prop[i].key.len);
*b->last++ = ':';
*b->last++ = '"';
if (escapes[i] == 0) {
b->last = ngx_copy(b->last, values[i].data, values[i].len);
} else {
b->last = (u_char *) ngx_http_rds_json_escape_json_str(b->last,
values[i].data, values[i].len);
}
*b->last++ = '"';
}
}
*b->last++ = '}';
if (b->last != b->end) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds_json: rds_json_ret: buffer error");
return NGX_ERROR;
}
/* send headers */
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_type = conf->content_type;
r->headers_out.content_type_len = conf->content_type.len;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
dd("output filter...");
return ngx_http_output_filter(r, cl);
}