Skip to content

Commit 8f90877

Browse files
committed
bugfix: the value of the Location response header set by the ngx.header.HEADER API might get overwritten by nginx's header filter to the fully qualified form (with the scheme and host parts).
1 parent 23afdc0 commit 8f90877

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

src/ngx_http_lua_headers_out.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ static ngx_int_t ngx_http_clear_last_modified_header(ngx_http_request_t *r,
3838
ngx_http_lua_header_val_t *hv, ngx_str_t *value);
3939
static ngx_int_t ngx_http_clear_content_length_header(ngx_http_request_t *r,
4040
ngx_http_lua_header_val_t *hv, ngx_str_t *value);
41+
static ngx_int_t ngx_http_set_location_header(ngx_http_request_t *r,
42+
ngx_http_lua_header_val_t *hv, ngx_str_t *value);
4143

4244

4345
static ngx_http_lua_set_header_t ngx_http_lua_set_handlers[] = {
@@ -58,7 +60,7 @@ static ngx_http_lua_set_header_t ngx_http_lua_set_handlers[] = {
5860

5961
{ ngx_string("Location"),
6062
offsetof(ngx_http_headers_out_t, location),
61-
ngx_http_set_builtin_header },
63+
ngx_http_set_location_header },
6264

6365
{ ngx_string("Refresh"),
6466
offsetof(ngx_http_headers_out_t, refresh),
@@ -237,6 +239,32 @@ ngx_http_set_header_helper(ngx_http_request_t *r, ngx_http_lua_header_val_t *hv,
237239
}
238240

239241

242+
static ngx_int_t
243+
ngx_http_set_location_header(ngx_http_request_t *r,
244+
ngx_http_lua_header_val_t *hv, ngx_str_t *value)
245+
{
246+
ngx_int_t rc;
247+
ngx_table_elt_t *h;
248+
249+
rc = ngx_http_set_builtin_header(r, hv, value);
250+
if (rc != NGX_OK) {
251+
return rc;
252+
}
253+
254+
/*
255+
* we do not set r->headers_out.location here to avoid the handling
256+
* the local redirects without a host name by ngx_http_header_filter()
257+
*/
258+
259+
h = r->headers_out.location;
260+
if (h && h->value.len && h->value.data[0] == '/') {
261+
r->headers_out.location = NULL;
262+
}
263+
264+
return NGX_OK;
265+
}
266+
267+
240268
static ngx_int_t
241269
ngx_http_set_builtin_header(ngx_http_request_t *r,
242270
ngx_http_lua_header_val_t *hv, ngx_str_t *value)

t/016-resp-header.t

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use Test::Nginx::Socket::Lua;
99

1010
repeat_each(2);
1111

12-
plan tests => repeat_each() * (blocks() * 3 + 24);
12+
plan tests => repeat_each() * (blocks() * 3 + 26);
1313

1414
#no_diff();
1515
no_long_string();
@@ -1346,3 +1346,41 @@ hi
13461346
--- error_log
13471347
my Transfer-Encoding: nil
13481348
1349+
1350+
1351+
=== TEST 65: set Location (no host)
1352+
--- config
1353+
location = /t {
1354+
content_by_lua '
1355+
ngx.header.location = "/foo/bar"
1356+
return ngx.exit(301)
1357+
';
1358+
}
1359+
--- request
1360+
GET /t
1361+
--- response_headers
1362+
Location: /foo/bar
1363+
--- response_body_like: 301 Moved Permanently
1364+
--- error_code: 301
1365+
--- no_error_log
1366+
[error]
1367+
1368+
1369+
1370+
=== TEST 66: set Location (with host)
1371+
--- config
1372+
location = /t {
1373+
content_by_lua '
1374+
ngx.header.location = "http://test.com/foo/bar"
1375+
return ngx.exit(301)
1376+
';
1377+
}
1378+
--- request
1379+
GET /t
1380+
--- response_headers
1381+
Location: http://test.com/foo/bar
1382+
--- response_body_like: 301 Moved Permanently
1383+
--- error_code: 301
1384+
--- no_error_log
1385+
[error]
1386+

0 commit comments

Comments
 (0)