now we prohibit use of true jump argument in ngx.req.set_uri() in contexts other than rewrite_by_lua and rewrite_by_lua_file. a lua exception will be thrown if the context is incorrect.
This commit is contained in:
Родитель
6ff491bd19
Коммит
6c1c1666b0
4
README
4
README
|
@ -1138,6 +1138,10 @@ Nginx API for Lua
|
|||
|
||||
ngx.req.set_uri("/foo")
|
||||
|
||||
The "jump" can only be set to "true" in rewrite_by_lua and
|
||||
rewrite_by_lua_file. Use of jump in other contexts is prohibited and
|
||||
will throw out a Lua exception.
|
||||
|
||||
A more sophisticated example involving regex substitutions is as follows
|
||||
|
||||
location /test {
|
||||
|
|
|
@ -1232,6 +1232,8 @@ or equivalently,
|
|||
ngx.req.set_uri("/foo")
|
||||
|
||||
|
||||
The `jump` can only be set to `true` in [rewrite_by_lua](http://wiki.nginx.org/HttpLuaModule#rewrite_by_lua) and [rewrite_by_lua_file](http://wiki.nginx.org/HttpLuaModule#rewrite_by_lua_file). Use of jump in other contexts is prohibited and will throw out a Lua exception.
|
||||
|
||||
A more sophisticated example involving regex substitutions is as follows
|
||||
|
||||
|
||||
|
|
|
@ -1197,6 +1197,8 @@ or equivalently,
|
|||
ngx.req.set_uri("/foo")
|
||||
</geshi>
|
||||
|
||||
The <code>jump</code> can only be set to <code>true</code> in [[#rewrite_by_lua|rewrite_by_lua]] and [[#rewrite_by_lua_file|rewrite_by_lua_file]]. Use of jump in other contexts is prohibited and will throw out a Lua exception.
|
||||
|
||||
A more sophisticated example involving regex substitutions is as follows
|
||||
|
||||
<geshi lang="nginx">
|
||||
|
|
|
@ -23,6 +23,7 @@ ngx_http_lua_ngx_req_set_uri(lua_State *L) {
|
|||
u_char *p;
|
||||
int n;
|
||||
int jump = 0;
|
||||
ngx_http_lua_ctx_t *ctx;
|
||||
|
||||
n = lua_gettop(L);
|
||||
|
||||
|
@ -60,9 +61,29 @@ ngx_http_lua_ngx_req_set_uri(lua_State *L) {
|
|||
ngx_http_set_exten(r);
|
||||
|
||||
if (jump) {
|
||||
r->uri_changed = 1;
|
||||
|
||||
return lua_yield(L, 0);
|
||||
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
|
||||
|
||||
#if defined(DDEBUG) && DDEBUG
|
||||
if (ctx) {
|
||||
dd("rewrite: %d, access: %d, content: %d",
|
||||
(int) ctx->entered_rewrite_phase,
|
||||
(int) ctx->entered_access_phase,
|
||||
(int) ctx->entered_content_phase);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (ctx && ctx->entered_rewrite_phase
|
||||
&& !ctx->entered_access_phase
|
||||
&& !ctx->entered_content_phase)
|
||||
{
|
||||
r->uri_changed = 1;
|
||||
return lua_yield(L, 0);
|
||||
}
|
||||
|
||||
return luaL_error(L, "attempt to call ngx.req.set_uri to do "
|
||||
"location jump in contexts other than rewrite_by_lua and "
|
||||
"rewrite_by_lua_file");
|
||||
}
|
||||
|
||||
r->valid_location = 0;
|
||||
|
|
121
t/030-uri-args.t
121
t/030-uri-args.t
|
@ -17,7 +17,7 @@ no_root_location();
|
|||
$ENV{TEST_NGINX_CLIENT_PORT} ||= $ENV{TEST_NGINX} ||= server_port();
|
||||
|
||||
#no_diff();
|
||||
#no_long_string();
|
||||
no_long_string();
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
@ -504,3 +504,122 @@ HTTP/1.0 ca%20t=%25
|
|||
--- response_body
|
||||
hello
|
||||
|
||||
|
||||
|
||||
=== TEST 20: ngx.req.set_uri with jump not allowed in access phase
|
||||
--- config
|
||||
location /bar {
|
||||
echo $query_string;
|
||||
}
|
||||
location /foo {
|
||||
#set $args 'hello';
|
||||
set $err '';
|
||||
access_by_lua '
|
||||
res, err = pcall(ngx.req.set_uri, "/bar", true);
|
||||
ngx.var.err = err
|
||||
';
|
||||
echo "err: $err";
|
||||
}
|
||||
--- request
|
||||
GET /foo?world
|
||||
--- response_body
|
||||
err: attempt to call ngx.req.set_uri to do location jump in contexts other than rewrite_by_lua and rewrite_by_lua_file
|
||||
|
||||
|
||||
|
||||
=== TEST 21: ngx.req.set_uri without jump allowed in access phase
|
||||
--- config
|
||||
location /bar {
|
||||
echo $query_string;
|
||||
}
|
||||
location /foo {
|
||||
#set $args 'hello';
|
||||
set $err '';
|
||||
access_by_lua '
|
||||
ngx.req.set_uri("/bar")
|
||||
';
|
||||
echo "uri: $uri";
|
||||
}
|
||||
--- request
|
||||
GET /foo?world
|
||||
--- response_body
|
||||
uri: /bar
|
||||
|
||||
|
||||
|
||||
=== TEST 22: ngx.req.set_uri with jump not allowed in content phase
|
||||
--- config
|
||||
location /bar {
|
||||
echo $query_string;
|
||||
}
|
||||
location /foo {
|
||||
#set $args 'hello';
|
||||
content_by_lua '
|
||||
res, err = pcall(ngx.req.set_uri, "/bar", true);
|
||||
ngx.say("err: ", err)
|
||||
';
|
||||
}
|
||||
--- request
|
||||
GET /foo?world
|
||||
--- response_body
|
||||
err: attempt to call ngx.req.set_uri to do location jump in contexts other than rewrite_by_lua and rewrite_by_lua_file
|
||||
|
||||
|
||||
|
||||
=== TEST 23: ngx.req.set_uri without jump allowed in content phase
|
||||
--- config
|
||||
location /bar {
|
||||
echo $query_string;
|
||||
}
|
||||
location /foo {
|
||||
#set $args 'hello';
|
||||
set $err '';
|
||||
content_by_lua '
|
||||
ngx.req.set_uri("/bar")
|
||||
ngx.say("uri: ", ngx.var.uri)
|
||||
';
|
||||
}
|
||||
--- request
|
||||
GET /foo?world
|
||||
--- response_body
|
||||
uri: /bar
|
||||
|
||||
|
||||
|
||||
=== TEST 24: ngx.req.set_uri with jump not allowed in set_by_lua
|
||||
--- config
|
||||
location /bar {
|
||||
echo $query_string;
|
||||
}
|
||||
location /foo {
|
||||
#set $args 'hello';
|
||||
set_by_lua $err '
|
||||
res, err = pcall(ngx.req.set_uri, "/bar", true);
|
||||
return err
|
||||
';
|
||||
echo "err: $err";
|
||||
}
|
||||
--- request
|
||||
GET /foo?world
|
||||
--- response_body
|
||||
err: attempt to call ngx.req.set_uri to do location jump in contexts other than rewrite_by_lua and rewrite_by_lua_file
|
||||
|
||||
|
||||
|
||||
=== TEST 25: ngx.req.set_uri without jump is allowed in set_by_lua
|
||||
--- config
|
||||
location /bar {
|
||||
echo $query_string;
|
||||
}
|
||||
location /foo {
|
||||
set_by_lua $dummy '
|
||||
ngx.req.set_uri("/bar")
|
||||
return ""
|
||||
';
|
||||
echo "uri: $uri";
|
||||
}
|
||||
--- request
|
||||
GET /foo?world
|
||||
--- response_body
|
||||
uri: /bar
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче