Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/ngx_cache_pilot_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ ngx_http_cache_pilot_protocol_attach(ngx_conf_t *cf,
ngx_http_cache_pilot_loc_conf_t *conf,
ngx_http_core_loc_conf_t *clcf,
void *protocol_loc_conf,
ngx_http_cache_pilot_protocol_t *protocol);
ngx_http_cache_pilot_protocol_t *protocol,
ngx_http_handler_pt prev_original_handler);

# if (NGX_HTTP_FASTCGI)
char *ngx_http_fastcgi_cache_purge_conf(ngx_conf_t *cf,
Expand Down Expand Up @@ -1290,7 +1291,8 @@ ngx_http_cache_pilot_protocol_attach(ngx_conf_t *cf,
ngx_http_cache_pilot_loc_conf_t *conf,
ngx_http_core_loc_conf_t *clcf,
void *protocol_loc_conf,
ngx_http_cache_pilot_protocol_t *protocol) {
ngx_http_cache_pilot_protocol_t *protocol,
ngx_http_handler_pt prev_original_handler) {
ngx_http_cache_pilot_conf_t *protocol_conf;
ngx_flag_t has_cache_key;
ngx_flag_t has_cache;
Expand All @@ -1314,7 +1316,16 @@ ngx_http_cache_pilot_protocol_attach(ngx_conf_t *cf,

conf->conf = protocol_conf;
conf->protocol = protocol->id;
conf->original_handler = clcf->handler;

if (!protocol_conf->configured && prev_original_handler != NULL) {
conf->original_handler = prev_original_handler;
} else if (clcf->handler == ngx_http_cache_pilot_access_handler
&& prev_original_handler != NULL) {
conf->original_handler = prev_original_handler;
} else {
conf->original_handler = clcf->handler;
}

clcf->handler = ngx_http_cache_pilot_access_handler;

if (has_pass) {
Expand Down Expand Up @@ -3527,7 +3538,8 @@ ngx_http_cache_pilot_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {

if (protocol_conf->enable) {
return ngx_http_cache_pilot_protocol_attach(
cf, conf, clcf, protocol->config_loc_conf(cf), protocol);
cf, conf, clcf, protocol->config_loc_conf(cf), protocol,
prev->original_handler);
}
}

Expand Down
121 changes: 121 additions & 0 deletions t/proxy_if.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# vi:filetype=perl

use lib 'lib';
use Test::Nginx::Socket;

repeat_each(1);

plan tests => 20;

our $http_config = <<'_EOC_';
proxy_cache_path /tmp/ngx_cache_pilot_if_cache keys_zone=if_cache:10m;
proxy_temp_path /tmp/ngx_cache_pilot_if_temp 1 2;
map $request_method $purge_method {
PURGE 1;
default 0;
}
_EOC_

our $config = <<'_EOC_';
location /proxy {
set $flag "0";
if ($http_x_trigger_if) {
set $flag "1";
}

proxy_pass $scheme://127.0.0.1:$server_port/origin;
proxy_cache if_cache;
proxy_cache_key $uri;
proxy_cache_valid 3m;
proxy_cache_purge $purge_method;
add_header X-Cache-Status $upstream_cache_status always;
}

location /origin {
return 200 "if-proxy";
}
_EOC_

worker_connections(128);
no_shuffle();
run_tests();

no_diff();

__DATA__

=== TEST 1: GET proxies correctly without if branch
--- http_config eval: $::http_config
--- config eval: $::config
--- request
GET /proxy/if
--- error_code: 200
--- response_body: if-proxy
--- response_headers
X-Cache-Status: MISS
--- no_error_log eval
qr/\[(warn|error|crit|alert|emerg)\]/



=== TEST 2: GET proxies correctly through if child location
--- http_config eval: $::http_config
--- config eval: $::config
--- more_headers
X-Trigger-If: 1
--- request
GET /proxy/if-if
--- error_code: 200
--- response_body: if-proxy
--- response_headers
X-Cache-Status: MISS
--- no_error_log eval
qr/\[(warn|error|crit|alert|emerg)\]/



=== TEST 3: cached response is served through if child location
--- http_config eval: $::http_config
--- config eval: $::config
--- more_headers
X-Trigger-If: 1
--- request
GET /proxy/if-if
--- error_code: 200
--- response_body: if-proxy
--- response_headers
X-Cache-Status: HIT
--- no_error_log eval
qr/\[(warn|error|crit|alert|emerg)\]/



=== TEST 4: PURGE still works through if child location
--- http_config eval: $::http_config
--- config eval: $::config
--- more_headers
X-Trigger-If: 1
--- request
PURGE /proxy/if-if
--- error_code: 200
--- response_headers
Content-Type: application/json
--- response_body_like: \{\"key\":
--- no_error_log eval
qr/\[(warn|error|crit|alert|emerg)\]/



=== TEST 5: purged cached response is fetched again through if child location
--- http_config eval: $::http_config
--- config eval: $::config
--- more_headers
X-Trigger-If: 1
--- request
GET /proxy/if-if
--- error_code: 200
--- response_body: if-proxy
--- response_headers
X-Cache-Status: MISS
--- no_error_log eval
qr/\[(warn|error|crit|alert|emerg)\]/
Loading