Skip to content

Commit 00f62a5

Browse files
committed
feature: added new API function ngx.req.is_internal() for testing if the current request is an internal request.
thanks Ruoshan Huang for the patch in #350.
1 parent c3ac14e commit 00f62a5

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

README.markdown

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,7 @@ Nginx API for Lua
26522652
* [ngx.status](#ngxstatus)
26532653
* [ngx.header.HEADER](#ngxheaderheader)
26542654
* [ngx.resp.get_headers](#ngxrespget_headers)
2655+
* [ngx.req.is_internal](#ngxreqis_internal)
26552656
* [ngx.req.start_time](#ngxreqstart_time)
26562657
* [ngx.req.http_version](#ngxreqhttp_version)
26572658
* [ngx.req.raw_header](#ngxreqraw_header)
@@ -3663,6 +3664,21 @@ This API was first introduced in the `v0.9.5` release.
36633664

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

3667+
ngx.req.is_internal
3668+
-------------------
3669+
**syntax:** *is_internal = ngx.req.is_internal()*
3670+
3671+
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua**
3672+
3673+
Returns a boolean indicating whether the current request is an "internal request", i.e.,
3674+
a request initiated from inside the current nginx server instead of from the client side.
3675+
3676+
Subrequests are all internal requests and so are requests after internal redirects.
3677+
3678+
This API was first introduced in the `v0.9.20` release.
3679+
3680+
[Back to TOC](#nginx-api-for-lua)
3681+
36663682
ngx.req.start_time
36673683
------------------
36683684
**syntax:** *secs = ngx.req.start_time()*

doc/HttpLuaModule.wiki

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,6 +3010,18 @@ This function has the same signature as [[#ngx.req.get_headers|ngx.req.get_heade
30103010
30113011
This API was first introduced in the <code>v0.9.5</code> release.
30123012
3013+
== ngx.req.is_internal ==
3014+
'''syntax:''' ''is_internal = ngx.req.is_internal()''
3015+
3016+
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*''
3017+
3018+
Returns a boolean indicating whether the current request is an "internal request", i.e.,
3019+
a request initiated from inside the current nginx server instead of from the client side.
3020+
3021+
Subrequests are all internal requests and so are requests after internal redirects.
3022+
3023+
This API was first introduced in the <code>v0.9.20</code> release.
3024+
30133025
== ngx.req.start_time ==
30143026
'''syntax:''' ''secs = ngx.req.start_time()''
30153027

src/ngx_http_lua_misc.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
static int ngx_http_lua_ngx_get(lua_State *L);
2020
static int ngx_http_lua_ngx_set(lua_State *L);
21+
static int ngx_http_lua_ngx_req_is_internal(lua_State *L);
2122

2223

2324
void
@@ -33,6 +34,29 @@ ngx_http_lua_inject_misc_api(lua_State *L)
3334
}
3435

3536

37+
void
38+
ngx_http_lua_inject_req_misc_api(lua_State *L)
39+
{
40+
lua_pushcfunction(L, ngx_http_lua_ngx_req_is_internal);
41+
lua_setfield(L, -2, "is_internal");
42+
}
43+
44+
45+
static int
46+
ngx_http_lua_ngx_req_is_internal(lua_State *L)
47+
{
48+
ngx_http_request_t *r;
49+
50+
r = ngx_http_lua_get_req(L);
51+
if (r == NULL) {
52+
return luaL_error(L, "no request object found");
53+
}
54+
55+
lua_pushboolean(L, r->internal == 1);
56+
return 1;
57+
}
58+
59+
3660
static int
3761
ngx_http_lua_ngx_get(lua_State *L)
3862
{

src/ngx_http_lua_misc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
void ngx_http_lua_inject_misc_api(lua_State *L);
1616

17+
void ngx_http_lua_inject_req_misc_api(lua_State *L);
18+
1719

1820
#endif /* _NGX_HTTP_LUA_MISC_H_INCLUDED_ */
1921

src/ngx_http_lua_util.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,7 @@ ngx_http_lua_inject_req_api(ngx_log_t *log, lua_State *L)
20992099
ngx_http_lua_inject_req_socket_api(L);
21002100
ngx_http_lua_inject_req_method_api(L);
21012101
ngx_http_lua_inject_req_time_api(L);
2102+
ngx_http_lua_inject_req_misc_api(L);
21022103

21032104
lua_setfield(L, -2, "req");
21042105
}

t/137-req-misc.t

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use Test::Nginx::Socket::Lua;
2+
3+
#master_on();
4+
#workers(1);
5+
#worker_connections(1014);
6+
#log_level('warn');
7+
#master_process_enabled(1);
8+
9+
repeat_each(2);
10+
11+
plan tests => repeat_each() * blocks() * 2;
12+
13+
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
14+
15+
#no_diff();
16+
no_long_string();
17+
#no_shuffle();
18+
19+
run_tests();
20+
21+
__DATA__
22+
23+
=== TEST 1: not internal request
24+
--- config
25+
location /test {
26+
rewrite ^/test$ /lua last;
27+
}
28+
location /lua {
29+
content_by_lua '
30+
if ngx.req.is_internal() then
31+
ngx.say("internal")
32+
else
33+
ngx.say("not internal")
34+
end
35+
';
36+
}
37+
--- request
38+
GET /lua
39+
--- response_body
40+
not internal
41+
42+
43+
44+
=== TEST 2: internal request
45+
--- config
46+
location /test {
47+
rewrite ^/test$ /lua last;
48+
}
49+
location /lua {
50+
content_by_lua '
51+
if ngx.req.is_internal() then
52+
ngx.say("internal")
53+
else
54+
ngx.say("not internal")
55+
end
56+
';
57+
}
58+
--- request
59+
GET /test
60+
--- response_body
61+
internal
62+

0 commit comments

Comments
 (0)