LCOV - code coverage report
Current view: top level - home/build/openresty-1.13.6.1/build/echo-nginx-module-0.61/src - ngx_http_echo_echo.c (source / functions) Hit Total Coverage
Test: coverage ngix Lines: 56 148 37.8 %
Date: 2020-03-03 04:25:50 Functions: 2 6 33.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #ifndef DDEBUG
       2             : #define DDEBUG 0
       3             : #endif
       4             : #include "ddebug.h"
       5             : 
       6             : #include "ngx_http_echo_echo.h"
       7             : #include "ngx_http_echo_util.h"
       8             : #include "ngx_http_echo_filter.h"
       9             : 
      10             : #include <nginx.h>
      11             : 
      12             : static ngx_buf_t ngx_http_echo_space_buf;
      13             : 
      14             : static ngx_buf_t ngx_http_echo_newline_buf;
      15             : 
      16             : 
      17             : ngx_int_t
      18          18 : ngx_http_echo_echo_init(ngx_conf_t *cf)
      19             : {
      20             :     static u_char space_str[]   = " ";
      21             :     static u_char newline_str[] = "\n";
      22             : 
      23             :     dd("global init...");
      24             : 
      25          18 :     ngx_memzero(&ngx_http_echo_space_buf, sizeof(ngx_buf_t));
      26             : 
      27          18 :     ngx_http_echo_space_buf.memory = 1;
      28             : 
      29          18 :     ngx_http_echo_space_buf.start =
      30          18 :         ngx_http_echo_space_buf.pos =
      31             :             space_str;
      32             : 
      33          18 :     ngx_http_echo_space_buf.end =
      34          18 :         ngx_http_echo_space_buf.last =
      35          18 :             space_str + sizeof(space_str) - 1;
      36             : 
      37          18 :     ngx_memzero(&ngx_http_echo_newline_buf, sizeof(ngx_buf_t));
      38             : 
      39          18 :     ngx_http_echo_newline_buf.memory = 1;
      40             : 
      41          18 :     ngx_http_echo_newline_buf.start =
      42          18 :         ngx_http_echo_newline_buf.pos =
      43             :             newline_str;
      44             : 
      45          18 :     ngx_http_echo_newline_buf.end =
      46          18 :         ngx_http_echo_newline_buf.last =
      47          18 :             newline_str + sizeof(newline_str) - 1;
      48             : 
      49          18 :     return NGX_OK;
      50             : }
      51             : 
      52             : 
      53             : ngx_int_t
      54           0 : ngx_http_echo_exec_echo_sync(ngx_http_request_t *r,
      55             :     ngx_http_echo_ctx_t *ctx)
      56             : {
      57             :     ngx_buf_t                   *buf;
      58           0 :     ngx_chain_t                 *cl = NULL; /* the head of the chain link */
      59             : 
      60           0 :     buf = ngx_calloc_buf(r->pool);
      61           0 :     if (buf == NULL) {
      62           0 :         return NGX_HTTP_INTERNAL_SERVER_ERROR;
      63             :     }
      64             : 
      65           0 :     buf->sync = 1;
      66             : 
      67           0 :     cl = ngx_alloc_chain_link(r->pool);
      68           0 :     if (cl == NULL) {
      69           0 :         return NGX_HTTP_INTERNAL_SERVER_ERROR;
      70             :     }
      71             : 
      72           0 :     cl->buf  = buf;
      73           0 :     cl->next = NULL;
      74             : 
      75           0 :     return ngx_http_echo_send_chain_link(r, ctx, cl);
      76             : }
      77             : 
      78             : 
      79             : ngx_int_t
      80          33 : ngx_http_echo_exec_echo(ngx_http_request_t *r,
      81             :     ngx_http_echo_ctx_t *ctx, ngx_array_t *computed_args,
      82             :     ngx_flag_t in_filter, ngx_array_t *opts)
      83             : {
      84             :     ngx_uint_t                  i;
      85             : 
      86             :     ngx_buf_t                   *space_buf;
      87             :     ngx_buf_t                   *newline_buf;
      88             :     ngx_buf_t                   *buf;
      89             : 
      90             :     ngx_str_t                   *computed_arg;
      91             :     ngx_str_t                   *computed_arg_elts;
      92             :     ngx_str_t                   *opt;
      93             : 
      94          33 :     ngx_chain_t *cl  = NULL; /* the head of the chain link */
      95          33 :     ngx_chain_t **ll = &cl;  /* always point to the address of the last link */
      96             : 
      97             :     dd_enter();
      98             : 
      99          33 :     if (computed_args == NULL) {
     100           0 :         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     101             :     }
     102             : 
     103          33 :     computed_arg_elts = computed_args->elts;
     104          66 :     for (i = 0; i < computed_args->nelts; i++) {
     105          33 :         computed_arg = &computed_arg_elts[i];
     106             : 
     107          33 :         if (computed_arg->len == 0) {
     108           8 :             buf = NULL;
     109             : 
     110             :         } else {
     111          25 :             buf = ngx_calloc_buf(r->pool);
     112          25 :             if (buf == NULL) {
     113           0 :                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
     114             :             }
     115             : 
     116          25 :             buf->start = buf->pos = computed_arg->data;
     117          50 :             buf->last = buf->end = computed_arg->data +
     118          25 :                 computed_arg->len;
     119             : 
     120          25 :             buf->memory = 1;
     121             :         }
     122             : 
     123          33 :         if (cl == NULL) {
     124          33 :             cl = ngx_alloc_chain_link(r->pool);
     125          33 :             if (cl == NULL) {
     126           0 :                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
     127             :             }
     128          33 :             cl->buf  = buf;
     129          33 :             cl->next = NULL;
     130          33 :             ll = &cl->next;
     131             : 
     132             :         } else {
     133             :             /* append a space first */
     134           0 :             *ll = ngx_alloc_chain_link(r->pool);
     135             : 
     136           0 :             if (*ll == NULL) {
     137           0 :                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
     138             :             }
     139             : 
     140           0 :             space_buf = ngx_calloc_buf(r->pool);
     141             : 
     142           0 :             if (space_buf == NULL) {
     143           0 :                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
     144             :             }
     145             : 
     146             :             /* nginx clears buf flags at the end of each request handling,
     147             :              * so we have to make a clone here. */
     148           0 :             *space_buf = ngx_http_echo_space_buf;
     149             : 
     150           0 :             (*ll)->buf = space_buf;
     151           0 :             (*ll)->next = NULL;
     152             : 
     153           0 :             ll = &(*ll)->next;
     154             : 
     155             :             /* then append the buf only if it's non-empty */
     156           0 :             if (buf) {
     157           0 :                 *ll = ngx_alloc_chain_link(r->pool);
     158           0 :                 if (*ll == NULL) {
     159           0 :                     return NGX_HTTP_INTERNAL_SERVER_ERROR;
     160             :                 }
     161           0 :                 (*ll)->buf  = buf;
     162           0 :                 (*ll)->next = NULL;
     163             : 
     164           0 :                 ll = &(*ll)->next;
     165             :             }
     166             :         }
     167             :     } /* end for */
     168             : 
     169          33 :     if (cl && cl->buf == NULL) {
     170           8 :         cl = cl->next;
     171             :     }
     172             : 
     173          33 :     if (opts && opts->nelts > 0) {
     174           0 :         opt = opts->elts;
     175             :         /* FIXME handle other unrecognized options here */
     176           0 :         if (opt[0].len == 1 && opt[0].data[0] == 'n') {
     177           0 :             goto done;
     178             :         }
     179             :     }
     180             : 
     181             :     /* append the newline character */
     182             : 
     183          33 :     newline_buf = ngx_calloc_buf(r->pool);
     184             : 
     185          33 :     if (newline_buf == NULL) {
     186           0 :         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     187             :     }
     188             : 
     189          33 :     *newline_buf = ngx_http_echo_newline_buf;
     190             : 
     191          33 :     if (cl == NULL) {
     192           8 :         cl = ngx_alloc_chain_link(r->pool);
     193             : 
     194           8 :         if (cl == NULL) {
     195           0 :             return NGX_HTTP_INTERNAL_SERVER_ERROR;
     196             :         }
     197             : 
     198           8 :         cl->buf = newline_buf;
     199           8 :         cl->next = NULL;
     200             :         /* ll = &cl->next; */
     201             : 
     202             :     } else {
     203          25 :         *ll = ngx_alloc_chain_link(r->pool);
     204             : 
     205          25 :         if (*ll == NULL) {
     206           0 :             return NGX_HTTP_INTERNAL_SERVER_ERROR;
     207             :         }
     208             : 
     209          25 :         (*ll)->buf  = newline_buf;
     210          25 :         (*ll)->next = NULL;
     211             :         /* ll = &(*ll)->next; */
     212             :     }
     213             : 
     214          33 : done:
     215             : 
     216          33 :     if (cl == NULL || cl->buf == NULL) {
     217           0 :         return NGX_OK;
     218             :     }
     219             : 
     220          33 :     if (in_filter) {
     221           0 :         return ngx_http_echo_next_body_filter(r, cl);
     222             :     }
     223             : 
     224          33 :     return ngx_http_echo_send_chain_link(r, ctx, cl);
     225             : }
     226             : 
     227             : 
     228             : ngx_int_t
     229           0 : ngx_http_echo_exec_echo_flush(ngx_http_request_t *r, ngx_http_echo_ctx_t *ctx)
     230             : {
     231           0 :     return ngx_http_send_special(r, NGX_HTTP_FLUSH);
     232             : }
     233             : 
     234             : 
     235             : ngx_int_t
     236           0 : ngx_http_echo_exec_echo_request_body(ngx_http_request_t *r,
     237             :     ngx_http_echo_ctx_t *ctx)
     238             : {
     239             :     ngx_buf_t       *b;
     240             :     ngx_chain_t     *out, *cl, **ll;
     241             : 
     242           0 :     if (r->request_body == NULL || r->request_body->bufs == NULL) {
     243           0 :         return NGX_OK;
     244             :     }
     245             : 
     246           0 :     out = NULL;
     247           0 :     ll = &out;
     248             : 
     249           0 :     for (cl = r->request_body->bufs; cl; cl = cl->next) {
     250           0 :         if (ngx_buf_special(cl->buf)) {
     251             :             /* we do not want to create zero-size bufs */
     252           0 :             continue;
     253             :         }
     254             : 
     255           0 :         *ll = ngx_alloc_chain_link(r->pool);
     256           0 :         if (*ll == NULL) {
     257           0 :             return NGX_ERROR;
     258             :         }
     259             : 
     260           0 :         b = ngx_alloc_buf(r->pool);
     261           0 :         if (b == NULL) {
     262           0 :             return NGX_ERROR;
     263             :         }
     264             : 
     265           0 :         (*ll)->buf = b;
     266           0 :         (*ll)->next = NULL;
     267             : 
     268           0 :         ngx_memcpy(b, cl->buf, sizeof(ngx_buf_t));
     269           0 :         b->tag = (ngx_buf_tag_t) &ngx_http_echo_exec_echo_request_body;
     270           0 :         b->last_buf = 0;
     271           0 :         b->last_in_chain = 0;
     272             : 
     273           0 :         ll = &(*ll)->next;
     274             :     }
     275             : 
     276           0 :     if (out == NULL) {
     277           0 :         return NGX_OK;
     278             :     }
     279             : 
     280           0 :     return ngx_http_echo_send_chain_link(r, ctx, out);
     281             : }
     282             : 
     283             : 
     284             : ngx_int_t
     285           0 : ngx_http_echo_exec_echo_duplicate(ngx_http_request_t *r,
     286             :     ngx_http_echo_ctx_t *ctx, ngx_array_t *computed_args)
     287             : {
     288             :     ngx_str_t                   *computed_arg;
     289             :     ngx_str_t                   *computed_arg_elts;
     290             :     ssize_t                      i, count;
     291             :     ngx_str_t                   *str;
     292             :     u_char                      *p;
     293             :     ngx_int_t                    rc;
     294             :     ngx_buf_t                   *buf;
     295             :     ngx_chain_t                 *cl;
     296             : 
     297             :     dd_enter();
     298             : 
     299           0 :     computed_arg_elts = computed_args->elts;
     300             : 
     301           0 :     computed_arg = &computed_arg_elts[0];
     302             : 
     303           0 :     count = ngx_http_echo_atosz(computed_arg->data, computed_arg->len);
     304             : 
     305           0 :     if (count == NGX_ERROR) {
     306           0 :         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     307             :                       "invalid size specified: \"%V\"", computed_arg);
     308             : 
     309           0 :         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     310             :     }
     311             : 
     312           0 :     str = &computed_arg_elts[1];
     313             : 
     314           0 :     if (count == 0 || str->len == 0) {
     315           0 :         rc = ngx_http_echo_send_header_if_needed(r, ctx);
     316           0 :         if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
     317           0 :             return rc;
     318             :         }
     319             : 
     320           0 :         return NGX_OK;
     321             :     }
     322             : 
     323           0 :     buf = ngx_create_temp_buf(r->pool, count * str->len);
     324           0 :     if (buf == NULL) {
     325           0 :         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     326             :     }
     327             : 
     328           0 :     p = buf->pos;
     329           0 :     for (i = 0; i < count; i++) {
     330           0 :         p = ngx_copy(p, str->data, str->len);
     331             :     }
     332           0 :     buf->last = p;
     333             : 
     334           0 :     cl = ngx_alloc_chain_link(r->pool);
     335           0 :     if (cl == NULL) {
     336           0 :         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     337             :     }
     338           0 :     cl->next = NULL;
     339           0 :     cl->buf = buf;
     340             : 
     341           0 :     return ngx_http_echo_send_chain_link(r, ctx, cl);
     342             : }

Generated by: LCOV version 1.13