Skip to content

Commit aeb77c0

Browse files
committed
bugfix: upon cache hits, we might let the nginx core's header filter module overwrite the Location response header's values like "/foo/bar" to the fully-qualified form (like "http://test.com/foo/bar"). thanks AlexClineBB for the report in openresty#32.
1 parent f1077bc commit aeb77c0

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

src/ngx_http_srcache_headers.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ static ngx_int_t ngx_http_srcache_process_accept_ranges(ngx_http_request_t *r,
2828
ngx_table_elt_t *h, ngx_uint_t offset);
2929
static ngx_int_t ngx_http_srcache_ignore_header_line(ngx_http_request_t *r,
3030
ngx_table_elt_t *h, ngx_uint_t offset);
31+
static ngx_int_t ngx_http_srcache_process_location(ngx_http_request_t *r,
32+
ngx_table_elt_t *h, ngx_uint_t offset);
3133

3234
#if (NGX_HTTP_GZIP)
3335
static ngx_int_t
@@ -64,8 +66,8 @@ ngx_http_srcache_header_t ngx_http_srcache_headers_in[] = {
6466
offsetof(ngx_http_headers_out_t, www_authenticate) },
6567

6668
{ ngx_string("Location"),
67-
ngx_http_srcache_process_header_line,
68-
offsetof(ngx_http_headers_out_t, location) },
69+
ngx_http_srcache_process_location,
70+
0 },
6971

7072
{ ngx_string("Refresh"),
7173
ngx_http_srcache_process_header_line,
@@ -125,6 +127,32 @@ ngx_http_srcache_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
125127
}
126128

127129

130+
static ngx_int_t
131+
ngx_http_srcache_process_location(ngx_http_request_t *r, ngx_table_elt_t *h,
132+
ngx_uint_t offset)
133+
{
134+
ngx_table_elt_t *ho;
135+
136+
ho = ngx_list_push(&r->headers_out.headers);
137+
if (ho == NULL) {
138+
return NGX_ERROR;
139+
}
140+
141+
*ho = *h;
142+
143+
if (ho->value.data[0] != '/') {
144+
r->headers_out.location = ho;
145+
}
146+
147+
/*
148+
* we do not set r->headers_out.location here to avoid the handling
149+
* the local redirects without a host name by ngx_http_header_filter()
150+
*/
151+
152+
return NGX_OK;
153+
}
154+
155+
128156
static ngx_int_t
129157
ngx_http_srcache_process_content_type(ngx_http_request_t *r, ngx_table_elt_t *h,
130158
ngx_uint_t offset)

t/status.t

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use lib 'lib';
44
use Test::Nginx::Socket;
55

6-
plan tests => repeat_each() * 92;
6+
plan tests => repeat_each() * 97;
77

88
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
99

@@ -312,6 +312,7 @@ GET /flush
312312
GET /foo HTTP/1.0
313313
--- response_headers
314314
Content-Type: text/html
315+
Location: /bah
315316
--- response_body_like: 302 Found
316317
--- error_code: 302
317318
@@ -339,7 +340,37 @@ Location: /bah\r
339340
340341
341342
342-
=== TEST 16: flush all
343+
=== TEST 16: cache hit
344+
--- config
345+
location /foo {
346+
default_type text/css;
347+
srcache_fetch GET /memc $uri;
348+
srcache_store PUT /memc $uri;
349+
srcache_store_statuses 303 304;
350+
351+
content_by_lua '
352+
ngx.say("hi")
353+
';
354+
}
355+
356+
location /memc {
357+
internal;
358+
359+
set $memc_key $query_string;
360+
set $memc_exptime 300;
361+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
362+
}
363+
--- request
364+
GET /foo HTTP/1.0
365+
--- response_headers
366+
Content-Type: text/html
367+
Location: /bah
368+
--- response_body_like: 302 Found
369+
--- error_code: 302
370+
371+
372+
373+
=== TEST 17: flush all
343374
--- config
344375
location /flush {
345376
set $memc_cmd 'flush_all';
@@ -355,7 +386,7 @@ GET /flush
355386
356387
357388
358-
=== TEST 17: basic fetch (201 not cached by default)
389+
=== TEST 18: basic fetch (201 not cached by default)
359390
--- config
360391
location /foo {
361392
default_type text/css;
@@ -385,7 +416,7 @@ Dog created
385416
386417
387418
388-
=== TEST 18: inspect the cached item
419+
=== TEST 19: inspect the cached item
389420
--- config
390421
location /memc {
391422
set $memc_key "/foo";
@@ -403,7 +434,7 @@ Content-Type: text/html
403434
404435
405436
406-
=== TEST 19: flush all
437+
=== TEST 20: flush all
407438
--- config
408439
location /flush {
409440
set $memc_cmd 'flush_all';
@@ -419,7 +450,7 @@ GET /flush
419450
420451
421452
422-
=== TEST 20: basic fetch (explicitly do not cache 302)
453+
=== TEST 21: basic fetch (explicitly do not cache 302)
423454
--- config
424455
location /foo {
425456
default_type text/css;
@@ -448,7 +479,7 @@ Content-Type: text/html
448479
449480
450481
451-
=== TEST 21: inspect the cached item
482+
=== TEST 22: inspect the cached item
452483
--- config
453484
location /memc {
454485
set $memc_key "/foo";
@@ -466,7 +497,7 @@ Content-Type: text/html
466497
467498
468499
469-
=== TEST 22: flush all
500+
=== TEST 23: flush all
470501
--- config
471502
location /flush {
472503
set $memc_cmd 'flush_all';
@@ -482,7 +513,7 @@ GET /flush
482513
483514
484515
485-
=== TEST 23: basic fetch (explicitly do not cache 302, and store_statuses are all bigger than 302)
516+
=== TEST 24: basic fetch (explicitly do not cache 302, and store_statuses are all bigger than 302)
486517
github pull #19
487518
--- config
488519
location /foo {
@@ -512,7 +543,7 @@ Content-Type: text/html
512543
513544
514545
515-
=== TEST 24: inspect the cached item
546+
=== TEST 25: inspect the cached item
516547
--- config
517548
location /memc {
518549
set $memc_key "/foo";

0 commit comments

Comments
 (0)