Skip to content

Commit

Permalink
log_by_lua & preread_by_lua: initial implementation. #3c4cf57
Browse files Browse the repository at this point in the history
  • Loading branch information
dndx authored Sep 16, 2017
1 parent fde198f commit 0314071
Show file tree
Hide file tree
Showing 32 changed files with 3,402 additions and 68 deletions.
4 changes: 4 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
$ngx_addon_dir/src/ngx_stream_lua_args.c \
$ngx_addon_dir/src/ngx_stream_lua_ssl.c \
$ngx_addon_dir/src/ngx_stream_lua_balancer.c \
$ngx_addon_dir/src/ngx_stream_lua_logby.c \
$ngx_addon_dir/src/ngx_stream_lua_prereadby.c \
"

NGX_ADDON_DEPS="$NGX_ADDON_DEPS \
Expand Down Expand Up @@ -379,6 +381,8 @@ NGX_ADDON_DEPS="$NGX_ADDON_DEPS \
$ngx_addon_dir/src/ngx_stream_lua_args.h \
$ngx_addon_dir/src/ngx_stream_lua_ssl.h \
$ngx_addon_dir/src/ngx_stream_lua_balancer.h \
$ngx_addon_dir/src/ngx_stream_lua_logby.h \
$ngx_addon_dir/src/ngx_stream_lua_prereadby.h \
"

ngx_feature="export symbols by default (-E)"
Expand Down
24 changes: 21 additions & 3 deletions src/ngx_stream_lua_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
#define NGX_STREAM_LUA_CONTEXT_TIMER 0x0004
#define NGX_STREAM_LUA_CONTEXT_INIT_WORKER 0x0008
#define NGX_STREAM_LUA_CONTEXT_BALANCER 0x0010
#define NGX_STREAM_LUA_CONTEXT_PREREAD 0x0020



Expand Down Expand Up @@ -216,9 +217,12 @@ struct ngx_stream_lua_main_conf_s {



unsigned requires_preread:1;

unsigned requires_log:1;
unsigned requires_shm:1;


};


Expand All @@ -240,10 +244,17 @@ struct ngx_stream_lua_srv_conf_s {
ngx_flag_t enable_code_cache; /* whether to enable
code cache */


ngx_stream_lua_handler_pt preread_handler;

ngx_stream_lua_handler_pt content_handler;
ngx_stream_lua_handler_pt log_handler;

u_char *preread_chunkname;
ngx_stream_complex_value_t preread_src; /* access_by_lua
inline script/script
file path */

u_char *preread_src_key; /* cached key for access_src */


u_char *content_chunkname;
Expand All @@ -253,6 +264,12 @@ struct ngx_stream_lua_srv_conf_s {

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

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

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



ngx_msec_t keepalive_timeout;
Expand Down Expand Up @@ -453,8 +470,9 @@ typedef struct ngx_stream_lua_ctx_s {
unsigned headers_set:1; /* whether the user has set custom
response headers */

unsigned entered_rewrite_phase:1;
unsigned entered_access_phase:1;

unsigned entered_preread_phase:1;

unsigned entered_content_phase:1;

unsigned buffering:1; /* HTTP 1.0 response body buffering flag */
Expand Down
2 changes: 1 addition & 1 deletion src/ngx_stream_lua_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ngx_stream_lua_ngx_exit(lua_State *L)
ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
| NGX_STREAM_LUA_CONTEXT_TIMER
| NGX_STREAM_LUA_CONTEXT_BALANCER

| NGX_STREAM_LUA_CONTEXT_PREREAD
);

rc = (ngx_int_t) luaL_checkinteger(L, 1);
Expand Down
8 changes: 4 additions & 4 deletions src/ngx_stream_lua_coroutine.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ ngx_stream_lua_coroutine_create_helper(lua_State *L, ngx_stream_lua_request_t *r

ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
| NGX_STREAM_LUA_CONTEXT_TIMER

| NGX_STREAM_LUA_CONTEXT_PREREAD
);

vm = ngx_stream_lua_get_lua_vm(r, ctx);
Expand Down Expand Up @@ -150,7 +150,7 @@ ngx_stream_lua_coroutine_resume(lua_State *L)

ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
| NGX_STREAM_LUA_CONTEXT_TIMER

| NGX_STREAM_LUA_CONTEXT_PREREAD
);

p_coctx = ctx->cur_co_ctx;
Expand Down Expand Up @@ -209,7 +209,7 @@ ngx_stream_lua_coroutine_yield(lua_State *L)

ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
| NGX_STREAM_LUA_CONTEXT_TIMER

| NGX_STREAM_LUA_CONTEXT_PREREAD
);

coctx = ctx->cur_co_ctx;
Expand Down Expand Up @@ -357,7 +357,7 @@ ngx_stream_lua_coroutine_status(lua_State *L)

ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
| NGX_STREAM_LUA_CONTEXT_TIMER

| NGX_STREAM_LUA_CONTEXT_PREREAD
);

coctx = ngx_stream_lua_get_co_ctx(co, ctx);
Expand Down
223 changes: 221 additions & 2 deletions src/ngx_stream_lua_directive.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
#include "ngx_stream_lua_util.h"
#include "ngx_stream_lua_cache.h"
#include "ngx_stream_lua_contentby.h"

#include "ngx_stream_lua_logby.h"
#include "ngx_stream_lua_initby.h"
#include "ngx_stream_lua_initworkerby.h"
#include "ngx_stream_lua_shdict.h"

#include "ngx_stream_lua_lex.h"
#include "api/ngx_stream_lua_api.h"

#include "ngx_stream_lua_prereadby.h"


typedef struct ngx_stream_lua_block_parser_ctx_s
Expand Down Expand Up @@ -222,6 +222,116 @@ ngx_stream_lua_package_path(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)




char *
ngx_stream_lua_preread_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
char *rv;
ngx_conf_t save;

save = *cf;
cf->handler = ngx_stream_lua_preread_by_lua;
cf->handler_conf = conf;

rv = ngx_stream_lua_conf_lua_block_parse(cf, cmd);

*cf = save;

return rv;
}


char *
ngx_stream_lua_preread_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
u_char *p, *chunkname;
ngx_str_t *value;
ngx_stream_lua_main_conf_t *lmcf;
ngx_stream_lua_srv_conf_t *lscf = conf;

ngx_stream_compile_complex_value_t ccv;

dd("enter");

/* must specify a content handler */
if (cmd->post == NULL) {
return NGX_CONF_ERROR;
}

if (lscf->preread_handler) {
return "is duplicate";
}

value = cf->args->elts;

if (value[1].len == 0) {
/* Oops...Invalid server conf */
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
"invalid server config: no runnable Lua code");

return NGX_CONF_ERROR;
}

if (cmd->post == ngx_stream_lua_preread_handler_inline) {
chunkname = ngx_stream_lua_gen_chunk_name(cf, "preread_by_lua",
sizeof("preread_by_lua") - 1);
if (chunkname == NULL) {
return NGX_CONF_ERROR;
}

lscf->preread_chunkname = chunkname;

/* Don't eval nginx variables for inline lua code */

lscf->preread_src.value = value[1];

p = ngx_palloc(cf->pool, NGX_STREAM_LUA_INLINE_KEY_LEN + 1);
if (p == NULL) {
return NGX_CONF_ERROR;
}

lscf->preread_src_key = p;

p = ngx_copy(p, NGX_STREAM_LUA_INLINE_TAG, NGX_STREAM_LUA_INLINE_TAG_LEN);
p = ngx_stream_lua_digest_hex(p, value[1].data, value[1].len);
*p = '\0';

} else {
ngx_memzero(&ccv, sizeof(ngx_stream_compile_complex_value_t));
ccv.cf = cf;
ccv.value = &value[1];
ccv.complex_value = &lscf->preread_src;

if (ngx_stream_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}

if (lscf->preread_src.lengths == NULL) {
/* no variable found */
p = ngx_palloc(cf->pool, NGX_STREAM_LUA_FILE_KEY_LEN + 1);
if (p == NULL) {
return NGX_CONF_ERROR;
}

lscf->preread_src_key = p;

p = ngx_copy(p, NGX_STREAM_LUA_FILE_TAG, NGX_STREAM_LUA_FILE_TAG_LEN);
p = ngx_stream_lua_digest_hex(p, value[1].data, value[1].len);
*p = '\0';
}
}

lscf->preread_handler = (ngx_stream_lua_handler_pt) cmd->post;

lmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_lua_module);

lmcf->requires_preread = 1;

return NGX_CONF_OK;
}


char *
ngx_stream_lua_content_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
Expand Down Expand Up @@ -353,6 +463,115 @@ ngx_stream_lua_content_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}


char *
ngx_stream_lua_log_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
char *rv;
ngx_conf_t save;

save = *cf;
cf->handler = ngx_stream_lua_log_by_lua;
cf->handler_conf = conf;

rv = ngx_stream_lua_conf_lua_block_parse(cf, cmd);

*cf = save;

return rv;
}


char *
ngx_stream_lua_log_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
u_char *p, *chunkname;
ngx_str_t *value;
ngx_stream_lua_main_conf_t *lmcf;
ngx_stream_lua_loc_conf_t *llcf = conf;

ngx_stream_compile_complex_value_t ccv;

dd("enter");

/* must specify a log handler */
if (cmd->post == NULL) {
return NGX_CONF_ERROR;
}

if (llcf->log_handler) {
return "is duplicate";
}

value = cf->args->elts;

if (value[1].len == 0) {
/* Oops...Invalid location conf */
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
"invalid location config: no runnable Lua code");

return NGX_CONF_ERROR;
}

if (cmd->post == ngx_stream_lua_log_handler_inline) {
chunkname = ngx_stream_lua_gen_chunk_name(cf, "log_by_lua",
sizeof("log_by_lua") - 1);
if (chunkname == NULL) {
return NGX_CONF_ERROR;
}

llcf->log_chunkname = chunkname;

/* Don't eval nginx variables for inline lua code */

llcf->log_src.value = value[1];

p = ngx_palloc(cf->pool, NGX_STREAM_LUA_INLINE_KEY_LEN + 1);
if (p == NULL) {
return NGX_CONF_ERROR;
}

llcf->log_src_key = p;

p = ngx_copy(p, NGX_STREAM_LUA_INLINE_TAG, NGX_STREAM_LUA_INLINE_TAG_LEN);
p = ngx_stream_lua_digest_hex(p, value[1].data, value[1].len);
*p = '\0';

} else {
ngx_memzero(&ccv, sizeof(ngx_stream_compile_complex_value_t));
ccv.cf = cf;
ccv.value = &value[1];
ccv.complex_value = &llcf->log_src;

if (ngx_stream_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}

if (llcf->log_src.lengths == NULL) {
/* no variable found */
p = ngx_palloc(cf->pool, NGX_STREAM_LUA_FILE_KEY_LEN + 1);
if (p == NULL) {
return NGX_CONF_ERROR;
}

llcf->log_src_key = p;

p = ngx_copy(p, NGX_STREAM_LUA_FILE_TAG, NGX_STREAM_LUA_FILE_TAG_LEN);
p = ngx_stream_lua_digest_hex(p, value[1].data, value[1].len);
*p = '\0';
}
}

llcf->log_handler = (ngx_stream_lua_handler_pt) cmd->post;

lmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_lua_module);

lmcf->requires_log = 1;

return NGX_CONF_OK;
}





Expand Down
Loading

0 comments on commit 0314071

Please sign in to comment.