Skip to content

Commit

Permalink
feature: added new API function ngx.req.is_internal() for testing if …
Browse files Browse the repository at this point in the history
…the current request is an internal request.

thanks Ruoshan Huang for the patch in #350.
  • Loading branch information
agentzh committed Dec 11, 2015
1 parent c3ac14e commit 00f62a5
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.markdown
Expand Up @@ -2652,6 +2652,7 @@ Nginx API for Lua
* [ngx.status](#ngxstatus)
* [ngx.header.HEADER](#ngxheaderheader)
* [ngx.resp.get_headers](#ngxrespget_headers)
* [ngx.req.is_internal](#ngxreqis_internal)
* [ngx.req.start_time](#ngxreqstart_time)
* [ngx.req.http_version](#ngxreqhttp_version)
* [ngx.req.raw_header](#ngxreqraw_header)
Expand Down Expand Up @@ -3663,6 +3664,21 @@ This API was first introduced in the `v0.9.5` release.

[Back to TOC](#nginx-api-for-lua)

ngx.req.is_internal
-------------------
**syntax:** *is_internal = ngx.req.is_internal()*

**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua**

Returns a boolean indicating whether the current request is an "internal request", i.e.,
a request initiated from inside the current nginx server instead of from the client side.

Subrequests are all internal requests and so are requests after internal redirects.

This API was first introduced in the `v0.9.20` release.

[Back to TOC](#nginx-api-for-lua)

ngx.req.start_time
------------------
**syntax:** *secs = ngx.req.start_time()*
Expand Down
12 changes: 12 additions & 0 deletions doc/HttpLuaModule.wiki
Expand Up @@ -3010,6 +3010,18 @@ This function has the same signature as [[#ngx.req.get_headers|ngx.req.get_heade
This API was first introduced in the <code>v0.9.5</code> release.
== ngx.req.is_internal ==
'''syntax:''' ''is_internal = ngx.req.is_internal()''
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*''
Returns a boolean indicating whether the current request is an "internal request", i.e.,
a request initiated from inside the current nginx server instead of from the client side.
Subrequests are all internal requests and so are requests after internal redirects.
This API was first introduced in the <code>v0.9.20</code> release.
== ngx.req.start_time ==
'''syntax:''' ''secs = ngx.req.start_time()''
Expand Down
24 changes: 24 additions & 0 deletions src/ngx_http_lua_misc.c
Expand Up @@ -18,6 +18,7 @@

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
Expand All @@ -33,6 +34,29 @@ ngx_http_lua_inject_misc_api(lua_State *L)
}


void
ngx_http_lua_inject_req_misc_api(lua_State *L)
{
lua_pushcfunction(L, ngx_http_lua_ngx_req_is_internal);
lua_setfield(L, -2, "is_internal");
}


static int
ngx_http_lua_ngx_req_is_internal(lua_State *L)
{
ngx_http_request_t *r;

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

lua_pushboolean(L, r->internal == 1);
return 1;
}


static int
ngx_http_lua_ngx_get(lua_State *L)
{
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_http_lua_misc.h
Expand Up @@ -14,6 +14,8 @@

void ngx_http_lua_inject_misc_api(lua_State *L);

void ngx_http_lua_inject_req_misc_api(lua_State *L);


#endif /* _NGX_HTTP_LUA_MISC_H_INCLUDED_ */

Expand Down
1 change: 1 addition & 0 deletions src/ngx_http_lua_util.c
Expand Up @@ -2099,6 +2099,7 @@ ngx_http_lua_inject_req_api(ngx_log_t *log, lua_State *L)
ngx_http_lua_inject_req_socket_api(L);
ngx_http_lua_inject_req_method_api(L);
ngx_http_lua_inject_req_time_api(L);
ngx_http_lua_inject_req_misc_api(L);

lua_setfield(L, -2, "req");
}
Expand Down
62 changes: 62 additions & 0 deletions t/137-req-misc.t
@@ -0,0 +1,62 @@
use Test::Nginx::Socket::Lua;

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

repeat_each(2);

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

$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;

#no_diff();
no_long_string();
#no_shuffle();

run_tests();

__DATA__

=== TEST 1: not internal request
--- config
location /test {
rewrite ^/test$ /lua last;
}
location /lua {
content_by_lua '
if ngx.req.is_internal() then
ngx.say("internal")
else
ngx.say("not internal")
end
';
}
--- request
GET /lua
--- response_body
not internal



=== TEST 2: internal request
--- config
location /test {
rewrite ^/test$ /lua last;
}
location /lua {
content_by_lua '
if ngx.req.is_internal() then
ngx.say("internal")
else
ngx.say("not internal")
end
';
}
--- request
GET /test
--- response_body
internal

0 comments on commit 00f62a5

Please sign in to comment.