Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature suggestion for an ngx.get_phase() function. #140

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
$ngx_addon_dir/src/ngx_http_lua_initby.c \
$ngx_addon_dir/src/ngx_http_lua_socket_udp.c \
$ngx_addon_dir/src/ngx_http_lua_req_method.c \
$ngx_addon_dir/src/ngx_http_lua_phase.c \
"

NGX_ADDON_DEPS="$NGX_ADDON_DEPS \
Expand Down Expand Up @@ -229,6 +230,7 @@ NGX_ADDON_DEPS="$NGX_ADDON_DEPS \
$ngx_addon_dir/src/ngx_http_lua_initby.h \
$ngx_addon_dir/src/ngx_http_lua_socket_udp.h \
$ngx_addon_dir/src/ngx_http_lua_req_method.h \
$ngx_addon_dir/src/ngx_http_lua_phase.h \
"

CFLAGS="$CFLAGS -DNDK_SET_VAR"
Expand Down
73 changes: 73 additions & 0 deletions src/ngx_http_lua_phase.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"

#include "ngx_http_lua_phase.h"

#include "ngx_http_lua_util.h"
#include "ngx_http_lua_ctx.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;

lua_pushlightuserdata(L, &ngx_http_lua_request_key);
lua_rawget(L, LUA_GLOBALSINDEX);
r = lua_touserdata(L, -1);
lua_pop(L, 1);

/* If we have no request object, assume we are called from the "init" phase. */
if (r == NULL) {
lua_pushlstring(L, (char *) "init", sizeof("init") - 1);
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_SET:
lua_pushlstring(L, (char *) "set", sizeof("set") - 1);
break;
case NGX_HTTP_LUA_CONTEXT_REWRITE:
lua_pushlstring(L, (char *) "rewrite", sizeof("rewrite") - 1);
break;
case NGX_HTTP_LUA_CONTEXT_ACCESS:
lua_pushlstring(L, (char *) "access", sizeof("access") - 1);
break;
case NGX_HTTP_LUA_CONTEXT_CONTENT:
lua_pushlstring(L, (char *) "content", sizeof("content") - 1);
break;
case NGX_HTTP_LUA_CONTEXT_LOG:
lua_pushlstring(L, (char *) "log", sizeof("log") - 1);
break;
case NGX_HTTP_LUA_CONTEXT_HEADER_FILTER:
lua_pushlstring(L, (char *) "header_filter", sizeof("header_filter") - 1);
break;
case NGX_HTTP_LUA_CONTEXT_BODY_FILTER:
lua_pushlstring(L, (char *) "body_filter", sizeof("body_filter") - 1);
break;

default:
luaL_error(L, "unknown phase: %d", 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");
}

11 changes: 11 additions & 0 deletions src/ngx_http_lua_phase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef NGX_HTTP_LUA_PHASE_H
#define NGX_HTTP_LUA_PHASE_H


#include "ngx_http_lua_common.h"


void ngx_http_lua_inject_phase_api(lua_State *L);


#endif /* NGX_HTTP_LUA_PHASE_H */
2 changes: 2 additions & 0 deletions src/ngx_http_lua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "ngx_http_lua_headerfilterby.h"
#include "ngx_http_lua_bodyfilterby.h"
#include "ngx_http_lua_logby.h"
#include "ngx_http_lua_phase.h"


char ngx_http_lua_code_cache_key;
Expand Down Expand Up @@ -587,6 +588,7 @@ ngx_http_lua_inject_ngx_api(ngx_conf_t *cf, lua_State *L)
ngx_http_lua_inject_control_api(cf->log, L);
ngx_http_lua_inject_subrequest_api(L);
ngx_http_lua_inject_sleep_api(L);
ngx_http_lua_inject_phase_api(L);
#if (NGX_PCRE)
ngx_http_lua_inject_regex_api(L);
#endif
Expand Down
130 changes: 130 additions & 0 deletions t/089-phase.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib 'lib';
use Test::Nginx::Socket;

#worker_connections(1014);
#master_process_enabled(1);
log_level('warn');

repeat_each(2);
#repeat_each(1);

plan tests => repeat_each() * (blocks() * 2) - 2;

#no_diff();
#no_long_string();
run_tests();

__DATA__

=== TEST 1: get_phase in init_by_lua
--- http_config
init_by_lua 'phase = ngx.get_phase()';
--- config
location /lua {
content_by_lua '
ngx.say(phase)
';
}
--- request
GET /lua
--- response_body
init


=== TEST 2: get_phase in set_by_lua
--- config
set_by_lua $phase 'return ngx.get_phase()';
location /lua {
content_by_lua '
ngx.say(ngx.var.phase)
';
}
--- request
GET /lua
--- response_body
set


=== TEST 3: get_phase in rewrite_by_lua
--- config
location /lua {
rewrite_by_lua '
ngx.say(ngx.get_phase())
ngx.exit(200)
';
}
--- request
GET /lua
--- response_body
rewrite


=== TEST 4: get_phase in access_by_lua
--- config
location /lua {
access_by_lua '
ngx.say(ngx.get_phase())
ngx.exit(200)
';
}
--- request
GET /lua
--- response_body
access


=== TEST 5: get_phase in content_by_lua
--- config
location /lua {
content_by_lua '
ngx.say(ngx.get_phase())
';
}
--- request
GET /lua
--- response_body
content


=== TEST 6: get_phase in header_filter_by_lua
--- config
location /lua {
echo "OK";
header_filter_by_lua '
ngx.header.Phase = ngx.get_phase()
';
}
--- request
GET /lua
--- response_header
Phase: header_filter


=== TEST 7: get_phase in body_filter_by_lua
--- config
location /lua {
content_by_lua '
ngx.exit(200)
';
body_filter_by_lua '
ngx.arg[1] = ngx.get_phase()
';
}
--- request
GET /lua
--- response_body chop
body_filter

=== TEST 8: get_phase in log_by_lua
--- config
location /lua {
echo "OK";
log_by_lua '
ngx.log(ngx.ERR, ngx.get_phase())
';
}
--- request
GET /lua
--- error_log
log