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_handler.c (source / functions) Hit Total Coverage
Test: coverage ngix Lines: 36 164 22.0 %
Date: 2020-03-03 04:25:50 Functions: 2 4 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : 
       2             : /*
       3             :  * Copyright (C) Yichun Zhang (agentzh)
       4             :  */
       5             : 
       6             : 
       7             : #ifndef DDEBUG
       8             : #define DDEBUG 0
       9             : #endif
      10             : #include "ddebug.h"
      11             : 
      12             : 
      13             : #include "ngx_http_echo_filter.h"
      14             : #include "ngx_http_echo_handler.h"
      15             : #include "ngx_http_echo_echo.h"
      16             : #include "ngx_http_echo_util.h"
      17             : #include "ngx_http_echo_sleep.h"
      18             : #include "ngx_http_echo_var.h"
      19             : #include "ngx_http_echo_timer.h"
      20             : #include "ngx_http_echo_location.h"
      21             : #include "ngx_http_echo_subrequest.h"
      22             : #include "ngx_http_echo_request_info.h"
      23             : #include "ngx_http_echo_foreach.h"
      24             : 
      25             : #include <nginx.h>
      26             : #include <ngx_log.h>
      27             : 
      28             : 
      29             : void
      30           0 : ngx_http_echo_wev_handler(ngx_http_request_t *r)
      31             : {
      32             :     ngx_int_t                    rc;
      33             :     ngx_http_echo_ctx_t         *ctx;
      34             : 
      35             :     dd("wev handler");
      36             : 
      37           0 :     ctx = ngx_http_get_module_ctx(r, ngx_http_echo_module);
      38             : 
      39           0 :     if (ctx == NULL) {
      40           0 :         ngx_http_finalize_request(r, NGX_ERROR);
      41           0 :         return;
      42             :     }
      43             : 
      44             :     dd("waiting: %d, done: %d", (int) ctx->waiting, (int) ctx->done);
      45             : 
      46           0 :     if (ctx->waiting && ! ctx->done) {
      47             : 
      48           0 :         if (r == r->connection->data && r->postponed) {
      49             : 
      50           0 :             if (r->postponed->request) {
      51           0 :                 r->connection->data = r->postponed->request;
      52             : 
      53             : #if defined(nginx_version) && nginx_version >= 8012
      54           0 :                 ngx_http_post_request(r->postponed->request, NULL);
      55             : #else
      56             :                 ngx_http_post_request(r->postponed->request);
      57             : #endif
      58             : 
      59             :             } else {
      60           0 :                 ngx_http_echo_flush_postponed_outputs(r);
      61             :             }
      62             :         }
      63             : 
      64           0 :         return;
      65             :     }
      66             : 
      67           0 :     ctx->done = 0;
      68             : 
      69           0 :     ctx->next_handler_cmd++;
      70             : 
      71           0 :     rc = ngx_http_echo_run_cmds(r);
      72             : 
      73             :     dd("rc: %d", (int) rc);
      74             : 
      75           0 :     if (rc == NGX_ERROR || rc == NGX_DONE) {
      76           0 :         ngx_http_finalize_request(r, rc);
      77           0 :         return;
      78             :     }
      79             : 
      80           0 :     if (rc == NGX_AGAIN) {
      81             :         dd("mark busy %d for %.*s", (int) ctx->next_handler_cmd,
      82             :            (int) r->uri.len,
      83             :            r->uri.data);
      84             : 
      85           0 :         ctx->waiting = 1;
      86           0 :         ctx->done = 0;
      87             : 
      88             :     } else {
      89             :         dd("mark ready %d", (int) ctx->next_handler_cmd);
      90           0 :         ctx->waiting = 0;
      91           0 :         ctx->done = 1;
      92             : 
      93             :         dd("finalizing with rc %d", (int) rc);
      94             : 
      95             :         dd("finalize request %.*s with %d", (int) r->uri.len, r->uri.data,
      96             :            (int) rc);
      97             : 
      98           0 :         ngx_http_finalize_request(r, rc);
      99             :     }
     100             : }
     101             : 
     102             : 
     103             : ngx_int_t
     104          15 : ngx_http_echo_handler(ngx_http_request_t *r)
     105             : {
     106             :     ngx_int_t                    rc;
     107             :     ngx_http_echo_ctx_t         *ctx;
     108             : 
     109             :     dd("subrequest in memory: %d", (int) r->subrequest_in_memory);
     110             : 
     111          15 :     rc = ngx_http_echo_run_cmds(r);
     112             : 
     113             :     dd("run cmds returned %d", (int) rc);
     114             : 
     115          15 :     if (rc == NGX_ERROR
     116          15 :         || rc == NGX_OK
     117           0 :         || rc == NGX_DONE
     118           0 :         || rc == NGX_DECLINED)
     119             :     {
     120          15 :         return rc;
     121             :     }
     122             : 
     123           0 :     ctx = ngx_http_get_module_ctx(r, ngx_http_echo_module);
     124             : 
     125           0 :     if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
     126           0 :         if (ctx && r->header_sent) {
     127           0 :             return NGX_ERROR;
     128             :         }
     129             : 
     130           0 :         return rc;
     131             :     }
     132             : 
     133             :     /* rc == NGX_AGAIN */
     134             : 
     135             : #if defined(nginx_version) && nginx_version >= 8011
     136           0 :     r->main->count++;
     137             : #endif
     138             : 
     139             :     dd("%d", r->connection->destroyed);
     140             :     dd("%d", r->done);
     141             : 
     142           0 :     if (ctx) {
     143             :         dd("mark busy %d for %.*s", (int) ctx->next_handler_cmd,
     144             :            (int) r->uri.len,
     145             :            r->uri.data);
     146             : 
     147           0 :         ctx->waiting = 1;
     148           0 :         ctx->done = 0;
     149             :     }
     150             : 
     151           0 :     return NGX_DONE;
     152             : }
     153             : 
     154             : 
     155             : ngx_int_t
     156          15 : ngx_http_echo_run_cmds(ngx_http_request_t *r)
     157             : {
     158             :     ngx_http_echo_loc_conf_t    *elcf;
     159             :     ngx_http_echo_ctx_t         *ctx;
     160             :     ngx_int_t                    rc;
     161             :     ngx_array_t                 *cmds;
     162          15 :     ngx_array_t                 *computed_args = NULL;
     163             :     ngx_http_echo_cmd_t         *cmd;
     164             :     ngx_http_echo_cmd_t         *cmd_elts;
     165          15 :     ngx_array_t                 *opts = NULL;
     166             : 
     167          15 :     elcf = ngx_http_get_module_loc_conf(r, ngx_http_echo_module);
     168          15 :     cmds = elcf->handler_cmds;
     169          15 :     if (cmds == NULL) {
     170           0 :         return NGX_DECLINED;
     171             :     }
     172             : 
     173          15 :     ctx = ngx_http_get_module_ctx(r, ngx_http_echo_module);
     174          15 :     if (ctx == NULL) {
     175          15 :         ctx = ngx_http_echo_create_ctx(r);
     176          15 :         if (ctx == NULL) {
     177           0 :             return NGX_HTTP_INTERNAL_SERVER_ERROR;
     178             :         }
     179             : 
     180          15 :         ngx_http_set_ctx(r, ctx, ngx_http_echo_module);
     181             :     }
     182             : 
     183             :     dd("exec handler: %.*s: %i", (int) r->uri.len, r->uri.data,
     184             :        (int) ctx->next_handler_cmd);
     185             : 
     186          15 :     cmd_elts = cmds->elts;
     187             : 
     188          48 :     for (; ctx->next_handler_cmd < cmds->nelts; ctx->next_handler_cmd++) {
     189             : 
     190          33 :         cmd = &cmd_elts[ctx->next_handler_cmd];
     191             : 
     192             :         /* evaluate arguments for the current cmd (if any) */
     193          33 :         if (cmd->args) {
     194          33 :             computed_args = ngx_array_create(r->pool, cmd->args->nelts,
     195             :                                              sizeof(ngx_str_t));
     196             : 
     197          33 :             if (computed_args == NULL) {
     198           0 :                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
     199             :             }
     200             : 
     201          33 :             opts = ngx_array_create(r->pool, 1, sizeof(ngx_str_t));
     202             : 
     203          33 :             if (opts == NULL) {
     204           0 :                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
     205             :             }
     206             : 
     207          33 :             rc = ngx_http_echo_eval_cmd_args(r, cmd, computed_args, opts);
     208          33 :             if (rc != NGX_OK) {
     209           0 :                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     210             :                               "Failed to evaluate arguments for "
     211             :                               "the directive.");
     212           0 :                 return rc;
     213             :             }
     214             :         }
     215             : 
     216             :         /* do command dispatch based on the opcode */
     217             : 
     218          33 :         switch (cmd->opcode) {
     219             : 
     220           0 :         case echo_opcode_echo_sync:
     221           0 :             rc = ngx_http_echo_exec_echo_sync(r, ctx);
     222           0 :             break;
     223             : 
     224          33 :         case echo_opcode_echo:
     225             :             /* XXX moved the following code to a separate
     226             :              * function */
     227             :             dd("found echo opcode");
     228          33 :             rc = ngx_http_echo_exec_echo(r, ctx, computed_args,
     229             :                                          0 /* in filter */, opts);
     230          33 :             break;
     231             : 
     232           0 :         case echo_opcode_echo_request_body:
     233           0 :             rc = ngx_http_echo_exec_echo_request_body(r, ctx);
     234           0 :             break;
     235             : 
     236           0 :         case echo_opcode_echo_location_async:
     237           0 :             if (!r->request_body) {
     238             :                 /* we require reading the request body before doing
     239             :                  * subrequests */
     240             : 
     241           0 :                 ctx->next_handler_cmd--; /* re-run the current cmd */
     242           0 :                 goto read_request_body;
     243             :             }
     244             : 
     245             :             dd("found opcode echo location async...");
     246           0 :             rc = ngx_http_echo_exec_echo_location_async(r, ctx,
     247             :                                                         computed_args);
     248           0 :             break;
     249             : 
     250           0 :         case echo_opcode_echo_location:
     251           0 :             if (!r->request_body) {
     252             :                 /* we require reading the request body before doing
     253             :                  * subrequests */
     254             : 
     255           0 :                 ctx->next_handler_cmd--; /* re-run the current cmd */
     256           0 :                 goto read_request_body;
     257             :             }
     258             : 
     259           0 :             return ngx_http_echo_exec_echo_location(r, ctx, computed_args);
     260             : 
     261           0 :         case echo_opcode_echo_subrequest_async:
     262           0 :             if (!r->request_body) {
     263             :                 /* we require reading the request body before doing
     264             :                  * subrequests */
     265             : 
     266           0 :                 ctx->next_handler_cmd--; /* re-run the current cmd */
     267           0 :                 goto read_request_body;
     268             :             }
     269             : 
     270             :             dd("found opcode echo subrequest async...");
     271           0 :             rc = ngx_http_echo_exec_echo_subrequest_async(r, ctx,
     272             :                                                           computed_args);
     273           0 :             break;
     274             : 
     275           0 :         case echo_opcode_echo_subrequest:
     276           0 :             if (!r->request_body) {
     277             :                 /* we require reading the request body before doing
     278             :                  * subrequests */
     279             : 
     280           0 :                 ctx->next_handler_cmd--; /* re-run the current cmd */
     281           0 :                 goto read_request_body;
     282             :             }
     283             : 
     284           0 :             return ngx_http_echo_exec_echo_subrequest(r, ctx, computed_args);
     285             : 
     286           0 :         case echo_opcode_echo_sleep:
     287           0 :             return ngx_http_echo_exec_echo_sleep(r, ctx, computed_args);
     288             : 
     289           0 :         case echo_opcode_echo_flush:
     290           0 :             rc = ngx_http_echo_exec_echo_flush(r, ctx);
     291           0 :             break;
     292             : 
     293           0 :         case echo_opcode_echo_blocking_sleep:
     294           0 :             rc = ngx_http_echo_exec_echo_blocking_sleep(r, ctx,
     295             :                                                         computed_args);
     296           0 :             break;
     297             : 
     298           0 :         case echo_opcode_echo_reset_timer:
     299           0 :             rc = ngx_http_echo_exec_echo_reset_timer(r, ctx);
     300           0 :             break;
     301             : 
     302           0 :         case echo_opcode_echo_duplicate:
     303           0 :             rc = ngx_http_echo_exec_echo_duplicate(r, ctx, computed_args);
     304           0 :             break;
     305             : 
     306             :         case echo_opcode_echo_read_request_body:
     307             : 
     308           0 : read_request_body:
     309             : 
     310           0 :             ctx->wait_read_request_body = 0;
     311             : 
     312           0 :             rc = ngx_http_echo_exec_echo_read_request_body(r, ctx);
     313             : 
     314           0 :             if (rc == NGX_ERROR) {
     315           0 :                 return NGX_ERROR;
     316             :             }
     317             : 
     318           0 :             if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
     319             : #if (nginx_version >= 8011 && nginx_version < 1002006)                       \
     320             :     || (nginx_version >= 1003000 && nginx_version < 1003009)
     321             :                 r->main->count--;
     322             : #endif
     323           0 :                 return rc;
     324             :             }
     325             : 
     326             : #if nginx_version >= 8011
     327           0 :             r->main->count--;
     328             : #endif
     329             :             dd("read request body: %d", (int) rc);
     330             : 
     331           0 :             if (rc == NGX_OK) {
     332           0 :                 continue;
     333             :             }
     334             : 
     335             :             /* rc == NGX_AGAIN */
     336           0 :             ctx->wait_read_request_body = 1;
     337           0 :             return NGX_AGAIN;
     338             : 
     339           0 :         case echo_opcode_echo_foreach_split:
     340           0 :             rc = ngx_http_echo_exec_echo_foreach_split(r, ctx, computed_args);
     341           0 :             break;
     342             : 
     343           0 :         case echo_opcode_echo_end:
     344           0 :             rc = ngx_http_echo_exec_echo_end(r, ctx);
     345           0 :             break;
     346             : 
     347           0 :         case echo_opcode_echo_exec:
     348             :             dd("echo_exec");
     349           0 :             return ngx_http_echo_exec_exec(r, ctx, computed_args);
     350             : 
     351           0 :         default:
     352           0 :             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
     353             :                           "unknown opcode: %d", cmd->opcode);
     354           0 :             return NGX_HTTP_INTERNAL_SERVER_ERROR;
     355             :         }
     356             : 
     357          33 :         if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
     358           0 :             return rc;
     359             :         }
     360             :     }
     361             : 
     362          15 :     rc = ngx_http_echo_send_chain_link(r, ctx, NULL /* indicate LAST */);
     363             : 
     364          15 :     if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
     365           0 :         return rc;
     366             :     }
     367             : 
     368          15 :     if (!r->request_body) {
     369          15 :         if (ngx_http_discard_request_body(r) != NGX_OK) {
     370           0 :             return NGX_ERROR;
     371             :         }
     372             :     }
     373             : 
     374          15 :     return NGX_OK;
     375             : }
     376             : 
     377             : 
     378             : ngx_int_t
     379           0 : ngx_http_echo_post_subrequest(ngx_http_request_t *r,
     380             :     void *data, ngx_int_t rc)
     381             : {
     382           0 :     ngx_http_echo_ctx_t         *ctx = data;
     383             :     ngx_http_request_t          *pr;
     384             :     ngx_http_echo_ctx_t         *pr_ctx;
     385             : 
     386             :     dd("echo post_subrequest: %.*s", (int) r->uri.len, r->uri.data);
     387             : 
     388           0 :     if (ctx->run_post_subrequest) {
     389             :         dd("already run post_subrequest: %p: %.*s", ctx,
     390             :            (int) r->uri.len, r->uri.data);
     391             : 
     392           0 :         return rc;
     393             :     }
     394             : 
     395             :     dd("setting run_post_subrequest to 1 for %p for %.*s", ctx,
     396             :        (int) r->uri.len, r->uri.data);
     397             : 
     398           0 :     ctx->run_post_subrequest = 1;
     399             : 
     400           0 :     pr = r->parent;
     401             : 
     402           0 :     pr_ctx = ngx_http_get_module_ctx(pr, ngx_http_echo_module);
     403           0 :     if (pr_ctx == NULL) {
     404           0 :         return NGX_ERROR;
     405             :     }
     406             : 
     407             :     dd("mark ready %d", (int) pr_ctx->next_handler_cmd);
     408             : 
     409           0 :     pr_ctx->waiting = 0;
     410           0 :     pr_ctx->done = 1;
     411             : 
     412           0 :     pr->write_event_handler = ngx_http_echo_wev_handler;
     413             : 
     414             :     /* work-around issues in nginx's event module */
     415             : 
     416           0 :     if (r != r->connection->data
     417           0 :         && r->postponed
     418           0 :         && (r->main->posted_requests == NULL
     419           0 :             || r->main->posted_requests->request != pr))
     420             :     {
     421             : #if defined(nginx_version) && nginx_version >= 8012
     422           0 :         ngx_http_post_request(pr, NULL);
     423             : #else
     424             :         ngx_http_post_request(pr);
     425             : #endif
     426             :     }
     427             : 
     428           0 :     return rc;
     429             : }

Generated by: LCOV version 1.13