Skip to content

Commit 932584b

Browse files
committed
feature: added new constant ngx.HTTP_TEMPORARY_REDIRECT (307) and support for 307 in ngx.redirect(). thanks RocFang for the patch in openresty#416.
1 parent b75aa93 commit 932584b

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

src/ngx_http_lua_consts.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ ngx_http_lua_inject_http_consts(lua_State *L)
112112
lua_pushinteger(L, NGX_HTTP_NOT_MODIFIED);
113113
lua_setfield(L, -2, "HTTP_NOT_MODIFIED");
114114

115+
lua_pushinteger(L, NGX_HTTP_TEMPORARY_REDIRECT);
116+
lua_setfield(L, -2, "HTTP_TEMPORARY_REDIRECT");
117+
115118
lua_pushinteger(L, NGX_HTTP_BAD_REQUEST);
116119
lua_setfield(L, -2, "HTTP_BAD_REQUEST");
117120

src/ngx_http_lua_control.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,15 @@ ngx_http_lua_ngx_redirect(lua_State *L)
209209
if (n == 2) {
210210
rc = (ngx_int_t) luaL_checknumber(L, 2);
211211

212-
if (rc != NGX_HTTP_MOVED_TEMPORARILY &&
213-
rc != NGX_HTTP_MOVED_PERMANENTLY)
212+
if (rc != NGX_HTTP_MOVED_TEMPORARILY
213+
&& rc != NGX_HTTP_MOVED_PERMANENTLY
214+
&& rc != NGX_HTTP_TEMPORARY_REDIRECT)
214215
{
215-
return luaL_error(L, "only ngx.HTTP_MOVED_TEMPORARILY and "
216-
"ngx.HTTP_MOVED_PERMANENTLY are allowed");
216+
return luaL_error(L, "only ngx.HTTP_MOVED_TEMPORARILY, "
217+
"ngx.HTTP_MOVED_PERMANENTLY, and "
218+
"ngx.HTTP_TEMPORARY_REDIRECT are allowed");
217219
}
220+
218221
} else {
219222
rc = NGX_HTTP_MOVED_TEMPORARILY;
220223
}

t/022-redirect.t

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use Test::Nginx::Socket::Lua;
99
repeat_each(2);
1010
#repeat_each(1);
1111

12-
plan tests => repeat_each() * (blocks() * 3 + 1);
12+
plan tests => repeat_each() * (blocks() * 3 + 2);
1313

1414
#no_diff();
1515
#no_long_string();
@@ -83,6 +83,8 @@ GET /read
8383
!Location
8484
--- response_body_like: 500 Internal Server Error
8585
--- error_code: 500
86+
--- error_log
87+
only ngx.HTTP_MOVED_TEMPORARILY, ngx.HTTP_MOVED_PERMANENTLY, and ngx.HTTP_TEMPORARY_REDIRECT are allowed
8688

8789

8890

@@ -166,3 +168,53 @@ Location: http://agentzh.org/foo?bar=3
166168
--- response_body eval
167169
[qr/302 Found/, qr/302 Found/]
168170

171+
172+
173+
=== TEST 9: explicit 307
174+
--- config
175+
location /read {
176+
content_by_lua '
177+
ngx.redirect("http://agentzh.org/foo", ngx.HTTP_TEMPORARY_REDIRECT);
178+
ngx.say("hi")
179+
';
180+
}
181+
--- request
182+
GET /read
183+
--- response_headers
184+
Location: http://agentzh.org/foo
185+
--- response_body_like: 307 Temporary Redirect
186+
--- error_code: 307
187+
188+
189+
190+
=== TEST 10: explicit 307 with args
191+
--- config
192+
location /read {
193+
content_by_lua '
194+
ngx.redirect("http://agentzh.org/foo?a=b&c=d", ngx.HTTP_TEMPORARY_REDIRECT);
195+
ngx.say("hi")
196+
';
197+
}
198+
--- request
199+
GET /read
200+
--- response_headers
201+
Location: http://agentzh.org/foo?a=b&c=d
202+
--- response_body_like: 307 Temporary Redirect
203+
--- error_code: 307
204+
205+
206+
207+
=== TEST 11: explicit 307
208+
--- config
209+
location /read {
210+
content_by_lua '
211+
ngx.redirect("http://agentzh.org/foo?a=b&c=d", 307);
212+
ngx.say("hi")
213+
';
214+
}
215+
--- request
216+
GET /read
217+
--- response_headers
218+
Location: http://agentzh.org/foo?a=b&c=d
219+
--- response_body_like: 307 Temporary Redirect
220+
--- error_code: 307

0 commit comments

Comments
 (0)