|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright (C) Yichun Zhang (agentzh) |
| 4 | + * |
| 5 | + * Author: Thibault Charbonnier (thibaultcha) |
| 6 | + * I hereby assign copyright in this code to the lua-nginx-module project, |
| 7 | + * to be licensed under the same terms as the rest of the code. |
| 8 | + */ |
| 9 | + |
| 10 | + |
| 11 | +#ifndef DDEBUG |
| 12 | +#define DDEBUG 0 |
| 13 | +#endif |
| 14 | + |
| 15 | + |
| 16 | +#include "ddebug.h" |
| 17 | +#include "ngx_http_lua_configureby.h" |
| 18 | +#include "ngx_http_lua_directive.h" |
| 19 | +#include "ngx_http_lua_util.h" |
| 20 | +#include "ngx_http_lua_shdict.h" |
| 21 | +#include "ngx_http_lua_api.h" |
| 22 | + |
| 23 | + |
| 24 | +static ngx_conf_t *cfp; |
| 25 | + |
| 26 | + |
| 27 | +char * |
| 28 | +ngx_http_lua_configure_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, |
| 29 | + void *conf) |
| 30 | +{ |
| 31 | + char *rv; |
| 32 | + ngx_conf_t save; |
| 33 | + |
| 34 | + save = *cf; |
| 35 | + cf->handler = ngx_http_lua_configure_by_lua; |
| 36 | + cf->handler_conf = conf; |
| 37 | + |
| 38 | + rv = ngx_http_lua_conf_lua_block_parse(cf, cmd); |
| 39 | + |
| 40 | + *cf = save; |
| 41 | + |
| 42 | + return rv; |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +char * |
| 47 | +ngx_http_lua_configure_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, |
| 48 | + void *conf) |
| 49 | +{ |
| 50 | + u_char *name; |
| 51 | + ngx_str_t *value; |
| 52 | + ngx_http_lua_main_conf_t *lmcf = conf; |
| 53 | + |
| 54 | + if (cmd->post == NULL) { |
| 55 | + return NGX_CONF_ERROR; |
| 56 | + } |
| 57 | + |
| 58 | + if (lmcf->configure_handler) { |
| 59 | + return "is duplicate"; |
| 60 | + } |
| 61 | + |
| 62 | + value = cf->args->elts; |
| 63 | + |
| 64 | + if (value[1].len == 0) { |
| 65 | + ngx_conf_log_error(NGX_LOG_ERR, cf, 0, |
| 66 | + "invalid location config: no runnable Lua code"); |
| 67 | + return NGX_CONF_ERROR; |
| 68 | + } |
| 69 | + |
| 70 | + lmcf->configure_handler = (ngx_http_lua_configure_handler_pt) cmd->post; |
| 71 | + |
| 72 | + if (cmd->post == ngx_http_lua_configure_handler_file) { |
| 73 | + name = ngx_http_lua_rebase_path(cf->pool, value[1].data, |
| 74 | + value[1].len); |
| 75 | + if (name == NULL) { |
| 76 | + return NGX_CONF_ERROR; |
| 77 | + } |
| 78 | + |
| 79 | + lmcf->configure_src.data = name; |
| 80 | + lmcf->configure_src.len = ngx_strlen(name); |
| 81 | + |
| 82 | + } else { |
| 83 | + lmcf->configure_src = value[1]; |
| 84 | + } |
| 85 | + |
| 86 | + return NGX_CONF_OK; |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +ngx_int_t |
| 91 | +ngx_http_lua_configure_handler_inline(ngx_conf_t *cf, |
| 92 | + ngx_http_lua_main_conf_t *lmcf) |
| 93 | +{ |
| 94 | + int status; |
| 95 | + lua_State *L = lmcf->lua; |
| 96 | + |
| 97 | + cfp = cf; |
| 98 | + |
| 99 | + status = luaL_loadbuffer(L, (char *) lmcf->configure_src.data, |
| 100 | + lmcf->configure_src.len, "=configure_by_lua") |
| 101 | + || ngx_http_lua_do_call(cf->log, L); |
| 102 | + |
| 103 | + cfp = NULL; |
| 104 | + |
| 105 | + return ngx_http_lua_report(cf->log, L, status, "configure_by_lua"); |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +ngx_int_t |
| 110 | +ngx_http_lua_configure_handler_file(ngx_conf_t *cf, |
| 111 | + ngx_http_lua_main_conf_t *lmcf) |
| 112 | +{ |
| 113 | + int status; |
| 114 | + lua_State *L = lmcf->lua; |
| 115 | + |
| 116 | + cfp = cf; |
| 117 | + |
| 118 | + status = luaL_loadfile(L, (char *) lmcf->configure_src.data) |
| 119 | + || ngx_http_lua_do_call(cf->log, L); |
| 120 | + |
| 121 | + cfp = NULL; |
| 122 | + |
| 123 | + return ngx_http_lua_report(cf->log, L, status, "configure_by_lua_file"); |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +ngx_uint_t |
| 128 | +ngx_http_lua_is_configure_phase() |
| 129 | +{ |
| 130 | + return cfp != NULL; |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +#ifndef NGX_LUA_NO_FFI_API |
| 135 | +unsigned int |
| 136 | +ngx_http_lua_ffi_is_configure_phase() |
| 137 | +{ |
| 138 | + return ngx_http_lua_is_configure_phase(); |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +int |
| 143 | +ngx_http_lua_ffi_configure_shared_dict(ngx_str_t *name, ngx_str_t *size, |
| 144 | + u_char *errstr, size_t *err_len) |
| 145 | +{ |
| 146 | + ssize_t ssize; |
| 147 | + ngx_shm_zone_t **zp; |
| 148 | + ngx_shm_zone_t *zone; |
| 149 | + ngx_http_lua_shdict_ctx_t *ctx; |
| 150 | + ngx_http_lua_main_conf_t *lmcf; |
| 151 | + lua_State *L; |
| 152 | + ngx_conf_t *cf = cfp; |
| 153 | + |
| 154 | + lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); |
| 155 | + |
| 156 | + ssize = ngx_parse_size(size); |
| 157 | + if (ssize <= NGX_HTTP_LUA_SHDICT_MINSIZE) { |
| 158 | + *err_len = ngx_snprintf(errstr, *err_len, |
| 159 | + "invalid lua shared dict size \"%s\"", |
| 160 | + size->data) |
| 161 | + - errstr; |
| 162 | + return NGX_ERROR; |
| 163 | + } |
| 164 | + |
| 165 | + ctx = ngx_pcalloc(cf->cycle->pool, sizeof(ngx_http_lua_shdict_ctx_t)); |
| 166 | + if (ctx == NULL) { |
| 167 | + *err_len = ngx_snprintf(errstr, *err_len, "no memory") |
| 168 | + - errstr; |
| 169 | + return NGX_ERROR; |
| 170 | + } |
| 171 | + |
| 172 | + ctx->name = *name; |
| 173 | + ctx->main_conf = lmcf; |
| 174 | + ctx->log = &cf->cycle->new_log; |
| 175 | + |
| 176 | + zone = ngx_http_lua_shared_memory_add(cf, name, (size_t) ssize, |
| 177 | + &ngx_http_lua_module); |
| 178 | + if (zone == NULL) { |
| 179 | + *err_len = ngx_snprintf(errstr, *err_len, "no memory") |
| 180 | + - errstr; |
| 181 | + return NGX_ERROR; |
| 182 | + } |
| 183 | + |
| 184 | + if (zone->data) { |
| 185 | + return NGX_DECLINED; |
| 186 | + } |
| 187 | + |
| 188 | + zone->init = ngx_http_lua_shdict_init_zone; |
| 189 | + zone->data = ctx; |
| 190 | + |
| 191 | + if (lmcf->shdict_zones == NULL) { |
| 192 | + lmcf->shdict_zones = ngx_palloc(cf->pool, sizeof(ngx_array_t)); |
| 193 | + if (lmcf->shdict_zones == NULL) { |
| 194 | + *err_len = ngx_snprintf(errstr, *err_len, "no memory") |
| 195 | + - errstr; |
| 196 | + return NGX_ERROR; |
| 197 | + } |
| 198 | + |
| 199 | + if (ngx_array_init(lmcf->shdict_zones, cf->pool, 2, |
| 200 | + sizeof(ngx_shm_zone_t *)) |
| 201 | + != NGX_OK) |
| 202 | + { |
| 203 | + *err_len = ngx_snprintf(errstr, *err_len, "no memory") |
| 204 | + - errstr; |
| 205 | + return NGX_ERROR; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + zp = ngx_array_push(lmcf->shdict_zones); |
| 210 | + if (zp == NULL) { |
| 211 | + *err_len = ngx_snprintf(errstr, *err_len, "no memory") |
| 212 | + - errstr; |
| 213 | + return NGX_ERROR; |
| 214 | + } |
| 215 | + |
| 216 | + *zp = zone; |
| 217 | + |
| 218 | + L = lmcf->lua; |
| 219 | + |
| 220 | + lua_getglobal(L, "ngx"); |
| 221 | + lua_getfield(L, -1, "shared"); |
| 222 | + if (!lua_getmetatable(L, -1)) { |
| 223 | + ngx_http_lua_create_shdict_mt(L); /* ngx.shared shmt */ |
| 224 | + } |
| 225 | + |
| 226 | + ngx_http_lua_attach_shdict(L, name, zone); |
| 227 | + lua_pop(L, 2); /* pop: ngx.shared + shmt */ |
| 228 | + |
| 229 | + return NGX_OK; |
| 230 | +} |
| 231 | + |
| 232 | + |
| 233 | +void |
| 234 | +ngx_http_lua_ffi_configure_max_pending_timers(int n_timers) |
| 235 | +{ |
| 236 | + |
| 237 | + ngx_http_lua_main_conf_t *lmcf; |
| 238 | + ngx_conf_t *cf = cfp; |
| 239 | + |
| 240 | + lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); |
| 241 | + |
| 242 | + lmcf->max_pending_timers = (ngx_int_t) n_timers; |
| 243 | +} |
| 244 | + |
| 245 | + |
| 246 | +void |
| 247 | +ngx_http_lua_ffi_configure_max_running_timers(int n_timers) |
| 248 | +{ |
| 249 | + |
| 250 | + ngx_http_lua_main_conf_t *lmcf; |
| 251 | + ngx_conf_t *cf = cfp; |
| 252 | + |
| 253 | + lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module); |
| 254 | + |
| 255 | + lmcf->max_running_timers = (ngx_int_t) n_timers; |
| 256 | +} |
| 257 | + |
| 258 | + |
| 259 | +int |
| 260 | +ngx_http_lua_ffi_configure_env(u_char *value, size_t name_len, size_t len) |
| 261 | +{ |
| 262 | + ngx_core_conf_t *ccf; |
| 263 | + ngx_str_t *var; |
| 264 | + ngx_conf_t *cf = cfp; |
| 265 | + |
| 266 | + ccf = (ngx_core_conf_t *) ngx_get_conf(cf->cycle->conf_ctx, |
| 267 | + ngx_core_module); |
| 268 | + |
| 269 | + var = ngx_array_push(&ccf->env); |
| 270 | + if (var == NULL) { |
| 271 | + return NGX_ERROR; |
| 272 | + } |
| 273 | + |
| 274 | + var->data = ngx_pnalloc(cf->pool, len); |
| 275 | + if (var->data == NULL) { |
| 276 | + return NGX_ERROR; |
| 277 | + } |
| 278 | + |
| 279 | + (void) ngx_copy(var->data, value, len); |
| 280 | + var->len = name_len; |
| 281 | + |
| 282 | + return NGX_OK; |
| 283 | +} |
| 284 | +#endif |
| 285 | + |
| 286 | + |
| 287 | +/* vi:set ft=c ts=4 sw=4 et fdm=marker: */ |
0 commit comments