Line data Source code
1 : #ifndef DDEBUG
2 : #define DDEBUG 0
3 : #endif
4 : #include "ddebug.h"
5 :
6 : #include "ngx_http_rds_json_handler.h"
7 : #include "ngx_http_rds_json_util.h"
8 :
9 :
10 : ngx_int_t
11 0 : ngx_http_rds_json_ret_handler(ngx_http_request_t *r)
12 : {
13 : ngx_chain_t *cl;
14 : ngx_buf_t *b;
15 : size_t len;
16 : ngx_str_t errstr;
17 : ngx_int_t rc;
18 0 : uintptr_t escape = 0;
19 0 : ngx_str_t *values = NULL;
20 0 : uintptr_t *escapes = NULL;
21 : ngx_uint_t i;
22 0 : ngx_http_rds_json_property_t *prop = NULL;
23 : ngx_http_rds_json_loc_conf_t *conf;
24 :
25 : dd("entered ret handler");
26 :
27 0 : conf = ngx_http_get_module_loc_conf(r,
28 : ngx_http_rds_json_filter_module);
29 :
30 : /* evaluate the final value of conf->errstr */
31 :
32 0 : if (ngx_http_complex_value(r, conf->errstr, &errstr) != NGX_OK) {
33 0 : return NGX_ERROR;
34 : }
35 :
36 : /* calculate the buffer size */
37 :
38 0 : len = sizeof("{") - 1
39 0 : + conf->errcode_key.len
40 : + sizeof(":") - 1
41 0 : + conf->errcode.len
42 : + sizeof("}") - 1
43 : ;
44 :
45 0 : if (errstr.len) {
46 0 : escape = ngx_http_rds_json_escape_json_str(NULL,
47 : errstr.data, errstr.len);
48 :
49 0 : len += sizeof(",") - 1
50 0 : + conf->errstr_key.len
51 : + sizeof(":") - 1
52 : + sizeof("\"") - 1
53 0 : + errstr.len
54 0 : + escape
55 : + sizeof("\"") - 1
56 : ;
57 : }
58 :
59 0 : if (conf->success.len) {
60 0 : len += conf->success.len + sizeof(":,") - 1;
61 0 : if (ngx_atoi(conf->errcode.data, conf->errcode.len) == 0) {
62 0 : len += sizeof("true") - 1;
63 :
64 : } else {
65 0 : len += sizeof("false") - 1;
66 : }
67 : }
68 :
69 0 : if (conf->user_props) {
70 0 : values = ngx_pnalloc(r->pool,
71 0 : conf->user_props->nelts * (sizeof(ngx_str_t) +
72 : sizeof(uintptr_t)));
73 :
74 0 : if (values == NULL) {
75 0 : return NGX_ERROR;
76 : }
77 :
78 0 : escapes = (uintptr_t *) ((u_char *) values +
79 0 : conf->user_props->nelts * sizeof(ngx_str_t));
80 :
81 0 : prop = conf->user_props->elts;
82 0 : for (i = 0; i < conf->user_props->nelts; i++) {
83 0 : if (ngx_http_complex_value(r, &prop[i].value, &values[i])
84 : != NGX_OK)
85 : {
86 0 : return NGX_ERROR;
87 : }
88 :
89 0 : escapes[i] = ngx_http_rds_json_escape_json_str(NULL, values[i].data,
90 0 : values[i].len);
91 :
92 0 : len += sizeof(":\"\",") - 1 + prop[i].key.len + values[i].len
93 0 : + escapes[i];
94 : }
95 : }
96 :
97 :
98 : /* create the buffer */
99 :
100 0 : cl = ngx_alloc_chain_link(r->pool);
101 0 : if (cl == NULL) {
102 0 : return NGX_ERROR;
103 : }
104 :
105 0 : b = ngx_create_temp_buf(r->pool, len);
106 0 : if (b == NULL) {
107 0 : return NGX_ERROR;
108 : }
109 :
110 0 : if (r == r->main) {
111 0 : b->last_buf = 1;
112 : }
113 :
114 0 : cl->buf = b;
115 0 : cl->next = NULL;
116 :
117 : /* copy data over to the buffer */
118 :
119 0 : *b->last++ = '{';
120 :
121 0 : b->last = ngx_copy(b->last, conf->errcode_key.data, conf->errcode_key.len);
122 0 : *b->last++ = ':';
123 0 : b->last = ngx_copy(b->last, conf->errcode.data, conf->errcode.len);
124 :
125 0 : if (errstr.len) {
126 0 : *b->last++ = ',';
127 0 : b->last = ngx_copy(b->last,
128 : conf->errstr_key.data, conf->errstr_key.len);
129 0 : *b->last++ = ':';
130 0 : *b->last++ = '"';
131 :
132 0 : if (escape == 0) {
133 0 : b->last = ngx_copy(b->last, errstr.data, errstr.len);
134 : } else {
135 0 : b->last = (u_char *) ngx_http_rds_json_escape_json_str(b->last,
136 : errstr.data, errstr.len);
137 : }
138 :
139 0 : *b->last++ = '"';
140 : }
141 :
142 0 : if (conf->success.len) {
143 0 : *b->last++ = ',';
144 0 : b->last = ngx_copy(b->last, conf->success.data, conf->success.len);
145 :
146 0 : if (ngx_atoi(conf->errcode.data, conf->errcode.len) == 0) {
147 0 : b->last = ngx_copy_literal(b->last, ":true");
148 :
149 : } else {
150 0 : b->last = ngx_copy_literal(b->last, ":false");
151 : }
152 : }
153 :
154 0 : if (conf->user_props) {
155 0 : for (i = 0; i < conf->user_props->nelts; i++) {
156 0 : *b->last++ = ',';
157 0 : b->last = ngx_copy(b->last, prop[i].key.data, prop[i].key.len);
158 0 : *b->last++ = ':';
159 0 : *b->last++ = '"';
160 :
161 0 : if (escapes[i] == 0) {
162 0 : b->last = ngx_copy(b->last, values[i].data, values[i].len);
163 :
164 : } else {
165 0 : b->last = (u_char *) ngx_http_rds_json_escape_json_str(b->last,
166 0 : values[i].data, values[i].len);
167 : }
168 :
169 0 : *b->last++ = '"';
170 : }
171 : }
172 :
173 0 : *b->last++ = '}';
174 :
175 0 : if (b->last != b->end) {
176 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
177 : "rds_json: rds_json_ret: buffer error");
178 :
179 0 : return NGX_ERROR;
180 : }
181 :
182 : /* send headers */
183 :
184 0 : r->headers_out.status = NGX_HTTP_OK;
185 0 : r->headers_out.content_type = conf->content_type;
186 0 : r->headers_out.content_type_len = conf->content_type.len;
187 :
188 0 : rc = ngx_http_send_header(r);
189 0 : if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
190 0 : return rc;
191 : }
192 :
193 : dd("output filter...");
194 :
195 0 : return ngx_http_output_filter(r, cl);
196 : }
197 :
|