Skip to content

Commit 80fa32c

Browse files
Mikhail Seninagentzh
Mikhail Senin
authored andcommitted
feature: added support for the 308 status code in ngx.redirect().
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent 439a02a commit 80fa32c

File tree

8 files changed

+69
-6
lines changed

8 files changed

+69
-6
lines changed

README.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3467,6 +3467,7 @@ HTTP status constants
34673467
value = ngx.HTTP_SEE_OTHER (303)
34683468
value = ngx.HTTP_NOT_MODIFIED (304)
34693469
value = ngx.HTTP_TEMPORARY_REDIRECT (307) (first added in the v0.9.20 release)
3470+
value = ngx.HTTP_PERMANENT_REDIRECT (308)
34703471
value = ngx.HTTP_BAD_REQUEST (400)
34713472
value = ngx.HTTP_UNAUTHORIZED (401)
34723473
value = ngx.HTTP_PAYMENT_REQUIRED (402) (first added in the v0.9.20 release)
@@ -5101,6 +5102,7 @@ The optional `status` parameter specifies the HTTP status code to be used. The f
51015102
* `302` (default)
51025103
* `303`
51035104
* `307`
5105+
* `308`
51045106

51055107
It is `302` (`ngx.HTTP_MOVED_TEMPORARILY`) by default.
51065108

doc/HttpLuaModule.wiki

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,6 +2807,7 @@ These constants are usually used in [[#ngx.location.capture|ngx.location.capture
28072807
value = ngx.HTTP_SEE_OTHER (303)
28082808
value = ngx.HTTP_NOT_MODIFIED (304)
28092809
value = ngx.HTTP_TEMPORARY_REDIRECT (307) (first added in the v0.9.20 release)
2810+
value = ngx.HTTP_PERMANENT_REDIRECT (308)
28102811
value = ngx.HTTP_BAD_REQUEST (400)
28112812
value = ngx.HTTP_UNAUTHORIZED (401)
28122813
value = ngx.HTTP_PAYMENT_REQUIRED (402) (first added in the v0.9.20 release)
@@ -4253,6 +4254,7 @@ The optional <code>status</code> parameter specifies the HTTP status code to be
42534254
* <code>302</code> (default)
42544255
* <code>303</code>
42554256
* <code>307</code>
4257+
* <code>308</code>
42564258
42574259
It is <code>302</code> (<code>ngx.HTTP_MOVED_TEMPORARILY</code>) by default.
42584260

src/ngx_http_lua_common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
# define NGX_HTTP_LUA_USE_OCSP 1
5555
#endif
5656

57+
#ifndef NGX_HTTP_PERMANENT_REDIRECT
58+
# define NGX_HTTP_PERMANENT_REDIRECT 308
59+
#endif
5760

5861
#ifndef NGX_HAVE_SHA1
5962
# if (nginx_version >= 1011002)

src/ngx_http_lua_consts.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ ngx_http_lua_inject_http_consts(lua_State *L)
124124
lua_setfield(L, -2, "HTTP_SEE_OTHER");
125125
#endif
126126

127+
lua_pushinteger(L, NGX_HTTP_PERMANENT_REDIRECT);
128+
lua_setfield(L, -2, "HTTP_PERMANENT_REDIRECT");
129+
127130
lua_pushinteger(L, NGX_HTTP_NOT_MODIFIED);
128131
lua_setfield(L, -2, "HTTP_NOT_MODIFIED");
129132

src/ngx_http_lua_control.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,12 @@ ngx_http_lua_ngx_redirect(lua_State *L)
213213
if (rc != NGX_HTTP_MOVED_TEMPORARILY
214214
&& rc != NGX_HTTP_MOVED_PERMANENTLY
215215
&& rc != NGX_HTTP_SEE_OTHER
216+
&& rc != NGX_HTTP_PERMANENT_REDIRECT
216217
&& rc != NGX_HTTP_TEMPORARY_REDIRECT)
217218
{
218219
return luaL_error(L, "only ngx.HTTP_MOVED_TEMPORARILY, "
219220
"ngx.HTTP_MOVED_PERMANENTLY, "
221+
"ngx.HTTP_PERMANENT_REDIRECT, "
220222
"ngx.HTTP_SEE_OTHER, and "
221223
"ngx.HTTP_TEMPORARY_REDIRECT are allowed");
222224
}

src/ngx_http_lua_util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ static void
710710
ngx_http_lua_inject_ngx_api(lua_State *L, ngx_http_lua_main_conf_t *lmcf,
711711
ngx_log_t *log)
712712
{
713-
lua_createtable(L, 0 /* narr */, 116 /* nrec */); /* ngx.* */
713+
lua_createtable(L, 0 /* narr */, 117 /* nrec */); /* ngx.* */
714714

715715
lua_pushcfunction(L, ngx_http_lua_get_raw_phase_context);
716716
lua_setfield(L, -2, "_phase_ctx");

t/022-redirect.t

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ GET /read
8484
--- response_body_like: 500 Internal Server Error
8585
--- error_code: 500
8686
--- error_log
87-
only ngx.HTTP_MOVED_TEMPORARILY, ngx.HTTP_MOVED_PERMANENTLY, ngx.HTTP_SEE_OTHER, and ngx.HTTP_TEMPORARY_REDIRECT are allowed
87+
only ngx.HTTP_MOVED_TEMPORARILY, ngx.HTTP_MOVED_PERMANENTLY, ngx.HTTP_PERMANENT_REDIRECT, ngx.HTTP_SEE_OTHER, and ngx.HTTP_TEMPORARY_REDIRECT are allowed
8888

8989

9090

@@ -269,3 +269,54 @@ GET /read
269269
Location: http://agentzh.org/foo?a=b&c=d
270270
--- response_body_like: 303 See Other
271271
--- error_code: 303
272+
273+
274+
275+
=== TEST 15: explicit 308 with args
276+
--- config
277+
location /read {
278+
content_by_lua '
279+
ngx.redirect("http://agentzh.org/foo?a=b&c=d", ngx.HTTP_PERMANENT_REDIRECT);
280+
ngx.say("hi")
281+
';
282+
}
283+
--- request
284+
GET /read
285+
--- response_body_like: 308 Permanent Redirect
286+
--- response_headers
287+
Location: http://agentzh.org/foo?a=b&c=d
288+
--- error_code: 308
289+
290+
291+
292+
=== TEST 16: explicit 308
293+
--- config
294+
location /read {
295+
content_by_lua '
296+
ngx.redirect("http://agentzh.org/foo?a=b&c=d", 308);
297+
ngx.say("hi")
298+
';
299+
}
300+
--- request
301+
GET /read
302+
--- response_body_like: 308 Permanent Redirect
303+
--- response_headers
304+
Location: http://agentzh.org/foo?a=b&c=d
305+
--- error_code: 308
306+
307+
308+
309+
=== TEST 17: explicit 308 with args
310+
--- config
311+
location /read {
312+
content_by_lua '
313+
ngx.redirect("http://agentzh.org/foo?a=b&c=d", 308);
314+
ngx.say("hi")
315+
';
316+
}
317+
--- request
318+
GET /read
319+
--- response_body_like: 308 Permanent Redirect
320+
--- response_headers
321+
Location: http://agentzh.org/foo?a=b&c=d
322+
--- error_code: 308

t/062-count.t

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ __DATA__
3434
--- request
3535
GET /test
3636
--- response_body
37-
ngx: 116
37+
ngx: 117
3838
--- no_error_log
3939
[error]
4040

@@ -55,7 +55,7 @@ ngx: 116
5555
--- request
5656
GET /test
5757
--- response_body
58-
116
58+
117
5959
--- no_error_log
6060
[error]
6161

@@ -83,7 +83,7 @@ GET /test
8383
--- request
8484
GET /test
8585
--- response_body
86-
n = 116
86+
n = 117
8787
--- no_error_log
8888
[error]
8989

@@ -305,7 +305,7 @@ GET /t
305305
--- response_body_like: 404 Not Found
306306
--- error_code: 404
307307
--- error_log
308-
ngx. entry count: 116
308+
ngx. entry count: 117
309309

310310

311311

0 commit comments

Comments
 (0)