796 changes: 533 additions & 263 deletions README.markdown

Large diffs are not rendered by default.

430 changes: 208 additions & 222 deletions config

Large diffs are not rendered by default.

784 changes: 526 additions & 258 deletions doc/HttpLuaModule.wiki

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/api/ngx_http_lua_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/* Public API for other Nginx modules */


#define ngx_http_lua_version 10015
#define ngx_http_lua_version 10016


typedef struct {
Expand Down
32 changes: 16 additions & 16 deletions src/ddebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

# if (NGX_HAVE_VARIADIC_MACROS)

# define dd(...) fprintf(stderr, "lua *** %s: ", __func__); \
fprintf(stderr, __VA_ARGS__); \
# define dd(...) fprintf(stderr, "lua *** %s: ", __func__); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, " at %s line %d.\n", __FILE__, __LINE__)

# else
Expand Down Expand Up @@ -54,22 +54,22 @@ dd(const char *fmt, ...) {

#if defined(DDEBUG) && (DDEBUG)

#define dd_check_read_event_handler(r) \
dd("r->read_event_handler = %s", \
r->read_event_handler == ngx_http_block_reading ? \
"ngx_http_block_reading" : \
r->read_event_handler == ngx_http_test_reading ? \
"ngx_http_test_reading" : \
r->read_event_handler == ngx_http_request_empty_handler ? \
#define dd_check_read_event_handler(r) \
dd("r->read_event_handler = %s", \
r->read_event_handler == ngx_http_block_reading ? \
"ngx_http_block_reading" : \
r->read_event_handler == ngx_http_test_reading ? \
"ngx_http_test_reading" : \
r->read_event_handler == ngx_http_request_empty_handler ? \
"ngx_http_request_empty_handler" : "UNKNOWN")

#define dd_check_write_event_handler(r) \
dd("r->write_event_handler = %s", \
r->write_event_handler == ngx_http_handler ? \
"ngx_http_handler" : \
r->write_event_handler == ngx_http_core_run_phases ? \
"ngx_http_core_run_phases" : \
r->write_event_handler == ngx_http_request_empty_handler ? \
#define dd_check_write_event_handler(r) \
dd("r->write_event_handler = %s", \
r->write_event_handler == ngx_http_handler ? \
"ngx_http_handler" : \
r->write_event_handler == ngx_http_core_run_phases ? \
"ngx_http_core_run_phases" : \
r->write_event_handler == ngx_http_request_empty_handler ? \
"ngx_http_request_empty_handler" : "UNKNOWN")

#else
Expand Down
7 changes: 2 additions & 5 deletions src/ngx_http_lua_accessby.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,6 @@ ngx_http_lua_access_handler(ngx_http_request_t *r)
ngx_http_lua_generic_phase_post_read);

if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
#if (nginx_version < 1002006) || \
(nginx_version >= 1003000 && nginx_version < 1003009)
r->main->count--;
#endif

return rc;
}

Expand Down Expand Up @@ -179,6 +174,7 @@ ngx_http_lua_access_handler_inline(ngx_http_request_t *r)
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
llcf->access_src.value.data,
llcf->access_src.value.len,
&llcf->access_src_ref,
llcf->access_src_key,
(const char *) llcf->access_chunkname);

Expand Down Expand Up @@ -217,6 +213,7 @@ ngx_http_lua_access_handler_file(ngx_http_request_t *r)

/* load Lua script file (w/ cache) sp = 1 */
rc = ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
&llcf->access_src_ref,
llcf->access_src_key);
if (rc != NGX_OK) {
if (rc < NGX_HTTP_SPECIAL_RESPONSE) {
Expand Down
2 changes: 1 addition & 1 deletion src/ngx_http_lua_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ ngx_http_lua_shared_memory_init(ngx_shm_zone_t *shm_zone, void *data)
}

zone->shm = shm_zone->shm;
#if defined(nginx_version) && nginx_version >= 1009000
#if (nginx_version >= 1009000)
zone->noreuse = shm_zone->noreuse;
#endif

Expand Down
157 changes: 88 additions & 69 deletions src/ngx_http_lua_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,69 @@


static int ngx_http_lua_ngx_req_set_uri_args(lua_State *L);
static int ngx_http_lua_ngx_req_get_uri_args(lua_State *L);
static int ngx_http_lua_ngx_req_get_post_args(lua_State *L);


uintptr_t
ngx_http_lua_escape_args(u_char *dst, u_char *src, size_t size)
{
ngx_uint_t n;
static u_char hex[] = "0123456789ABCDEF";

/* %00-%20 %7F*/

static uint32_t escape[] = {
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */

/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0x00000001, /* 0000 0000 0000 0000 0000 0000 0000 0001 */

/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */

/* ~}| {zyx wvut srqp onml kjih gfed cba` */
0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */

0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
};

if (dst == NULL) {

/* find the number of the characters to be escaped */

n = 0;

while (size) {
if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
n++;
}
src++;
size--;
}

return (uintptr_t) n;
}

while (size) {
if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
*dst++ = '%';
*dst++ = hex[*src >> 4];
*dst++ = hex[*src & 0xf];
src++;

} else {
*dst++ = *src++;
}
size--;
}

return (uintptr_t) dst;
}


static int
ngx_http_lua_ngx_req_set_uri_args(lua_State *L)
{
Expand All @@ -28,6 +87,7 @@ ngx_http_lua_ngx_req_set_uri_args(lua_State *L)
const char *msg;
size_t len;
u_char *p;
uintptr_t escape;

if (lua_gettop(L) != 1) {
return luaL_error(L, "expecting 1 argument but seen %d",
Expand All @@ -43,7 +103,6 @@ ngx_http_lua_ngx_req_set_uri_args(lua_State *L)

switch (lua_type(L, 1)) {
case LUA_TNUMBER:
case LUA_TSTRING:
p = (u_char *) lua_tolstring(L, 1, &len);

args.data = ngx_palloc(r->pool, len);
Expand All @@ -56,6 +115,32 @@ ngx_http_lua_ngx_req_set_uri_args(lua_State *L)
args.len = len;
break;

case LUA_TSTRING:
p = (u_char *) lua_tolstring(L, 1, &len);

escape = ngx_http_lua_escape_args(NULL, p, len);
if (escape > 0) {
args.len = len + 2 * escape;
args.data = ngx_palloc(r->pool, args.len);
if (args.data == NULL) {
return NGX_ERROR;
}

ngx_http_lua_escape_args(args.data, p, len);

} else {
args.data = ngx_palloc(r->pool, len);
if (args.data == NULL) {
return luaL_error(L, "no memory");
}

ngx_memcpy(args.data, p, len);

args.len = len;
}

break;

case LUA_TTABLE:
ngx_http_lua_process_args_option(r, L, 1, &args);

Expand All @@ -65,7 +150,7 @@ ngx_http_lua_ngx_req_set_uri_args(lua_State *L)

default:
msg = lua_pushfstring(L, "string, number, or table expected, "
"but got %s", luaL_typename(L, 2));
"but got %s", luaL_typename(L, 1));
return luaL_argerror(L, 1, msg);
}

Expand All @@ -80,64 +165,6 @@ ngx_http_lua_ngx_req_set_uri_args(lua_State *L)
}


static int
ngx_http_lua_ngx_req_get_uri_args(lua_State *L)
{
ngx_http_request_t *r;
u_char *buf;
u_char *last;
int retval;
int n;
int max;

n = lua_gettop(L);

if (n != 0 && n != 1) {
return luaL_error(L, "expecting 0 or 1 arguments but seen %d", n);
}

if (n == 1) {
max = luaL_checkinteger(L, 1);
lua_pop(L, 1);

} else {
max = NGX_HTTP_LUA_MAX_ARGS;
}

r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request object found");
}

ngx_http_lua_check_fake_request(L, r);

if (r->args.len == 0) {
lua_createtable(L, 0, 0);
return 1;
}

/* we copy r->args over to buf to simplify
* unescaping query arg keys and values */

buf = ngx_palloc(r->pool, r->args.len);
if (buf == NULL) {
return luaL_error(L, "no memory");
}

lua_createtable(L, 0, 4);

ngx_memcpy(buf, r->args.data, r->args.len);

last = buf + r->args.len;

retval = ngx_http_lua_parse_args(L, buf, last, max);

ngx_pfree(r->pool, buf);

return retval;
}


static int
ngx_http_lua_ngx_req_get_post_args(lua_State *L)
{
Expand Down Expand Up @@ -368,18 +395,11 @@ ngx_http_lua_inject_req_args_api(lua_State *L)
lua_pushcfunction(L, ngx_http_lua_ngx_req_set_uri_args);
lua_setfield(L, -2, "set_uri_args");

lua_pushcfunction(L, ngx_http_lua_ngx_req_get_uri_args);
lua_setfield(L, -2, "get_uri_args");

lua_pushcfunction(L, ngx_http_lua_ngx_req_get_uri_args);
lua_setfield(L, -2, "get_query_args"); /* deprecated */

lua_pushcfunction(L, ngx_http_lua_ngx_req_get_post_args);
lua_setfield(L, -2, "get_post_args");
}


#ifndef NGX_LUA_NO_FFI_API
size_t
ngx_http_lua_ffi_req_get_querystring_len(ngx_http_request_t *r)
{
Expand Down Expand Up @@ -549,7 +569,6 @@ ngx_http_lua_ffi_req_get_uri_args(ngx_http_request_t *r, u_char *buf,

return i;
}
#endif /* NGX_LUA_NO_FFI_API */


/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
52 changes: 21 additions & 31 deletions src/ngx_http_lua_balancer.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ static ngx_int_t ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc,
void *data);
static ngx_int_t ngx_http_lua_balancer_by_chunk(lua_State *L,
ngx_http_request_t *r);
void ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
ngx_uint_t state);
static void ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc,
void *data, ngx_uint_t state);


ngx_int_t
Expand All @@ -66,6 +66,7 @@ ngx_http_lua_balancer_handler_file(ngx_http_request_t *r,

rc = ngx_http_lua_cache_loadfile(r->connection->log, L,
lscf->balancer.src.data,
&lscf->balancer.src_ref,
lscf->balancer.src_key);
if (rc != NGX_OK) {
return rc;
Expand All @@ -87,6 +88,7 @@ ngx_http_lua_balancer_handler_inline(ngx_http_request_t *r,
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
lscf->balancer.src.data,
lscf->balancer.src.len,
&lscf->balancer.src_ref,
lscf->balancer.src_key,
"=balancer_by_lua");
if (rc != NGX_OK) {
Expand Down Expand Up @@ -123,7 +125,7 @@ char *
ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
u_char *p;
u_char *cache_key = NULL;
u_char *name;
ngx_str_t *value;
ngx_http_lua_srv_conf_t *lscf = conf;
Expand All @@ -147,46 +149,35 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,

if (cmd->post == ngx_http_lua_balancer_handler_file) {
/* Lua code in an external file */

name = ngx_http_lua_rebase_path(cf->pool, value[1].data,
value[1].len);
if (name == NULL) {
return NGX_CONF_ERROR;
}

lscf->balancer.src.data = name;
lscf->balancer.src.len = ngx_strlen(name);

p = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
if (p == NULL) {
cache_key = ngx_http_lua_gen_file_cache_key(cf, value[1].data,
value[1].len);
if (cache_key == NULL) {
return NGX_CONF_ERROR;
}

lscf->balancer.src_key = p;

p = ngx_copy(p, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
*p = '\0';
lscf->balancer.src.data = name;
lscf->balancer.src.len = ngx_strlen(name);

} else {
/* inlined Lua code */

lscf->balancer.src = value[1];

p = ngx_palloc(cf->pool,
sizeof("balancer_by_lua") + NGX_HTTP_LUA_INLINE_KEY_LEN);
if (p == NULL) {
cache_key = ngx_http_lua_gen_chunk_cache_key(cf, "balancer_by_lua",
value[1].data,
value[1].len);
if (cache_key == NULL) {
return NGX_CONF_ERROR;
}

lscf->balancer.src_key = p;

p = ngx_copy(p, "balancer_by_lua", sizeof("balancer_by_lua") - 1);
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
p = ngx_http_lua_digest_hex(p, value[1].data, value[1].len);
*p = '\0';
/* Don't eval nginx variables for inline lua code */
lscf->balancer.src = value[1];
}

lscf->balancer.src_key = cache_key;

uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);

if (uscf->peer.init_upstream) {
Expand Down Expand Up @@ -408,7 +399,7 @@ ngx_http_lua_balancer_by_chunk(lua_State *L, ngx_http_request_t *r)
}


void
static void
ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
ngx_uint_t state)
{
Expand Down Expand Up @@ -466,8 +457,6 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
#endif


#ifndef NGX_LUA_NO_FFI_API

int
ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
const u_char *addr, size_t addr_len, int port, char **err)
Expand Down Expand Up @@ -763,4 +752,5 @@ ngx_http_lua_ffi_balancer_get_last_failure(ngx_http_request_t *r,
return bp->last_peer_state;
}

#endif /* NGX_LUA_NO_FFI_API */

/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
9 changes: 2 additions & 7 deletions src/ngx_http_lua_bodyfilterby.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
#include "ngx_http_lua_exception.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_pcrefix.h"
#include "ngx_http_lua_time.h"
#include "ngx_http_lua_log.h"
#include "ngx_http_lua_regex.h"
#include "ngx_http_lua_cache.h"
#include "ngx_http_lua_headers.h"
#include "ngx_http_lua_variable.h"
#include "ngx_http_lua_string.h"
#include "ngx_http_lua_misc.h"
#include "ngx_http_lua_consts.h"
Expand Down Expand Up @@ -162,6 +159,7 @@ ngx_http_lua_body_filter_inline(ngx_http_request_t *r, ngx_chain_t *in)
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
llcf->body_filter_src.value.data,
llcf->body_filter_src.value.len,
&llcf->body_filter_src_ref,
llcf->body_filter_src_key,
"=body_filter_by_lua");
if (rc != NGX_OK) {
Expand Down Expand Up @@ -209,6 +207,7 @@ ngx_http_lua_body_filter_file(ngx_http_request_t *r, ngx_chain_t *in)

/* load Lua script file (w/ cache) sp = 1 */
rc = ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
&llcf->body_filter_src_ref,
llcf->body_filter_src_key);
if (rc != NGX_OK) {
return NGX_ERROR;
Expand Down Expand Up @@ -317,11 +316,7 @@ ngx_http_lua_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
return NGX_ERROR;
}

#if nginx_version >= 1001004
ngx_chain_update_chains(r->pool,
#else
ngx_chain_update_chains(
#endif
&ctx->free_bufs, &ctx->busy_bufs, &out,
(ngx_buf_tag_t) &ngx_http_lua_module);

Expand Down
163 changes: 128 additions & 35 deletions src/ngx_http_lua_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include "ngx_http_lua_util.h"


static u_char *ngx_http_lua_gen_file_cache_key_helper(u_char *out,
const u_char *src, size_t src_len);


/**
* Find code chunk associated with the given key in code cache,
* and push it to the top of Lua stack if found.
Expand All @@ -33,7 +37,7 @@
* */
static ngx_int_t
ngx_http_lua_cache_load_code(ngx_log_t *log, lua_State *L,
const char *key)
int *ref, const char *key)
{
#ifndef OPENRESTY_LUAJIT
int rc;
Expand All @@ -45,16 +49,45 @@ ngx_http_lua_cache_load_code(ngx_log_t *log, lua_State *L,
code_cache_key));
lua_rawget(L, LUA_REGISTRYINDEX); /* sp++ */

dd("Code cache table to load: %p", lua_topointer(L, -1));
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
"code cache lookup (key='%s', ref=%d)", key, *ref);

dd("code cache table to load: %p", lua_topointer(L, -1));

if (!lua_istable(L, -1)) {
dd("Error: code cache table to load did not exist!!");
return NGX_ERROR;
}

lua_getfield(L, -1, key); /* sp++ */
ngx_http_lua_assert(key != NULL);

if (*ref == LUA_NOREF) {
lua_getfield(L, -1, key); /* cache closure */

} else {
if (*ref == LUA_REFNIL) {
lua_getfield(L, -1, key); /* cache ref */

if (!lua_isnumber(L, -1)) {
goto not_found;
}

*ref = lua_tonumber(L, -1);

ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
"code cache setting ref (key='%s', ref=%d)",
key, *ref);

lua_pop(L, 1); /* cache */
}

lua_rawgeti(L, -1, *ref); /* cache closure */
}

if (lua_isfunction(L, -1)) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
"code cache hit (key='%s', ref=%d)", key, *ref);

#ifdef OPENRESTY_LUAJIT
lua_remove(L, -2); /* sp-- */
return NGX_OK;
Expand Down Expand Up @@ -83,13 +116,18 @@ ngx_http_lua_cache_load_code(ngx_log_t *log, lua_State *L,
#endif /* OPENRESTY_LUAJIT */
}

not_found:

dd("Value associated with given key in code cache table is not code "
"chunk: stack top=%d, top value type=%s\n",
lua_gettop(L), lua_typename(L, -1));

/* remove cache table and value from stack */
lua_pop(L, 2); /* sp-=2 */

ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
"code cache miss (key='%s', ref=%d)", key, *ref);

return NGX_DECLINED;
}

Expand All @@ -108,7 +146,7 @@ ngx_http_lua_cache_load_code(ngx_log_t *log, lua_State *L,
*
* */
static ngx_int_t
ngx_http_lua_cache_store_code(lua_State *L, const char *key)
ngx_http_lua_cache_store_code(lua_State *L, int *ref, const char *key)
{
#ifndef OPENRESTY_LUAJIT
int rc;
Expand All @@ -126,8 +164,22 @@ ngx_http_lua_cache_store_code(lua_State *L, const char *key)
return NGX_ERROR;
}

ngx_http_lua_assert(key != NULL);

lua_pushvalue(L, -2); /* closure cache closure */
lua_setfield(L, -2, key); /* closure cache */

if (*ref == LUA_NOREF) {
/* cache closure by cache key */
lua_setfield(L, -2, key); /* closure cache */

} else {
/* cache closure with reference */
*ref = luaL_ref(L, -2); /* closure cache */

/* cache reference by cache key */
lua_pushnumber(L, *ref); /* closure cache ref */
lua_setfield(L, -2, key); /* closure cache */
}

/* remove cache table, leave closure factory at top of stack */
lua_pop(L, 1); /* closure */
Expand All @@ -147,7 +199,7 @@ ngx_http_lua_cache_store_code(lua_State *L, const char *key)

ngx_int_t
ngx_http_lua_cache_loadbuffer(ngx_log_t *log, lua_State *L,
const u_char *src, size_t src_len, const u_char *cache_key,
const u_char *src, size_t src_len, int *cache_ref, const u_char *cache_key,
const char *name)
{
int n;
Expand All @@ -156,14 +208,8 @@ ngx_http_lua_cache_loadbuffer(ngx_log_t *log, lua_State *L,

n = lua_gettop(L);

ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
"looking up Lua code cache with key '%s'", cache_key);

rc = ngx_http_lua_cache_load_code(log, L, (char *) cache_key);
rc = ngx_http_lua_cache_load_code(log, L, cache_ref, (char *) cache_key);
if (rc == NGX_OK) {
/* code chunk loaded from cache, sp++ */
dd("Code cache hit! cache key='%s', stack top=%d, script='%.*s'",
cache_key, lua_gettop(L), (int) src_len, src);
return NGX_OK;
}

Expand All @@ -173,9 +219,6 @@ ngx_http_lua_cache_loadbuffer(ngx_log_t *log, lua_State *L,

/* rc == NGX_DECLINED */

dd("Code cache missed! cache key='%s', stack top=%d, script='%.*s'",
cache_key, lua_gettop(L), (int) src_len, src);

/* load closure factory of inline script to the top of lua stack, sp++ */
rc = ngx_http_lua_clfactory_loadbuffer(L, (char *) src, src_len, name);

Expand All @@ -198,7 +241,7 @@ ngx_http_lua_cache_loadbuffer(ngx_log_t *log, lua_State *L,

/* store closure factory and gen new closure at the top of lua stack to
* code cache */
rc = ngx_http_lua_cache_store_code(L, (char *) cache_key);
rc = ngx_http_lua_cache_store_code(L, cache_ref, (char *) cache_key);
if (rc != NGX_OK) {
err = "fail to generate new closure from the closure factory";
goto error;
Expand All @@ -217,11 +260,10 @@ ngx_http_lua_cache_loadbuffer(ngx_log_t *log, lua_State *L,

ngx_int_t
ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,
const u_char *script, const u_char *cache_key)
const u_char *script, int *cache_ref, const u_char *cache_key)
{
int n;
ngx_int_t rc, errcode = NGX_ERROR;
u_char *p;
u_char buf[NGX_HTTP_LUA_FILE_KEY_LEN + 1];
const char *err = NULL;

Expand All @@ -230,25 +272,19 @@ ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,
/* calculate digest of script file path */
if (cache_key == NULL) {
dd("CACHE file key not pre-calculated...calculating");
p = ngx_copy(buf, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);

p = ngx_http_lua_digest_hex(p, script, ngx_strlen(script));

*p = '\0';
cache_key = buf;
cache_key = ngx_http_lua_gen_file_cache_key_helper(buf, script,
ngx_strlen(script));
*cache_ref = LUA_NOREF;

} else {
dd("CACHE file key already pre-calculated");
}

ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
"looking up Lua code cache with key '%s'", cache_key);
ngx_http_lua_assert(cache_ref != NULL && *cache_ref != LUA_NOREF);
}

rc = ngx_http_lua_cache_load_code(log, L, (char *) cache_key);
rc = ngx_http_lua_cache_load_code(log, L, cache_ref, (char *) cache_key);
if (rc == NGX_OK) {
/* code chunk loaded from cache, sp++ */
dd("Code cache hit! cache key='%s', stack top=%d, file path='%s'",
cache_key, lua_gettop(L), script);
return NGX_OK;
}

Expand All @@ -258,9 +294,6 @@ ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,

/* rc == NGX_DECLINED */

dd("Code cache missed! cache key='%s', stack top=%d, file path='%s'",
cache_key, lua_gettop(L), script);

/* load closure factory of script file to the top of lua stack, sp++ */
rc = ngx_http_lua_clfactory_loadfile(L, (char *) script);

Expand Down Expand Up @@ -291,7 +324,7 @@ ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,

/* store closure factory and gen new closure at the top of lua stack
* to code cache */
rc = ngx_http_lua_cache_store_code(L, (char *) cache_key);
rc = ngx_http_lua_cache_store_code(L, cache_ref, (char *) cache_key);
if (rc != NGX_OK) {
err = "fail to generate new closure from the closure factory";
goto error;
Expand All @@ -308,4 +341,64 @@ ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,
return errcode;
}


u_char *
ngx_http_lua_gen_chunk_cache_key(ngx_conf_t *cf, const char *tag,
const u_char *src, size_t src_len)
{
u_char *p, *out;
size_t tag_len;

tag_len = ngx_strlen(tag);

out = ngx_palloc(cf->pool, tag_len + NGX_HTTP_LUA_INLINE_KEY_LEN + 2);
if (out == NULL) {
return NULL;
}

p = ngx_copy(out, tag, tag_len);
p = ngx_copy(p, "_", 1);
p = ngx_copy(p, NGX_HTTP_LUA_INLINE_TAG, NGX_HTTP_LUA_INLINE_TAG_LEN);
p = ngx_http_lua_digest_hex(p, src, src_len);
*p = '\0';

return out;
}


static u_char *
ngx_http_lua_gen_file_cache_key_helper(u_char *out, const u_char *src,
size_t src_len)
{
u_char *p;

ngx_http_lua_assert(out != NULL);

if (out == NULL) {
return NULL;
}

p = ngx_copy(out, NGX_HTTP_LUA_FILE_TAG, NGX_HTTP_LUA_FILE_TAG_LEN);
p = ngx_http_lua_digest_hex(p, src, src_len);
*p = '\0';

return out;
}


u_char *
ngx_http_lua_gen_file_cache_key(ngx_conf_t *cf, const u_char *src,
size_t src_len)
{
u_char *out;

out = ngx_palloc(cf->pool, NGX_HTTP_LUA_FILE_KEY_LEN + 1);
if (out == NULL) {
return NULL;
}

return ngx_http_lua_gen_file_cache_key_helper(out, src, src_len);
}


/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
8 changes: 6 additions & 2 deletions src/ngx_http_lua_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@


ngx_int_t ngx_http_lua_cache_loadbuffer(ngx_log_t *log, lua_State *L,
const u_char *src, size_t src_len, const u_char *cache_key,
const u_char *src, size_t src_len, int *cache_ref, const u_char *cache_key,
const char *name);
ngx_int_t ngx_http_lua_cache_loadfile(ngx_log_t *log, lua_State *L,
const u_char *script, const u_char *cache_key);
const u_char *script, int *cache_ref, const u_char *cache_key);
u_char *ngx_http_lua_gen_chunk_cache_key(ngx_conf_t *cf, const char *tag,
const u_char *src, size_t src_len);
u_char *ngx_http_lua_gen_file_cache_key(ngx_conf_t *cf, const u_char *src,
size_t src_len);


#endif /* _NGX_HTTP_LUA_CACHE_H_INCLUDED_ */
Expand Down
16 changes: 8 additions & 8 deletions src/ngx_http_lua_clfactory.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,12 @@
typedef enum {
NGX_LUA_TEXT_FILE,
NGX_LUA_BT_LUA,
NGX_LUA_BT_LJ
NGX_LUA_BT_LJ,
} ngx_http_lua_clfactory_file_type_e;


enum {
NGX_LUA_READER_BUFSIZE = 4096
NGX_LUA_READER_BUFSIZE = 4096,
};


Expand Down Expand Up @@ -380,12 +380,12 @@ ngx_http_lua_clfactory_bytecode_prepare(lua_State *L,

#if defined(DDEBUG) && (DDEBUG)
{
dd("==LJ_BT_HEADER==");
size_t i;
for (i = 0; i < LJ_HEADERSIZE; i++) {
dd("%ld: 0x%02X", i, (unsigned)(u_char) lf->begin_code.str[i]);
}
dd("==LJ_BT_HEADER_END==");
dd("==LJ_BT_HEADER==");
size_t i;
for (i = 0; i < LJ_HEADERSIZE; i++) {
dd("%ld: 0x%02X", i, (unsigned)(u_char) lf->begin_code.str[i]);
}
dd("==LJ_BT_HEADER_END==");
}
#endif

Expand Down
158 changes: 78 additions & 80 deletions src/ngx_http_lua_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#define _NGX_HTTP_LUA_COMMON_H_INCLUDED_


#include "ngx_http_lua_autoconf.h"

#include <nginx.h>
#include <ngx_core.h>
#include <ngx_http.h>
Expand All @@ -22,59 +24,73 @@
#include <lauxlib.h>


#if (NGX_PCRE)

#include <pcre.h>
#if defined(NDK) && NDK
#include <ndk.h>

#if (PCRE_MAJOR > 8) || (PCRE_MAJOR == 8 && PCRE_MINOR >= 21)
# define LUA_HAVE_PCRE_JIT 1
#else
# define LUA_HAVE_PCRE_JIT 0
typedef struct {
size_t size;
int ref;
u_char *key;
ngx_str_t script;
} ngx_http_lua_set_var_data_t;
#endif


#ifdef NGX_LUA_USE_ASSERT
#include <assert.h>
# define ngx_http_lua_assert(a) assert(a)
#else
# define ngx_http_lua_assert(a)
#endif


#if !defined(nginx_version) || (nginx_version < 1006000)
#error at least nginx 1.6.0 is required but found an older version
#if (NGX_PCRE)
#include <pcre.h>
# if (PCRE_MAJOR > 8) || (PCRE_MAJOR == 8 && PCRE_MINOR >= 21)
# define LUA_HAVE_PCRE_JIT 1
# else
# define LUA_HAVE_PCRE_JIT 0
# endif
#endif


#if defined(NDK) && NDK
#include <ndk.h>
#if (nginx_version < 1006000)
# error at least nginx 1.6.0 is required but found an older version
#endif


#if LUA_VERSION_NUM != 501
# error unsupported Lua language version
#endif

#if !defined(LUAJIT_VERSION_NUM) || (LUAJIT_VERSION_NUM < 20000)
# error unsupported LuaJIT version
#endif


#if (!defined OPENSSL_NO_OCSP && defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB)
# define NGX_HTTP_LUA_USE_OCSP 1
#endif

#ifndef NGX_HTTP_PERMANENT_REDIRECT
# define NGX_HTTP_PERMANENT_REDIRECT 308
# define NGX_HTTP_PERMANENT_REDIRECT 308
#endif

#ifndef NGX_HAVE_SHA1
# if (nginx_version >= 1011002)
# define NGX_HAVE_SHA1 1
# define NGX_HAVE_SHA1 1
# endif
#endif


#ifndef MD5_DIGEST_LENGTH
#define MD5_DIGEST_LENGTH 16
# define MD5_DIGEST_LENGTH 16
#endif

#ifndef NGX_HTTP_LUA_MAX_ARGS
# define NGX_HTTP_LUA_MAX_ARGS 100
#endif

#ifdef NGX_LUA_USE_ASSERT
# include <assert.h>
# define ngx_http_lua_assert(a) assert(a)
#else
# define ngx_http_lua_assert(a)
#ifndef NGX_HTTP_LUA_MAX_HEADERS
# define NGX_HTTP_LUA_MAX_HEADERS 100
#endif


Expand All @@ -99,25 +115,6 @@
(NGX_HTTP_LUA_FILE_TAG_LEN + 2 * MD5_DIGEST_LENGTH)


#if defined(NDK) && NDK
typedef struct {
size_t size;
u_char *key;
ngx_str_t script;
} ngx_http_lua_set_var_data_t;
#endif


#ifndef NGX_HTTP_LUA_MAX_ARGS
#define NGX_HTTP_LUA_MAX_ARGS 100
#endif


#ifndef NGX_HTTP_LUA_MAX_HEADERS
#define NGX_HTTP_LUA_MAX_HEADERS 100
#endif


/* must be within 16 bit */
#define NGX_HTTP_LUA_CONTEXT_SET 0x0001
#define NGX_HTTP_LUA_CONTEXT_REWRITE 0x0002
Expand All @@ -134,37 +131,43 @@ typedef struct {
#define NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH 0x1000


#ifndef NGX_LUA_NO_FFI_API
#define NGX_HTTP_LUA_FFI_NO_REQ_CTX -100
#define NGX_HTTP_LUA_FFI_BAD_CONTEXT -101
#endif


#if (NGX_PTR_SIZE >= 8 && !defined(_WIN64))
#define ngx_http_lua_lightudata_mask(ludata) \
((void *) ((uintptr_t) (&ngx_http_lua_##ludata) & ((1UL << 47) - 1)))

# define ngx_http_lua_lightudata_mask(ludata) \
((void *) ((uintptr_t) (&ngx_http_lua_##ludata) & ((1UL << 47) - 1)))
#else
#define ngx_http_lua_lightudata_mask(ludata) (&ngx_http_lua_##ludata)
# define ngx_http_lua_lightudata_mask(ludata) \
(&ngx_http_lua_##ludata)
#endif


typedef struct ngx_http_lua_main_conf_s ngx_http_lua_main_conf_t;
typedef union ngx_http_lua_srv_conf_u ngx_http_lua_srv_conf_t;
typedef struct ngx_http_lua_co_ctx_s ngx_http_lua_co_ctx_t;

typedef struct ngx_http_lua_sema_mm_s ngx_http_lua_sema_mm_t;

typedef struct ngx_http_lua_balancer_peer_data_s
ngx_http_lua_balancer_peer_data_t;
typedef union ngx_http_lua_srv_conf_u ngx_http_lua_srv_conf_t;

typedef struct ngx_http_lua_main_conf_s ngx_http_lua_main_conf_t;

typedef struct ngx_http_lua_sema_mm_s ngx_http_lua_sema_mm_t;
typedef struct ngx_http_lua_header_val_s ngx_http_lua_header_val_t;

typedef struct ngx_http_lua_posted_thread_s ngx_http_lua_posted_thread_t;

typedef struct ngx_http_lua_balancer_peer_data_s
ngx_http_lua_balancer_peer_data_t;

typedef ngx_int_t (*ngx_http_lua_main_conf_handler_pt)(ngx_log_t *log,
ngx_http_lua_main_conf_t *lmcf, lua_State *L);

typedef ngx_int_t (*ngx_http_lua_srv_conf_handler_pt)(ngx_http_request_t *r,
ngx_http_lua_srv_conf_t *lscf, lua_State *L);

typedef ngx_int_t (*ngx_http_lua_set_header_pt)(ngx_http_request_t *r,
ngx_http_lua_header_val_t *hv, ngx_str_t *value);


typedef struct {
u_char *package;
Expand All @@ -182,8 +185,6 @@ struct ngx_http_lua_main_conf_s {
ngx_cycle_t *cycle;
ngx_pool_t *pool;

ngx_flag_t load_resty_core;

ngx_int_t max_pending_timers;
ngx_int_t pending_timers;

Expand All @@ -196,11 +197,9 @@ struct ngx_http_lua_main_conf_s {
ngx_int_t regex_cache_entries;
ngx_int_t regex_cache_max_entries;
ngx_int_t regex_match_limit;

#if (LUA_HAVE_PCRE_JIT)
# if (LUA_HAVE_PCRE_JIT)
pcre_jit_stack *jit_stack;
#endif

# endif
#endif

ngx_array_t *shm_zones; /* of ngx_shm_zone_t* */
Expand Down Expand Up @@ -254,7 +253,7 @@ struct ngx_http_lua_main_conf_s {
of reqeusts */
ngx_uint_t malloc_trim_req_count;

#if nginx_version >= 1011011
#if (nginx_version >= 1011011)
/* the following 2 fields are only used by ngx.req.raw_headers() for now */
ngx_buf_t **busy_buf_ptrs;
ngx_int_t busy_buf_ptr_count;
Expand All @@ -281,22 +280,25 @@ union ngx_http_lua_srv_conf_u {
ngx_http_lua_srv_conf_handler_pt ssl_cert_handler;
ngx_str_t ssl_cert_src;
u_char *ssl_cert_src_key;
int ssl_cert_src_ref;

ngx_http_lua_srv_conf_handler_pt ssl_sess_store_handler;
ngx_str_t ssl_sess_store_src;
u_char *ssl_sess_store_src_key;
int ssl_sess_store_src_ref;

ngx_http_lua_srv_conf_handler_pt ssl_sess_fetch_handler;
ngx_str_t ssl_sess_fetch_src;
u_char *ssl_sess_fetch_src_key;
int ssl_sess_fetch_src_ref;
} srv;
#endif

struct {
ngx_str_t src;
u_char *src_key;

ngx_http_lua_srv_conf_handler_pt handler;
ngx_http_lua_srv_conf_handler_pt handler;
ngx_str_t src;
u_char *src_key;
int src_ref;
} balancer;
};

Expand Down Expand Up @@ -333,38 +335,44 @@ typedef struct {
file path */

u_char *rewrite_src_key; /* cached key for rewrite_src */
int rewrite_src_ref;

u_char *access_chunkname;
ngx_http_complex_value_t access_src; /* access_by_lua
inline script/script
file path */

u_char *access_src_key; /* cached key for access_src */
int access_src_ref;

u_char *content_chunkname;
ngx_http_complex_value_t content_src; /* content_by_lua
inline script/script
file path */

u_char *content_src_key; /* cached key for content_src */
int content_src_ref;


u_char *log_chunkname;
ngx_http_complex_value_t log_src; /* log_by_lua inline script/script
file path */

u_char *log_src_key; /* cached key for log_src */
int log_src_ref;

ngx_http_complex_value_t header_filter_src; /* header_filter_by_lua
inline script/script
file path */

u_char *header_filter_src_key;
/* cached key for header_filter_src */
int header_filter_src_ref;


ngx_http_complex_value_t body_filter_src;
u_char *body_filter_src_key;
int body_filter_src_ref;

ngx_msec_t keepalive_timeout;
ngx_msec_t connect_timeout;
Expand All @@ -387,7 +395,7 @@ typedef enum {
NGX_HTTP_LUA_USER_CORO_NOP = 0,
NGX_HTTP_LUA_USER_CORO_RESUME = 1,
NGX_HTTP_LUA_USER_CORO_YIELD = 2,
NGX_HTTP_LUA_USER_THREAD_RESUME = 3
NGX_HTTP_LUA_USER_THREAD_RESUME = 3,
} ngx_http_lua_user_coro_op_t;


Expand All @@ -400,21 +408,12 @@ typedef enum {
} ngx_http_lua_co_status_t;


typedef struct ngx_http_lua_co_ctx_s ngx_http_lua_co_ctx_t;

typedef struct ngx_http_lua_posted_thread_s ngx_http_lua_posted_thread_t;

struct ngx_http_lua_posted_thread_s {
ngx_http_lua_co_ctx_t *co_ctx;
ngx_http_lua_posted_thread_t *next;
};


enum {
NGX_HTTP_LUA_SUBREQ_TRUNCATED = 1
};


struct ngx_http_lua_co_ctx_s {
void *data; /* user state for cosockets */

Expand Down Expand Up @@ -471,6 +470,13 @@ struct ngx_http_lua_co_ctx_s {
the ngx.thread.spawn()
call */
unsigned sem_resume_status:1;

unsigned is_wrap:1; /* set when creating coroutines via
coroutine.wrap */

unsigned propagate_error:1; /* set when propagating an error
from a coroutine to its
parent */
};


Expand Down Expand Up @@ -597,13 +603,6 @@ typedef struct ngx_http_lua_ctx_s {
} ngx_http_lua_ctx_t;


typedef struct ngx_http_lua_header_val_s ngx_http_lua_header_val_t;


typedef ngx_int_t (*ngx_http_lua_set_header_pt)(ngx_http_request_t *r,
ngx_http_lua_header_val_t *hv, ngx_str_t *value);


struct ngx_http_lua_header_val_s {
ngx_http_complex_value_t value;
ngx_uint_t hash;
Expand All @@ -618,7 +617,6 @@ typedef struct {
ngx_str_t name;
ngx_uint_t offset;
ngx_http_lua_set_header_pt handler;

} ngx_http_lua_set_header_t;


Expand Down
2 changes: 0 additions & 2 deletions src/ngx_http_lua_consts.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,8 @@ ngx_http_lua_inject_http_consts(lua_State *L)
lua_pushinteger(L, NGX_HTTP_MOVED_TEMPORARILY);
lua_setfield(L, -2, "HTTP_MOVED_TEMPORARILY");

#if defined(nginx_version) && nginx_version >= 8042
lua_pushinteger(L, NGX_HTTP_SEE_OTHER);
lua_setfield(L, -2, "HTTP_SEE_OTHER");
#endif

lua_pushinteger(L, NGX_HTTP_PERMANENT_REDIRECT);
lua_setfield(L, -2, "HTTP_PERMANENT_REDIRECT");
Expand Down
6 changes: 2 additions & 4 deletions src/ngx_http_lua_contentby.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,6 @@ ngx_http_lua_content_handler(ngx_http_request_t *r)
ngx_http_lua_content_phase_post_read);

if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
#if (nginx_version < 1002006) || \
(nginx_version >= 1003000 && nginx_version < 1003009)
r->main->count--;
#endif
return rc;
}

Expand Down Expand Up @@ -271,6 +267,7 @@ ngx_http_lua_content_handler_file(ngx_http_request_t *r)

/* load Lua script file (w/ cache) sp = 1 */
rc = ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
&llcf->content_src_ref,
llcf->content_src_key);
if (rc != NGX_OK) {
if (rc < NGX_HTTP_SPECIAL_RESPONSE) {
Expand Down Expand Up @@ -302,6 +299,7 @@ ngx_http_lua_content_handler_inline(ngx_http_request_t *r)
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
llcf->content_src.value.data,
llcf->content_src.value.len,
&llcf->content_src_ref,
llcf->content_src_key,
(const char *)
llcf->content_chunkname);
Expand Down
130 changes: 17 additions & 113 deletions src/ngx_http_lua_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

static int ngx_http_lua_ngx_exec(lua_State *L);
static int ngx_http_lua_ngx_redirect(lua_State *L);
static int ngx_http_lua_ngx_exit(lua_State *L);
static int ngx_http_lua_on_abort(lua_State *L);


Expand All @@ -35,14 +34,6 @@ ngx_http_lua_inject_control_api(ngx_log_t *log, lua_State *L)
lua_pushcfunction(L, ngx_http_lua_ngx_exec);
lua_setfield(L, -2, "exec");

lua_pushcfunction(L, ngx_http_lua_ngx_exit);
lua_setfield(L, -2, "throw_error"); /* deprecated */

/* ngx.exit */

lua_pushcfunction(L, ngx_http_lua_ngx_exit);
lua_setfield(L, -2, "exit");

/* ngx.on_abort */

lua_pushcfunction(L, ngx_http_lua_on_abort);
Expand Down Expand Up @@ -195,9 +186,12 @@ ngx_http_lua_ngx_redirect(lua_State *L)
int n;
u_char *p;
u_char *uri;
u_char byte;
size_t len;
ngx_table_elt_t *h;
ngx_http_request_t *r;
size_t buf_len;
u_char *buf;

n = lua_gettop(L);

Expand Down Expand Up @@ -248,6 +242,19 @@ ngx_http_lua_ngx_redirect(lua_State *L)
"the headers");
}

if (ngx_http_lua_check_unsafe_uri_bytes(r, p, len, &byte) != NGX_OK) {
buf_len = ngx_http_lua_escape_log(NULL, p, len) + 1;
buf = ngx_palloc(r->pool, buf_len);
if (buf == NULL) {
return NGX_ERROR;
}

ngx_http_lua_escape_log(buf, p, len);
buf[buf_len - 1] = '\0';
return luaL_error(L, "unsafe byte \"0x%02x\" in redirect uri \"%s\"",
byte, buf);
}

uri = ngx_palloc(r->pool, len);
if (uri == NULL) {
return luaL_error(L, "no memory");
Expand Down Expand Up @@ -295,108 +302,6 @@ ngx_http_lua_ngx_redirect(lua_State *L)
}


static int
ngx_http_lua_ngx_exit(lua_State *L)
{
ngx_int_t rc;
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;

if (lua_gettop(L) != 1) {
return luaL_error(L, "expecting one argument");
}

r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request object found");
}

ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no request ctx found");
}

ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
| NGX_HTTP_LUA_CONTEXT_ACCESS
| NGX_HTTP_LUA_CONTEXT_CONTENT
| NGX_HTTP_LUA_CONTEXT_TIMER
| NGX_HTTP_LUA_CONTEXT_HEADER_FILTER
| NGX_HTTP_LUA_CONTEXT_BALANCER
| NGX_HTTP_LUA_CONTEXT_SSL_CERT
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH);

rc = (ngx_int_t) luaL_checkinteger(L, 1);

if (ctx->context & (NGX_HTTP_LUA_CONTEXT_SSL_CERT
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE
| NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH))
{

#if (NGX_HTTP_SSL)

ctx->exit_code = rc;
ctx->exited = 1;

ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua exit with code %i", rc);

if (ctx->context == NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE) {
return 0;
}

return lua_yield(L, 0);

#else

return luaL_error(L, "no SSL support");

#endif
}

if (ctx->no_abort
&& rc != NGX_ERROR
&& rc != NGX_HTTP_CLOSE
&& rc != NGX_HTTP_REQUEST_TIME_OUT
&& rc != NGX_HTTP_CLIENT_CLOSED_REQUEST)
{
return luaL_error(L, "attempt to abort with pending subrequests");
}

if ((r->header_sent || ctx->header_sent)
&& rc >= NGX_HTTP_SPECIAL_RESPONSE
&& rc != NGX_HTTP_REQUEST_TIME_OUT
&& rc != NGX_HTTP_CLIENT_CLOSED_REQUEST
&& rc != NGX_HTTP_CLOSE)
{
if (rc != (ngx_int_t) r->headers_out.status) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "attempt to "
"set status %i via ngx.exit after sending out the "
"response status %ui", rc, r->headers_out.status);
}

rc = NGX_HTTP_OK;
}

dd("setting exit code: %d", (int) rc);

ctx->exit_code = rc;
ctx->exited = 1;

ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua exit with code %i", ctx->exit_code);

if (ctx->context & (NGX_HTTP_LUA_CONTEXT_HEADER_FILTER
| NGX_HTTP_LUA_CONTEXT_BALANCER))
{
return 0;
}

dd("calling yield");
return lua_yield(L, 0);
}


static int
ngx_http_lua_on_abort(lua_State *L)
{
Expand Down Expand Up @@ -455,7 +360,6 @@ ngx_http_lua_on_abort(lua_State *L)
}


#ifndef NGX_LUA_NO_FFI_API
int
ngx_http_lua_ffi_exit(ngx_http_request_t *r, int status, u_char *err,
size_t *errlen)
Expand Down Expand Up @@ -551,6 +455,6 @@ ngx_http_lua_ffi_exit(ngx_http_request_t *r, int status, u_char *err,

return NGX_OK;
}
#endif /* NGX_LUA_NO_FFI_API */


/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
64 changes: 52 additions & 12 deletions src/ngx_http_lua_coroutine.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@


static int ngx_http_lua_coroutine_create(lua_State *L);
static int ngx_http_lua_coroutine_wrap(lua_State *L);
static int ngx_http_lua_coroutine_resume(lua_State *L);
static int ngx_http_lua_coroutine_yield(lua_State *L);
static int ngx_http_lua_coroutine_status(lua_State *L);
Expand Down Expand Up @@ -62,6 +63,45 @@ ngx_http_lua_coroutine_create(lua_State *L)
}


static int
ngx_http_lua_coroutine_wrap_runner(lua_State *L)
{
/* retrieve closure and insert it at the bottom of
* the stack for coroutine.resume() */
lua_pushvalue(L, lua_upvalueindex(1));
lua_insert(L, 1);

return ngx_http_lua_coroutine_resume(L);
}


static int
ngx_http_lua_coroutine_wrap(lua_State *L)
{
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;
ngx_http_lua_co_ctx_t *coctx = NULL;

r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request found");
}

ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no request ctx found");
}

ngx_http_lua_coroutine_create_helper(L, r, ctx, &coctx);

coctx->is_wrap = 1;

lua_pushcclosure(L, ngx_http_lua_coroutine_wrap_runner, 1);

return 1;
}


int
ngx_http_lua_coroutine_create_helper(lua_State *L, ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx, ngx_http_lua_co_ctx_t **pcoctx)
Expand Down Expand Up @@ -250,7 +290,7 @@ ngx_http_lua_inject_coroutine_api(ngx_log_t *log, lua_State *L)
int rc;

/* new coroutine table */
lua_createtable(L, 0 /* narr */, 14 /* nrec */);
lua_createtable(L, 0 /* narr */, 16 /* nrec */);

/* get old coroutine table */
lua_getglobal(L, "coroutine");
Expand All @@ -262,6 +302,9 @@ ngx_http_lua_inject_coroutine_api(ngx_log_t *log, lua_State *L)
lua_getfield(L, -1, "create");
lua_setfield(L, -3, "_create");

lua_getfield(L, -1, "wrap");
lua_setfield(L, -3, "_wrap");

lua_getfield(L, -1, "resume");
lua_setfield(L, -3, "_resume");

Expand All @@ -277,6 +320,9 @@ ngx_http_lua_inject_coroutine_api(ngx_log_t *log, lua_State *L)
lua_pushcfunction(L, ngx_http_lua_coroutine_create);
lua_setfield(L, -2, "__create");

lua_pushcfunction(L, ngx_http_lua_coroutine_wrap);
lua_setfield(L, -2, "__wrap");

lua_pushcfunction(L, ngx_http_lua_coroutine_resume);
lua_setfield(L, -2, "__resume");

Expand All @@ -291,7 +337,7 @@ ngx_http_lua_inject_coroutine_api(ngx_log_t *log, lua_State *L)
/* inject coroutine APIs */
{
const char buf[] =
"local keys = {'create', 'yield', 'resume', 'status'}\n"
"local keys = {'create', 'yield', 'resume', 'status', 'wrap'}\n"
#ifdef OPENRESTY_LUAJIT
"local get_req = require 'thread.exdata'\n"
#else
Expand Down Expand Up @@ -321,24 +367,18 @@ ngx_http_lua_inject_coroutine_api(ngx_log_t *log, lua_State *L)
"return std(...)\n"
"end\n"
"end\n"
"local create, resume = coroutine.create, coroutine.resume\n"
"coroutine.wrap = function(f)\n"
"local co = create(f)\n"
"return function(...) return select(2, resume(co, ...)) end\n"
"end\n"
"package.loaded.coroutine = coroutine";

"package.loaded.coroutine = coroutine"
#if 0
"debug.sethook(function () collectgarbage() end, 'rl', 1)"
#endif
;

rc = luaL_loadbuffer(L, buf, sizeof(buf) - 1, "=coroutine.wrap");
rc = luaL_loadbuffer(L, buf, sizeof(buf) - 1, "=coroutine_api");
}

if (rc != 0) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"failed to load Lua code for coroutine.wrap(): %i: %s",
"failed to load Lua code for coroutine_api: %i: %s",
rc, lua_tostring(L, -1));

lua_pop(L, 1);
Expand All @@ -348,7 +388,7 @@ ngx_http_lua_inject_coroutine_api(ngx_log_t *log, lua_State *L)
rc = lua_pcall(L, 0, 0, 0);
if (rc != 0) {
ngx_log_error(NGX_LOG_ERR, log, 0,
"failed to run the Lua code for coroutine.wrap(): %i: %s",
"failed to run the Lua code for coroutine_api: %i: %s",
rc, lua_tostring(L, -1));
lua_pop(L, 1);
}
Expand Down
67 changes: 0 additions & 67 deletions src/ngx_http_lua_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,71 +25,6 @@ static ngx_int_t ngx_http_lua_ngx_ctx_add_cleanup(ngx_http_request_t *r,
static void ngx_http_lua_ngx_ctx_cleanup(void *data);


int
ngx_http_lua_ngx_get_ctx(lua_State *L)
{
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;

r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request found");
}

ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no request ctx found");
}

if (ctx->ctx_ref == LUA_NOREF) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua create ngx.ctx table for the current request");

lua_pushliteral(L, ngx_http_lua_ctx_tables_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_createtable(L, 0 /* narr */, 4 /* nrec */);
lua_pushvalue(L, -1);
ctx->ctx_ref = luaL_ref(L, -3);

if (ngx_http_lua_ngx_ctx_add_cleanup(r, ctx->ctx_ref) != NGX_OK) {
return luaL_error(L, "no memory");
}

return 1;
}

ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua fetching existing ngx.ctx table for the current "
"request");

lua_pushliteral(L, ngx_http_lua_ctx_tables_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, -1, ctx->ctx_ref);

return 1;
}


int
ngx_http_lua_ngx_set_ctx(lua_State *L)
{
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;

r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request found");
}

ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no request ctx found");
}

return ngx_http_lua_ngx_set_ctx_helper(L, r, ctx, 3);
}


int
ngx_http_lua_ngx_set_ctx_helper(lua_State *L, ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx, int index)
Expand Down Expand Up @@ -130,7 +65,6 @@ ngx_http_lua_ngx_set_ctx_helper(lua_State *L, ngx_http_request_t *r,
}


#ifndef NGX_LUA_NO_FFI_API
int
ngx_http_lua_ffi_get_ctx_ref(ngx_http_request_t *r)
{
Expand Down Expand Up @@ -163,7 +97,6 @@ ngx_http_lua_ffi_set_ctx_ref(ngx_http_request_t *r, int ref)

return NGX_OK;
}
#endif /* NGX_LUA_NO_FFI_API */


static ngx_int_t
Expand Down
2 changes: 0 additions & 2 deletions src/ngx_http_lua_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "ngx_http_lua_common.h"


int ngx_http_lua_ngx_get_ctx(lua_State *L);
int ngx_http_lua_ngx_set_ctx(lua_State *L);
int ngx_http_lua_ngx_set_ctx_helper(lua_State *L, ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx, int index);

Expand Down
317 changes: 141 additions & 176 deletions src/ngx_http_lua_directive.c

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/ngx_http_lua_directive.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ char *ngx_http_lua_package_cpath(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
char *ngx_http_lua_package_path(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
char *ngx_http_lua_regex_cache_max_entries(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
char *ngx_http_lua_regex_match_limit(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
char *ngx_http_lua_content_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
char *ngx_http_lua_content_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
Expand Down Expand Up @@ -50,6 +54,8 @@ char *ngx_http_lua_init_worker_by_lua_block(ngx_conf_t *cf,
char *ngx_http_lua_init_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
char *ngx_http_lua_code_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
char *ngx_http_lua_load_resty_core(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);

#if defined(NDK) && NDK

Expand Down
5 changes: 2 additions & 3 deletions src/ngx_http_lua_headerfilterby.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
#include "ngx_http_lua_exception.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_pcrefix.h"
#include "ngx_http_lua_time.h"
#include "ngx_http_lua_log.h"
#include "ngx_http_lua_regex.h"
#include "ngx_http_lua_cache.h"
#include "ngx_http_lua_headers.h"
#include "ngx_http_lua_variable.h"
#include "ngx_http_lua_string.h"
#include "ngx_http_lua_misc.h"
#include "ngx_http_lua_consts.h"
Expand Down Expand Up @@ -172,6 +169,7 @@ ngx_http_lua_header_filter_inline(ngx_http_request_t *r)
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
llcf->header_filter_src.value.data,
llcf->header_filter_src.value.len,
&llcf->header_filter_src_ref,
llcf->header_filter_src_key,
"=header_filter_by_lua");
if (rc != NGX_OK) {
Expand Down Expand Up @@ -213,6 +211,7 @@ ngx_http_lua_header_filter_file(ngx_http_request_t *r)

/* load Lua script file (w/ cache) sp = 1 */
rc = ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
&llcf->header_filter_src_ref,
llcf->header_filter_src_key);
if (rc != NGX_OK) {
return NGX_ERROR;
Expand Down
467 changes: 90 additions & 377 deletions src/ngx_http_lua_headers.c

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/ngx_http_lua_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
void ngx_http_lua_inject_resp_header_api(lua_State *L);
void ngx_http_lua_inject_req_header_api(lua_State *L);
void ngx_http_lua_create_headers_metatable(ngx_log_t *log, lua_State *L);
#if nginx_version >= 1011011
#if (nginx_version >= 1011011)
void ngx_http_lua_ngx_raw_header_cleanup(void *data);
#endif

Expand Down
20 changes: 12 additions & 8 deletions src/ngx_http_lua_headers_in.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,17 @@ static ngx_http_lua_set_header_t ngx_http_lua_set_handlers[] = {
offsetof(ngx_http_headers_in_t, if_modified_since),
ngx_http_set_builtin_header },

#if defined(nginx_version) && nginx_version >= 9002
{ ngx_string("If-Unmodified-Since"),
offsetof(ngx_http_headers_in_t, if_unmodified_since),
ngx_http_set_builtin_header },
#endif

#if defined(nginx_version) && nginx_version >= 1003003
{ ngx_string("If-Match"),
offsetof(ngx_http_headers_in_t, if_match),
ngx_http_set_builtin_header },

{ ngx_string("If-None-Match"),
offsetof(ngx_http_headers_in_t, if_none_match),
ngx_http_set_builtin_header },
#endif

{ ngx_string("User-Agent"),
offsetof(ngx_http_headers_in_t, user_agent),
Expand Down Expand Up @@ -104,11 +100,9 @@ static ngx_http_lua_set_header_t ngx_http_lua_set_handlers[] = {
offsetof(ngx_http_headers_in_t, expect),
ngx_http_set_builtin_header },

#if defined(nginx_version) && nginx_version >= 1003013
{ ngx_string("Upgrade"),
offsetof(ngx_http_headers_in_t, upgrade),
ngx_http_set_builtin_header },
#endif

#if (NGX_HTTP_GZIP)
{ ngx_string("Accept-Encoding"),
Expand Down Expand Up @@ -346,7 +340,7 @@ ngx_http_lua_validate_host(ngx_str_t *host, ngx_pool_t *pool, ngx_uint_t alloc)
enum {
sw_usual = 0,
sw_literal,
sw_rest
sw_rest,
} state;

dot_pos = host->len;
Expand Down Expand Up @@ -659,11 +653,21 @@ ngx_http_lua_set_input_header(ngx_http_request_t *r, ngx_str_t key,
{
ngx_http_lua_header_val_t hv;
ngx_http_lua_set_header_t *handlers = ngx_http_lua_set_handlers;

ngx_int_t rc;
ngx_uint_t i;

dd("set header value: %.*s", (int) value.len, value.data);

rc = ngx_http_lua_copy_escaped_header(r, &key, 1);
if (rc != NGX_OK) {
return NGX_ERROR;
}

rc = ngx_http_lua_copy_escaped_header(r, &value, 0);
if (rc != NGX_OK) {
return NGX_ERROR;
}

hv.hash = ngx_hash_key_lc(key.data, key.len);
hv.key = key;

Expand Down
10 changes: 9 additions & 1 deletion src/ngx_http_lua_headers_out.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static ngx_http_lua_set_header_t ngx_http_lua_set_handlers[] = {
offsetof(ngx_http_headers_out_t, cache_control),
ngx_http_set_builtin_multi_header },

#if defined(nginx_version) && nginx_version >= 1013009
#if (nginx_version >= 1013009)
{ ngx_string("Link"),
offsetof(ngx_http_headers_out_t, link),
ngx_http_set_builtin_multi_header },
Expand Down Expand Up @@ -491,6 +491,14 @@ ngx_http_lua_set_output_header(ngx_http_request_t *r, ngx_http_lua_ctx_t *ctx,

dd("set header value: %.*s", (int) value.len, value.data);

if (ngx_http_lua_copy_escaped_header(r, &key, 1) != NGX_OK) {
return NGX_ERROR;
}

if (ngx_http_lua_copy_escaped_header(r, &value, 0) != NGX_OK) {
return NGX_ERROR;
}

hv.hash = ngx_hash_key_lc(key.data, key.len);
hv.key = key;

Expand Down
36 changes: 8 additions & 28 deletions src/ngx_http_lua_initworkerby.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)
return NGX_OK;
}

#ifdef HAVE_NGX_LUA_PIPE
# ifdef HAVE_NGX_LUA_PIPE
if (ngx_http_lua_pipe_add_signal_handler(cycle) != NGX_OK) {
return NGX_ERROR;
}
#endif
# endif

#endif /* NGX_WIN32 */

Expand Down Expand Up @@ -115,12 +115,8 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)

ngx_memcpy(fake_cycle, cycle, sizeof(ngx_cycle_t));

#if defined(nginx_version) && nginx_version >= 9007

ngx_queue_init(&fake_cycle->reusable_connections_queue);

#endif

if (ngx_array_init(&fake_cycle->listening, cycle->pool,
cycle->listening.nelts || 1,
sizeof(ngx_listening_t))
Expand All @@ -129,17 +125,13 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)
goto failed;
}

#if defined(nginx_version) && nginx_version >= 1003007

if (ngx_array_init(&fake_cycle->paths, cycle->pool, cycle->paths.nelts || 1,
sizeof(ngx_path_t *))
!= NGX_OK)
{
goto failed;
}

#endif

part = &cycle->open_files.part;
ofile = part->elts;

Expand Down Expand Up @@ -197,7 +189,7 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)
return NGX_ERROR;
}

#if defined(nginx_version) && nginx_version >= 1009011
#if (nginx_version >= 1009011)
modules = cycle->modules;
#else
modules = ngx_modules;
Expand Down Expand Up @@ -282,26 +274,11 @@ ngx_http_lua_init_worker(ngx_cycle_t *cycle)

clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

#if defined(nginx_version) && nginx_version >= 1003014

# if nginx_version >= 1009000

#if (nginx_version >= 1009000)
ngx_set_connection_log(r->connection, clcf->error_log);

# else

ngx_http_set_connection_log(r->connection, clcf->error_log);

# endif

#else

c->log->file = clcf->error_log->file;

if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
c->log->log_level = clcf->error_log->log_level;
}

ngx_http_set_connection_log(r->connection, clcf->error_log);
#endif

ctx = ngx_http_lua_create_ctx(r);
Expand Down Expand Up @@ -374,3 +351,6 @@ ngx_http_lua_log_init_worker_error(ngx_log_t *log, u_char *buf, size_t len)

return ngx_snprintf(buf, len, ", context: init_worker_by_lua*");
}


/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
4 changes: 1 addition & 3 deletions src/ngx_http_lua_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ log_wrapper(ngx_log_t *log, const char *ident, ngx_uint_t level,
*p++ = ':';

p = ngx_snprintf(p, NGX_INT_T_LEN, "%d",
ar.currentline ? ar.currentline : ar.linedefined);
ar.currentline > 0 ? ar.currentline : ar.linedefined);

*p++ = ':'; *p++ = ' ';

Expand Down Expand Up @@ -340,7 +340,6 @@ ngx_http_lua_capture_log_handler(ngx_log_t *log,
#endif


#ifndef NGX_LUA_NO_FFI_API
int
ngx_http_lua_ffi_errlog_set_filter_level(int level, u_char *err, size_t *errlen)
{
Expand Down Expand Up @@ -451,6 +450,5 @@ ngx_http_lua_ffi_raw_log(ngx_http_request_t *r, int level, u_char *s,
return NGX_OK;
}

#endif

/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
5 changes: 4 additions & 1 deletion src/ngx_http_lua_log_ringbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef struct {


enum {
HEADER_LEN = sizeof(ngx_http_lua_log_ringbuf_header_t)
HEADER_LEN = sizeof(ngx_http_lua_log_ringbuf_header_t),
};


Expand Down Expand Up @@ -223,3 +223,6 @@ ngx_http_lua_log_ringbuf_read(ngx_http_lua_log_ringbuf_t *rb, int *log_level,

return NGX_OK;
}


/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
7 changes: 2 additions & 5 deletions src/ngx_http_lua_logby.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@
#include "ngx_http_lua_exception.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_pcrefix.h"
#include "ngx_http_lua_time.h"
#include "ngx_http_lua_log.h"
#include "ngx_http_lua_regex.h"
#include "ngx_http_lua_cache.h"
#include "ngx_http_lua_headers.h"
#include "ngx_http_lua_variable.h"
#include "ngx_http_lua_string.h"
#include "ngx_http_lua_misc.h"
#include "ngx_http_lua_consts.h"
#include "ngx_http_lua_shdict.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_exception.h"
#if (NGX_HTTP_LUA_HAVE_MALLOC_TRIM)
#include <malloc.h>
#endif
Expand Down Expand Up @@ -154,6 +149,7 @@ ngx_http_lua_log_handler_inline(ngx_http_request_t *r)
rc = ngx_http_lua_cache_loadbuffer(r->connection->log, L,
llcf->log_src.value.data,
llcf->log_src.value.len,
&llcf->log_src_ref,
llcf->log_src_key,
(const char *) llcf->log_chunkname);
if (rc != NGX_OK) {
Expand Down Expand Up @@ -190,6 +186,7 @@ ngx_http_lua_log_handler_file(ngx_http_request_t *r)

/* load Lua script file (w/ cache) sp = 1 */
rc = ngx_http_lua_cache_loadfile(r->connection->log, L, script_path,
&llcf->log_src_ref,
llcf->log_src_key);
if (rc != NGX_OK) {
return NGX_ERROR;
Expand Down
1 change: 0 additions & 1 deletion src/ngx_http_lua_logby.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
ngx_int_t ngx_http_lua_log_handler(ngx_http_request_t *r);
ngx_int_t ngx_http_lua_log_handler_inline(ngx_http_request_t *r);
ngx_int_t ngx_http_lua_log_handler_file(ngx_http_request_t *r);
void ngx_http_lua_inject_logby_ngx_api(ngx_conf_t *cf, lua_State *L);


#endif /* _NGX_HTTP_LUA_LOGBY_H_INCLUDED_ */
Expand Down
161 changes: 0 additions & 161 deletions src/ngx_http_lua_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,12 @@


#include "ngx_http_lua_misc.h"
#include "ngx_http_lua_ctx.h"
#include "ngx_http_lua_util.h"


static int ngx_http_lua_ngx_get(lua_State *L);
static int ngx_http_lua_ngx_set(lua_State *L);
static int ngx_http_lua_ngx_req_is_internal(lua_State *L);


void
ngx_http_lua_inject_misc_api(lua_State *L)
{
/* ngx. getter and setter */
lua_createtable(L, 0, 2); /* metatable for .ngx */
lua_pushcfunction(L, ngx_http_lua_ngx_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, ngx_http_lua_ngx_set);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2);
}


void
ngx_http_lua_inject_req_misc_api(lua_State *L)
{
Expand All @@ -57,150 +41,6 @@ ngx_http_lua_ngx_req_is_internal(lua_State *L)
}


static int
ngx_http_lua_ngx_get(lua_State *L)
{
int status;
ngx_http_request_t *r;
u_char *p;
size_t len;
ngx_http_lua_ctx_t *ctx;

r = ngx_http_lua_get_req(L);
if (r == NULL) {
lua_pushnil(L);
return 1;
}

ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
lua_pushnil(L);
return 1;
}

p = (u_char *) luaL_checklstring(L, -1, &len);

dd("ngx get %s", p);

if (len == sizeof("status") - 1
&& ngx_strncmp(p, "status", sizeof("status") - 1) == 0)
{
ngx_http_lua_check_fake_request(L, r);

if (r->err_status) {
status = r->err_status;

} else if (r->headers_out.status) {
status = r->headers_out.status;

} else if (r->http_version == NGX_HTTP_VERSION_9) {
status = 9;

} else {
status = 0;
}

lua_pushinteger(L, status);
return 1;
}

if (len == sizeof("ctx") - 1
&& ngx_strncmp(p, "ctx", sizeof("ctx") - 1) == 0)
{
return ngx_http_lua_ngx_get_ctx(L);
}

if (len == sizeof("is_subrequest") - 1
&& ngx_strncmp(p, "is_subrequest", sizeof("is_subrequest") - 1) == 0)
{
lua_pushboolean(L, r != r->main);
return 1;
}

if (len == sizeof("headers_sent") - 1
&& ngx_strncmp(p, "headers_sent", sizeof("headers_sent") - 1) == 0)
{
ngx_http_lua_check_fake_request(L, r);

dd("headers sent: %d", r->header_sent || ctx->header_sent);

lua_pushboolean(L, r->header_sent || ctx->header_sent);
return 1;
}

dd("key %s not matched", p);

lua_pushnil(L);
return 1;
}


static int
ngx_http_lua_ngx_set(lua_State *L)
{
ngx_http_request_t *r;
u_char *p;
size_t len;

/* we skip the first argument that is the table */
p = (u_char *) luaL_checklstring(L, 2, &len);

if (len == sizeof("status") - 1
&& ngx_strncmp(p, "status", sizeof("status") - 1) == 0)
{
r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request object found");
}

if (r->header_sent) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"attempt to set ngx.status after sending out "
"response headers");
return 0;
}

if (r->err_status) {
r->err_status = 0;
}

ngx_http_lua_check_fake_request(L, r);

/* get the value */
r->headers_out.status = (ngx_uint_t) luaL_checknumber(L, 3);

if (r->headers_out.status == 101) {
/*
* XXX work-around a bug in the Nginx core that 101 does
* not have a default status line
*/

ngx_str_set(&r->headers_out.status_line, "101 Switching Protocols");

} else {
r->headers_out.status_line.len = 0;
}

return 0;
}

if (len == sizeof("ctx") - 1
&& ngx_strncmp(p, "ctx", sizeof("ctx") - 1) == 0)
{
r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request object found");
}

return ngx_http_lua_ngx_set_ctx(L);
}

lua_rawset(L, -3);
return 0;
}


#ifndef NGX_LUA_NO_FFI_API
int
ngx_http_lua_ffi_get_resp_status(ngx_http_request_t *r)
{
Expand Down Expand Up @@ -313,7 +153,6 @@ ngx_http_lua_ffi_get_conf_env(u_char *name, u_char **env_buf, size_t *name_len)

return NGX_DECLINED;
}
#endif /* NGX_LUA_NO_FFI_API */


/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
2 changes: 0 additions & 2 deletions src/ngx_http_lua_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "ngx_http_lua_common.h"


void ngx_http_lua_inject_misc_api(lua_State *L);

void ngx_http_lua_inject_req_misc_api(lua_State *L);


Expand Down
142 changes: 78 additions & 64 deletions src/ngx_http_lua_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static ngx_conf_post_t ngx_http_lua_lowat_post =
static volatile ngx_cycle_t *ngx_http_lua_prev_cycle = NULL;


#if (NGX_HTTP_SSL) && defined(nginx_version) && nginx_version >= 1001013
#if (NGX_HTTP_SSL)

static ngx_conf_bitmask_t ngx_http_lua_ssl_protocols[] = {
{ ngx_string("SSLv2"), NGX_SSL_SSLv2 },
Expand All @@ -79,9 +79,9 @@ static ngx_command_t ngx_http_lua_cmds[] = {

{ ngx_string("lua_load_resty_core"),
NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
ngx_http_lua_load_resty_core,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_lua_main_conf_t, load_resty_core),
0,
NULL },

{ ngx_string("lua_max_running_timers"),
Expand Down Expand Up @@ -119,21 +119,27 @@ static ngx_command_t ngx_http_lua_cmds[] = {
offsetof(ngx_http_lua_main_conf_t, set_sa_restart),
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_lua_regex_cache_max_entries,
NGX_HTTP_MAIN_CONF_OFFSET,
#if (NGX_PCRE)
offsetof(ngx_http_lua_main_conf_t, regex_cache_max_entries),
#else
0,
#endif
NULL },

{ ngx_string("lua_regex_match_limit"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
ngx_http_lua_regex_match_limit,
NGX_HTTP_MAIN_CONF_OFFSET,
#if (NGX_PCRE)
offsetof(ngx_http_lua_main_conf_t, regex_match_limit),
NULL },
#else
0,
#endif
NULL },

{ ngx_string("lua_package_cpath"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
Expand Down Expand Up @@ -224,10 +230,10 @@ static ngx_command_t ngx_http_lua_cmds[] = {
(void *) ngx_http_lua_init_worker_by_file },

#if defined(NDK) && NDK
/* set_by_lua $res { inline Lua code } [$arg1 [$arg2 [...]]] */
/* set_by_lua_block $res { inline Lua code } */
{ ngx_string("set_by_lua_block"),
NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_1MORE|NGX_CONF_BLOCK,
|NGX_CONF_TAKE1|NGX_CONF_BLOCK,
ngx_http_lua_set_by_lua_block,
NGX_HTTP_LOC_CONF_OFFSET,
0,
Expand Down Expand Up @@ -515,17 +521,13 @@ static ngx_command_t ngx_http_lua_cmds[] = {

#if (NGX_HTTP_SSL)

# if defined(nginx_version) && nginx_version >= 1001013

{ ngx_string("lua_ssl_protocols"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_lua_loc_conf_t, ssl_protocols),
&ngx_http_lua_ssl_protocols },

# endif

{ ngx_string("lua_ssl_ciphers"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
Expand Down Expand Up @@ -650,9 +652,7 @@ ngx_http_lua_init(ngx_conf_t *cf)
volatile ngx_cycle_t *saved_cycle;
ngx_http_core_main_conf_t *cmcf;
ngx_http_lua_main_conf_t *lmcf;
#if !defined(NGX_LUA_NO_FFI_API) || nginx_version >= 1011011
ngx_pool_cleanup_t *cln;
#endif
ngx_str_t name = ngx_string("host");

if (ngx_process == NGX_PROCESS_SIGNALLER || ngx_test_config) {
Expand Down Expand Up @@ -741,7 +741,6 @@ ngx_http_lua_init(ngx_conf_t *cf)
}
}

#ifndef NGX_LUA_NO_FFI_API
/* add the cleanup of semaphores after the lua_close */
cln = ngx_pool_cleanup_add(cf->pool, 0);
if (cln == NULL) {
Expand All @@ -755,9 +754,7 @@ ngx_http_lua_init(ngx_conf_t *cf)
ngx_http_lua_pipe_init();
#endif

#endif

#if nginx_version >= 1011011
#if (nginx_version >= 1011011)
cln = ngx_pool_cleanup_add(cf->pool, 0);
if (cln == NULL) {
return NGX_ERROR;
Expand Down Expand Up @@ -787,14 +784,33 @@ ngx_http_lua_init(ngx_conf_t *cf)
ngx_http_lua_hash_literal("content-length");
ngx_http_lua_location_hash = ngx_http_lua_hash_literal("location");

lmcf->lua = ngx_http_lua_init_vm(NULL, cf->cycle, cf->pool, lmcf,
cf->log, NULL);
if (lmcf->lua == NULL) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
"failed to initialize Lua VM");
rc = ngx_http_lua_init_vm(&lmcf->lua, NULL, cf->cycle, cf->pool,
lmcf, cf->log, NULL);
if (rc != NGX_OK) {
if (rc == NGX_DECLINED) {
ngx_http_lua_assert(lmcf->lua != NULL);

ngx_conf_log_error(NGX_LOG_ALERT, cf, 0,
"failed to load the 'resty.core' module "
"(https://github.com/openresty/lua-resty"
"-core); ensure you are using an OpenResty "
"release from https://openresty.org/en/"
"download.html (reason: %s)",
lua_tostring(lmcf->lua, -1));

} else {
/* rc == NGX_ERROR */
ngx_conf_log_error(NGX_LOG_ALERT, cf, 0,
"failed to initialize Lua VM");
}

return NGX_ERROR;
}

/* rc == NGX_OK */

ngx_http_lua_assert(lmcf->lua != NULL);

if (!lmcf->requires_shm && lmcf->init_handler) {
saved_cycle = ngx_cycle;
ngx_cycle = cf->cycle;
Expand Down Expand Up @@ -848,9 +864,7 @@ ngx_http_lua_lowat_check(ngx_conf_t *cf, void *post, void *data)
static void *
ngx_http_lua_create_main_conf(ngx_conf_t *cf)
{
#ifndef NGX_LUA_NO_FFI_API
ngx_int_t rc;
#endif

ngx_http_lua_main_conf_t *lmcf;

Expand Down Expand Up @@ -884,7 +898,6 @@ ngx_http_lua_create_main_conf(ngx_conf_t *cf)
*/

lmcf->pool = cf->pool;
lmcf->load_resty_core = NGX_CONF_UNSET;
lmcf->max_pending_timers = NGX_CONF_UNSET;
lmcf->max_running_timers = NGX_CONF_UNSET;
#if (NGX_PCRE)
Expand All @@ -900,14 +913,12 @@ ngx_http_lua_create_main_conf(ngx_conf_t *cf)
lmcf->malloc_trim_cycle = NGX_CONF_UNSET_UINT;
#endif

#ifndef NGX_LUA_NO_FFI_API
rc = ngx_http_lua_sema_mm_init(cf, lmcf);
if (rc != NGX_OK) {
return NULL;
}

dd("nginx Lua module main config structure initialized!");
#endif

return lmcf;
}
Expand All @@ -918,10 +929,6 @@ ngx_http_lua_init_main_conf(ngx_conf_t *cf, void *conf)
{
ngx_http_lua_main_conf_t *lmcf = conf;

if (lmcf->load_resty_core == NGX_CONF_UNSET) {
lmcf->load_resty_core = 1;
}

#if (NGX_PCRE)
if (lmcf->regex_cache_max_entries == NGX_CONF_UNSET) {
lmcf->regex_cache_max_entries = 1024;
Expand Down Expand Up @@ -986,6 +993,14 @@ ngx_http_lua_create_srv_conf(ngx_conf_t *cf)
* lscf->balancer.src_key = NULL;
*/

#if (NGX_HTTP_SSL)
lscf->srv.ssl_cert_src_ref = LUA_REFNIL;
lscf->srv.ssl_sess_store_src_ref = LUA_REFNIL;
lscf->srv.ssl_sess_fetch_src_ref = LUA_REFNIL;
#endif

lscf->balancer.src_ref = LUA_REFNIL;

return lscf;
}

Expand All @@ -1003,6 +1018,7 @@ ngx_http_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)

if (conf->srv.ssl_cert_src.len == 0) {
conf->srv.ssl_cert_src = prev->srv.ssl_cert_src;
conf->srv.ssl_cert_src_ref = prev->srv.ssl_cert_src_ref;
conf->srv.ssl_cert_src_key = prev->srv.ssl_cert_src_key;
conf->srv.ssl_cert_handler = prev->srv.ssl_cert_handler;
}
Expand All @@ -1019,7 +1035,7 @@ ngx_http_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
#ifdef LIBRESSL_VERSION_NUMBER

ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"LibreSSL does not support ssl_certificate_by_lua*");
"LibreSSL is not supported by ssl_certificate_by_lua*");
return NGX_CONF_ERROR;

#else
Expand All @@ -1041,6 +1057,7 @@ ngx_http_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)

if (conf->srv.ssl_sess_store_src.len == 0) {
conf->srv.ssl_sess_store_src = prev->srv.ssl_sess_store_src;
conf->srv.ssl_sess_store_src_ref = prev->srv.ssl_sess_store_src_ref;
conf->srv.ssl_sess_store_src_key = prev->srv.ssl_sess_store_src_key;
conf->srv.ssl_sess_store_handler = prev->srv.ssl_sess_store_handler;
}
Expand All @@ -1050,7 +1067,7 @@ ngx_http_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
if (sscf && sscf->ssl.ctx) {
#ifdef LIBRESSL_VERSION_NUMBER
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"LibreSSL does not support "
"LibreSSL is not supported by "
"ssl_session_store_by_lua*");

return NGX_CONF_ERROR;
Expand All @@ -1063,6 +1080,7 @@ ngx_http_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)

if (conf->srv.ssl_sess_fetch_src.len == 0) {
conf->srv.ssl_sess_fetch_src = prev->srv.ssl_sess_fetch_src;
conf->srv.ssl_sess_fetch_src_ref = prev->srv.ssl_sess_fetch_src_ref;
conf->srv.ssl_sess_fetch_src_key = prev->srv.ssl_sess_fetch_src_key;
conf->srv.ssl_sess_fetch_handler = prev->srv.ssl_sess_fetch_handler;
}
Expand All @@ -1072,7 +1090,7 @@ ngx_http_lua_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
if (sscf && sscf->ssl.ctx) {
#ifdef LIBRESSL_VERSION_NUMBER
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"LibreSSL does not support "
"LibreSSL is not supported by "
"ssl_session_fetch_by_lua*");

return NGX_CONF_ERROR;
Expand Down Expand Up @@ -1102,23 +1120,23 @@ ngx_http_lua_create_loc_conf(ngx_conf_t *cf)
* conf->access_src = {{ 0, NULL }, NULL, NULL, NULL};
* conf->access_src_key = NULL
* conf->rewrite_src = {{ 0, NULL }, NULL, NULL, NULL};
* conf->rewrite_src_key = NULL
* conf->rewrite_src_key = NULL;
* conf->rewrite_handler = NULL;
*
* conf->content_src = {{ 0, NULL }, NULL, NULL, NULL};
* conf->content_src_key = NULL
* conf->content_src_key = NULL;
* conf->content_handler = NULL;
*
* conf->log_src = {{ 0, NULL }, NULL, NULL, NULL};
* conf->log_src_key = NULL
* conf->log_src_key = NULL;
* conf->log_handler = NULL;
*
* conf->header_filter_src = {{ 0, NULL }, NULL, NULL, NULL};
* conf->header_filter_src_key = NULL
* conf->header_filter_src_key = NULL;
* conf->header_filter_handler = NULL;
*
* conf->body_filter_src = {{ 0, NULL }, NULL, NULL, NULL};
* conf->body_filter_src_key = NULL
* conf->body_filter_src_key = NULL;
* conf->body_filter_handler = NULL;
*
* conf->ssl = 0;
Expand All @@ -1145,6 +1163,13 @@ ngx_http_lua_create_loc_conf(ngx_conf_t *cf)
conf->transform_underscores_in_resp_headers = NGX_CONF_UNSET;
conf->log_socket_errors = NGX_CONF_UNSET;

conf->rewrite_src_ref = LUA_REFNIL;
conf->access_src_ref = LUA_REFNIL;
conf->content_src_ref = LUA_REFNIL;
conf->header_filter_src_ref = LUA_REFNIL;
conf->body_filter_src_ref = LUA_REFNIL;
conf->log_src_ref = LUA_REFNIL;

#if (NGX_HTTP_SSL)
conf->ssl_verify_depth = NGX_CONF_UNSET_UINT;
#endif
Expand All @@ -1162,54 +1187,56 @@ ngx_http_lua_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
if (conf->rewrite_src.value.len == 0) {
conf->rewrite_src = prev->rewrite_src;
conf->rewrite_handler = prev->rewrite_handler;
conf->rewrite_src_ref = prev->rewrite_src_ref;
conf->rewrite_src_key = prev->rewrite_src_key;
conf->rewrite_chunkname = prev->rewrite_chunkname;
}

if (conf->access_src.value.len == 0) {
conf->access_src = prev->access_src;
conf->access_handler = prev->access_handler;
conf->access_src_ref = prev->access_src_ref;
conf->access_src_key = prev->access_src_key;
conf->access_chunkname = prev->access_chunkname;
}

if (conf->content_src.value.len == 0) {
conf->content_src = prev->content_src;
conf->content_handler = prev->content_handler;
conf->content_src_ref = prev->content_src_ref;
conf->content_src_key = prev->content_src_key;
conf->content_chunkname = prev->content_chunkname;
}

if (conf->log_src.value.len == 0) {
conf->log_src = prev->log_src;
conf->log_handler = prev->log_handler;
conf->log_src_ref = prev->log_src_ref;
conf->log_src_key = prev->log_src_key;
conf->log_chunkname = prev->log_chunkname;
}

if (conf->header_filter_src.value.len == 0) {
conf->header_filter_src = prev->header_filter_src;
conf->header_filter_handler = prev->header_filter_handler;
conf->header_filter_src_ref = prev->header_filter_src_ref;
conf->header_filter_src_key = prev->header_filter_src_key;
}

if (conf->body_filter_src.value.len == 0) {
conf->body_filter_src = prev->body_filter_src;
conf->body_filter_handler = prev->body_filter_handler;
conf->body_filter_src_ref = prev->body_filter_src_ref;
conf->body_filter_src_key = prev->body_filter_src_key;
}

#if (NGX_HTTP_SSL)

# if defined(nginx_version) && nginx_version >= 1001013

ngx_conf_merge_bitmask_value(conf->ssl_protocols, prev->ssl_protocols,
(NGX_CONF_BITMASK_SET|NGX_SSL_SSLv3
|NGX_SSL_TLSv1|NGX_SSL_TLSv1_1
|NGX_SSL_TLSv1_2));

# endif

ngx_conf_merge_str_value(conf->ssl_ciphers, prev->ssl_ciphers,
"DEFAULT");

Expand Down Expand Up @@ -1297,26 +1324,13 @@ ngx_http_lua_set_ssl(ngx_conf_t *cf, ngx_http_lua_loc_conf_t *llcf)
return NGX_ERROR;
}

if (llcf->ssl_trusted_certificate.len) {

#if defined(nginx_version) && nginx_version >= 1003007

if (ngx_ssl_trusted_certificate(cf, llcf->ssl,
&llcf->ssl_trusted_certificate,
llcf->ssl_verify_depth)
!= NGX_OK)
{
return NGX_ERROR;
}

#else

ngx_log_error(NGX_LOG_CRIT, cf->log, 0, "at least nginx 1.3.7 is "
"required for the \"lua_ssl_trusted_certificate\" "
"directive");
if (llcf->ssl_trusted_certificate.len
&& ngx_ssl_trusted_certificate(cf, llcf->ssl,
&llcf->ssl_trusted_certificate,
llcf->ssl_verify_depth)
!= NGX_OK)
{
return NGX_ERROR;

#endif
}

dd("ssl crl: %.*s", (int) llcf->ssl_crl.len, llcf->ssl_crl.data);
Expand Down
120 changes: 11 additions & 109 deletions src/ngx_http_lua_ndk.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,93 +19,20 @@

static ndk_set_var_value_pt ngx_http_lookup_ndk_set_var_directive(u_char *name,
size_t name_len);
static int ngx_http_lua_ndk_set_var_get(lua_State *L);
static int ngx_http_lua_ndk_set_var_set(lua_State *L);
static int ngx_http_lua_run_set_var_directive(lua_State *L);


int
ngx_http_lua_ndk_set_var_get(lua_State *L)
{
ndk_set_var_value_pt func;
size_t len;
u_char *p;

p = (u_char *) luaL_checklstring(L, 2, &len);

dd("ndk.set_var metatable __index: %s", p);

func = ngx_http_lookup_ndk_set_var_directive(p, len);

if (func == NULL) {
return luaL_error(L, "ndk.set_var: directive \"%s\" not found "
"or does not use ndk_set_var_value", p);
}

lua_pushvalue(L, -1); /* table key key */
lua_pushvalue(L, -1); /* table key key key */
lua_pushlightuserdata(L, (void *) func); /* table key key key func */
lua_pushcclosure(L, ngx_http_lua_run_set_var_directive, 2);
/* table key key closure */
lua_rawset(L, 1); /* table key */
lua_rawget(L, 1); /* table closure */

return 1;
}


int
ngx_http_lua_ndk_set_var_set(lua_State *L)
{
return luaL_error(L, "Not allowed");
}


int
ngx_http_lua_run_set_var_directive(lua_State *L)
void
ngx_http_lua_inject_ndk_api(lua_State *L)
{
ngx_int_t rc;
ndk_set_var_value_pt func;
ngx_str_t res;
ngx_http_variable_value_t arg;
u_char *p;
size_t len;
ngx_http_request_t *r;

if (lua_gettop(L) != 1) {
return luaL_error(L, "expecting one argument");
}

#if 1
ngx_memzero(&arg, sizeof(ngx_http_variable_value_t));

arg.valid = 1;
#endif

arg.data = (u_char *) luaL_checklstring(L, 1, &len);
arg.len = len;

r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request object found");
}

p = (u_char *) luaL_checklstring(L, lua_upvalueindex(1), &len);

dd("calling set_var func for %s", p);

func = (ndk_set_var_value_pt) lua_touserdata(L, lua_upvalueindex(2));

rc = func(r, &res, &arg);

if (rc != NGX_OK) {
return luaL_error(L, "calling directive %s failed with code %d",
p, (int) rc);
}
lua_createtable(L, 0, 1 /* nrec */); /* ndk.* */

lua_pushlstring(L, (char *) res.data, res.len);
lua_getglobal(L, "package"); /* ndk package */
lua_getfield(L, -1, "loaded"); /* ndk package loaded */
lua_pushvalue(L, -3); /* ndk package loaded ndk */
lua_setfield(L, -2, "ndk"); /* ndk package loaded */
lua_pop(L, 2);

return 1;
lua_setglobal(L, "ndk");
}


Expand All @@ -119,7 +46,7 @@ ngx_http_lookup_ndk_set_var_directive(u_char *name,
ngx_module_t **modules;
ngx_command_t *cmd;

#if defined(nginx_version) && nginx_version >= 1009011
#if (nginx_version >= 1009011)
modules = ngx_cycle->modules;
#else
modules = ngx_modules;
Expand Down Expand Up @@ -160,32 +87,6 @@ ngx_http_lookup_ndk_set_var_directive(u_char *name,
}


void
ngx_http_lua_inject_ndk_api(lua_State *L)
{
lua_createtable(L, 0, 1 /* nrec */); /* ndk.* */

lua_newtable(L); /* .set_var */

lua_createtable(L, 0, 2 /* nrec */); /* metatable for .set_var */
lua_pushcfunction(L, ngx_http_lua_ndk_set_var_get);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, ngx_http_lua_ndk_set_var_set);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2);

lua_setfield(L, -2, "set_var");

lua_getglobal(L, "package"); /* ndk package */
lua_getfield(L, -1, "loaded"); /* ndk package loaded */
lua_pushvalue(L, -3); /* ndk package loaded ndk */
lua_setfield(L, -2, "ndk"); /* ndk package loaded */
lua_pop(L, 2);

lua_setglobal(L, "ndk");
}


int
ngx_http_lua_ffi_ndk_lookup_directive(const u_char *var_data,
size_t var_len, ndk_set_var_value_pt *func)
Expand Down Expand Up @@ -229,4 +130,5 @@ ngx_http_lua_ffi_ndk_set_var_get(ngx_http_request_t *r,

#endif /* defined(NDK) && NDK */


/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
99 changes: 1 addition & 98 deletions src/ngx_http_lua_phase.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,105 +10,9 @@
#include "ddebug.h"


#include "ngx_http_lua_phase.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_ctx.h"
#include "ngx_http_lua_common.h"


static int ngx_http_lua_ngx_get_phase(lua_State *L);


static int
ngx_http_lua_ngx_get_phase(lua_State *L)
{
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;

r = ngx_http_lua_get_req(L);

/* If we have no request object, assume we are called from the "init"
* phase. */

if (r == NULL) {
lua_pushliteral(L, "init");
return 1;
}

ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no request ctx found");
}

switch (ctx->context) {
case NGX_HTTP_LUA_CONTEXT_INIT_WORKER:
lua_pushliteral(L, "init_worker");
break;

case NGX_HTTP_LUA_CONTEXT_SET:
lua_pushliteral(L, "set");
break;

case NGX_HTTP_LUA_CONTEXT_REWRITE:
lua_pushliteral(L, "rewrite");
break;

case NGX_HTTP_LUA_CONTEXT_ACCESS:
lua_pushliteral(L, "access");
break;

case NGX_HTTP_LUA_CONTEXT_CONTENT:
lua_pushliteral(L, "content");
break;

case NGX_HTTP_LUA_CONTEXT_LOG:
lua_pushliteral(L, "log");
break;

case NGX_HTTP_LUA_CONTEXT_HEADER_FILTER:
lua_pushliteral(L, "header_filter");
break;

case NGX_HTTP_LUA_CONTEXT_BODY_FILTER:
lua_pushliteral(L, "body_filter");
break;

case NGX_HTTP_LUA_CONTEXT_TIMER:
lua_pushliteral(L, "timer");
break;

case NGX_HTTP_LUA_CONTEXT_BALANCER:
lua_pushliteral(L, "balancer");
break;

case NGX_HTTP_LUA_CONTEXT_SSL_CERT:
lua_pushliteral(L, "ssl_cert");
break;

case NGX_HTTP_LUA_CONTEXT_SSL_SESS_STORE:
lua_pushliteral(L, "ssl_session_store");
break;

case NGX_HTTP_LUA_CONTEXT_SSL_SESS_FETCH:
lua_pushliteral(L, "ssl_session_fetch");
break;

default:
return luaL_error(L, "unknown phase: %#x", (int) ctx->context);
}

return 1;
}


void
ngx_http_lua_inject_phase_api(lua_State *L)
{
lua_pushcfunction(L, ngx_http_lua_ngx_get_phase);
lua_setfield(L, -2, "get_phase");
}


#ifndef NGX_LUA_NO_FFI_API
int
ngx_http_lua_ffi_get_phase(ngx_http_request_t *r, char **err)
{
Expand All @@ -122,7 +26,6 @@ ngx_http_lua_ffi_get_phase(ngx_http_request_t *r, char **err)

return ctx->context;
}
#endif


/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
13 changes: 0 additions & 13 deletions src/ngx_http_lua_phase.h

This file was deleted.

Loading