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_filter.c (source / functions) Hit Total Coverage
Test: coverage ngix Lines: 7 98 7.1 %
Date: 2020-03-03 04:25:50 Functions: 1 4 25.0 %
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_filter.h"
       7             : #include "ngx_http_echo_util.h"
       8             : #include "ngx_http_echo_echo.h"
       9             : 
      10             : #include <ngx_log.h>
      11             : 
      12             : 
      13             : 
      14             : ngx_http_output_header_filter_pt ngx_http_echo_next_header_filter;
      15             : 
      16             : ngx_http_output_body_filter_pt ngx_http_echo_next_body_filter;
      17             : 
      18             : static ngx_int_t ngx_http_echo_header_filter(ngx_http_request_t *r);
      19             : 
      20             : static ngx_int_t ngx_http_echo_body_filter(ngx_http_request_t *r,
      21             :         ngx_chain_t *in);
      22             : 
      23             : /* filter handlers */
      24             : static ngx_int_t ngx_http_echo_exec_filter_cmds(ngx_http_request_t *r,
      25             :     ngx_http_echo_ctx_t *ctx, ngx_array_t *cmds, ngx_uint_t *iterator);
      26             : 
      27             : 
      28             : static volatile ngx_cycle_t  *ngx_http_echo_prev_cycle = NULL;
      29             : 
      30             : 
      31             : ngx_int_t
      32          18 : ngx_http_echo_filter_init(ngx_conf_t *cf)
      33             : {
      34             :     int                              multi_http_blocks;
      35             :     ngx_http_echo_main_conf_t       *emcf;
      36             : 
      37          18 :     emcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_echo_module);
      38             : 
      39          18 :     if (ngx_http_echo_prev_cycle != ngx_cycle) {
      40          18 :         ngx_http_echo_prev_cycle = ngx_cycle;
      41          18 :         multi_http_blocks = 0;
      42             : 
      43             :     } else {
      44           0 :         multi_http_blocks = 1;
      45             :     }
      46             : 
      47          18 :     if (multi_http_blocks || emcf->requires_filter) {
      48             :         dd("top header filter: %ld",
      49             :            (unsigned long) ngx_http_top_header_filter);
      50             : 
      51           0 :         ngx_http_echo_next_header_filter = ngx_http_top_header_filter;
      52           0 :         ngx_http_top_header_filter = ngx_http_echo_header_filter;
      53             : 
      54             :         dd("top body filter: %ld", (unsigned long) ngx_http_top_body_filter);
      55             : 
      56           0 :         ngx_http_echo_next_body_filter = ngx_http_top_body_filter;
      57           0 :         ngx_http_top_body_filter  = ngx_http_echo_body_filter;
      58             :     }
      59             : 
      60          18 :     return NGX_OK;
      61             : }
      62             : 
      63             : 
      64             : static ngx_int_t
      65           0 : ngx_http_echo_header_filter(ngx_http_request_t *r)
      66             : {
      67             :     ngx_http_echo_loc_conf_t    *conf;
      68             :     ngx_http_echo_ctx_t         *ctx;
      69             : 
      70           0 :     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
      71             :                    "echo header filter, uri \"%V?%V\"", &r->uri, &r->args);
      72             : 
      73           0 :     ctx = ngx_http_get_module_ctx(r, ngx_http_echo_module);
      74             : 
      75             :     /* XXX we should add option to insert contents for responses
      76             :      * of non-200 status code here... */
      77             :     /*
      78             :     if (r->headers_out.status != NGX_HTTP_OK) {
      79             :         if (ctx != NULL) {
      80             :             ctx->skip_filter = 1;
      81             :         }
      82             :         return ngx_http_echo_next_header_filter(r);
      83             :     }
      84             :     */
      85             : 
      86           0 :     conf = ngx_http_get_module_loc_conf(r, ngx_http_echo_module);
      87           0 :     if (conf->before_body_cmds == NULL && conf->after_body_cmds == NULL) {
      88           0 :         if (ctx != NULL) {
      89           0 :             ctx->skip_filter = 1;
      90             :         }
      91           0 :         return ngx_http_echo_next_header_filter(r);
      92             :     }
      93             : 
      94           0 :     if (ctx == NULL) {
      95           0 :         ctx = ngx_http_echo_create_ctx(r);
      96           0 :         if (ctx == NULL) {
      97           0 :             return NGX_ERROR;
      98             :         }
      99             : 
     100           0 :         ngx_http_set_ctx(r, ctx, ngx_http_echo_module);
     101             :     }
     102             : 
     103             :     /* enable streaming here (use chunked encoding) */
     104           0 :     ngx_http_clear_content_length(r);
     105           0 :     ngx_http_clear_accept_ranges(r);
     106             : 
     107           0 :     return ngx_http_echo_next_header_filter(r);
     108             : }
     109             : 
     110             : 
     111             : static ngx_int_t
     112           0 : ngx_http_echo_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
     113             : {
     114             :     ngx_http_echo_ctx_t         *ctx;
     115             :     ngx_int_t                    rc;
     116             :     ngx_http_echo_loc_conf_t    *conf;
     117             :     unsigned                     last;
     118             :     ngx_chain_t                 *cl;
     119             :     ngx_buf_t                   *b;
     120             : 
     121           0 :     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
     122             :                    "echo body filter, uri \"%V?%V\"", &r->uri, &r->args);
     123             : 
     124           0 :     if (in == NULL || r->header_only) {
     125           0 :         return ngx_http_echo_next_body_filter(r, in);
     126             :     }
     127             : 
     128           0 :     ctx = ngx_http_get_module_ctx(r, ngx_http_echo_module);
     129             : 
     130           0 :     if (ctx == NULL || ctx->skip_filter) {
     131           0 :         return ngx_http_echo_next_body_filter(r, in);
     132             :     }
     133             : 
     134           0 :     conf = ngx_http_get_module_loc_conf(r, ngx_http_echo_module);
     135             : 
     136           0 :     if (!ctx->before_body_sent) {
     137           0 :         ctx->before_body_sent = 1;
     138             : 
     139           0 :         if (conf->before_body_cmds != NULL) {
     140           0 :             rc = ngx_http_echo_exec_filter_cmds(r, ctx, conf->before_body_cmds,
     141             :                                                 &ctx->next_before_body_cmd);
     142           0 :             if (rc != NGX_OK) {
     143           0 :                 return NGX_ERROR;
     144             :             }
     145             :         }
     146             :     }
     147             : 
     148           0 :     if (conf->after_body_cmds == NULL) {
     149           0 :         ctx->skip_filter = 1;
     150           0 :         return ngx_http_echo_next_body_filter(r, in);
     151             :     }
     152             : 
     153           0 :     last = 0;
     154             : 
     155           0 :     for (cl = in; cl; cl = cl->next) {
     156             :         dd("cl %p, special %d", cl, ngx_buf_special(cl->buf));
     157             : 
     158           0 :         if (cl->buf->last_buf || cl->buf->last_in_chain) {
     159           0 :             cl->buf->last_buf = 0;
     160           0 :             cl->buf->last_in_chain = 0;
     161           0 :             cl->buf->sync = 1;
     162           0 :             last = 1;
     163             :         }
     164             :     }
     165             : 
     166             :     dd("in %p, last %d", in, (int) last);
     167             : 
     168           0 :     if (in) {
     169           0 :         rc = ngx_http_echo_next_body_filter(r, in);
     170             : 
     171             : #if 0
     172             :         if (rc == NGX_AGAIN) {
     173             :             return NGX_ERROR;
     174             :         }
     175             : #endif
     176             : 
     177             :         dd("next filter returns %d, last %d", (int) rc, (int) last);
     178             : 
     179           0 :         if (rc == NGX_ERROR || rc > NGX_OK || !last) {
     180           0 :             return rc;
     181             :         }
     182             :     }
     183             : 
     184             :     dd("exec filter cmds for after body cmds");
     185             : 
     186           0 :     rc = ngx_http_echo_exec_filter_cmds(r, ctx, conf->after_body_cmds,
     187             :                                         &ctx->next_after_body_cmd);
     188           0 :     if (rc == NGX_ERROR || rc > NGX_OK) {
     189             :         dd("FAILED: exec filter cmds for after body cmds");
     190           0 :         return NGX_ERROR;
     191             :     }
     192             : 
     193           0 :     ctx->skip_filter = 1;
     194             : 
     195             :     dd("after body cmds executed...terminating...");
     196             : 
     197             :     /* XXX we can NOT use
     198             :      * ngx_http_send_special(r, NGX_HTTP_LAST) here
     199             :      * because we should bypass the upstream filters. */
     200             : 
     201           0 :     b = ngx_calloc_buf(r->pool);
     202           0 :     if (b == NULL) {
     203           0 :         return NGX_ERROR;
     204             :     }
     205             : 
     206           0 :     if (r == r->main && !r->post_action) {
     207           0 :         b->last_buf = 1;
     208             : 
     209             :     } else {
     210           0 :         b->sync = 1;
     211           0 :         b->last_in_chain = 1;
     212             :     }
     213             : 
     214           0 :     cl = ngx_alloc_chain_link(r->pool);
     215           0 :     if (cl == NULL) {
     216           0 :         return NGX_ERROR;
     217             :     }
     218             : 
     219           0 :     cl->next = NULL;
     220           0 :     cl->buf = b;
     221             : 
     222           0 :     return ngx_http_echo_next_body_filter(r, cl);
     223             : }
     224             : 
     225             : 
     226             : static ngx_int_t
     227           0 : ngx_http_echo_exec_filter_cmds(ngx_http_request_t *r,
     228             :     ngx_http_echo_ctx_t *ctx, ngx_array_t *cmds, ngx_uint_t *iterator)
     229             : {
     230             :     ngx_int_t                    rc;
     231           0 :     ngx_array_t                 *opts = NULL;
     232           0 :     ngx_array_t                 *computed_args = NULL;
     233             :     ngx_http_echo_cmd_t         *cmd;
     234             :     ngx_http_echo_cmd_t         *cmd_elts;
     235             : 
     236           0 :     for (cmd_elts = cmds->elts; *iterator < cmds->nelts; (*iterator)++) {
     237           0 :         cmd = &cmd_elts[*iterator];
     238             : 
     239             :         /* evaluate arguments for the current cmd (if any) */
     240           0 :         if (cmd->args) {
     241           0 :             computed_args = ngx_array_create(r->pool, cmd->args->nelts,
     242             :                                              sizeof(ngx_str_t));
     243           0 :             if (computed_args == NULL) {
     244           0 :                 return NGX_ERROR;
     245             :             }
     246             : 
     247           0 :             opts = ngx_array_create(r->pool, 1, sizeof(ngx_str_t));
     248           0 :             if (opts == NULL) {
     249           0 :                 return NGX_ERROR;
     250             :             }
     251             : 
     252           0 :             rc = ngx_http_echo_eval_cmd_args(r, cmd, computed_args, opts);
     253             : 
     254           0 :             if (rc != NGX_OK) {
     255           0 :                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     256             :                               "Failed to evaluate arguments for "
     257             :                               "the directive.");
     258           0 :                 return rc;
     259             :             }
     260             :         }
     261             : 
     262             :         /* do command dispatch based on the opcode */
     263           0 :         switch (cmd->opcode) {
     264           0 :         case echo_opcode_echo_before_body:
     265             :         case echo_opcode_echo_after_body:
     266             :             dd("exec echo_before_body or echo_after_body...");
     267             : 
     268           0 :             rc = ngx_http_echo_exec_echo(r, ctx, computed_args,
     269             :                                          1 /* in filter */, opts);
     270             : 
     271           0 :             if (rc == NGX_ERROR || rc > NGX_OK) {
     272           0 :                 return rc;
     273             :             }
     274             : 
     275           0 :             break;
     276           0 :         default:
     277           0 :             break;
     278             :         }
     279             :     }
     280             : 
     281           0 :     return NGX_OK;
     282             : }

Generated by: LCOV version 1.13