Line data Source code
1 :
2 : /*
3 : * Copyright (C) agentzh
4 : */
5 :
6 :
7 : #ifndef DDEBUG
8 : #define DDEBUG 0
9 : #endif
10 : #include "ddebug.h"
11 :
12 :
13 : #include "ngx_http_rds_json_util.h"
14 : #include "ngx_http_rds_json_processor.h"
15 : #include "ngx_http_rds_json_output.h"
16 : #include "ngx_http_rds.h"
17 : #include "ngx_http_rds_utils.h"
18 :
19 : #include <ngx_core.h>
20 : #include <ngx_http.h>
21 :
22 :
23 : ngx_int_t
24 0 : ngx_http_rds_json_process_header(ngx_http_request_t *r,
25 : ngx_chain_t *in, ngx_http_rds_json_ctx_t *ctx)
26 : {
27 : ngx_buf_t *b;
28 : ngx_http_rds_header_t header;
29 : ngx_int_t rc;
30 :
31 0 : if (in == NULL) {
32 0 : return NGX_OK;
33 : }
34 :
35 0 : b = in->buf;
36 :
37 0 : if (!ngx_buf_in_memory(b)) {
38 0 : if (!ngx_buf_special(b)) {
39 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
40 : "rds_json: process header: buf from "
41 : "upstream not in memory");
42 0 : goto invalid;
43 : }
44 :
45 0 : in = in->next;
46 :
47 0 : if (in == NULL) {
48 0 : return NGX_OK;
49 : }
50 :
51 0 : b = in->buf;
52 : }
53 :
54 0 : rc = ngx_http_rds_parse_header(r, b, &header);
55 :
56 0 : if (rc != NGX_OK) {
57 0 : goto invalid;
58 : }
59 :
60 : dd("col count: %d", (int) header.col_count);
61 :
62 0 : if (header.col_count == 0) {
63 : /* for empty result set, just return the JSON
64 : * representation of the RDS header */
65 :
66 : dd("col count == 0");
67 :
68 0 : if (b->pos != b->last) {
69 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
70 : "rds_json: header: there's unexpected remaining data "
71 : "in the buf");
72 0 : goto invalid;
73 : }
74 :
75 0 : ctx->state = state_done;
76 :
77 : /* now we send the postponed response header */
78 0 : if (!ctx->header_sent) {
79 0 : ctx->header_sent = 1;
80 :
81 0 : rc = ngx_http_rds_json_next_header_filter(r);
82 :
83 0 : if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
84 0 : return rc;
85 : }
86 : }
87 :
88 0 : rc = ngx_http_rds_json_output_header(r, ctx, &header);
89 :
90 0 : if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
91 0 : return rc;
92 : }
93 :
94 0 : ngx_http_rds_json_discard_bufs(r->pool, in);
95 :
96 0 : return rc;
97 : }
98 :
99 0 : ctx->cols = ngx_palloc(r->pool,
100 0 : header.col_count * sizeof(ngx_http_rds_column_t));
101 :
102 0 : if (ctx->cols == NULL) {
103 0 : goto invalid;
104 : }
105 :
106 0 : ctx->state = state_expect_col;
107 0 : ctx->cur_col = 0;
108 0 : ctx->col_count = header.col_count;
109 :
110 : /* now we send the postponed response header */
111 0 : if (!ctx->header_sent) {
112 0 : ctx->header_sent = 1;
113 :
114 0 : rc = ngx_http_rds_json_next_header_filter(r);
115 0 : if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
116 0 : return rc;
117 : }
118 : }
119 :
120 0 : return ngx_http_rds_json_process_col(r, b->pos == b->last ? in->next : in,
121 : ctx);
122 :
123 0 : invalid:
124 :
125 : dd("return 500");
126 0 : if (!ctx->header_sent) {
127 0 : ctx->header_sent = 1;
128 :
129 0 : r->headers_out.status = NGX_HTTP_INTERNAL_SERVER_ERROR;
130 0 : ngx_http_send_header(r);
131 0 : ngx_http_send_special(r, NGX_HTTP_LAST);
132 :
133 0 : return NGX_ERROR;
134 : }
135 :
136 0 : return NGX_ERROR;
137 : }
138 :
139 :
140 : ngx_int_t
141 0 : ngx_http_rds_json_process_col(ngx_http_request_t *r, ngx_chain_t *in,
142 : ngx_http_rds_json_ctx_t *ctx)
143 : {
144 : ngx_buf_t *b;
145 : ngx_int_t rc;
146 : ngx_http_rds_json_loc_conf_t *conf;
147 :
148 0 : if (in == NULL) {
149 0 : return NGX_OK;
150 : }
151 :
152 0 : b = in->buf;
153 :
154 0 : if (!ngx_buf_in_memory(b)) {
155 0 : if (!ngx_buf_special(b)) {
156 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
157 : "rds_json: process col: buf from "
158 : "upstream not in memory");
159 0 : return NGX_ERROR;
160 : }
161 :
162 0 : in = in->next;
163 :
164 0 : if (in == NULL) {
165 0 : return NGX_OK;
166 : }
167 :
168 0 : b = in->buf;
169 : }
170 :
171 : dd("parsing rds column");
172 :
173 0 : rc = ngx_http_rds_parse_col(r, b, &ctx->cols[ctx->cur_col]);
174 :
175 : dd("parse col returns %d (%d)", (int) rc, (int) NGX_OK);
176 :
177 0 : if (rc != NGX_OK) {
178 0 : return NGX_ERROR;
179 : }
180 :
181 0 : if (b->pos == b->last) {
182 : dd("parse col buf consumed");
183 0 : in = in->next;
184 : }
185 :
186 0 : ctx->cur_col++;
187 :
188 0 : if (ctx->cur_col >= ctx->col_count) {
189 : dd("end of column list");
190 :
191 0 : ctx->state = state_expect_row;
192 0 : ctx->row = 0;
193 :
194 : dd("output \"[\"");
195 : dd("before output literal");
196 :
197 0 : conf = ngx_http_get_module_loc_conf(r, ngx_http_rds_json_filter_module);
198 :
199 0 : if (conf->root.len) {
200 0 : rc = ngx_http_rds_json_output_props(r, ctx, conf);
201 :
202 : dd("after output literal");
203 :
204 0 : if (rc == NGX_ERROR || rc > NGX_OK) {
205 0 : return rc;
206 : }
207 : }
208 :
209 0 : rc = ngx_http_rds_json_output_literal(r, ctx,
210 : (u_char *)"[", sizeof("[") - 1,
211 : 0 /* last buf */);
212 :
213 : dd("after output literal");
214 :
215 0 : if (rc == NGX_ERROR || rc > NGX_OK) {
216 0 : return rc;
217 : }
218 :
219 0 : if (conf->format == json_format_compact) {
220 0 : rc = ngx_http_rds_json_output_cols(r, ctx);
221 :
222 0 : if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
223 0 : return rc;
224 : }
225 : }
226 :
227 : dd("after output literal");
228 :
229 0 : if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
230 0 : return rc;
231 : }
232 :
233 : dd("process col is entering process row...");
234 0 : return ngx_http_rds_json_process_row(r, in, ctx);
235 : }
236 :
237 0 : return ngx_http_rds_json_process_col(r, in, ctx);
238 : }
239 :
240 :
241 : ngx_int_t
242 0 : ngx_http_rds_json_process_row(ngx_http_request_t *r, ngx_chain_t *in,
243 : ngx_http_rds_json_ctx_t *ctx)
244 : {
245 : ngx_buf_t *b;
246 : ngx_int_t rc;
247 :
248 : ngx_http_rds_json_loc_conf_t *conf;
249 :
250 0 : if (in == NULL) {
251 0 : return NGX_OK;
252 : }
253 :
254 : dd("process row");
255 :
256 0 : b = in->buf;
257 :
258 0 : if (!ngx_buf_in_memory(b)) {
259 0 : if (!ngx_buf_special(b)) {
260 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
261 : "rds_json: process row: buf from "
262 : "upstream not in memory");
263 0 : return NGX_ERROR;
264 : }
265 :
266 0 : in = in->next;
267 :
268 0 : if (in == NULL) {
269 0 : return NGX_OK;
270 : }
271 :
272 0 : b = in->buf;
273 : }
274 :
275 0 : if (b->last - b->pos < (ssize_t) sizeof(uint8_t)) {
276 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
277 : "rds_json: row flag is incomplete in the buf");
278 0 : return NGX_ERROR;
279 : }
280 :
281 : dd("row flag: %d (offset %d)",
282 : (char) *b->pos,
283 : (int) (b->pos - b->start));
284 :
285 0 : if (*b->pos++ == 0) {
286 : /* end of row list */
287 0 : ctx->state = state_done;
288 :
289 0 : if (b->pos != b->last) {
290 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
291 : "rds_json: row: there's unexpected remaining data "
292 : "in the buf");
293 0 : return NGX_ERROR;
294 : }
295 :
296 0 : conf = ngx_http_get_module_loc_conf(r, ngx_http_rds_json_filter_module);
297 :
298 0 : if (conf->root.len) {
299 0 : rc = ngx_http_rds_json_output_literal(r, ctx,
300 : (u_char *)"]}",
301 : sizeof("]}") - 1,
302 : 1 /* last buf*/);
303 :
304 : } else {
305 0 : rc = ngx_http_rds_json_output_literal(r, ctx,
306 : (u_char *)"]",
307 : sizeof("]") - 1,
308 : 1 /* last buf*/);
309 : }
310 :
311 0 : if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
312 0 : return rc;
313 : }
314 :
315 0 : return rc;
316 : }
317 :
318 0 : ctx->row++;
319 0 : ctx->cur_col = 0;
320 0 : ctx->state = state_expect_field;
321 :
322 0 : if (b->pos == b->last) {
323 0 : in = in->next;
324 :
325 : } else {
326 : dd("process row: buf not consumed completely");
327 : }
328 :
329 0 : return ngx_http_rds_json_process_field(r, in, ctx);
330 : }
331 :
332 :
333 : ngx_int_t
334 0 : ngx_http_rds_json_process_field(ngx_http_request_t *r, ngx_chain_t *in,
335 : ngx_http_rds_json_ctx_t *ctx)
336 : {
337 : size_t total, len;
338 : ngx_buf_t *b;
339 : ngx_int_t rc;
340 :
341 : for (;;) {
342 0 : if (in == NULL) {
343 0 : return NGX_OK;
344 : }
345 :
346 0 : b = in->buf;
347 :
348 0 : if (!ngx_buf_in_memory(b)) {
349 : dd("buf not in memory");
350 :
351 0 : if (!ngx_buf_special(b)) {
352 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
353 : "rds_json: process field: buf from "
354 : "upstream not in memory");
355 0 : return NGX_ERROR;
356 : }
357 :
358 0 : in = in->next;
359 :
360 0 : if (in == NULL) {
361 0 : return NGX_OK;
362 : }
363 :
364 0 : b = in->buf;
365 : }
366 :
367 : dd("process field: buf size: %d", (int) ngx_buf_size(b));
368 :
369 0 : if (b->last - b->pos < (ssize_t) sizeof(uint32_t)) {
370 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
371 : "rds_json: field size is incomplete in the buf: %*s "
372 : "(len: %d)", b->last - b->pos, b->pos,
373 : (int) (b->last - b->pos));
374 :
375 0 : return NGX_ERROR;
376 : }
377 :
378 0 : total = *(uint32_t *) b->pos;
379 :
380 : dd("total: %d", (int) total);
381 :
382 0 : b->pos += sizeof(uint32_t);
383 :
384 0 : if (total == (uint32_t) -1) {
385 : /* SQL NULL found */
386 0 : total = 0;
387 0 : len = 0;
388 0 : ctx->field_data_rest = 0;
389 :
390 0 : rc = ngx_http_rds_json_output_field(r, ctx, b->pos, len,
391 : 1 /* is null */);
392 :
393 : } else {
394 0 : len = (uint32_t) (b->last - b->pos);
395 :
396 0 : if (len >= total) {
397 0 : len = total;
398 : }
399 :
400 0 : ctx->field_data_rest = total - len;
401 :
402 0 : rc = ngx_http_rds_json_output_field(r, ctx, b->pos, len,
403 : 0 /* not null */);
404 : }
405 :
406 0 : if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
407 0 : return rc;
408 : }
409 :
410 0 : b->pos += len;
411 :
412 0 : if (b->pos == b->last) {
413 0 : in = in->next;
414 : }
415 :
416 0 : if (len < total) {
417 : dd("process field: need to read more field data");
418 :
419 0 : ctx->state = state_expect_more_field_data;
420 :
421 0 : return ngx_http_rds_json_process_more_field_data(r, in, ctx);
422 : }
423 :
424 0 : ctx->cur_col++;
425 :
426 0 : if (ctx->cur_col >= ctx->col_count) {
427 : dd("reached the end of the current row");
428 :
429 0 : ctx->state = state_expect_row;
430 :
431 0 : return ngx_http_rds_json_process_row(r, in, ctx);
432 : }
433 :
434 : /* continue to process the next field (if any) */
435 : }
436 :
437 : /* impossible to reach here */
438 : }
439 :
440 :
441 : ngx_int_t
442 0 : ngx_http_rds_json_process_more_field_data(ngx_http_request_t *r,
443 : ngx_chain_t *in, ngx_http_rds_json_ctx_t *ctx)
444 : {
445 : ngx_int_t rc;
446 : ngx_buf_t *b;
447 : size_t len;
448 :
449 : for (;;) {
450 0 : if (in == NULL) {
451 0 : return NGX_OK;
452 : }
453 :
454 0 : b = in->buf;
455 :
456 0 : if (!ngx_buf_in_memory(b)) {
457 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
458 : "rds_json: buf from upstream not in memory");
459 0 : return NGX_ERROR;
460 : }
461 :
462 0 : len = b->last - b->pos;
463 :
464 0 : if (len >= ctx->field_data_rest) {
465 0 : len = ctx->field_data_rest;
466 0 : ctx->field_data_rest = 0;
467 :
468 : } else {
469 0 : ctx->field_data_rest -= len;
470 : }
471 :
472 0 : rc = ngx_http_rds_json_output_more_field_data(r, ctx, b->pos, len);
473 :
474 0 : if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
475 0 : return rc;
476 : }
477 :
478 0 : b->pos += len;
479 :
480 0 : if (b->pos == b->last) {
481 0 : in = in->next;
482 : }
483 :
484 0 : if (ctx->field_data_rest) {
485 : dd("process more field data: still some data remaining");
486 0 : continue;
487 : }
488 :
489 : dd("process more field data: reached the end of the current field");
490 :
491 0 : ctx->cur_col++;
492 :
493 0 : if (ctx->cur_col >= ctx->col_count) {
494 : dd("process more field data: reached the end of the current row");
495 :
496 0 : ctx->state = state_expect_row;
497 :
498 0 : return ngx_http_rds_json_process_row(r, in, ctx);
499 : }
500 :
501 : dd("proces more field data: read the next field");
502 :
503 0 : ctx->state = state_expect_field;
504 :
505 0 : return ngx_http_rds_json_process_field(r, in, ctx);
506 : }
507 :
508 : /* impossible to reach here */
509 : }
|