Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 07a6647

Browse files
jmarantzcrowell
authored andcommitted
respect purge requests when serving ipro requests from ngx_pagespeed (#1193)
* respect purge requests when serving ipro requests from ngx_pagespeed * Add TODO to share common base with ApacheFetch. During system-tests, don't write into the source-controlled htdocs area. (#1197) Instead, use a new (not-yet-committed) target in Makefile.tests to build a mirror htdocs in test/tmp/root that sym-links the large readonly section and deep copies the test areas for purge and flush tests. Pass the install-path into Makefile (#1200) * Pass the install-path into Makefile, rather than having the Makefile depend on PWD. * Move the double-quotes be before the value, to be consistent with other Makefiles.
1 parent 787239d commit 07a6647

File tree

5 files changed

+76
-40
lines changed

5 files changed

+76
-40
lines changed

src/ngx_base_fetch.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include "ngx_pagespeed.h"
2828

29+
#include "net/instaweb/rewriter/public/rewrite_driver.h"
30+
#include "net/instaweb/rewriter/public/rewrite_options.h"
2931
#include "net/instaweb/rewriter/public/rewrite_stats.h"
3032
#include "pagespeed/kernel/base/google_message_handler.h"
3133
#include "pagespeed/kernel/base/message_handler.h"
@@ -41,14 +43,18 @@ const char kDone = 'D';
4143
NgxEventConnection* NgxBaseFetch::event_connection = NULL;
4244
int NgxBaseFetch::active_base_fetches = 0;
4345

44-
NgxBaseFetch::NgxBaseFetch(ngx_http_request_t* r,
46+
NgxBaseFetch::NgxBaseFetch(StringPiece url,
47+
ngx_http_request_t* r,
4548
NgxServerContext* server_context,
4649
const RequestContextPtr& request_ctx,
4750
PreserveCachingHeaders preserve_caching_headers,
48-
NgxBaseFetchType base_fetch_type)
51+
NgxBaseFetchType base_fetch_type,
52+
const RewriteOptions* options)
4953
: AsyncFetch(request_ctx),
54+
url_(url.data(), url.size()),
5055
request_(r),
5156
server_context_(server_context),
57+
options_(options),
5258
done_called_(false),
5359
last_buf_sent_(false),
5460
references_(2),
@@ -341,4 +347,9 @@ void NgxBaseFetch::HandleDone(bool success) {
341347
DecrefAndDeleteIfUnreferenced();
342348
}
343349

350+
bool NgxBaseFetch::IsCachedResultValid(const ResponseHeaders& headers) {
351+
return OptionsAwareHTTPCacheCallback::IsCacheValid(
352+
url_, *options_, request_context(), headers);
353+
}
354+
344355
} // namespace net_instaweb

src/ngx_base_fetch.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
// events it handles.
4444
//
4545
// When the last reference is dropped, this class will delete itself.
46+
//
47+
// TODO(jmarantz): consider sharing the cache-invalidation infrastructure
48+
// with ApacheFetch, using a common base class.
4649

4750
#ifndef NGX_BASE_FETCH_H_
4851
#define NGX_BASE_FETCH_H_
@@ -59,6 +62,7 @@ extern "C" {
5962
#include "ngx_server_context.h"
6063

6164
#include "net/instaweb/http/public/async_fetch.h"
65+
#include "net/instaweb/rewriter/public/rewrite_options.h"
6266
#include "pagespeed/kernel/base/string.h"
6367
#include "pagespeed/kernel/http/headers.h"
6468

@@ -74,10 +78,12 @@ enum NgxBaseFetchType {
7478

7579
class NgxBaseFetch : public AsyncFetch {
7680
public:
77-
NgxBaseFetch(ngx_http_request_t* r, NgxServerContext* server_context,
81+
NgxBaseFetch(StringPiece url, ngx_http_request_t* r,
82+
NgxServerContext* server_context,
7883
const RequestContextPtr& request_ctx,
7984
PreserveCachingHeaders preserve_caching_headers,
80-
NgxBaseFetchType base_fetch_type);
85+
NgxBaseFetchType base_fetch_type,
86+
const RewriteOptions* options);
8187
virtual ~NgxBaseFetch();
8288

8389
// Statically initializes event_connection, require for PSOL and nginx to
@@ -125,6 +131,8 @@ class NgxBaseFetch : public AsyncFetch {
125131
ngx_http_request_t* request() { return request_; }
126132
NgxBaseFetchType base_fetch_type() { return base_fetch_type_; }
127133

134+
bool IsCachedResultValid(const ResponseHeaders& headers) override;
135+
128136
private:
129137
virtual bool HandleWrite(const StringPiece& sp, MessageHandler* handler);
130138
virtual bool HandleFlush(MessageHandler* handler);
@@ -152,13 +160,15 @@ class NgxBaseFetch : public AsyncFetch {
152160
int DecrefAndDeleteIfUnreferenced();
153161

154162
static NgxEventConnection* event_connection;
155-
163+
156164
// Live count of NgxBaseFetch instances that are currently in use.
157165
static int active_base_fetches;
158166

167+
GoogleString url_;
159168
ngx_http_request_t* request_;
160169
GoogleString buffer_;
161170
NgxServerContext* server_context_;
171+
const RewriteOptions* options_;
162172
bool done_called_;
163173
bool last_buf_sent_;
164174
// How many active references there are to this fetch. Starts at two,

src/ngx_pagespeed.cc

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,10 +1589,12 @@ void ps_release_base_fetch(ps_request_ctx_t* ctx) {
15891589
}
15901590

15911591
// TODO(chaizhenhua): merge into NgxBaseFetch ctor
1592-
void ps_create_base_fetch(ps_request_ctx_t* ctx,
1593-
RequestContextPtr request_context,
1594-
RequestHeaders* request_headers,
1595-
NgxBaseFetchType type) {
1592+
void ps_create_base_fetch(StringPiece url,
1593+
ps_request_ctx_t* ctx,
1594+
RequestContextPtr request_context,
1595+
RequestHeaders* request_headers,
1596+
NgxBaseFetchType type,
1597+
const RewriteOptions* options) {
15961598
CHECK(ctx->base_fetch == NULL) << "Pre-existing base fetch!";
15971599

15981600
ngx_http_request_t* r = ctx->r;
@@ -1602,9 +1604,9 @@ void ps_create_base_fetch(ps_request_ctx_t* ctx,
16021604
// it, and call Done() on the associated parent (Proxy or Resource) fetch. If
16031605
// we fail before creating the associated fetch then we need to call Done() on
16041606
// the BaseFetch ourselves.
1605-
ctx->base_fetch = new NgxBaseFetch(r, cfg_s->server_context,
1606-
request_context,
1607-
ctx->preserve_caching_headers, type);
1607+
ctx->base_fetch = new NgxBaseFetch(url, r, cfg_s->server_context, request_context,
1608+
ctx->preserve_caching_headers, type,
1609+
options);
16081610
ctx->base_fetch->SetRequestHeadersTakingOwnership(request_headers);
16091611
}
16101612

@@ -1869,16 +1871,17 @@ ngx_int_t ps_resource_handler(ngx_http_request_t* r,
18691871
if (pagespeed_resource) {
18701872
// TODO(jefftk): Set using_spdy appropriately. See
18711873
// ProxyInterface::ProxyRequestCallback
1872-
ps_create_base_fetch(ctx, request_context, request_headers.release(),
1873-
kPageSpeedResource);
1874+
ps_create_base_fetch(url.Spec(), ctx, request_context,
1875+
request_headers.release(), kPageSpeedResource,
1876+
options);
18741877
ResourceFetch::Start(
18751878
url,
18761879
custom_options.release() /* null if there aren't custom options */,
18771880
false /* using_spdy */, cfg_s->server_context, ctx->base_fetch);
18781881
return ps_async_wait_response(r);
18791882
} else if (is_an_admin_handler) {
1880-
ps_create_base_fetch(ctx, request_context, request_headers.release(),
1881-
kAdminPage);
1883+
ps_create_base_fetch(url.Spec(), ctx, request_context,
1884+
request_headers.release(), kAdminPage, options);
18821885
QueryParams query_params;
18831886
query_params.ParseFromUrl(url);
18841887

@@ -1926,8 +1929,8 @@ ngx_int_t ps_resource_handler(ngx_http_request_t* r,
19261929

19271930
if (options->domain_lawyer()->MapOriginUrl(
19281931
url, &mapped_url, &host_header, &is_proxy) && is_proxy) {
1929-
ps_create_base_fetch(ctx, request_context, request_headers.release(),
1930-
kPageSpeedProxy);
1932+
ps_create_base_fetch(url.Spec(), ctx, request_context,
1933+
request_headers.release(), kPageSpeedProxy, options);
19311934

19321935
RewriteDriver* driver;
19331936
if (custom_options.get() == NULL) {
@@ -1951,8 +1954,8 @@ ngx_int_t ps_resource_handler(ngx_http_request_t* r,
19511954
}
19521955

19531956
if (html_rewrite) {
1954-
ps_create_base_fetch(ctx, request_context, request_headers.release(),
1955-
kHtmlTransform);
1957+
ps_create_base_fetch(url.Spec(), ctx, request_context,
1958+
request_headers.release(), kHtmlTransform, options);
19561959
// Do not store driver in request_context, it's not safe.
19571960
RewriteDriver* driver;
19581961

@@ -1996,8 +1999,8 @@ ngx_int_t ps_resource_handler(ngx_http_request_t* r,
19961999
if (options->in_place_rewriting_enabled() &&
19972000
options->enabled() &&
19982001
options->IsAllowed(url.Spec())) {
1999-
ps_create_base_fetch(ctx, request_context, request_headers.release(),
2000-
kIproLookup);
2002+
ps_create_base_fetch(url.Spec(), ctx, request_context, request_headers.release(),
2003+
kIproLookup, options);
20012004

20022005
// Do not store driver in request_context, it's not safe.
20032006
RewriteDriver* driver;

test/nginx_system_test.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,23 @@ POSITION_AUX="${POSITION_AUX:-unset}"
4646
PRIMARY_HOSTNAME="localhost:$PRIMARY_PORT"
4747
SECONDARY_HOSTNAME="localhost:$SECONDARY_PORT"
4848

49-
SERVER_ROOT="$MOD_PAGESPEED_DIR/src/install/"
49+
this_dir="$( cd $(dirname "$0") && pwd)"
50+
echo this_dir=$this_dir
51+
TEST_TMP="$this_dir/tmp"
52+
rm -rf "$TEST_TMP"
53+
mkdir -p "$TEST_TMP"
54+
echo TEST_TMP=$TEST_TMP
55+
56+
APACHE_DOC_SRC="$MOD_PAGESPEED_DIR/src/install/"
57+
SERVER_ROOT="$TEST_TMP/root"
58+
echo SERVER_ROOT=$SERVER_ROOT
59+
rm -rf "$SERVER_ROOT"
60+
mkdir -p "$SERVER_ROOT"
61+
export APACHE_DOC_ROOT="$SERVER_ROOT"
62+
63+
mkdir -p "$APACHE_DOC_ROOT"
64+
make -f "$APACHE_DOC_SRC/Makefile.tests" setup_doc_root \
65+
INSTALL_DATA_DIR="$APACHE_DOC_SRC"
5066

5167
# We need check and check_not before we source SYSTEM_TEST_FILE that provides
5268
# them.
@@ -136,17 +152,13 @@ function fire_ab_load() {
136152
sleep 2
137153
}
138154

139-
this_dir="$( cd $(dirname "$0") && pwd)"
140-
141155
# stop nginx/valgrind
142156
killall -s KILL nginx
143157
# TODO(oschaaf): Fix waiting for valgrind on 32 bits systems.
144158
killall -s KILL memcheck-amd64-
145159
while pgrep nginx > /dev/null; do sleep 1; done
146160
while pgrep memcheck > /dev/null; do sleep 1; done
147161

148-
TEST_TMP="$this_dir/tmp"
149-
rm -r "$TEST_TMP"
150162
check_simple mkdir -p "$TEST_TMP"
151163
PROXY_CACHE="$TEST_TMP/proxycache"
152164
TMP_PROXY_CACHE="$TEST_TMP/tmpproxycache"
@@ -646,14 +658,14 @@ check touch "$SECONDARY_CACHE/cache.flush"
646658
check touch "$IPRO_CACHE/cache.flush"
647659
sleep 1
648660
649-
CACHE_TESTING_DIR="$SERVER_ROOT/mod_pagespeed_test/cache_flush/"
661+
CACHE_TESTING_DIR="$SERVER_ROOT/cache_flush/"
650662
CACHE_TESTING_TMPDIR="$CACHE_TESTING_DIR/$$"
651663
mkdir "$CACHE_TESTING_TMPDIR"
652664
cp "$CACHE_TESTING_DIR/cache_flush_test.html" "$CACHE_TESTING_TMPDIR/"
653665
CSS_FILE="$CACHE_TESTING_TMPDIR/update.css"
654666
echo ".class myclass { color: $COLOR0; }" > "$CSS_FILE"
655667
656-
URL_PATH="mod_pagespeed_test/cache_flush/$$/cache_flush_test.html"
668+
URL_PATH="cache_flush/$$/cache_flush_test.html"
657669
658670
URL="$SECONDARY_HOSTNAME/$URL_PATH"
659671
CACHE_A="--header=Host:cache_a.example.com"

test/pagespeed_test.conf.template

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ http {
812812
server_name www.example.com;
813813
pagespeed FileCachePath "@@FILE_CACHE@@";
814814

815-
pagespeed LoadFromFile http://cdn.example.com @@SERVER_ROOT@@;
815+
pagespeed LoadFromFile http://cdn.example.com @@SERVER_ROOT@@/;
816816
pagespeed MapRewriteDomain cdn.example.com origin.example.com;
817817
pagespeed RewriteLevel PassThrough;
818818
pagespeed EnableFilters rewrite_css,rewrite_images;
@@ -826,7 +826,7 @@ http {
826826
server_name origin.example.com;
827827
pagespeed FileCachePath "@@FILE_CACHE@@";
828828

829-
pagespeed LoadFromFile http://cdn.example.com @@SERVER_ROOT@@;
829+
pagespeed LoadFromFile http://cdn.example.com @@SERVER_ROOT@@/;
830830
pagespeed MapRewriteDomain cdn.example.com origin.example.com;
831831
pagespeed RewriteLevel PassThrough;
832832
pagespeed EnableFilters rewrite_css,rewrite_images;
@@ -840,7 +840,7 @@ http {
840840
server_name cdn.example.com;
841841
pagespeed FileCachePath "@@FILE_CACHE@@";
842842

843-
pagespeed LoadFromFile http://cdn.example.com @@SERVER_ROOT@@;
843+
pagespeed LoadFromFile http://cdn.example.com @@SERVER_ROOT@@/;
844844
pagespeed MapRewriteDomain cdn.example.com origin.example.com;
845845
pagespeed RewriteLevel PassThrough;
846846
pagespeed EnableFilters rewrite_css,rewrite_images;
@@ -1332,7 +1332,7 @@ http {
13321332
listen [::]:@@SECONDARY_PORT@@;
13331333
server_name proxy-post-origin.example.com;
13341334
pagespeed FileCachePath "@@FILE_CACHE@@";
1335-
root "@@SERVER_ROOT@@";
1335+
root "@@SERVER_ROOT@@/";
13361336
}
13371337

13381338
server {
@@ -1350,7 +1350,7 @@ http {
13501350
listen [::]:@@SECONDARY_PORT@@;
13511351
server_name script-filters.example.com;
13521352
pagespeed FileCachePath "@@FILE_CACHE@@";
1353-
root "@@SERVER_ROOT@@";
1353+
root "@@SERVER_ROOT@@/";
13541354
set $filters "";
13551355
set $domain_shards "cdn1.example.com,cdn2.example.com";
13561356
if ($http_X_Script) {
@@ -1372,7 +1372,7 @@ http {
13721372
pagespeed EnableCachePurge on;
13731373

13741374
pagespeed PurgeMethod PURGE;
1375-
root "@@SERVER_ROOT@@/mod_pagespeed_test/purge";
1375+
root "@@SERVER_ROOT@@/purge";
13761376
pagespeed FileCachePath "@@FILE_CACHE@@_purge";
13771377
pagespeed DisableFilters add_instrumentation;
13781378
pagespeed RewriteLevel PassThrough;
@@ -1388,7 +1388,7 @@ http {
13881388
pagespeed EnableCachePurge on;
13891389

13901390
pagespeed PurgeMethod PURGE;
1391-
root "@@SERVER_ROOT@@/mod_pagespeed_test/purge";
1391+
root "@@SERVER_ROOT@@/purge";
13921392
pagespeed FileCachePath "@@FILE_CACHE@@_dir_on";
13931393
pagespeed DisableFilters add_instrumentation;
13941394
pagespeed RewriteLevel PassThrough;
@@ -1406,7 +1406,7 @@ http {
14061406
pagespeed InPlaceResourceOptimization off;
14071407
pagespeed off;
14081408
pagespeed FileCachePath "@@FILE_CACHE@@";
1409-
root "@@SERVER_ROOT@@";
1409+
root "@@SERVER_ROOT@@/";
14101410
location /mod_pagespeed_test/nostore {
14111411
add_header "Cache-Control" "max-age=12345";
14121412
add_header "Cache-Control" "public, no-store";
@@ -1421,7 +1421,7 @@ http {
14211421
pagespeed InPlaceResourceOptimization off;
14221422
pagespeed off;
14231423
pagespeed FileCachePath "@@FILE_CACHE@@";
1424-
root "@@SERVER_ROOT@@";
1424+
root "@@SERVER_ROOT@@/";
14251425
location / {
14261426
add_header "PageSpeedFilters" "add_instrumentation";
14271427
}
@@ -1434,7 +1434,7 @@ http {
14341434
pagespeed InPlaceResourceOptimization off;
14351435
pagespeed off;
14361436
pagespeed FileCachePath "@@FILE_CACHE@@";
1437-
root "@@SERVER_ROOT@@";
1437+
root "@@SERVER_ROOT@@/";
14381438
location / {
14391439
add_header "PageSpeed" "off";
14401440
}
@@ -1759,7 +1759,7 @@ http {
17591759
# optimized.
17601760
location /mod_pagespeed_test/auth/ {
17611761
auth_basic "Restricted";
1762-
auth_basic_user_file "@@SERVER_ROOT@@mod_pagespeed_test/auth/passwd.conf";
1762+
auth_basic_user_file "@@SERVER_ROOT@@/mod_pagespeed_test/auth/passwd.conf";
17631763
}
17641764

17651765
location /mod_pagespeed_test/ipro/cookie/ {

0 commit comments

Comments
 (0)