Skip to content

Commit

Permalink
bugfix: the value of the Location response header set by ngx.redirect…
Browse files Browse the repository at this point in the history
…() might get overwritten by nginx's header filter to the fully qualified form (with the scheme and host parts).
  • Loading branch information
agentzh committed Feb 6, 2015
1 parent 8f90877 commit 05a4a71
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
26 changes: 18 additions & 8 deletions src/ngx_http_lua_control.c
Expand Up @@ -195,6 +195,7 @@ ngx_http_lua_ngx_redirect(lua_State *L)
u_char *p;
u_char *uri;
size_t len;
ngx_table_elt_t *h;
ngx_http_request_t *r;

n = lua_gettop(L);
Expand Down Expand Up @@ -246,23 +247,23 @@ ngx_http_lua_ngx_redirect(lua_State *L)

ngx_memcpy(uri, p, len);

r->headers_out.location = ngx_list_push(&r->headers_out.headers);
if (r->headers_out.location == NULL) {
h = ngx_list_push(&r->headers_out.headers);
if (h == NULL) {
return luaL_error(L, "no memory");
}

r->headers_out.location->hash = ngx_http_lua_location_hash;
h->hash = ngx_http_lua_location_hash;

#if 0
dd("location hash: %lu == %lu",
(unsigned long) r->headers_out.location->hash,
(unsigned long) h->hash,
(unsigned long) ngx_hash_key_lc((u_char *) "Location",
sizeof("Location") - 1));
#endif

r->headers_out.location->value.len = len;
r->headers_out.location->value.data = uri;
ngx_str_set(&r->headers_out.location->key, "Location");
h->value.len = len;
h->value.data = uri;
ngx_str_set(&h->key, "Location");

r->headers_out.status = rc;

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

ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua redirect to \"%V\" with code %i",
&r->headers_out.location->value, ctx->exit_code);
&h->value, ctx->exit_code);

if (len && uri[0] != '/') {
r->headers_out.location = h;
}

/*
* we do not set r->headers_out.location here to avoid the handling
* the local redirects without a host name by ngx_http_header_filter()
*/

return lua_yield(L, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion t/022-redirect.t
Expand Up @@ -121,7 +121,7 @@ GET /read
}
--- request
GET /read
--- raw_response_headers_like: Location: http://localhost(?::\d+)?/echo\r\n
--- raw_response_headers_like: Location: /echo\r\n
--- response_body_like: 302 Found
--- error_code: 302
Expand Down
2 changes: 1 addition & 1 deletion t/023-rewrite/redirect.t
Expand Up @@ -120,7 +120,7 @@ GET /read
}
--- request
GET /read
--- raw_response_headers_like: Location: http://localhost(?::\d+)?/foo\r\n
--- raw_response_headers_like: Location: /foo\r\n
--- response_body_like: 302 Found
--- error_code: 302
2 changes: 1 addition & 1 deletion t/024-access/auth.t
Expand Up @@ -76,7 +76,7 @@ POST /lua
He fucks himself!
--- response_body_like: 302 Found
--- response_headers_like
Location: http://[^:]+:\d+/terms_of_use\.html
Location: /terms_of_use\.html
--- error_code: 302
Expand Down
2 changes: 1 addition & 1 deletion t/024-access/redirect.t
Expand Up @@ -120,7 +120,7 @@ GET /read
}
--- request
GET /read
--- raw_response_headers_like: Location: http://localhost(?::\d+)?/foo\r\n
--- raw_response_headers_like: Location: /foo\r\n
--- response_body_like: 302 Found
--- error_code: 302

0 comments on commit 05a4a71

Please sign in to comment.