text: /* vim:set ft=c ts=4 sw=4 et fdm=marker: */
  
  #include "ngx_http_lua_directive.h"
  #include "ngx_http_lua_conf.h"
  #include "ngx_http_lua_capturefilter.h"
  #include "ngx_http_lua_contentby.h"
  #include "ngx_http_lua_rewriteby.h"
  #include "ngx_http_lua_accessby.h"
  #include "ngx_http_lua_headerfilterby.h"
  
  
  #if !defined(nginx_version) || nginx_version < 8054
  #error "at least nginx 0.8.54 is required"
  #endif
  
  
  static ngx_int_t ngx_http_lua_init(ngx_conf_t *cf);
  static ngx_int_t ngx_http_lua_pre_config(ngx_conf_t *cf);
  static char * ngx_http_lua_lowat_check(ngx_conf_t *cf, void *post, void *data);
  
  
  static ngx_conf_post_t  ngx_http_lua_lowat_post =
      { ngx_http_lua_lowat_check };
  
  
  static ngx_command_t ngx_http_lua_cmds[] = {
  
      { ngx_string("lua_shared_dict"),
        NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2,
        ngx_http_lua_shared_dict,
        0,
        0,
        NULL },
  
  #if (NGX_PCRE)
      { ngx_string("lua_regex_cache_max_entries"),
        NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
        ngx_conf_set_num_slot,
        NGX_HTTP_MAIN_CONF_OFFSET,
        offsetof(ngx_http_lua_main_conf_t, regex_cache_max_entries),
        NULL },
  #endif
  
      { ngx_string("lua_package_cpath"),
        NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
        ngx_http_lua_package_cpath,
        NGX_HTTP_MAIN_CONF_OFFSET,
        0,
        NULL },
  
      { ngx_string("lua_package_path"),
        NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
        ngx_http_lua_package_path,
        NGX_HTTP_MAIN_CONF_OFFSET,
        0,
        NULL },
  
      { ngx_string("lua_code_cache"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
                          |NGX_CONF_FLAG,
        ngx_http_lua_code_cache,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_lua_loc_conf_t, enable_code_cache),
        NULL },
  
      { ngx_string("lua_need_request_body"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
                          |NGX_CONF_FLAG,
        ngx_conf_set_flag_slot,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_lua_loc_conf_t, force_read_body),
        NULL },
  
  #if defined(NDK) && NDK
      /* set_by_lua $res <inline script> [$arg1 [$arg2 [...]]] */
      { ngx_string("set_by_lua"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF
                          |NGX_HTTP_LIF_CONF|NGX_CONF_2MORE,
        ngx_http_lua_set_by_lua,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        ngx_http_lua_filter_set_by_lua_inline },
  
      /* set_by_lua_file $res rel/or/abs/path/to/script [$arg1 [$arg2 [..]]] */
      { ngx_string("set_by_lua_file"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF
                          |NGX_HTTP_LIF_CONF|NGX_CONF_2MORE,
        ngx_http_lua_set_by_lua_file,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        ngx_http_lua_filter_set_by_lua_file },
  #endif
  
      /* rewrite_by_lua <inline script> */
      { ngx_string("rewrite_by_lua"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
                          |NGX_CONF_TAKE1,
        ngx_http_lua_rewrite_by_lua,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        ngx_http_lua_rewrite_handler_inline },
  
      /* access_by_lua <inline script> */
      { ngx_string("access_by_lua"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
                          |NGX_CONF_TAKE1,
        ngx_http_lua_access_by_lua,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        ngx_http_lua_access_handler_inline },
  
      /* content_by_lua <inline script> */
      { ngx_string("content_by_lua"),
        NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
        ngx_http_lua_content_by_lua,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        ngx_http_lua_content_handler_inline },
  
      /* header_filter_by_lua <inline script> */
      { ngx_string("header_filter_by_lua"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
                          |NGX_CONF_TAKE1,
        ngx_http_lua_header_filter_by_lua,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        ngx_http_lua_header_filter_inline },
  
      { ngx_string("rewrite_by_lua_file"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
                          |NGX_CONF_TAKE1,
        ngx_http_lua_rewrite_by_lua,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        ngx_http_lua_rewrite_handler_file },
  
      { ngx_string("rewrite_by_lua_no_postpone"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
                          |NGX_CONF_FLAG,
        ngx_conf_set_flag_slot,
        NGX_HTTP_MAIN_CONF_OFFSET,
        offsetof(ngx_http_lua_main_conf_t, postponed_to_rewrite_phase_end),
        NULL },
  
      { ngx_string("access_by_lua_file"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
                          |NGX_CONF_TAKE1,
        ngx_http_lua_access_by_lua,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        ngx_http_lua_access_handler_file },
  
      /* content_by_lua_file rel/or/abs/path/to/script */
      { ngx_string("content_by_lua_file"),
        NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
        ngx_http_lua_content_by_lua,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        ngx_http_lua_content_handler_file },
  
      { ngx_string("header_filter_by_lua_file"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
                          |NGX_CONF_TAKE1,
        ngx_http_lua_header_filter_by_lua,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        ngx_http_lua_header_filter_file },
  
      { ngx_string("lua_socket_keepalive_timeout"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
            |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
        ngx_conf_set_msec_slot,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_lua_loc_conf_t, keepalive_timeout),
        NULL },
  
      { ngx_string("lua_socket_connect_timeout"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
            |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
        ngx_conf_set_msec_slot,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_lua_loc_conf_t, connect_timeout),
        NULL },
  
      { ngx_string("lua_socket_send_timeout"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
            |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
        ngx_conf_set_msec_slot,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_lua_loc_conf_t, send_timeout),
        NULL },
  
      { ngx_string("lua_socket_send_lowat"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
            |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
        ngx_conf_set_size_slot,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_lua_loc_conf_t, send_lowat),
        &ngx_http_lua_lowat_post },
  
      { ngx_string("lua_socket_buffer_size"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
            |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
        ngx_conf_set_size_slot,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_lua_loc_conf_t, buffer_size),
        NULL },
  
      { ngx_string("lua_socket_pool_size"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
                          |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
        ngx_conf_set_num_slot,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_lua_loc_conf_t, pool_size),
        NULL },
  
      { ngx_string("lua_socket_read_timeout"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
            |NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
        ngx_conf_set_msec_slot,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_lua_loc_conf_t, read_timeout),
        NULL },
  
      { ngx_string("lua_http10_buffering"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
                          |NGX_CONF_FLAG,
        ngx_conf_set_flag_slot,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_lua_loc_conf_t, http10_buffering),
        NULL },
  
      ngx_null_command
  };
  
  ngx_http_module_t ngx_http_lua_module_ctx = {
      ngx_http_lua_pre_config,          /*  preconfiguration */
      ngx_http_lua_init,                /*  postconfiguration */
  
      ngx_http_lua_create_main_conf,    /*  create main configuration */
      ngx_http_lua_init_main_conf,      /*  init main configuration */
  
      NULL,                             /*  create server configuration */
      NULL,                             /*  merge server configuration */
  
      ngx_http_lua_create_loc_conf,     /*  create location configuration */
      ngx_http_lua_merge_loc_conf       /*  merge location configuration */
  };
  
  
  ngx_module_t ngx_http_lua_module = {
      NGX_MODULE_V1,
      &ngx_http_lua_module_ctx,   /*  module context */
      ngx_http_lua_cmds,          /*  module directives */
      NGX_HTTP_MODULE,            /*  module type */
      NULL,                       /*  init master */
      NULL,                       /*  init module */
      NULL,                       /*  init process */
      NULL,                       /*  init thread */
      NULL,                       /*  exit thread */
      NULL,                       /*  exit process */
      NULL,                       /*  exit master */
      NGX_MODULE_V1_PADDING
  };
  
  
  static ngx_int_t
  ngx_http_lua_init(ngx_conf_t *cf)
  {
      ngx_int_t                   rc;
      ngx_http_handler_pt        *h;
      ngx_http_core_main_conf_t  *cmcf;
      ngx_http_lua_main_conf_t   *lmcf;
  
      rc = ngx_http_lua_capture_filter_init(cf);
      if (rc != NGX_OK) {
          return rc;
      }
  
      lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
  
      if (lmcf->postponed_to_rewrite_phase_end == NGX_CONF_UNSET) {
          lmcf->postponed_to_rewrite_phase_end = 0;
      }
  
      cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
  
      if (ngx_http_lua_requires_rewrite) {
          h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
          if (h == NULL) {
              return NGX_ERROR;
          }
  
          *h = ngx_http_lua_rewrite_handler;
      }
  
      if (ngx_http_lua_requires_access) {
          h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
          if (h == NULL) {
              return NGX_ERROR;
          }
  
          *h = ngx_http_lua_access_handler;
      }
  
      if (ngx_http_lua_requires_header_filter) {
          rc = ngx_http_lua_header_filter_init();
          if (rc != NGX_OK) {
              return rc;
          }
      }
  
      return NGX_OK;
  }
  
  
  static ngx_int_t
  ngx_http_lua_pre_config(ngx_conf_t *cf)
  {
      ngx_http_lua_requires_rewrite = 0;
      ngx_http_lua_requires_access = 0;
      ngx_http_lua_requires_header_filter = 0;
      ngx_http_lua_requires_capture_filter = 0;
  
      return NGX_OK;
  }
  
  
  static char *
  ngx_http_lua_lowat_check(ngx_conf_t *cf, void *post, void *data)
  {
  #if (NGX_FREEBSD)
      ssize_t *np = data;
  
      if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
          ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                             "\"fastcgi_send_lowat\" must be less than %d "
                             "(sysctl net.inet.tcp.sendspace)",
                             ngx_freebsd_net_inet_tcp_sendspace);
  
          return NGX_CONF_ERROR;
      }
  
  #elif !(NGX_HAVE_SO_SNDLOWAT)
      ssize_t *np = data;
  
      ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
                         "\"fastcgi_send_lowat\" is not supported, ignored");
  
      *np = 0;
  
  #endif
  
      return NGX_CONF_OK;
  }
  
  