LCOV - code coverage report
Current view: top level - home/build/openresty-1.13.6.1/build/rds-json-nginx-module-0.15/src - ngx_http_rds_json_output.c (source / functions) Hit Total Coverage
Test: coverage ngix Lines: 0 461 0.0 %
Date: 2020-03-03 04:25:50 Functions: 0 12 0.0 %
Legend: Lines: hit not hit

          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_filter_module.h"
      14             : #include "ngx_http_rds_json_output.h"
      15             : #include "ngx_http_rds_json_util.h"
      16             : #include "resty_dbd_stream.h"
      17             : #include <nginx.h>
      18             : 
      19             : 
      20             : static u_char *ngx_http_rds_json_request_mem(ngx_http_request_t *r,
      21             :     ngx_http_rds_json_ctx_t *ctx, size_t len);
      22             : static ngx_int_t ngx_http_rds_json_get_buf(ngx_http_request_t *r,
      23             :     ngx_http_rds_json_ctx_t *ctx);
      24             : static u_char *ngx_http_rds_json_get_postponed(ngx_http_request_t *r,
      25             :     ngx_http_rds_json_ctx_t *ctx, size_t len);
      26             : static ngx_int_t ngx_http_rds_json_submit_mem(ngx_http_request_t *r,
      27             :     ngx_http_rds_json_ctx_t *ctx, size_t len, unsigned last_buf);
      28             : 
      29             : 
      30             : static size_t ngx_get_num_size(uint64_t i);
      31             : 
      32             : 
      33             : ngx_int_t
      34           0 : ngx_http_rds_json_output_literal(ngx_http_request_t *r,
      35             :     ngx_http_rds_json_ctx_t *ctx, u_char *data, size_t len, int last_buf)
      36             : {
      37             :     u_char                      *pos;
      38             : 
      39           0 :     pos = ngx_http_rds_json_request_mem(r, ctx, len);
      40           0 :     if (pos == NULL) {
      41           0 :         return NGX_ERROR;
      42             :     }
      43             : 
      44           0 :     ngx_memcpy(pos, data, len);
      45             : 
      46             :     dd("before output chain");
      47             : 
      48           0 :     if (last_buf) {
      49           0 :         ctx->seen_stream_end = 1;
      50             : 
      51           0 :         if (r != r->main) {
      52           0 :             last_buf = 0;
      53             :         }
      54             :     }
      55             : 
      56           0 :     return ngx_http_rds_json_submit_mem(r, ctx, len, (unsigned) last_buf);
      57             : }
      58             : 
      59             : 
      60             : ngx_int_t
      61           0 : ngx_http_rds_json_output_bufs(ngx_http_request_t *r,
      62             :     ngx_http_rds_json_ctx_t *ctx)
      63             : {
      64             :     ngx_int_t                rc;
      65             :     ngx_chain_t             *cl;
      66             : 
      67             :     dd("entered output chain");
      68             : 
      69           0 :     if (ctx->seen_stream_end) {
      70           0 :         ctx->seen_stream_end = 0;
      71             : 
      72           0 :         if (ctx->avail_out) {
      73           0 :             cl = ngx_alloc_chain_link(r->pool);
      74           0 :             if (cl == NULL) {
      75           0 :                 return NGX_ERROR;
      76             :             }
      77             : 
      78           0 :             cl->buf = ctx->out_buf;
      79           0 :             cl->next = NULL;
      80           0 :             *ctx->last_out = cl;
      81           0 :             ctx->last_out = &cl->next;
      82             : 
      83           0 :             ctx->avail_out = 0;
      84             :         }
      85             :     }
      86             : 
      87             :     dd_dump_chain_size();
      88             : 
      89             :     for ( ;; ) {
      90           0 :         if (ctx->out == NULL) {
      91             :             /* fprintf(stderr, "\n"); */
      92           0 :             return NGX_OK;
      93             :         }
      94             : 
      95             :         /* fprintf(stderr, "XXX Relooping..."); */
      96             : 
      97           0 :         rc = ngx_http_rds_json_next_body_filter(r, ctx->out);
      98             : 
      99           0 :         if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
     100           0 :             return rc;
     101             :         }
     102             : 
     103             : #if defined(nginx_version) && nginx_version >= 1001004
     104           0 :         ngx_chain_update_chains(r->pool, &ctx->free_bufs, &ctx->busy_bufs,
     105             :                                 &ctx->out, ctx->tag);
     106             : #else
     107             :         ngx_chain_update_chains(&ctx->free_bufs, &ctx->busy_bufs, &ctx->out,
     108             :                                 ctx->tag);
     109             : #endif
     110             : 
     111           0 :         ctx->last_out = &ctx->out;
     112             :     }
     113             : 
     114             :     /* impossible to reach here */
     115             : }
     116             : 
     117             : 
     118             : ngx_int_t
     119           0 : ngx_http_rds_json_output_header(ngx_http_request_t *r,
     120             :     ngx_http_rds_json_ctx_t *ctx, ngx_http_rds_header_t *header)
     121             : {
     122             :     u_char                  *pos, *last;
     123             :     size_t                   size;
     124             :     uintptr_t                escape;
     125           0 :     unsigned                 last_buf = 0;
     126             :     ngx_uint_t               i;
     127           0 :     ngx_str_t               *values = NULL;
     128           0 :     uintptr_t               *escapes = NULL;
     129             : 
     130           0 :     ngx_http_rds_json_property_t        *prop = NULL;
     131             :     ngx_http_rds_json_loc_conf_t        *conf;
     132             : 
     133           0 :     conf = ngx_http_get_module_loc_conf(r, ngx_http_rds_json_filter_module);
     134             : 
     135             :     /* calculate the buffer size */
     136             : 
     137           0 :     size = sizeof("{") - 1
     138           0 :            + conf->errcode_key.len
     139             :            + sizeof(":") - 1
     140           0 :            + ngx_get_num_size(header->std_errcode)
     141             :            + sizeof("}") - 1;
     142             : 
     143           0 :     if (conf->success.len) {
     144           0 :         size += conf->success.len + sizeof(":,") - 1;
     145           0 :         if (header->std_errcode == 0) {
     146           0 :             size += sizeof("true") - 1;
     147             : 
     148             :         } else {
     149           0 :             size += sizeof("false") - 1;
     150             :         }
     151             :     }
     152             : 
     153           0 :     if (conf->user_props) {
     154           0 :         values = ngx_pnalloc(r->pool,
     155           0 :                              conf->user_props->nelts * (sizeof(ngx_str_t)
     156             :                              + sizeof(uintptr_t)));
     157             : 
     158           0 :         if (values == NULL) {
     159           0 :             return NGX_ERROR;
     160             :         }
     161             : 
     162           0 :         escapes = (uintptr_t *) ((u_char *) values
     163           0 :                   + conf->user_props->nelts * sizeof(ngx_str_t));
     164             : 
     165           0 :         prop = conf->user_props->elts;
     166           0 :         for (i = 0; i < conf->user_props->nelts; i++) {
     167           0 :             if (ngx_http_complex_value(r, &prop[i].value, &values[i])
     168             :                 != NGX_OK)
     169             :             {
     170           0 :                 return NGX_ERROR;
     171             :             }
     172             : 
     173           0 :             escapes[i] = ngx_http_rds_json_escape_json_str(NULL,
     174           0 :                                                            values[i].data,
     175           0 :                                                            values[i].len);
     176             : 
     177           0 :             size += sizeof(":\"\",") - 1 + prop[i].key.len + values[i].len
     178           0 :                     + escapes[i];
     179             :         }
     180             :     }
     181             : 
     182             : 
     183           0 :     if (header->errstr.len) {
     184           0 :         escape = ngx_http_rds_json_escape_json_str(NULL, header->errstr.data,
     185             :                                                    header->errstr.len);
     186             : 
     187           0 :         size += sizeof(",") - 1
     188           0 :                 + conf->errstr_key.len
     189             :                 + sizeof(":") - 1
     190             :                 + sizeof("\"") - 1
     191           0 :                 + header->errstr.len
     192           0 :                 + escape
     193             :                 + sizeof("\"") - 1;
     194             : 
     195             :     } else {
     196           0 :         escape = (uintptr_t) 0;
     197             :     }
     198             : 
     199           0 :     if (header->insert_id) {
     200           0 :         size += sizeof(",\"insert_id\":") - 1
     201           0 :                 + ngx_get_num_size(header->insert_id);
     202             :     }
     203             : 
     204           0 :     if (header->affected_rows) {
     205           0 :         size += sizeof(",\"affected_rows\":") - 1
     206           0 :                 + ngx_get_num_size(header->affected_rows);
     207             :     }
     208             : 
     209             :     /* create the buffer */
     210             : 
     211           0 :     pos = ngx_http_rds_json_request_mem(r, ctx, size);
     212           0 :     if (pos == NULL) {
     213           0 :         return NGX_ERROR;
     214             :     }
     215             : 
     216           0 :     last = pos;
     217             : 
     218             :     /* fill up the buffer */
     219             : 
     220           0 :     *last++ = '{';
     221             : 
     222           0 :     if (conf->success.len) {
     223           0 :         last = ngx_copy(last, conf->success.data, conf->success.len);
     224             : 
     225           0 :         if (header->std_errcode == 0) {
     226           0 :             last = ngx_copy_literal(last, ":true,");
     227             : 
     228             :         } else {
     229           0 :             last = ngx_copy_literal(last, ":false,");
     230             :         }
     231             :     }
     232             : 
     233           0 :     if (conf->user_props) {
     234           0 :         for (i = 0; i < conf->user_props->nelts; i++) {
     235           0 :             last = ngx_copy(last, prop[i].key.data, prop[i].key.len);
     236           0 :             *last++ = ':';
     237           0 :             *last++ = '"';
     238             : 
     239           0 :             if (escapes[i] == 0) {
     240           0 :                 last = ngx_copy(last, values[i].data, values[i].len);
     241             : 
     242             :             } else {
     243           0 :                 last = (u_char *)
     244           0 :                         ngx_http_rds_json_escape_json_str(last,
     245           0 :                                                           values[i].data,
     246           0 :                                                           values[i].len);
     247             :             }
     248             : 
     249           0 :             *last++ = '"';
     250           0 :             *last++ = ',';
     251             :         }
     252             :     }
     253             : 
     254           0 :     last = ngx_copy(last, conf->errcode_key.data, conf->errcode_key.len);
     255           0 :     *last++ = ':';
     256             : 
     257           0 :     last = ngx_snprintf(last, NGX_UINT16_LEN, "%uD",
     258           0 :                         (uint32_t) header->std_errcode);
     259             : 
     260           0 :     if (header->errstr.len) {
     261           0 :         *last++ = ',';
     262           0 :         last = ngx_copy(last, conf->errstr_key.data, conf->errstr_key.len);
     263           0 :         *last++ = ':';
     264           0 :         *last++ = '"';
     265             : 
     266           0 :         if (escape == 0) {
     267           0 :             last = ngx_copy(last, header->errstr.data, header->errstr.len);
     268             : 
     269             :         } else {
     270           0 :             last = (u_char *)
     271           0 :                     ngx_http_rds_json_escape_json_str(last,
     272             :                                                       header->errstr.data,
     273             :                                                       header->errstr.len);
     274             :         }
     275             : 
     276           0 :         *last++ = '"';
     277             :     }
     278             : 
     279           0 :     if (header->insert_id) {
     280           0 :         last = ngx_copy_literal(last, ",\"insert_id\":");
     281           0 :         last = ngx_snprintf(last, NGX_UINT64_LEN, "%uL", header->insert_id);
     282             :     }
     283             : 
     284           0 :     if (header->affected_rows) {
     285           0 :         last = ngx_copy_literal(last, ",\"affected_rows\":");
     286           0 :         last = ngx_snprintf(last, NGX_UINT64_LEN, "%uL",
     287             :                             header->affected_rows);
     288             :     }
     289             : 
     290           0 :     *last++ = '}';
     291             : 
     292           0 :     if ((size_t) (last - pos) != size) {
     293           0 :         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     294             :                       "rds_json: output header buffer error: %O != %uz",
     295             :                       (off_t) (last - pos), size);
     296             : 
     297           0 :         return NGX_ERROR;
     298             :     }
     299             : 
     300           0 :     if (r == r->main) {
     301           0 :         last_buf = 1;
     302             :     }
     303             : 
     304           0 :     ctx->seen_stream_end = 1;
     305             : 
     306           0 :     return ngx_http_rds_json_submit_mem(r, ctx, size, (unsigned) last_buf);
     307             : }
     308             : 
     309             : 
     310             : ngx_int_t
     311           0 : ngx_http_rds_json_output_props(ngx_http_request_t *r,
     312             :     ngx_http_rds_json_ctx_t *ctx, ngx_http_rds_json_loc_conf_t *conf)
     313             : {
     314             :     size_t                   size;
     315             :     u_char                  *pos, *last;
     316             :     ngx_uint_t               i;
     317           0 :     ngx_str_t               *values = NULL;
     318           0 :     uintptr_t               *escapes = NULL;
     319             : 
     320           0 :     ngx_http_rds_json_property_t        *prop = NULL;
     321             : 
     322           0 :     size = sizeof("{:") - 1 + conf->root.len;
     323             : 
     324           0 :     if (conf->success.len) {
     325           0 :         size += sizeof(",:true") - 1 + conf->success.len;
     326             :     }
     327             : 
     328           0 :     if (conf->user_props) {
     329           0 :         values = ngx_pnalloc(r->pool,
     330           0 :                              conf->user_props->nelts * (sizeof(ngx_str_t)
     331             :                              + sizeof(uintptr_t)));
     332             : 
     333           0 :         if (values == NULL) {
     334           0 :             return NGX_ERROR;
     335             :         }
     336             : 
     337           0 :         escapes = (uintptr_t *) ((u_char *) values
     338           0 :                   + conf->user_props->nelts * sizeof(ngx_str_t));
     339             : 
     340           0 :         prop = conf->user_props->elts;
     341           0 :         for (i = 0; i < conf->user_props->nelts; i++) {
     342           0 :             if (ngx_http_complex_value(r, &prop[i].value, &values[i])
     343             :                 != NGX_OK)
     344             :             {
     345           0 :                 return NGX_ERROR;
     346             :             }
     347             : 
     348           0 :             escapes[i] = ngx_http_rds_json_escape_json_str(NULL,
     349           0 :                                                            values[i].data,
     350           0 :                                                            values[i].len);
     351             : 
     352           0 :             size += sizeof(":\"\",") - 1 + prop[i].key.len + values[i].len
     353           0 :                     + escapes[i];
     354             :         }
     355             :     }
     356             : 
     357           0 :     pos = ngx_http_rds_json_request_mem(r, ctx, size);
     358           0 :     if (pos == NULL) {
     359           0 :         return NGX_ERROR;
     360             :     }
     361             : 
     362           0 :     last = pos;
     363             : 
     364           0 :     *last++ = '{';
     365             : 
     366           0 :     if (conf->success.len) {
     367           0 :         last = ngx_copy(last, conf->success.data, conf->success.len);
     368           0 :         last = ngx_copy_literal(last, ":true,");
     369             :     }
     370             : 
     371           0 :     if (conf->user_props) {
     372           0 :         for (i = 0; i < conf->user_props->nelts; i++) {
     373           0 :             last = ngx_copy(last, prop[i].key.data, prop[i].key.len);
     374           0 :             *last++ = ':';
     375           0 :             *last++ = '"';
     376             : 
     377           0 :             if (escapes[i] == 0) {
     378           0 :                 last = ngx_copy(last, values[i].data, values[i].len);
     379             : 
     380             :             } else {
     381           0 :                 last = (u_char *)
     382           0 :                         ngx_http_rds_json_escape_json_str(last,
     383           0 :                                                           values[i].data,
     384           0 :                                                           values[i].len);
     385             :             }
     386             : 
     387           0 :             *last++ = '"';
     388           0 :             *last++ = ',';
     389             :         }
     390             :     }
     391             : 
     392           0 :     last = ngx_copy(last, conf->root.data, conf->root.len);
     393           0 :     *last++ = ':';
     394             : 
     395           0 :     if (last - pos != (ssize_t) size) {
     396           0 :         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     397             :                       "rds_json: output props begin: buffer error: %O != %uz",
     398             :                       (off_t) (last - pos), size);
     399             : 
     400           0 :         return NGX_ERROR;
     401             :     }
     402             : 
     403           0 :     return ngx_http_rds_json_submit_mem(r, ctx, size, 0);
     404             : }
     405             : 
     406             : 
     407             : ngx_int_t
     408           0 : ngx_http_rds_json_output_cols(ngx_http_request_t *r,
     409             :     ngx_http_rds_json_ctx_t *ctx)
     410             : {
     411             :     ngx_uint_t                           i;
     412             :     ngx_http_rds_column_t               *col;
     413             :     size_t                               size;
     414             :     u_char                              *pos, *last;
     415           0 :     uintptr_t                            key_escape = 0;
     416             : 
     417           0 :     size = sizeof("[]") - 1;
     418             : 
     419           0 :     for (i = 0; i < ctx->col_count; i++) {
     420           0 :         col = &ctx->cols[i];
     421           0 :         key_escape = ngx_http_rds_json_escape_json_str(NULL, col->name.data,
     422             :                                                        col->name.len);
     423             : 
     424             :         dd("key_escape: %d", (int) key_escape);
     425             : 
     426           0 :         size += col->name.len + key_escape + sizeof("\"\"") - 1;
     427             : 
     428           0 :         if (i != ctx->col_count - 1) {
     429             :             /* not the last column */
     430           0 :             size += sizeof(",") - 1;
     431             :         }
     432             :     }
     433             : 
     434           0 :     ctx->generated_col_names = 1;
     435             : 
     436           0 :     pos = ngx_http_rds_json_request_mem(r, ctx, size);
     437           0 :     if (pos == NULL) {
     438           0 :         return NGX_ERROR;
     439             :     }
     440             : 
     441           0 :     last = pos;
     442             : 
     443           0 :     *last++ = '[';
     444             : 
     445           0 :     for (i = 0; i < ctx->col_count; i++) {
     446           0 :         col = &ctx->cols[i];
     447             : 
     448           0 :         *last++ = '"';
     449             : 
     450           0 :         last = (u_char *) ngx_http_rds_json_escape_json_str(last,
     451             :                                                             col->name.data,
     452             :                                                             col->name.len);
     453             : 
     454           0 :         *last++ = '"';
     455             : 
     456           0 :         if (i != ctx->col_count - 1) {
     457           0 :             *last++ = ',';
     458             :         }
     459             :     }
     460             : 
     461           0 :     *last++ = ']';
     462             : 
     463           0 :     return ngx_http_rds_json_submit_mem(r, ctx, size, 0);
     464             : }
     465             : 
     466             : 
     467             : ngx_int_t
     468           0 : ngx_http_rds_json_output_field(ngx_http_request_t *r,
     469             :     ngx_http_rds_json_ctx_t *ctx, u_char *data, size_t len, int is_null)
     470             : {
     471             :     u_char                              *pos, *last;
     472             :     ngx_http_rds_column_t               *col;
     473           0 :     ngx_flag_t                           bool_val = 0;
     474             :     size_t                               size;
     475           0 :     uintptr_t                            key_escape = 0;
     476           0 :     uintptr_t                            val_escape = 0;
     477             :     u_char                              *p;
     478             :     ngx_uint_t                           i;
     479             :     ngx_http_rds_json_loc_conf_t        *conf;
     480             :     ngx_uint_t                           format;
     481             : 
     482           0 :     conf = ngx_http_get_module_loc_conf(r, ngx_http_rds_json_filter_module);
     483             : 
     484           0 :     format = conf->format;
     485             : 
     486             :     dd("reading row %llu, col %d, len %d",
     487             :        (unsigned long long) ctx->row,
     488             :        (int) ctx->cur_col, (int) len);
     489             : 
     490             :     /* calculate the buffer size */
     491             : 
     492           0 :     if (format == json_format_normal) {
     493           0 :         if (ctx->cur_col == 0) {
     494             :             dd("first column");
     495           0 :             if (ctx->row == 1) {
     496             :                 dd("first column, first row");
     497           0 :                 size = sizeof("{\"") - 1;
     498             : 
     499             :             } else {
     500           0 :                 size = sizeof(",{\"") - 1;
     501             :             }
     502             : 
     503             :         } else {
     504           0 :             size = sizeof(",\"") - 1;
     505             :         }
     506             : 
     507           0 :     } else if (format == json_format_compact) {
     508           0 :         if (ctx->cur_col == 0) {
     509             :             dd("first column");
     510           0 :             size = sizeof(",[") - 1;
     511             : 
     512             :         } else {
     513           0 :             size = sizeof(",") - 1;
     514             :         }
     515             : 
     516             :     } else {
     517           0 :         return NGX_ERROR;
     518             :     }
     519             : 
     520           0 :     col = &ctx->cols[ctx->cur_col];
     521             : 
     522           0 :     if (format == json_format_normal) {
     523           0 :         key_escape = ngx_http_rds_json_escape_json_str(NULL, col->name.data,
     524             :                                                        col->name.len);
     525             : 
     526             :         dd("key_escape: %d", (int) key_escape);
     527             : 
     528           0 :         size += col->name.len + key_escape + sizeof("\":") - 1;
     529             : 
     530           0 :     } else if (format == json_format_compact) {
     531             :         /* do nothing */
     532             : 
     533             :     } else {
     534           0 :         return NGX_ERROR;
     535             :     }
     536             : 
     537           0 :     if (len == 0 && ctx->field_data_rest > 0) {
     538           0 :         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     539             :                       "rds_json: at least one octet should go with the "
     540             :                       "field size in one buf");
     541             : 
     542           0 :         return NGX_ERROR;
     543             :     }
     544             : 
     545           0 :     if (is_null) {
     546           0 :         size += sizeof("null") - 1;
     547             : 
     548           0 :     } else if (len == 0) {
     549             :         dd("empty string value found");
     550             : 
     551           0 :         size += sizeof("\"\"") - 1;
     552             : 
     553             :     } else {
     554           0 :         switch (col->std_type & 0xc000) {
     555           0 :         case rds_rough_col_type_float:
     556             :             dd("float field found");
     557             :             /* TODO check validity of floating numbers */
     558           0 :             size += len;
     559           0 :             break;
     560             : 
     561           0 :         case rds_rough_col_type_int:
     562             :             dd("int field found");
     563             : 
     564           0 :             for (p = data, i = 0; i < len; i++, p++) {
     565           0 :                 if (i == 0 && *p == '-') {
     566           0 :                     continue;
     567             :                 }
     568             : 
     569           0 :                 if (*p < '0' || *p > '9') {
     570           0 :                     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     571             :                                   "rds_json: invalid integral field value: "
     572             :                                   "\"%*s\"", len, data);
     573           0 :                     return NGX_ERROR;
     574             :                 }
     575             :             }
     576             : 
     577           0 :             size += len;
     578           0 :             break;
     579             : 
     580           0 :         case rds_rough_col_type_bool:
     581             :             dd("bool field found");
     582             : 
     583           0 :             if (*data == '0' || *data == 'f' || *data == 'F'
     584           0 :                 || *data == '1' || *data == 't' || *data == 'T' )
     585             :             {
     586           0 :                 if (len != 1 || ctx->field_data_rest) {
     587           0 :                     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     588             :                                   "rds_json: invalid boolean field value "
     589             :                                   "leading by \"%*s\"", len, data);
     590           0 :                     return NGX_ERROR;
     591             :                 }
     592             : 
     593           0 :                 if (*data == '0' || *data == 'f' || *data == 'F') {
     594           0 :                     bool_val = 0;
     595           0 :                     size += sizeof("false") - 1;
     596             : 
     597             :                 } else {
     598           0 :                     bool_val = 1;
     599           0 :                     size += sizeof("true") - 1;
     600             :                 }
     601             : 
     602             :             } else {
     603           0 :                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     604             :                               "rds_json: output field: invalid boolean value "
     605             :                               "leading by \"%*s\"", len, data);
     606           0 :                 return NGX_ERROR;
     607             :             }
     608             : 
     609           0 :             break;
     610             : 
     611           0 :         default:
     612             :             dd("string field found");
     613             : 
     614             :             /* TODO: further inspect array types and key-value types */
     615             : 
     616           0 :             val_escape = ngx_http_rds_json_escape_json_str(NULL, data, len);
     617             : 
     618           0 :             size += sizeof("\"") - 1 + len + val_escape;
     619             : 
     620           0 :             if (ctx->field_data_rest == 0) {
     621           0 :                 size += sizeof("\"") - 1;
     622             :             }
     623             :         }
     624             :     }
     625             : 
     626           0 :     if (ctx->field_data_rest == 0
     627           0 :         && ctx->cur_col == ctx->col_count - 1)
     628             :     {
     629             :         /* last column in the row */
     630           0 :         if (format == json_format_normal) {
     631           0 :             size += sizeof("}") - 1;
     632             : 
     633           0 :         } else if (format == json_format_compact) {
     634           0 :             size += sizeof("]") - 1;
     635             : 
     636             :         } else {
     637           0 :             return NGX_ERROR;
     638             :         }
     639             :     }
     640             : 
     641             :     /* allocate the buffer */
     642             : 
     643           0 :     pos = ngx_http_rds_json_request_mem(r, ctx, size);
     644           0 :     if (pos == NULL) {
     645           0 :         return NGX_ERROR;
     646             :     }
     647             : 
     648           0 :     last = pos;
     649             : 
     650             :     /* fill up the buffer */
     651             : 
     652           0 :     if (format == json_format_normal) {
     653           0 :         if (ctx->cur_col == 0) {
     654             :             dd("first column");
     655           0 :             if (ctx->row == 1) {
     656             :                 dd("first column, first row");
     657           0 :                 *last++ = '{';
     658             : 
     659             :             } else {
     660           0 :                 *last++ = ','; *last++ = '{';
     661             :             }
     662             : 
     663             :         } else {
     664           0 :             *last++ = ',';
     665             :         }
     666             : 
     667           0 :         *last++ = '"';
     668             : 
     669           0 :         if (key_escape == 0) {
     670           0 :             last = ngx_copy(last, col->name.data, col->name.len);
     671             : 
     672             :         } else {
     673           0 :             last = (u_char *)
     674           0 :                     ngx_http_rds_json_escape_json_str(last, col->name.data,
     675             :                                                       col->name.len);
     676             :         }
     677             : 
     678           0 :         *last++ = '"'; *last++ = ':';
     679             : 
     680           0 :     } else if (format == json_format_compact) {
     681             : 
     682           0 :         if (ctx->cur_col == 0) {
     683             :             dd("first column");
     684           0 :             *last++ = ',';
     685           0 :             *last++ = '[';
     686             : 
     687             :         } else {
     688           0 :             *last++ = ',';
     689             :         }
     690             : 
     691             :     } else {
     692           0 :         return NGX_ERROR;
     693             :     }
     694             : 
     695           0 :     if (is_null) {
     696             :         dd("copy null value over");
     697           0 :         last = ngx_copy_literal(last, "null");
     698             : 
     699           0 :     } else if (len == 0) {
     700             :         dd("copy emtpy string over");
     701           0 :         last = ngx_copy_literal(last, "\"\"");
     702             : 
     703             :     } else {
     704           0 :         switch (col->std_type & 0xc000) {
     705           0 :         case rds_rough_col_type_int:
     706             :         case rds_rough_col_type_float:
     707           0 :             last = ngx_copy(last, data, len);
     708             : 
     709             :             dd("copy over int/float value: %.*s", (int) len, data);
     710             : 
     711           0 :             break;
     712             : 
     713           0 :         case rds_rough_col_type_bool:
     714           0 :             if (bool_val) {
     715           0 :                 last = ngx_copy_literal(last, "true");
     716             : 
     717             :             } else {
     718           0 :                 last = ngx_copy_literal(last, "false");
     719             :             }
     720             : 
     721           0 :             break;
     722             : 
     723           0 :         default:
     724             :             /* string */
     725           0 :             *last++ = '"';
     726             : 
     727           0 :             if (val_escape == 0) {
     728           0 :                 last = ngx_copy(last, data, len);
     729             : 
     730             :             } else {
     731             :                 dd("field: string value escape non-zero: %d",
     732             :                    (int) val_escape);
     733             : 
     734             : #if DDEBUG
     735             :                 p = last;
     736             : #endif
     737             : 
     738           0 :                 last = (u_char *)
     739           0 :                         ngx_http_rds_json_escape_json_str(last, data, len);
     740             : 
     741             : #if DDEBUG
     742             :                 dd("escaped value \"%.*s\" (len %d, escape %d, escape2 %d)",
     743             :                    (int) (len + val_escape),
     744             :                    p, (int) (len + val_escape),
     745             :                    (int) val_escape,
     746             :                    (int) ((last - p) - len));
     747             : #endif
     748             :             }
     749             : 
     750           0 :             if (ctx->field_data_rest == 0) {
     751           0 :                 *last++ = '"';
     752             :             }
     753             : 
     754           0 :             break;
     755             :         }
     756             :     }
     757             : 
     758           0 :     if (ctx->field_data_rest == 0 && ctx->cur_col == ctx->col_count - 1) {
     759             :         dd("last column in the row");
     760           0 :         if (format == json_format_normal) {
     761           0 :             *last++ = '}';
     762             : 
     763           0 :         } else if (format == json_format_compact) {
     764           0 :             *last++ = ']';
     765             : 
     766             :         } else {
     767           0 :             return NGX_ERROR;
     768             :         }
     769             :     }
     770             : 
     771           0 :     if ((size_t) (last - pos) != size) {
     772           0 :         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     773             :                       "rds_json: output field: buffer error (%d left)",
     774             :                       (int) size - (last - pos));
     775             : 
     776           0 :         return NGX_ERROR;
     777             :     }
     778             : 
     779           0 :     return ngx_http_rds_json_submit_mem(r, ctx, size, 0);
     780             : }
     781             : 
     782             : 
     783             : ngx_int_t
     784           0 : ngx_http_rds_json_output_more_field_data(ngx_http_request_t *r,
     785             :     ngx_http_rds_json_ctx_t *ctx, u_char *data, size_t len)
     786             : {
     787             :     u_char                          *pos, *last;
     788           0 :     size_t                           size = 0;
     789             :     ngx_http_rds_column_t           *col;
     790           0 :     uintptr_t                        escape = 0;
     791             :     u_char                          *p;
     792             :     ngx_uint_t                       i;
     793             :     ngx_http_rds_json_loc_conf_t    *conf;
     794             : 
     795           0 :     conf = ngx_http_get_module_loc_conf(r, ngx_http_rds_json_filter_module);
     796             : 
     797             :     /* calculate the buffer size */
     798             : 
     799           0 :     col = &ctx->cols[ctx->cur_col];
     800             : 
     801           0 :     switch (col->std_type & 0xc000) {
     802           0 :     case rds_rough_col_type_int:
     803           0 :         for (p = data, i = 0; i < len; i++, p++) {
     804           0 :             if (*p < '0' || *p > '9') {
     805           0 :                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     806             :                               "rds_json: invalid integral field value: \"%*s\"",
     807             :                               len, data);
     808           0 :                 return NGX_ERROR;
     809             :             }
     810             :         }
     811             : 
     812           0 :         size += len;
     813           0 :         break;
     814             : 
     815           0 :     case rds_rough_col_type_float:
     816             :         /* TODO: check validity of floating-point field values */
     817           0 :         size += len;
     818           0 :         break;
     819             : 
     820           0 :     case rds_rough_col_type_bool:
     821           0 :         break;
     822             : 
     823           0 :     default:
     824             :         /* string */
     825           0 :         escape = ngx_http_rds_json_escape_json_str(NULL, data, len);
     826           0 :         size = len + escape;
     827             : 
     828           0 :         if (ctx->field_data_rest == 0) {
     829           0 :             size += sizeof("\"") - 1;
     830             :         }
     831             : 
     832           0 :         break;
     833             :     }
     834             : 
     835           0 :     if (ctx->field_data_rest == 0 && ctx->cur_col == ctx->col_count - 1) {
     836             :         /* last column in the row */
     837           0 :         if (conf->format == json_format_normal) {
     838           0 :             size += sizeof("}") - 1;
     839             : 
     840           0 :         } else if (conf->format == json_format_compact) {
     841           0 :             size += sizeof("]") - 1;
     842             : 
     843             :         } else {
     844           0 :             return NGX_ERROR;
     845             :         }
     846             :     }
     847             : 
     848             :     /* allocate the buffer */
     849             : 
     850           0 :     pos = ngx_http_rds_json_request_mem(r, ctx, size);
     851           0 :     if (pos == NULL) {
     852           0 :         return NGX_ERROR;
     853             :     }
     854             : 
     855           0 :     last = pos;
     856             : 
     857             :     /* fill up the buffer */
     858             : 
     859           0 :     switch (col->std_type & 0xc000) {
     860           0 :     case rds_rough_col_type_int:
     861             :     case rds_rough_col_type_float:
     862           0 :         last = ngx_copy(last, data, len);
     863           0 :         break;
     864             : 
     865           0 :     case rds_rough_col_type_bool:
     866             :         /* no op */
     867           0 :         break;
     868             : 
     869           0 :     default:
     870             :         /* string */
     871           0 :         if (escape == 0) {
     872           0 :             last = ngx_copy(last, data, len);
     873             : 
     874             :         } else {
     875             :             dd("more field data: string value escape non-zero: %d",
     876             :                 (int) escape);
     877             : 
     878             : #if DDEBUG
     879             :                 p = last;
     880             : #endif
     881             : 
     882           0 :             last = (u_char *) ngx_http_rds_json_escape_json_str(last,
     883             :                     data, len);
     884             : 
     885             : #if DDEBUG
     886             :             dd("escaped value \"%.*s\" (len %d, escape %d, escape2 %d)",
     887             :                (int) (len + escape),
     888             :                p, (int) (len + escape),
     889             :                (int) escape,
     890             :                (int) ((last - p) - len));
     891             : #endif
     892             :         }
     893             : 
     894           0 :         if (ctx->field_data_rest == 0) {
     895           0 :             *last++ = '"';
     896             :         }
     897             : 
     898           0 :         break;
     899             :     } /* switch */
     900             : 
     901           0 :     if (ctx->field_data_rest == 0 && ctx->cur_col == ctx->col_count - 1) {
     902             :         /* last column in the row */
     903           0 :         if (conf->format == json_format_normal) {
     904           0 :             *last++ = '}';
     905             : 
     906           0 :         } else if (conf->format == json_format_compact) {
     907           0 :             *last++ = ']';
     908             : 
     909             :         } else {
     910           0 :             return NGX_ERROR;
     911             :         }
     912             :     }
     913             : 
     914           0 :     if ((size_t) (last - pos) != size) {
     915           0 :         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     916             :                       "rds_json: output more field data: buffer error "
     917             :                       "(%d left)", (int) (size - (last - pos)));
     918           0 :         return NGX_ERROR;
     919             :     }
     920             : 
     921           0 :     return ngx_http_rds_json_submit_mem(r, ctx, size, 0);
     922             : }
     923             : 
     924             : 
     925             : static u_char *
     926           0 : ngx_http_rds_json_request_mem(ngx_http_request_t *r,
     927             :     ngx_http_rds_json_ctx_t *ctx, size_t len)
     928             : {
     929             :     ngx_int_t                rc;
     930             :     u_char                  *p;
     931             : 
     932           0 :     rc = ngx_http_rds_json_get_buf(r, ctx);
     933             : 
     934           0 :     if (rc != NGX_OK) {
     935           0 :         return NULL;
     936             :     }
     937             : 
     938           0 :     if (ctx->avail_out < len) {
     939           0 :         p = ngx_http_rds_json_get_postponed(r, ctx, len);
     940           0 :         if (p == NULL) {
     941           0 :             return NULL;
     942             :         }
     943             : 
     944           0 :         ctx->postponed.pos = p;
     945           0 :         ctx->postponed.last = p + len;
     946             : 
     947           0 :         return p;
     948             :     }
     949             : 
     950           0 :     return ctx->out_buf->last;
     951             : }
     952             : 
     953             : 
     954             : static ngx_int_t
     955           0 : ngx_http_rds_json_get_buf(ngx_http_request_t *r,
     956             :     ngx_http_rds_json_ctx_t *ctx)
     957             : {
     958             :     ngx_http_rds_json_loc_conf_t         *conf;
     959             : 
     960             :     dd("MEM enter");
     961             : 
     962           0 :     if (ctx->avail_out) {
     963           0 :         return NGX_OK;
     964             :     }
     965             : 
     966           0 :     conf = ngx_http_get_module_loc_conf(r, ngx_http_rds_json_filter_module);
     967             : 
     968           0 :     if (ctx->free_bufs) {
     969             :         dd("MEM reusing temp buf from free_bufs");
     970             : 
     971           0 :         ctx->out_buf = ctx->free_bufs->buf;
     972           0 :         ctx->free_bufs = ctx->free_bufs->next;
     973             : 
     974             :     } else {
     975             :         dd("MEM creating temp buf with size: %d", (int) conf->buf_size);
     976           0 :         ctx->out_buf = ngx_create_temp_buf(r->pool, conf->buf_size);
     977             : 
     978           0 :         if (ctx->out_buf == NULL) {
     979           0 :             return NGX_ERROR;
     980             :         }
     981             : 
     982           0 :         ctx->out_buf->tag = (ngx_buf_tag_t) &ngx_http_rds_json_filter_module;
     983           0 :         ctx->out_buf->recycled = 1;
     984             :     }
     985             : 
     986           0 :     ctx->avail_out = conf->buf_size;
     987             : 
     988           0 :     return NGX_OK;
     989             : }
     990             : 
     991             : 
     992             : static u_char *
     993           0 : ngx_http_rds_json_get_postponed(ngx_http_request_t *r,
     994             :     ngx_http_rds_json_ctx_t *ctx, size_t len)
     995             : {
     996             :     u_char          *p;
     997             : 
     998             :     dd("MEM enter");
     999             : 
    1000           0 :     if (ctx->cached.start == NULL) {
    1001           0 :         goto alloc;
    1002             :     }
    1003             : 
    1004           0 :     if ((size_t) (ctx->cached.end - ctx->cached.start) < len) {
    1005           0 :         ngx_pfree(r->pool, ctx->cached.start);
    1006           0 :         goto alloc;
    1007             :     }
    1008             : 
    1009           0 :     return ctx->cached.start;
    1010             : 
    1011           0 : alloc:
    1012             : 
    1013           0 :     p = ngx_palloc(r->pool, len);
    1014           0 :     if (p == NULL) {
    1015           0 :         return NULL;
    1016             :     }
    1017             : 
    1018           0 :     ctx->cached.start = p;
    1019           0 :     ctx->cached.end = p + len;
    1020             : 
    1021           0 :     return p;
    1022             : }
    1023             : 
    1024             : 
    1025             : static ngx_int_t
    1026           0 : ngx_http_rds_json_submit_mem(ngx_http_request_t *r,
    1027             :     ngx_http_rds_json_ctx_t *ctx, size_t len, unsigned last_buf)
    1028             : {
    1029             :     ngx_chain_t             *cl;
    1030             :     ngx_int_t                rc;
    1031             : 
    1032           0 :     if (ctx->postponed.pos != NULL) {
    1033             :         dd("MEM copy postponed data over to ctx->out for len %d", (int) len);
    1034             : 
    1035             :         for ( ;; ) {
    1036           0 :             len = ctx->postponed.last - ctx->postponed.pos;
    1037           0 :             if (len > ctx->avail_out) {
    1038           0 :                 len = ctx->avail_out;
    1039             :             }
    1040             : 
    1041           0 :             ctx->out_buf->last = ngx_copy(ctx->out_buf->last,
    1042             :                                           ctx->postponed.pos, len);
    1043             : 
    1044           0 :             ctx->avail_out -= len;
    1045             : 
    1046           0 :             ctx->postponed.pos += len;
    1047             : 
    1048           0 :             if (ctx->postponed.pos == ctx->postponed.last) {
    1049           0 :                 ctx->postponed.pos = NULL;
    1050             :             }
    1051             : 
    1052           0 :             if (ctx->avail_out > 0) {
    1053           0 :                 break;
    1054             :             }
    1055             : 
    1056             :             dd("MEM save ctx->out_buf");
    1057             : 
    1058           0 :             cl = ngx_alloc_chain_link(r->pool);
    1059           0 :             if (cl == NULL) {
    1060           0 :                 return NGX_ERROR;
    1061             :             }
    1062             : 
    1063           0 :             cl->buf = ctx->out_buf;
    1064           0 :             cl->next = NULL;
    1065           0 :             *ctx->last_out = cl;
    1066           0 :             ctx->last_out = &cl->next;
    1067             : 
    1068           0 :             if (ctx->postponed.pos == NULL) {
    1069           0 :                 ctx->out_buf->last_buf = last_buf;
    1070           0 :                 break;
    1071             :             }
    1072             : 
    1073           0 :             rc = ngx_http_rds_json_get_buf(r, ctx);
    1074           0 :             if (rc != NGX_OK) {
    1075           0 :                 return NGX_ERROR;
    1076             :             }
    1077             :         }
    1078             : 
    1079           0 :         return NGX_OK;
    1080             :     }
    1081             : 
    1082             :     dd("MEM consuming out_buf for %d", (int) len);
    1083             : 
    1084           0 :     ctx->out_buf->last += len;
    1085           0 :     ctx->avail_out -= len;
    1086           0 :     ctx->out_buf->last_buf = last_buf;
    1087             : 
    1088           0 :     if (ctx->avail_out == 0) {
    1089             :         dd("MEM save ctx->out_buf");
    1090             : 
    1091           0 :         cl = ngx_alloc_chain_link(r->pool);
    1092           0 :         if (cl == NULL) {
    1093           0 :             return NGX_ERROR;
    1094             :         }
    1095             : 
    1096           0 :         cl->buf = ctx->out_buf;
    1097           0 :         cl->next = NULL;
    1098           0 :         *ctx->last_out = cl;
    1099           0 :         ctx->last_out = &cl->next;
    1100             :     }
    1101             : 
    1102           0 :     return NGX_OK;
    1103             : }
    1104             : 
    1105             : 
    1106             : static size_t
    1107           0 : ngx_get_num_size(uint64_t i)
    1108             : {
    1109           0 :     size_t          n = 0;
    1110             : 
    1111             :     do {
    1112           0 :         i = i / 10;
    1113           0 :         n++;
    1114           0 :     } while (i > 0);
    1115             : 
    1116           0 :     return n;
    1117             : }

Generated by: LCOV version 1.13