LCOV - code coverage report
Current view: top level - home/build/openresty-1.13.6.1/build/ngx_lua-0.10.11/src - ngx_http_lua_module.c (source / functions) Hit Total Coverage
Test: coverage ngix Lines: 170 245 69.4 %
Date: 2020-03-03 04:25:50 Functions: 8 10 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : 
       2             : /*
       3             :  * Copyright (C) Xiaozhe Wang (chaoslawful)
       4             :  * Copyright (C) Yichun Zhang (agentzh)
       5             :  */
       6             : 
       7             : 
       8             : #ifndef DDEBUG
       9             : #define DDEBUG 0
      10             : #endif
      11             : #include "ddebug.h"
      12             : 
      13             : 
      14             : #include "ngx_http_lua_directive.h"
      15             : #include "ngx_http_lua_capturefilter.h"
      16             : #include "ngx_http_lua_contentby.h"
      17             : #include "ngx_http_lua_rewriteby.h"
      18             : #include "ngx_http_lua_accessby.h"
      19             : #include "ngx_http_lua_logby.h"
      20             : #include "ngx_http_lua_util.h"
      21             : #include "ngx_http_lua_headerfilterby.h"
      22             : #include "ngx_http_lua_bodyfilterby.h"
      23             : #include "ngx_http_lua_initby.h"
      24             : #include "ngx_http_lua_initworkerby.h"
      25             : #include "ngx_http_lua_probe.h"
      26             : #include "ngx_http_lua_semaphore.h"
      27             : #include "ngx_http_lua_balancer.h"
      28             : #include "ngx_http_lua_ssl_certby.h"
      29             : #include "ngx_http_lua_ssl_session_storeby.h"
      30             : #include "ngx_http_lua_ssl_session_fetchby.h"
      31             : #include "ngx_http_lua_headers.h"
      32             : 
      33             : 
      34             : static void *ngx_http_lua_create_main_conf(ngx_conf_t *cf);
      35             : static char *ngx_http_lua_init_main_conf(ngx_conf_t *cf, void *conf);
      36             : static void *ngx_http_lua_create_srv_conf(ngx_conf_t *cf);
      37             : static char *ngx_http_lua_merge_srv_conf(ngx_conf_t *cf, void *parent,
      38             :     void *child);
      39             : static void *ngx_http_lua_create_loc_conf(ngx_conf_t *cf);
      40             : 
      41             : static char *ngx_http_lua_merge_loc_conf(ngx_conf_t *cf, void *parent,
      42             :     void *child);
      43             : static ngx_int_t ngx_http_lua_init(ngx_conf_t *cf);
      44             : static char *ngx_http_lua_lowat_check(ngx_conf_t *cf, void *post, void *data);
      45             : #if (NGX_HTTP_SSL)
      46             : static ngx_int_t ngx_http_lua_set_ssl(ngx_conf_t *cf,
      47             :     ngx_http_lua_loc_conf_t *llcf);
      48             : #endif
      49             : static char *ngx_http_lua_malloc_trim(ngx_conf_t *cf, ngx_command_t *cmd,
      50             :     void *conf);
      51             : 
      52             : 
      53             : static ngx_conf_post_t  ngx_http_lua_lowat_post =
      54             :     { ngx_http_lua_lowat_check };
      55             : 
      56             : 
      57             : static volatile ngx_cycle_t  *ngx_http_lua_prev_cycle = NULL;
      58             : 
      59             : 
      60             : #if (NGX_HTTP_SSL) && defined(nginx_version) && nginx_version >= 1001013
      61             : 
      62             : static ngx_conf_bitmask_t  ngx_http_lua_ssl_protocols[] = {
      63             :     { ngx_string("SSLv2"), NGX_SSL_SSLv2 },
      64             :     { ngx_string("SSLv3"), NGX_SSL_SSLv3 },
      65             :     { ngx_string("TLSv1"), NGX_SSL_TLSv1 },
      66             :     { ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 },
      67             :     { ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 },
      68             :     { ngx_null_string, 0 }
      69             : };
      70             : 
      71             : #endif
      72             : 
      73             : 
      74             : static ngx_command_t ngx_http_lua_cmds[] = {
      75             : 
      76             :     { ngx_string("lua_max_running_timers"),
      77             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
      78             :       ngx_conf_set_num_slot,
      79             :       NGX_HTTP_MAIN_CONF_OFFSET,
      80             :       offsetof(ngx_http_lua_main_conf_t, max_running_timers),
      81             :       NULL },
      82             : 
      83             :     { ngx_string("lua_max_pending_timers"),
      84             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
      85             :       ngx_conf_set_num_slot,
      86             :       NGX_HTTP_MAIN_CONF_OFFSET,
      87             :       offsetof(ngx_http_lua_main_conf_t, max_pending_timers),
      88             :       NULL },
      89             : 
      90             :     { ngx_string("lua_shared_dict"),
      91             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2,
      92             :       ngx_http_lua_shared_dict,
      93             :       0,
      94             :       0,
      95             :       NULL },
      96             : 
      97             :     { ngx_string("lua_capture_error_log"),
      98             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
      99             :       ngx_http_lua_capture_error_log,
     100             :       0,
     101             :       0,
     102             :       NULL },
     103             : 
     104             : #if (NGX_PCRE)
     105             :     { ngx_string("lua_regex_cache_max_entries"),
     106             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
     107             :       ngx_conf_set_num_slot,
     108             :       NGX_HTTP_MAIN_CONF_OFFSET,
     109             :       offsetof(ngx_http_lua_main_conf_t, regex_cache_max_entries),
     110             :       NULL },
     111             : 
     112             :     { ngx_string("lua_regex_match_limit"),
     113             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
     114             :       ngx_conf_set_num_slot,
     115             :       NGX_HTTP_MAIN_CONF_OFFSET,
     116             :       offsetof(ngx_http_lua_main_conf_t, regex_match_limit),
     117             :       NULL },
     118             : #endif
     119             : 
     120             :     { ngx_string("lua_package_cpath"),
     121             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
     122             :       ngx_http_lua_package_cpath,
     123             :       NGX_HTTP_MAIN_CONF_OFFSET,
     124             :       0,
     125             :       NULL },
     126             : 
     127             :     { ngx_string("lua_package_path"),
     128             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
     129             :       ngx_http_lua_package_path,
     130             :       NGX_HTTP_MAIN_CONF_OFFSET,
     131             :       0,
     132             :       NULL },
     133             : 
     134             :     { ngx_string("lua_code_cache"),
     135             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     136             :                         |NGX_CONF_FLAG,
     137             :       ngx_http_lua_code_cache,
     138             :       NGX_HTTP_LOC_CONF_OFFSET,
     139             :       offsetof(ngx_http_lua_loc_conf_t, enable_code_cache),
     140             :       NULL },
     141             : 
     142             :     { ngx_string("lua_need_request_body"),
     143             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     144             :                         |NGX_CONF_FLAG,
     145             :       ngx_conf_set_flag_slot,
     146             :       NGX_HTTP_LOC_CONF_OFFSET,
     147             :       offsetof(ngx_http_lua_loc_conf_t, force_read_body),
     148             :       NULL },
     149             : 
     150             :     { ngx_string("lua_transform_underscores_in_response_headers"),
     151             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     152             :                         |NGX_CONF_FLAG,
     153             :       ngx_conf_set_flag_slot,
     154             :       NGX_HTTP_LOC_CONF_OFFSET,
     155             :       offsetof(ngx_http_lua_loc_conf_t, transform_underscores_in_resp_headers),
     156             :       NULL },
     157             : 
     158             :      { ngx_string("lua_socket_log_errors"),
     159             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     160             :                         |NGX_CONF_FLAG,
     161             :       ngx_conf_set_flag_slot,
     162             :       NGX_HTTP_LOC_CONF_OFFSET,
     163             :       offsetof(ngx_http_lua_loc_conf_t, log_socket_errors),
     164             :       NULL },
     165             : 
     166             :     { ngx_string("init_by_lua_block"),
     167             :       NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
     168             :       ngx_http_lua_init_by_lua_block,
     169             :       NGX_HTTP_MAIN_CONF_OFFSET,
     170             :       0,
     171             :       (void *) ngx_http_lua_init_by_inline },
     172             : 
     173             :     { ngx_string("init_by_lua"),
     174             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
     175             :       ngx_http_lua_init_by_lua,
     176             :       NGX_HTTP_MAIN_CONF_OFFSET,
     177             :       0,
     178             :       (void *) ngx_http_lua_init_by_inline },
     179             : 
     180             :     { ngx_string("init_by_lua_file"),
     181             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
     182             :       ngx_http_lua_init_by_lua,
     183             :       NGX_HTTP_MAIN_CONF_OFFSET,
     184             :       0,
     185             :       (void *) ngx_http_lua_init_by_file },
     186             : 
     187             :     { ngx_string("init_worker_by_lua_block"),
     188             :       NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
     189             :       ngx_http_lua_init_worker_by_lua_block,
     190             :       NGX_HTTP_MAIN_CONF_OFFSET,
     191             :       0,
     192             :       (void *) ngx_http_lua_init_worker_by_inline },
     193             : 
     194             :     { ngx_string("init_worker_by_lua"),
     195             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
     196             :       ngx_http_lua_init_worker_by_lua,
     197             :       NGX_HTTP_MAIN_CONF_OFFSET,
     198             :       0,
     199             :       (void *) ngx_http_lua_init_worker_by_inline },
     200             : 
     201             :     { ngx_string("init_worker_by_lua_file"),
     202             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
     203             :       ngx_http_lua_init_worker_by_lua,
     204             :       NGX_HTTP_MAIN_CONF_OFFSET,
     205             :       0,
     206             :       (void *) ngx_http_lua_init_worker_by_file },
     207             : 
     208             : #if defined(NDK) && NDK
     209             :     /* set_by_lua $res { inline Lua code } [$arg1 [$arg2 [...]]] */
     210             :     { ngx_string("set_by_lua_block"),
     211             :       NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     212             :                        |NGX_CONF_1MORE|NGX_CONF_BLOCK,
     213             :       ngx_http_lua_set_by_lua_block,
     214             :       NGX_HTTP_LOC_CONF_OFFSET,
     215             :       0,
     216             :       (void *) ngx_http_lua_filter_set_by_lua_inline },
     217             : 
     218             :     /* set_by_lua $res <inline script> [$arg1 [$arg2 [...]]] */
     219             :     { ngx_string("set_by_lua"),
     220             :       NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     221             :                        |NGX_CONF_2MORE,
     222             :       ngx_http_lua_set_by_lua,
     223             :       NGX_HTTP_LOC_CONF_OFFSET,
     224             :       0,
     225             :       (void *) ngx_http_lua_filter_set_by_lua_inline },
     226             : 
     227             :     /* set_by_lua_file $res rel/or/abs/path/to/script [$arg1 [$arg2 [..]]] */
     228             :     { ngx_string("set_by_lua_file"),
     229             :       NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     230             :                        |NGX_CONF_2MORE,
     231             :       ngx_http_lua_set_by_lua_file,
     232             :       NGX_HTTP_LOC_CONF_OFFSET,
     233             :       0,
     234             :       (void *) ngx_http_lua_filter_set_by_lua_file },
     235             : #endif
     236             : 
     237             :     /* rewrite_by_lua "<inline script>" */
     238             :     { ngx_string("rewrite_by_lua"),
     239             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     240             :                         |NGX_CONF_TAKE1,
     241             :       ngx_http_lua_rewrite_by_lua,
     242             :       NGX_HTTP_LOC_CONF_OFFSET,
     243             :       0,
     244             :       (void *) ngx_http_lua_rewrite_handler_inline },
     245             : 
     246             :     /* rewrite_by_lua_block { <inline script> } */
     247             :     { ngx_string("rewrite_by_lua_block"),
     248             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     249             :                         |NGX_CONF_BLOCK|NGX_CONF_NOARGS,
     250             :       ngx_http_lua_rewrite_by_lua_block,
     251             :       NGX_HTTP_LOC_CONF_OFFSET,
     252             :       0,
     253             :       (void *) ngx_http_lua_rewrite_handler_inline },
     254             : 
     255             :     /* access_by_lua "<inline script>" */
     256             :     { ngx_string("access_by_lua"),
     257             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     258             :                         |NGX_CONF_TAKE1,
     259             :       ngx_http_lua_access_by_lua,
     260             :       NGX_HTTP_LOC_CONF_OFFSET,
     261             :       0,
     262             :       (void *) ngx_http_lua_access_handler_inline },
     263             : 
     264             :     /* access_by_lua_block { <inline script> } */
     265             :     { ngx_string("access_by_lua_block"),
     266             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     267             :                         |NGX_CONF_BLOCK|NGX_CONF_NOARGS,
     268             :       ngx_http_lua_access_by_lua_block,
     269             :       NGX_HTTP_LOC_CONF_OFFSET,
     270             :       0,
     271             :       (void *) ngx_http_lua_access_handler_inline },
     272             : 
     273             :     /* content_by_lua "<inline script>" */
     274             :     { ngx_string("content_by_lua"),
     275             :       NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
     276             :       ngx_http_lua_content_by_lua,
     277             :       NGX_HTTP_LOC_CONF_OFFSET,
     278             :       0,
     279             :       (void *) ngx_http_lua_content_handler_inline },
     280             : 
     281             :     /* content_by_lua_block { <inline script> } */
     282             :     { ngx_string("content_by_lua_block"),
     283             :       NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
     284             :       ngx_http_lua_content_by_lua_block,
     285             :       NGX_HTTP_LOC_CONF_OFFSET,
     286             :       0,
     287             :       (void *) ngx_http_lua_content_handler_inline },
     288             : 
     289             :     /* log_by_lua <inline script> */
     290             :     { ngx_string("log_by_lua"),
     291             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     292             :                         |NGX_CONF_TAKE1,
     293             :       ngx_http_lua_log_by_lua,
     294             :       NGX_HTTP_LOC_CONF_OFFSET,
     295             :       0,
     296             :       (void *) ngx_http_lua_log_handler_inline },
     297             : 
     298             :     /* log_by_lua_block { <inline script> } */
     299             :     { ngx_string("log_by_lua_block"),
     300             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     301             :                         |NGX_CONF_BLOCK|NGX_CONF_NOARGS,
     302             :       ngx_http_lua_log_by_lua_block,
     303             :       NGX_HTTP_LOC_CONF_OFFSET,
     304             :       0,
     305             :       (void *) ngx_http_lua_log_handler_inline },
     306             : 
     307             :     { ngx_string("rewrite_by_lua_file"),
     308             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     309             :                         |NGX_CONF_TAKE1,
     310             :       ngx_http_lua_rewrite_by_lua,
     311             :       NGX_HTTP_LOC_CONF_OFFSET,
     312             :       0,
     313             :       (void *) ngx_http_lua_rewrite_handler_file },
     314             : 
     315             :     { ngx_string("rewrite_by_lua_no_postpone"),
     316             :       NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
     317             :       ngx_conf_set_flag_slot,
     318             :       NGX_HTTP_MAIN_CONF_OFFSET,
     319             :       offsetof(ngx_http_lua_main_conf_t, postponed_to_rewrite_phase_end),
     320             :       NULL },
     321             : 
     322             :     { ngx_string("access_by_lua_file"),
     323             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     324             :                         |NGX_CONF_TAKE1,
     325             :       ngx_http_lua_access_by_lua,
     326             :       NGX_HTTP_LOC_CONF_OFFSET,
     327             :       0,
     328             :       (void *) ngx_http_lua_access_handler_file },
     329             : 
     330             :     { ngx_string("access_by_lua_no_postpone"),
     331             :       NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
     332             :       ngx_conf_set_flag_slot,
     333             :       NGX_HTTP_MAIN_CONF_OFFSET,
     334             :       offsetof(ngx_http_lua_main_conf_t, postponed_to_access_phase_end),
     335             :       NULL },
     336             : 
     337             :     /* content_by_lua_file rel/or/abs/path/to/script */
     338             :     { ngx_string("content_by_lua_file"),
     339             :       NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
     340             :       ngx_http_lua_content_by_lua,
     341             :       NGX_HTTP_LOC_CONF_OFFSET,
     342             :       0,
     343             :       (void *) ngx_http_lua_content_handler_file },
     344             : 
     345             :     { ngx_string("log_by_lua_file"),
     346             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     347             :                         |NGX_CONF_TAKE1,
     348             :       ngx_http_lua_log_by_lua,
     349             :       NGX_HTTP_LOC_CONF_OFFSET,
     350             :       0,
     351             :       (void *) ngx_http_lua_log_handler_file },
     352             : 
     353             :     /* header_filter_by_lua <inline script> */
     354             :     { ngx_string("header_filter_by_lua"),
     355             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     356             :                         |NGX_CONF_TAKE1,
     357             :       ngx_http_lua_header_filter_by_lua,
     358             :       NGX_HTTP_LOC_CONF_OFFSET,
     359             :       0,
     360             :       (void *) ngx_http_lua_header_filter_inline },
     361             : 
     362             :     /* header_filter_by_lua_block { <inline script> } */
     363             :     { ngx_string("header_filter_by_lua_block"),
     364             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     365             :                         |NGX_CONF_BLOCK|NGX_CONF_NOARGS,
     366             :       ngx_http_lua_header_filter_by_lua_block,
     367             :       NGX_HTTP_LOC_CONF_OFFSET,
     368             :       0,
     369             :       (void *) ngx_http_lua_header_filter_inline },
     370             : 
     371             :     { ngx_string("header_filter_by_lua_file"),
     372             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     373             :                         |NGX_CONF_TAKE1,
     374             :       ngx_http_lua_header_filter_by_lua,
     375             :       NGX_HTTP_LOC_CONF_OFFSET,
     376             :       0,
     377             :       (void *) ngx_http_lua_header_filter_file },
     378             : 
     379             :     { ngx_string("body_filter_by_lua"),
     380             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     381             :                         |NGX_CONF_TAKE1,
     382             :       ngx_http_lua_body_filter_by_lua,
     383             :       NGX_HTTP_LOC_CONF_OFFSET,
     384             :       0,
     385             :       (void *) ngx_http_lua_body_filter_inline },
     386             : 
     387             :     /* body_filter_by_lua_block { <inline script> } */
     388             :     { ngx_string("body_filter_by_lua_block"),
     389             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     390             :                         |NGX_CONF_BLOCK|NGX_CONF_NOARGS,
     391             :       ngx_http_lua_body_filter_by_lua_block,
     392             :       NGX_HTTP_LOC_CONF_OFFSET,
     393             :       0,
     394             :       (void *) ngx_http_lua_body_filter_inline },
     395             : 
     396             :     { ngx_string("body_filter_by_lua_file"),
     397             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     398             :                         |NGX_CONF_TAKE1,
     399             :       ngx_http_lua_body_filter_by_lua,
     400             :       NGX_HTTP_LOC_CONF_OFFSET,
     401             :       0,
     402             :       (void *) ngx_http_lua_body_filter_file },
     403             : 
     404             :     { ngx_string("balancer_by_lua_block"),
     405             :       NGX_HTTP_UPS_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
     406             :       ngx_http_lua_balancer_by_lua_block,
     407             :       NGX_HTTP_SRV_CONF_OFFSET,
     408             :       0,
     409             :       (void *) ngx_http_lua_balancer_handler_inline },
     410             : 
     411             :     { ngx_string("balancer_by_lua_file"),
     412             :       NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
     413             :       ngx_http_lua_balancer_by_lua,
     414             :       NGX_HTTP_SRV_CONF_OFFSET,
     415             :       0,
     416             :       (void *) ngx_http_lua_balancer_handler_file },
     417             : 
     418             :     { ngx_string("lua_socket_keepalive_timeout"),
     419             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
     420             :           |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
     421             :       ngx_conf_set_msec_slot,
     422             :       NGX_HTTP_LOC_CONF_OFFSET,
     423             :       offsetof(ngx_http_lua_loc_conf_t, keepalive_timeout),
     424             :       NULL },
     425             : 
     426             :     { ngx_string("lua_socket_connect_timeout"),
     427             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
     428             :           |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
     429             :       ngx_conf_set_msec_slot,
     430             :       NGX_HTTP_LOC_CONF_OFFSET,
     431             :       offsetof(ngx_http_lua_loc_conf_t, connect_timeout),
     432             :       NULL },
     433             : 
     434             :     { ngx_string("lua_socket_send_timeout"),
     435             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
     436             :           |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
     437             :       ngx_conf_set_msec_slot,
     438             :       NGX_HTTP_LOC_CONF_OFFSET,
     439             :       offsetof(ngx_http_lua_loc_conf_t, send_timeout),
     440             :       NULL },
     441             : 
     442             :     { ngx_string("lua_socket_send_lowat"),
     443             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
     444             :           |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
     445             :       ngx_conf_set_size_slot,
     446             :       NGX_HTTP_LOC_CONF_OFFSET,
     447             :       offsetof(ngx_http_lua_loc_conf_t, send_lowat),
     448             :       &ngx_http_lua_lowat_post },
     449             : 
     450             :     { ngx_string("lua_socket_buffer_size"),
     451             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
     452             :           |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
     453             :       ngx_conf_set_size_slot,
     454             :       NGX_HTTP_LOC_CONF_OFFSET,
     455             :       offsetof(ngx_http_lua_loc_conf_t, buffer_size),
     456             :       NULL },
     457             : 
     458             :     { ngx_string("lua_socket_pool_size"),
     459             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
     460             :                         |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
     461             :       ngx_conf_set_num_slot,
     462             :       NGX_HTTP_LOC_CONF_OFFSET,
     463             :       offsetof(ngx_http_lua_loc_conf_t, pool_size),
     464             :       NULL },
     465             : 
     466             :     { ngx_string("lua_socket_read_timeout"),
     467             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
     468             :           |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
     469             :       ngx_conf_set_msec_slot,
     470             :       NGX_HTTP_LOC_CONF_OFFSET,
     471             :       offsetof(ngx_http_lua_loc_conf_t, read_timeout),
     472             :       NULL },
     473             : 
     474             :     { ngx_string("lua_http10_buffering"),
     475             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     476             :                         |NGX_CONF_FLAG,
     477             :       ngx_conf_set_flag_slot,
     478             :       NGX_HTTP_LOC_CONF_OFFSET,
     479             :       offsetof(ngx_http_lua_loc_conf_t, http10_buffering),
     480             :       NULL },
     481             : 
     482             :     { ngx_string("lua_check_client_abort"),
     483             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     484             :                         |NGX_CONF_FLAG,
     485             :       ngx_conf_set_flag_slot,
     486             :       NGX_HTTP_LOC_CONF_OFFSET,
     487             :       offsetof(ngx_http_lua_loc_conf_t, check_client_abort),
     488             :       NULL },
     489             : 
     490             :     { ngx_string("lua_use_default_type"),
     491             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
     492             :                         |NGX_CONF_FLAG,
     493             :       ngx_conf_set_flag_slot,
     494             :       NGX_HTTP_LOC_CONF_OFFSET,
     495             :       offsetof(ngx_http_lua_loc_conf_t, use_default_type),
     496             :       NULL },
     497             : 
     498             : #if (NGX_HTTP_SSL)
     499             : 
     500             : #   if defined(nginx_version) && nginx_version >= 1001013
     501             : 
     502             :     { ngx_string("lua_ssl_protocols"),
     503             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
     504             :       ngx_conf_set_bitmask_slot,
     505             :       NGX_HTTP_LOC_CONF_OFFSET,
     506             :       offsetof(ngx_http_lua_loc_conf_t, ssl_protocols),
     507             :       &ngx_http_lua_ssl_protocols },
     508             : 
     509             : #   endif
     510             : 
     511             :     { ngx_string("lua_ssl_ciphers"),
     512             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
     513             :       ngx_conf_set_str_slot,
     514             :       NGX_HTTP_LOC_CONF_OFFSET,
     515             :       offsetof(ngx_http_lua_loc_conf_t, ssl_ciphers),
     516             :       NULL },
     517             : 
     518             :     { ngx_string("ssl_certificate_by_lua_block"),
     519             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
     520             :       ngx_http_lua_ssl_cert_by_lua_block,
     521             :       NGX_HTTP_SRV_CONF_OFFSET,
     522             :       0,
     523             :       (void *) ngx_http_lua_ssl_cert_handler_inline },
     524             : 
     525             :     { ngx_string("ssl_certificate_by_lua_file"),
     526             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
     527             :       ngx_http_lua_ssl_cert_by_lua,
     528             :       NGX_HTTP_SRV_CONF_OFFSET,
     529             :       0,
     530             :       (void *) ngx_http_lua_ssl_cert_handler_file },
     531             : 
     532             :     { ngx_string("ssl_session_store_by_lua_block"),
     533             :       NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
     534             :       ngx_http_lua_ssl_sess_store_by_lua_block,
     535             :       NGX_HTTP_SRV_CONF_OFFSET,
     536             :       0,
     537             :       (void *) ngx_http_lua_ssl_sess_store_handler_inline },
     538             : 
     539             :     { ngx_string("ssl_session_store_by_lua_file"),
     540             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
     541             :       ngx_http_lua_ssl_sess_store_by_lua,
     542             :       NGX_HTTP_SRV_CONF_OFFSET,
     543             :       0,
     544             :       (void *) ngx_http_lua_ssl_sess_store_handler_file },
     545             : 
     546             :     { ngx_string("ssl_session_fetch_by_lua_block"),
     547             :       NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
     548             :       ngx_http_lua_ssl_sess_fetch_by_lua_block,
     549             :       NGX_HTTP_SRV_CONF_OFFSET,
     550             :       0,
     551             :       (void *) ngx_http_lua_ssl_sess_fetch_handler_inline },
     552             : 
     553             :     { ngx_string("ssl_session_fetch_by_lua_file"),
     554             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
     555             :       ngx_http_lua_ssl_sess_fetch_by_lua,
     556             :       NGX_HTTP_SRV_CONF_OFFSET,
     557             :       0,
     558             :       (void *) ngx_http_lua_ssl_sess_fetch_handler_file },
     559             : 
     560             :     { ngx_string("lua_ssl_verify_depth"),
     561             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
     562             :       ngx_conf_set_num_slot,
     563             :       NGX_HTTP_LOC_CONF_OFFSET,
     564             :       offsetof(ngx_http_lua_loc_conf_t, ssl_verify_depth),
     565             :       NULL },
     566             : 
     567             :     { ngx_string("lua_ssl_trusted_certificate"),
     568             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
     569             :       ngx_conf_set_str_slot,
     570             :       NGX_HTTP_LOC_CONF_OFFSET,
     571             :       offsetof(ngx_http_lua_loc_conf_t, ssl_trusted_certificate),
     572             :       NULL },
     573             : 
     574             :     { ngx_string("lua_ssl_crl"),
     575             :       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
     576             :       ngx_conf_set_str_slot,
     577             :       NGX_HTTP_LOC_CONF_OFFSET,
     578             :       offsetof(ngx_http_lua_loc_conf_t, ssl_crl),
     579             :       NULL },
     580             : 
     581             : #endif  /* NGX_HTTP_SSL */
     582             : 
     583             :      { ngx_string("lua_malloc_trim"),
     584             :       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
     585             :       ngx_http_lua_malloc_trim,
     586             :       NGX_HTTP_MAIN_CONF_OFFSET,
     587             :       0,
     588             :       NULL },
     589             : 
     590             :     ngx_null_command
     591             : };
     592             : 
     593             : 
     594             : ngx_http_module_t ngx_http_lua_module_ctx = {
     595             :     NULL,                             /*  preconfiguration */
     596             :     ngx_http_lua_init,                /*  postconfiguration */
     597             : 
     598             :     ngx_http_lua_create_main_conf,    /*  create main configuration */
     599             :     ngx_http_lua_init_main_conf,      /*  init main configuration */
     600             : 
     601             :     ngx_http_lua_create_srv_conf,     /*  create server configuration */
     602             :     ngx_http_lua_merge_srv_conf,      /*  merge server configuration */
     603             : 
     604             :     ngx_http_lua_create_loc_conf,     /*  create location configuration */
     605             :     ngx_http_lua_merge_loc_conf       /*  merge location configuration */
     606             : };
     607             : 
     608             : 
     609             : ngx_module_t ngx_http_lua_module = {
     610             :     NGX_MODULE_V1,
     611             :     &ngx_http_lua_module_ctx,   /*  module context */
     612             :     ngx_http_lua_cmds,          /*  module directives */
     613             :     NGX_HTTP_MODULE,            /*  module type */
     614             :     NULL,                       /*  init master */
     615             :     NULL,                       /*  init module */
     616             :     ngx_http_lua_init_worker,   /*  init process */
     617             :     NULL,                       /*  init thread */
     618             :     NULL,                       /*  exit thread */
     619             :     NULL,                       /*  exit process */
     620             :     NULL,                       /*  exit master */
     621             :     NGX_MODULE_V1_PADDING
     622             : };
     623             : 
     624             : 
     625             : static ngx_int_t
     626          18 : ngx_http_lua_init(ngx_conf_t *cf)
     627             : {
     628             :     int                         multi_http_blocks;
     629             :     ngx_int_t                   rc;
     630             :     ngx_array_t                *arr;
     631             :     ngx_http_handler_pt        *h;
     632             :     volatile ngx_cycle_t       *saved_cycle;
     633             :     ngx_http_core_main_conf_t  *cmcf;
     634             :     ngx_http_lua_main_conf_t   *lmcf;
     635             : #if !defined(NGX_LUA_NO_FFI_API) || nginx_version >= 1011011
     636             :     ngx_pool_cleanup_t         *cln;
     637             : #endif
     638             : 
     639          18 :     lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
     640             : 
     641          18 :     if (ngx_http_lua_prev_cycle != ngx_cycle) {
     642          18 :         ngx_http_lua_prev_cycle = ngx_cycle;
     643          18 :         multi_http_blocks = 0;
     644             : 
     645             :     } else {
     646           0 :         multi_http_blocks = 1;
     647             :     }
     648             : 
     649          18 :     if (multi_http_blocks || lmcf->requires_capture_filter) {
     650           1 :         rc = ngx_http_lua_capture_filter_init(cf);
     651           1 :         if (rc != NGX_OK) {
     652           0 :             return rc;
     653             :         }
     654             :     }
     655             : 
     656          18 :     if (lmcf->postponed_to_rewrite_phase_end == NGX_CONF_UNSET) {
     657          18 :         lmcf->postponed_to_rewrite_phase_end = 0;
     658             :     }
     659             : 
     660          18 :     if (lmcf->postponed_to_access_phase_end == NGX_CONF_UNSET) {
     661          18 :         lmcf->postponed_to_access_phase_end = 0;
     662             :     }
     663             : 
     664          18 :     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
     665             : 
     666          18 :     if (lmcf->requires_rewrite) {
     667           0 :         h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
     668           0 :         if (h == NULL) {
     669           0 :             return NGX_ERROR;
     670             :         }
     671             : 
     672           0 :         *h = ngx_http_lua_rewrite_handler;
     673             :     }
     674             : 
     675          18 :     if (lmcf->requires_access) {
     676           0 :         h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
     677           0 :         if (h == NULL) {
     678           0 :             return NGX_ERROR;
     679             :         }
     680             : 
     681           0 :         *h = ngx_http_lua_access_handler;
     682             :     }
     683             : 
     684             :     dd("requires log: %d", (int) lmcf->requires_log);
     685             : 
     686          18 :     if (lmcf->requires_log) {
     687           0 :         arr = &cmcf->phases[NGX_HTTP_LOG_PHASE].handlers;
     688           0 :         h = ngx_array_push(arr);
     689           0 :         if (h == NULL) {
     690           0 :             return NGX_ERROR;
     691             :         }
     692             : 
     693           0 :         if (arr->nelts > 1) {
     694           0 :             h = arr->elts;
     695           0 :             ngx_memmove(&h[1], h,
     696             :                         (arr->nelts - 1) * sizeof(ngx_http_handler_pt));
     697             :         }
     698             : 
     699           0 :         *h = ngx_http_lua_log_handler;
     700             :     }
     701             : 
     702          18 :     if (multi_http_blocks || lmcf->requires_header_filter) {
     703           0 :         rc = ngx_http_lua_header_filter_init();
     704           0 :         if (rc != NGX_OK) {
     705           0 :             return rc;
     706             :         }
     707             :     }
     708             : 
     709          18 :     if (multi_http_blocks || lmcf->requires_body_filter) {
     710           0 :         rc = ngx_http_lua_body_filter_init();
     711           0 :         if (rc != NGX_OK) {
     712           0 :             return rc;
     713             :         }
     714             :     }
     715             : 
     716             : #ifndef NGX_LUA_NO_FFI_API
     717             :     /* add the cleanup of semaphores after the lua_close */
     718          18 :     cln = ngx_pool_cleanup_add(cf->pool, 0);
     719          18 :     if (cln == NULL) {
     720           0 :         return NGX_ERROR;
     721             :     }
     722             : 
     723          18 :     cln->data = lmcf;
     724          18 :     cln->handler = ngx_http_lua_sema_mm_cleanup;
     725             : #endif
     726             : 
     727             : #if nginx_version >= 1011011
     728          18 :     cln = ngx_pool_cleanup_add(cf->pool, 0);
     729          18 :     if (cln == NULL) {
     730           0 :         return NGX_ERROR;
     731             :     }
     732             : 
     733          18 :     cln->data = lmcf;
     734          18 :     cln->handler = ngx_http_lua_ngx_raw_header_cleanup;
     735             : #endif
     736             : 
     737          18 :     if (lmcf->lua == NULL) {
     738             :         dd("initializing lua vm");
     739             : 
     740          18 :         ngx_http_lua_content_length_hash =
     741          18 :                                   ngx_http_lua_hash_literal("content-length");
     742          18 :         ngx_http_lua_location_hash = ngx_http_lua_hash_literal("location");
     743             : 
     744          18 :         lmcf->lua = ngx_http_lua_init_vm(NULL, cf->cycle, cf->pool, lmcf,
     745             :                                          cf->log, NULL);
     746          18 :         if (lmcf->lua == NULL) {
     747           0 :             ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
     748             :                                "failed to initialize Lua VM");
     749           0 :             return NGX_ERROR;
     750             :         }
     751             : 
     752          18 :         if (!lmcf->requires_shm && lmcf->init_handler) {
     753           0 :             saved_cycle = ngx_cycle;
     754           0 :             ngx_cycle = cf->cycle;
     755             : 
     756           0 :             rc = lmcf->init_handler(cf->log, lmcf, lmcf->lua);
     757             : 
     758           0 :             ngx_cycle = saved_cycle;
     759             : 
     760           0 :             if (rc != NGX_OK) {
     761             :                 /* an error happened */
     762           0 :                 return NGX_ERROR;
     763             :             }
     764             :         }
     765             : 
     766             :         dd("Lua VM initialized!");
     767             :     }
     768             : 
     769          18 :     return NGX_OK;
     770             : }
     771             : 
     772             : 
     773             : static char *
     774           0 : ngx_http_lua_lowat_check(ngx_conf_t *cf, void *post, void *data)
     775             : {
     776             : #if (NGX_FREEBSD)
     777             :     ssize_t *np = data;
     778             : 
     779             :     if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
     780             :         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
     781             :                            "\"lua_send_lowat\" must be less than %d "
     782             :                            "(sysctl net.inet.tcp.sendspace)",
     783             :                            ngx_freebsd_net_inet_tcp_sendspace);
     784             : 
     785             :         return NGX_CONF_ERROR;
     786             :     }
     787             : 
     788             : #elif !(NGX_HAVE_SO_SNDLOWAT)
     789           0 :     ssize_t *np = data;
     790             : 
     791           0 :     ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
     792             :                        "\"lua_send_lowat\" is not supported, ignored");
     793             : 
     794           0 :     *np = 0;
     795             : 
     796             : #endif
     797             : 
     798           0 :     return NGX_CONF_OK;
     799             : }
     800             : 
     801             : 
     802             : static void *
     803          18 : ngx_http_lua_create_main_conf(ngx_conf_t *cf)
     804             : {
     805             : #ifndef NGX_LUA_NO_FFI_API
     806             :     ngx_int_t       rc;
     807             : #endif
     808             : 
     809             :     ngx_http_lua_main_conf_t    *lmcf;
     810             : 
     811          18 :     lmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_lua_main_conf_t));
     812          18 :     if (lmcf == NULL) {
     813           0 :         return NULL;
     814             :     }
     815             : 
     816             :     /* set by ngx_pcalloc:
     817             :      *      lmcf->lua = NULL;
     818             :      *      lmcf->lua_path = { 0, NULL };
     819             :      *      lmcf->lua_cpath = { 0, NULL };
     820             :      *      lmcf->pending_timers = 0;
     821             :      *      lmcf->running_timers = 0;
     822             :      *      lmcf->watcher = NULL;
     823             :      *      lmcf->regex_cache_entries = 0;
     824             :      *      lmcf->jit_stack = NULL;
     825             :      *      lmcf->shm_zones = NULL;
     826             :      *      lmcf->init_handler = NULL;
     827             :      *      lmcf->init_src = { 0, NULL };
     828             :      *      lmcf->shm_zones_inited = 0;
     829             :      *      lmcf->shdict_zones = NULL;
     830             :      *      lmcf->preload_hooks = NULL;
     831             :      *      lmcf->requires_header_filter = 0;
     832             :      *      lmcf->requires_body_filter = 0;
     833             :      *      lmcf->requires_capture_filter = 0;
     834             :      *      lmcf->requires_rewrite = 0;
     835             :      *      lmcf->requires_access = 0;
     836             :      *      lmcf->requires_log = 0;
     837             :      *      lmcf->requires_shm = 0;
     838             :      */
     839             : 
     840          18 :     lmcf->pool = cf->pool;
     841          18 :     lmcf->max_pending_timers = NGX_CONF_UNSET;
     842          18 :     lmcf->max_running_timers = NGX_CONF_UNSET;
     843             : #if (NGX_PCRE)
     844          18 :     lmcf->regex_cache_max_entries = NGX_CONF_UNSET;
     845          18 :     lmcf->regex_match_limit = NGX_CONF_UNSET;
     846             : #endif
     847          18 :     lmcf->postponed_to_rewrite_phase_end = NGX_CONF_UNSET;
     848          18 :     lmcf->postponed_to_access_phase_end = NGX_CONF_UNSET;
     849             : 
     850             : #if (NGX_HTTP_LUA_HAVE_MALLOC_TRIM)
     851          18 :     lmcf->malloc_trim_cycle = NGX_CONF_UNSET_UINT;
     852             : #endif
     853             : 
     854             : #ifndef NGX_LUA_NO_FFI_API
     855          18 :     rc = ngx_http_lua_sema_mm_init(cf, lmcf);
     856          18 :     if (rc != NGX_OK) {
     857           0 :         return NULL;
     858             :     }
     859             : 
     860             :     dd("nginx Lua module main config structure initialized!");
     861             : #endif
     862             : 
     863          18 :     return lmcf;
     864             : }
     865             : 
     866             : 
     867             : static char *
     868          18 : ngx_http_lua_init_main_conf(ngx_conf_t *cf, void *conf)
     869             : {
     870          18 :     ngx_http_lua_main_conf_t *lmcf = conf;
     871             : 
     872             : #if (NGX_PCRE)
     873          18 :     if (lmcf->regex_cache_max_entries == NGX_CONF_UNSET) {
     874          18 :         lmcf->regex_cache_max_entries = 1024;
     875             :     }
     876             : 
     877          18 :     if (lmcf->regex_match_limit == NGX_CONF_UNSET) {
     878          18 :         lmcf->regex_match_limit = 0;
     879             :     }
     880             : #endif
     881             : 
     882          18 :     if (lmcf->max_pending_timers == NGX_CONF_UNSET) {
     883          18 :         lmcf->max_pending_timers = 1024;
     884             :     }
     885             : 
     886          18 :     if (lmcf->max_running_timers == NGX_CONF_UNSET) {
     887          18 :         lmcf->max_running_timers = 256;
     888             :     }
     889             : 
     890             : #if (NGX_HTTP_LUA_HAVE_MALLOC_TRIM)
     891          18 :     if (lmcf->malloc_trim_cycle == NGX_CONF_UNSET_UINT) {
     892          18 :         lmcf->malloc_trim_cycle = 1000;  /* number of reqs */
     893             :     }
     894             : #endif
     895             : 
     896          18 :     lmcf->cycle = cf->cycle;
     897             : 
     898          18 :     return NGX_CONF_OK;
     899             : }
     900             : 
     901             : 
     902             : static void *
     903          54 : ngx_http_lua_create_srv_conf(ngx_conf_t *cf)
     904             : {
     905             :     ngx_http_lua_srv_conf_t     *lscf;
     906             : 
     907          54 :     lscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_lua_srv_conf_t));
     908          54 :     if (lscf == NULL) {
     909           0 :         return NULL;
     910             :     }
     911             : 
     912             :     /* set by ngx_pcalloc:
     913             :      *      lscf->srv.ssl_cert_handler = NULL;
     914             :      *      lscf->srv.ssl_cert_src = { 0, NULL };
     915             :      *      lscf->srv.ssl_cert_src_key = NULL;
     916             :      *
     917             :      *      lscf->srv.ssl_session_store_handler = NULL;
     918             :      *      lscf->srv.ssl_session_store_src = { 0, NULL };
     919             :      *      lscf->srv.ssl_session_store_src_key = NULL;
     920             :      *
     921             :      *      lscf->srv.ssl_session_fetch_handler = NULL;
     922             :      *      lscf->srv.ssl_session_fetch_src = { 0, NULL };
     923             :      *      lscf->srv.ssl_session_fetch_src_key = NULL;
     924             :      *
     925             :      *      lscf->balancer.handler = NULL;
     926             :      *      lscf->balancer.src = { 0, NULL };
     927             :      *      lscf->balancer.src_key = NULL;
     928             :      */
     929             : 
     930          54 :     return lscf;
     931             : }
     932             : 
     933             : 
     934             : static char *
     935          36 : ngx_http_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
     936             : {
     937             : #if (NGX_HTTP_SSL)
     938             : 
     939          36 :     ngx_http_lua_srv_conf_t *prev = parent;
     940          36 :     ngx_http_lua_srv_conf_t *conf = child;
     941             :     ngx_http_ssl_srv_conf_t *sscf;
     942             : 
     943             :     dd("merge srv conf");
     944             : 
     945          36 :     if (conf->srv.ssl_cert_src.len == 0) {
     946          36 :         conf->srv.ssl_cert_src = prev->srv.ssl_cert_src;
     947          36 :         conf->srv.ssl_cert_src_key = prev->srv.ssl_cert_src_key;
     948          36 :         conf->srv.ssl_cert_handler = prev->srv.ssl_cert_handler;
     949             :     }
     950             : 
     951          36 :     if (conf->srv.ssl_cert_src.len) {
     952           0 :         sscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_ssl_module);
     953           0 :         if (sscf == NULL || sscf->ssl.ctx == NULL) {
     954           0 :             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
     955             :                           "no ssl configured for the server");
     956             : 
     957           0 :             return NGX_CONF_ERROR;
     958             :         }
     959             : 
     960             : #ifdef LIBRESSL_VERSION_NUMBER
     961             : 
     962             :         ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
     963             :                       "LibreSSL does not support ssl_certificate_by_lua*");
     964             :         return NGX_CONF_ERROR;
     965             : 
     966             : #else
     967             : 
     968             : #   if OPENSSL_VERSION_NUMBER >= 0x1000205fL
     969             : 
     970           0 :         SSL_CTX_set_cert_cb(sscf->ssl.ctx, ngx_http_lua_ssl_cert_handler, NULL);
     971             : 
     972             : #   else
     973             : 
     974             :         ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
     975             :                       "OpenSSL too old to support ssl_certificate_by_lua*");
     976             :         return NGX_CONF_ERROR;
     977             : 
     978             : #   endif
     979             : 
     980             : #endif
     981             :     }
     982             : 
     983          36 :     if (conf->srv.ssl_sess_store_src.len == 0) {
     984          36 :         conf->srv.ssl_sess_store_src = prev->srv.ssl_sess_store_src;
     985          36 :         conf->srv.ssl_sess_store_src_key = prev->srv.ssl_sess_store_src_key;
     986          36 :         conf->srv.ssl_sess_store_handler = prev->srv.ssl_sess_store_handler;
     987             :     }
     988             : 
     989          36 :     if (conf->srv.ssl_sess_store_src.len) {
     990           0 :         sscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_ssl_module);
     991           0 :         if (sscf && sscf->ssl.ctx) {
     992             : #ifdef LIBRESSL_VERSION_NUMBER
     993             :             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
     994             :                           "LibreSSL does not support "
     995             :                           "ssl_session_store_by_lua*");
     996             : 
     997             :             return NGX_CONF_ERROR;
     998             : #else
     999           0 :             SSL_CTX_sess_set_new_cb(sscf->ssl.ctx,
    1000             :                                     ngx_http_lua_ssl_sess_store_handler);
    1001             : #endif
    1002             :         }
    1003             :     }
    1004             : 
    1005          36 :     if (conf->srv.ssl_sess_fetch_src.len == 0) {
    1006          36 :         conf->srv.ssl_sess_fetch_src = prev->srv.ssl_sess_fetch_src;
    1007          36 :         conf->srv.ssl_sess_fetch_src_key = prev->srv.ssl_sess_fetch_src_key;
    1008          36 :         conf->srv.ssl_sess_fetch_handler = prev->srv.ssl_sess_fetch_handler;
    1009             :     }
    1010             : 
    1011          36 :     if (conf->srv.ssl_sess_fetch_src.len) {
    1012           0 :         sscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_ssl_module);
    1013           0 :         if (sscf && sscf->ssl.ctx) {
    1014             : #ifdef LIBRESSL_VERSION_NUMBER
    1015             :             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
    1016             :                           "LibreSSL does not support "
    1017             :                           "ssl_session_fetch_by_lua*");
    1018             : 
    1019             :             return NGX_CONF_ERROR;
    1020             : #else
    1021           0 :             SSL_CTX_sess_set_get_cb(sscf->ssl.ctx,
    1022             :                                     ngx_http_lua_ssl_sess_fetch_handler);
    1023             : #endif
    1024             :         }
    1025             :     }
    1026             : 
    1027             : #endif  /* NGX_HTTP_SSL */
    1028          36 :     return NGX_CONF_OK;
    1029             : }
    1030             : 
    1031             : 
    1032             : static void *
    1033         108 : ngx_http_lua_create_loc_conf(ngx_conf_t *cf)
    1034             : {
    1035             :     ngx_http_lua_loc_conf_t *conf;
    1036             : 
    1037         108 :     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_lua_loc_conf_t));
    1038         108 :     if (conf == NULL) {
    1039           0 :         return NULL;
    1040             :     }
    1041             : 
    1042             :     /* set by ngx_pcalloc:
    1043             :      *      conf->access_src  = {{ 0, NULL }, NULL, NULL, NULL};
    1044             :      *      conf->access_src_key = NULL
    1045             :      *      conf->rewrite_src = {{ 0, NULL }, NULL, NULL, NULL};
    1046             :      *      conf->rewrite_src_key = NULL
    1047             :      *      conf->rewrite_handler = NULL;
    1048             :      *
    1049             :      *      conf->content_src = {{ 0, NULL }, NULL, NULL, NULL};
    1050             :      *      conf->content_src_key = NULL
    1051             :      *      conf->content_handler = NULL;
    1052             :      *
    1053             :      *      conf->log_src = {{ 0, NULL }, NULL, NULL, NULL};
    1054             :      *      conf->log_src_key = NULL
    1055             :      *      conf->log_handler = NULL;
    1056             :      *
    1057             :      *      conf->header_filter_src = {{ 0, NULL }, NULL, NULL, NULL};
    1058             :      *      conf->header_filter_src_key = NULL
    1059             :      *      conf->header_filter_handler = NULL;
    1060             :      *
    1061             :      *      conf->body_filter_src = {{ 0, NULL }, NULL, NULL, NULL};
    1062             :      *      conf->body_filter_src_key = NULL
    1063             :      *      conf->body_filter_handler = NULL;
    1064             :      *
    1065             :      *      conf->ssl = 0;
    1066             :      *      conf->ssl_protocols = 0;
    1067             :      *      conf->ssl_ciphers = { 0, NULL };
    1068             :      *      conf->ssl_trusted_certificate = { 0, NULL };
    1069             :      *      conf->ssl_crl = { 0, NULL };
    1070             :      */
    1071             : 
    1072         108 :     conf->force_read_body    = NGX_CONF_UNSET;
    1073         108 :     conf->enable_code_cache  = NGX_CONF_UNSET;
    1074         108 :     conf->http10_buffering   = NGX_CONF_UNSET;
    1075         108 :     conf->check_client_abort = NGX_CONF_UNSET;
    1076         108 :     conf->use_default_type   = NGX_CONF_UNSET;
    1077             : 
    1078         108 :     conf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
    1079         108 :     conf->connect_timeout = NGX_CONF_UNSET_MSEC;
    1080         108 :     conf->send_timeout = NGX_CONF_UNSET_MSEC;
    1081         108 :     conf->read_timeout = NGX_CONF_UNSET_MSEC;
    1082         108 :     conf->send_lowat = NGX_CONF_UNSET_SIZE;
    1083         108 :     conf->buffer_size = NGX_CONF_UNSET_SIZE;
    1084         108 :     conf->pool_size = NGX_CONF_UNSET_UINT;
    1085             : 
    1086         108 :     conf->transform_underscores_in_resp_headers = NGX_CONF_UNSET;
    1087         108 :     conf->log_socket_errors = NGX_CONF_UNSET;
    1088             : 
    1089             : #if (NGX_HTTP_SSL)
    1090         108 :     conf->ssl_verify_depth = NGX_CONF_UNSET_UINT;
    1091             : #endif
    1092             : 
    1093         108 :     return conf;
    1094             : }
    1095             : 
    1096             : 
    1097             : static char *
    1098          90 : ngx_http_lua_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
    1099             : {
    1100          90 :     ngx_http_lua_loc_conf_t *prev = parent;
    1101          90 :     ngx_http_lua_loc_conf_t *conf = child;
    1102             : 
    1103          90 :     if (conf->rewrite_src.value.len == 0) {
    1104          90 :         conf->rewrite_src = prev->rewrite_src;
    1105          90 :         conf->rewrite_handler = prev->rewrite_handler;
    1106          90 :         conf->rewrite_src_key = prev->rewrite_src_key;
    1107          90 :         conf->rewrite_chunkname = prev->rewrite_chunkname;
    1108             :     }
    1109             : 
    1110          90 :     if (conf->access_src.value.len == 0) {
    1111          90 :         conf->access_src = prev->access_src;
    1112          90 :         conf->access_handler = prev->access_handler;
    1113          90 :         conf->access_src_key = prev->access_src_key;
    1114          90 :         conf->access_chunkname = prev->access_chunkname;
    1115             :     }
    1116             : 
    1117          90 :     if (conf->content_src.value.len == 0) {
    1118          89 :         conf->content_src = prev->content_src;
    1119          89 :         conf->content_handler = prev->content_handler;
    1120          89 :         conf->content_src_key = prev->content_src_key;
    1121          89 :         conf->content_chunkname = prev->content_chunkname;
    1122             :     }
    1123             : 
    1124          90 :     if (conf->log_src.value.len == 0) {
    1125          90 :         conf->log_src = prev->log_src;
    1126          90 :         conf->log_handler = prev->log_handler;
    1127          90 :         conf->log_src_key = prev->log_src_key;
    1128          90 :         conf->log_chunkname = prev->log_chunkname;
    1129             :     }
    1130             : 
    1131          90 :     if (conf->header_filter_src.value.len == 0) {
    1132          90 :         conf->header_filter_src = prev->header_filter_src;
    1133          90 :         conf->header_filter_handler = prev->header_filter_handler;
    1134          90 :         conf->header_filter_src_key = prev->header_filter_src_key;
    1135             :     }
    1136             : 
    1137          90 :     if (conf->body_filter_src.value.len == 0) {
    1138          90 :         conf->body_filter_src = prev->body_filter_src;
    1139          90 :         conf->body_filter_handler = prev->body_filter_handler;
    1140          90 :         conf->body_filter_src_key = prev->body_filter_src_key;
    1141             :     }
    1142             : 
    1143             : #if (NGX_HTTP_SSL)
    1144             : 
    1145             : #   if defined(nginx_version) && nginx_version >= 1001013
    1146             : 
    1147          90 :     ngx_conf_merge_bitmask_value(conf->ssl_protocols, prev->ssl_protocols,
    1148             :                                  (NGX_CONF_BITMASK_SET|NGX_SSL_SSLv3
    1149             :                                   |NGX_SSL_TLSv1|NGX_SSL_TLSv1_1
    1150             :                                   |NGX_SSL_TLSv1_2));
    1151             : 
    1152             : #   endif
    1153             : 
    1154          90 :     ngx_conf_merge_str_value(conf->ssl_ciphers, prev->ssl_ciphers,
    1155             :                              "DEFAULT");
    1156             : 
    1157          90 :     ngx_conf_merge_uint_value(conf->ssl_verify_depth,
    1158             :                               prev->ssl_verify_depth, 1);
    1159          90 :     ngx_conf_merge_str_value(conf->ssl_trusted_certificate,
    1160             :                              prev->ssl_trusted_certificate, "");
    1161          90 :     ngx_conf_merge_str_value(conf->ssl_crl, prev->ssl_crl, "");
    1162             : 
    1163          90 :     if (ngx_http_lua_set_ssl(cf, conf) != NGX_OK) {
    1164           0 :         return NGX_CONF_ERROR;
    1165             :     }
    1166             : 
    1167             : #endif
    1168             : 
    1169          90 :     ngx_conf_merge_value(conf->force_read_body, prev->force_read_body, 0);
    1170          90 :     ngx_conf_merge_value(conf->enable_code_cache, prev->enable_code_cache, 1);
    1171          90 :     ngx_conf_merge_value(conf->http10_buffering, prev->http10_buffering, 1);
    1172          90 :     ngx_conf_merge_value(conf->check_client_abort, prev->check_client_abort, 0);
    1173          90 :     ngx_conf_merge_value(conf->use_default_type, prev->use_default_type, 1);
    1174             : 
    1175          90 :     ngx_conf_merge_msec_value(conf->keepalive_timeout,
    1176             :                               prev->keepalive_timeout, 60000);
    1177             : 
    1178          90 :     ngx_conf_merge_msec_value(conf->connect_timeout,
    1179             :                               prev->connect_timeout, 60000);
    1180             : 
    1181          90 :     ngx_conf_merge_msec_value(conf->send_timeout,
    1182             :                               prev->send_timeout, 60000);
    1183             : 
    1184          90 :     ngx_conf_merge_msec_value(conf->read_timeout,
    1185             :                               prev->read_timeout, 60000);
    1186             : 
    1187          90 :     ngx_conf_merge_size_value(conf->send_lowat,
    1188             :                               prev->send_lowat, 0);
    1189             : 
    1190          90 :     ngx_conf_merge_size_value(conf->buffer_size,
    1191             :                               prev->buffer_size,
    1192             :                               (size_t) ngx_pagesize);
    1193             : 
    1194          90 :     ngx_conf_merge_uint_value(conf->pool_size, prev->pool_size, 30);
    1195             : 
    1196          90 :     ngx_conf_merge_value(conf->transform_underscores_in_resp_headers,
    1197             :                          prev->transform_underscores_in_resp_headers, 1);
    1198             : 
    1199          90 :     ngx_conf_merge_value(conf->log_socket_errors, prev->log_socket_errors, 1);
    1200             : 
    1201          90 :     return NGX_CONF_OK;
    1202             : }
    1203             : 
    1204             : 
    1205             : #if (NGX_HTTP_SSL)
    1206             : 
    1207             : static ngx_int_t
    1208          90 : ngx_http_lua_set_ssl(ngx_conf_t *cf, ngx_http_lua_loc_conf_t *llcf)
    1209             : {
    1210             :     ngx_pool_cleanup_t  *cln;
    1211             : 
    1212          90 :     llcf->ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
    1213          90 :     if (llcf->ssl == NULL) {
    1214           0 :         return NGX_ERROR;
    1215             :     }
    1216             : 
    1217          90 :     llcf->ssl->log = cf->log;
    1218             : 
    1219          90 :     if (ngx_ssl_create(llcf->ssl, llcf->ssl_protocols, NULL) != NGX_OK) {
    1220           0 :         return NGX_ERROR;
    1221             :     }
    1222             : 
    1223          90 :     cln = ngx_pool_cleanup_add(cf->pool, 0);
    1224          90 :     if (cln == NULL) {
    1225           0 :         return NGX_ERROR;
    1226             :     }
    1227             : 
    1228          90 :     cln->handler = ngx_ssl_cleanup_ctx;
    1229          90 :     cln->data = llcf->ssl;
    1230             : 
    1231          90 :     if (SSL_CTX_set_cipher_list(llcf->ssl->ctx,
    1232          90 :                                 (const char *) llcf->ssl_ciphers.data)
    1233             :         == 0)
    1234             :     {
    1235           0 :         ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0,
    1236             :                       "SSL_CTX_set_cipher_list(\"%V\") failed",
    1237             :                       &llcf->ssl_ciphers);
    1238           0 :         return NGX_ERROR;
    1239             :     }
    1240             : 
    1241          90 :     if (llcf->ssl_trusted_certificate.len) {
    1242             : 
    1243             : #if defined(nginx_version) && nginx_version >= 1003007
    1244             : 
    1245           0 :         if (ngx_ssl_trusted_certificate(cf, llcf->ssl,
    1246             :                                         &llcf->ssl_trusted_certificate,
    1247           0 :                                         llcf->ssl_verify_depth)
    1248             :             != NGX_OK)
    1249             :         {
    1250           0 :             return NGX_ERROR;
    1251             :         }
    1252             : 
    1253             : #else
    1254             : 
    1255             :         ngx_log_error(NGX_LOG_CRIT, cf->log, 0, "at least nginx 1.3.7 is "
    1256             :                       "required for the \"lua_ssl_trusted_certificate\" "
    1257             :                       "directive");
    1258             :         return NGX_ERROR;
    1259             : 
    1260             : #endif
    1261             :     }
    1262             : 
    1263             :     dd("ssl crl: %.*s", (int) llcf->ssl_crl.len, llcf->ssl_crl.data);
    1264             : 
    1265          90 :     if (ngx_ssl_crl(cf, llcf->ssl, &llcf->ssl_crl) != NGX_OK) {
    1266           0 :         return NGX_ERROR;
    1267             :     }
    1268             : 
    1269          90 :     return NGX_OK;
    1270             : }
    1271             : 
    1272             : #endif  /* NGX_HTTP_SSL */
    1273             : 
    1274             : 
    1275             : static char *
    1276           0 : ngx_http_lua_malloc_trim(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
    1277             : {
    1278             : #if (NGX_HTTP_LUA_HAVE_MALLOC_TRIM)
    1279             : 
    1280             :     ngx_int_t       nreqs;
    1281             :     ngx_str_t      *value;
    1282             : 
    1283           0 :     ngx_http_lua_main_conf_t    *lmcf = conf;
    1284             : 
    1285           0 :     value = cf->args->elts;
    1286             : 
    1287           0 :     nreqs = ngx_atoi(value[1].data, value[1].len);
    1288           0 :     if (nreqs == NGX_ERROR) {
    1289           0 :         return "invalid number in the 1st argument";
    1290             :     }
    1291             : 
    1292           0 :     lmcf->malloc_trim_cycle = (ngx_uint_t) nreqs;
    1293             : 
    1294           0 :     if (nreqs == 0) {
    1295           0 :         return NGX_CONF_OK;
    1296             :     }
    1297             : 
    1298           0 :     lmcf->requires_log = 1;
    1299             : 
    1300             : #else
    1301             : 
    1302             :     ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "lua_malloc_trim is not supported "
    1303             :                        "on this platform, ignored");
    1304             : 
    1305             : #endif
    1306           0 :     return NGX_CONF_OK;
    1307             : }
    1308             : 
    1309             : /* vi:set ft=c ts=4 sw=4 et fdm=marker: */

Generated by: LCOV version 1.13