diff --git a/src/ngx_cache_pilot_module.c b/src/ngx_cache_pilot_module.c index 854952a..47bd6ad 100644 --- a/src/ngx_cache_pilot_module.c +++ b/src/ngx_cache_pilot_module.c @@ -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, @@ -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; @@ -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) { @@ -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); } } diff --git a/t/proxy_if.t b/t/proxy_if.t new file mode 100644 index 0000000..63be70c --- /dev/null +++ b/t/proxy_if.t @@ -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)\]/