Skip to content

Commit 05a4a71

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

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

src/ngx_http_lua_control.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ ngx_http_lua_ngx_redirect(lua_State *L)
195195
u_char *p;
196196
u_char *uri;
197197
size_t len;
198+
ngx_table_elt_t *h;
198199
ngx_http_request_t *r;
199200

200201
n = lua_gettop(L);
@@ -246,23 +247,23 @@ ngx_http_lua_ngx_redirect(lua_State *L)
246247

247248
ngx_memcpy(uri, p, len);
248249

249-
r->headers_out.location = ngx_list_push(&r->headers_out.headers);
250-
if (r->headers_out.location == NULL) {
250+
h = ngx_list_push(&r->headers_out.headers);
251+
if (h == NULL) {
251252
return luaL_error(L, "no memory");
252253
}
253254

254-
r->headers_out.location->hash = ngx_http_lua_location_hash;
255+
h->hash = ngx_http_lua_location_hash;
255256

256257
#if 0
257258
dd("location hash: %lu == %lu",
258-
(unsigned long) r->headers_out.location->hash,
259+
(unsigned long) h->hash,
259260
(unsigned long) ngx_hash_key_lc((u_char *) "Location",
260261
sizeof("Location") - 1));
261262
#endif
262263

263-
r->headers_out.location->value.len = len;
264-
r->headers_out.location->value.data = uri;
265-
ngx_str_set(&r->headers_out.location->key, "Location");
264+
h->value.len = len;
265+
h->value.data = uri;
266+
ngx_str_set(&h->key, "Location");
266267

267268
r->headers_out.status = rc;
268269

@@ -271,7 +272,16 @@ ngx_http_lua_ngx_redirect(lua_State *L)
271272

272273
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
273274
"lua redirect to \"%V\" with code %i",
274-
&r->headers_out.location->value, ctx->exit_code);
275+
&h->value, ctx->exit_code);
276+
277+
if (len && uri[0] != '/') {
278+
r->headers_out.location = h;
279+
}
280+
281+
/*
282+
* we do not set r->headers_out.location here to avoid the handling
283+
* the local redirects without a host name by ngx_http_header_filter()
284+
*/
275285

276286
return lua_yield(L, 0);
277287
}

t/022-redirect.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ GET /read
121121
}
122122
--- request
123123
GET /read
124-
--- raw_response_headers_like: Location: http://localhost(?::\d+)?/echo\r\n
124+
--- raw_response_headers_like: Location: /echo\r\n
125125
--- response_body_like: 302 Found
126126
--- error_code: 302
127127

t/023-rewrite/redirect.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ GET /read
120120
}
121121
--- request
122122
GET /read
123-
--- raw_response_headers_like: Location: http://localhost(?::\d+)?/foo\r\n
123+
--- raw_response_headers_like: Location: /foo\r\n
124124
--- response_body_like: 302 Found
125125
--- error_code: 302
126126

t/024-access/auth.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ POST /lua
7676
He fucks himself!
7777
--- response_body_like: 302 Found
7878
--- response_headers_like
79-
Location: http://[^:]+:\d+/terms_of_use\.html
79+
Location: /terms_of_use\.html
8080
--- error_code: 302
8181
8282

t/024-access/redirect.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ GET /read
120120
}
121121
--- request
122122
GET /read
123-
--- raw_response_headers_like: Location: http://localhost(?::\d+)?/foo\r\n
123+
--- raw_response_headers_like: Location: /foo\r\n
124124
--- response_body_like: 302 Found
125125
--- error_code: 302
126126

0 commit comments

Comments
 (0)