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

Commit 19cf3bb

Browse files
morlovichcrowell
authored andcommitted
Always parse process config into RewriteDriverFactory::default_options,
and then sync it back to per-ServerContext options. Should fix the regression with ImageMaxRewritesAtOnce (#1305) and give RewriteDriverFactory and all the ServerContexts a consistent view of their value.
1 parent 6dfe527 commit 19cf3bb

File tree

6 files changed

+112
-15
lines changed

6 files changed

+112
-15
lines changed

install/debug.conf.template

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,24 @@ ModPagespeedMessagesDomains Allow localhost
17611761
ModPagespeed off
17621762
</VirtualHost>
17631763

1764+
# For testing how we handle process-scope options.
1765+
ModPagespeedIproMaxResponseBytes 1048576001
1766+
<VirtualHost localhost:@@APACHE_SECONDARY_PORT@@>
1767+
ServerName ps1.example.com
1768+
DocumentRoot "@@APACHE_DOC_ROOT@@"
1769+
ModPagespeedFileCachePath "@@MOD_PAGESPEED_CACHE@@_mpr"
1770+
ModPagespeedIproMaxResponseBytes 1048576002
1771+
ModPagespeedEnableFilters debug
1772+
</VirtualHost>
1773+
1774+
<VirtualHost localhost:@@APACHE_SECONDARY_PORT@@>
1775+
ServerName ps2.example.com
1776+
DocumentRoot "@@APACHE_DOC_ROOT@@"
1777+
ModPagespeedFileCachePath "@@MOD_PAGESPEED_CACHE@@_mpr"
1778+
ModPagespeedIproMaxResponseBytes 1048576003
1779+
ModPagespeedEnableFilters debug
1780+
</VirtualHost>
1781+
17641782
# For testing with a custom origin header. In this VirtualHost,
17651783
# /mod_pagespeed_test is included in our DocumentRoot and thus does
17661784
# not need to be in any resource URL paths. This helps us verify that

net/instaweb/rewriter/public/rewrite_options.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,6 +2853,10 @@ class RewriteOptions {
28532853
// not true, this function will DCHECK.
28542854
virtual void Merge(const RewriteOptions& src);
28552855

2856+
// Merge the process scope options (including strict ones) from src into
2857+
// this.
2858+
void MergeOnlyProcessScopeOptions(const RewriteOptions& src);
2859+
28562860
// Registers a wildcard pattern for to be allowed, potentially overriding
28572861
// previous Disallow wildcards.
28582862
void Allow(StringPiece wildcard_pattern) {

net/instaweb/rewriter/rewrite_options.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,6 +3937,28 @@ void RewriteOptions::Merge(const RewriteOptions& src) {
39373937
}
39383938
}
39393939

3940+
void RewriteOptions::MergeOnlyProcessScopeOptions(const RewriteOptions& src) {
3941+
DCHECK(!frozen_);
3942+
#ifndef NDEBUG // MergeOK is only around in CHECK-enabled builds.
3943+
CHECK(src.MergeOK());
3944+
#endif
3945+
3946+
DCHECK_EQ(all_options_.size(), src.all_options_.size());
3947+
DCHECK_EQ(initialized_options_, src.initialized_options_);
3948+
DCHECK_EQ(initialized_options_, all_options_.size());
3949+
3950+
size_t options_to_merge = std::min(all_options_.size(),
3951+
src.all_options_.size());
3952+
for (size_t i = 0; i < options_to_merge; ++i) {
3953+
OptionScope scope = all_options_[i]->scope();
3954+
if (scope == kProcessScope || scope == kProcessScopeStrict) {
3955+
all_options_[i]->Merge(src.all_options_[i]);
3956+
}
3957+
}
3958+
3959+
Modify();
3960+
}
3961+
39403962
RewriteOptions* RewriteOptions::Clone() const {
39413963
RewriteOptions* options = NewOptions();
39423964
options->Merge(*this);

net/instaweb/rewriter/rewrite_options_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,20 @@ TEST_F(RewriteOptionsTest, MergeDistributed) {
661661
EXPECT_FALSE(options_.Distributable(RewriteOptions::kCssFilterId));
662662
}
663663

664+
TEST_F(RewriteOptionsTest, MergeOnlyProcessScopeOptions) {
665+
RewriteOptions dest(&thread_system_), src(&thread_system_);
666+
dest.set_image_max_rewrites_at_once(2);
667+
dest.set_max_url_segment_size(1);
668+
src.set_image_max_rewrites_at_once(5);
669+
src.set_max_url_segment_size(4);
670+
671+
dest.MergeOnlyProcessScopeOptions(src);
672+
// Pulled in set_image_max_rewrites_at_once, which is process scope,
673+
// but not the other option.
674+
EXPECT_EQ(5, dest.image_max_rewrites_at_once());
675+
EXPECT_EQ(1, dest.max_url_segment_size());
676+
}
677+
664678
TEST_F(RewriteOptionsTest, Allow) {
665679
options_.Allow("*.css");
666680
EXPECT_TRUE(options_.IsAllowed("abcd.css"));

pagespeed/apache/mod_instaweb.cc

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,9 @@ class ApacheProcessContext {
332332
return factory_.get();
333333
}
334334

335-
// Checks cmd to see if it's used in a vhost or conditional context, and
336-
// if so, if that's an error or warning condition.
337-
const char* CheckCommandForVhost(const cmd_parms* cmd);
335+
// Checks cmd to see if it's process scope, and if so if it's used in an
336+
// incorrect context, returning an error message if so.
337+
const char* CheckProcessScope(const cmd_parms* cmd, bool* is_process_scope);
338338

339339
scoped_ptr<ApacheRewriteDriverFactory> factory_;
340340
// Process-scoped static variable cleanups, mainly for valgrind.
@@ -949,6 +949,18 @@ int pagespeed_post_config(apr_pool_t* pool, apr_pool_t* plog, apr_pool_t* ptemp,
949949
CHECK(server_context != NULL);
950950
server_contexts.push_back(server_context);
951951
}
952+
953+
// We also want propagate all the per-process options to each vhost. The
954+
// normal merge in merge_server_config isn't enough since that merges the
955+
// non-per process things from a dummy ServerContext corresponding to the
956+
// top-level config, not ApacheRewriteDriverFactory::default_options where
957+
// the process scope options go.
958+
//
959+
// We do this here rather than merge_server_config since we want to touch
960+
// the ServerContext corresponding to the top-level/non-<VirtualHost>
961+
// block, too.
962+
server_context->global_config()->MergeOnlyProcessScopeOptions(
963+
*factory->default_options());
952964
}
953965

954966
GoogleString error_message;
@@ -1291,17 +1303,14 @@ static char* CheckGlobalOption(const cmd_parms* cmd,
12911303
return NULL;
12921304
}
12931305

1294-
const char* ApacheProcessContext::CheckCommandForVhost(const cmd_parms* cmd) {
1295-
// Only do the vhost_command_handling_map_ lookup if it's going
1296-
// to be used by CheckGlobalOption.
1297-
//
1298-
// TODO(jmarantz): Add a scope argument ParseAndSetOptionFromName[123] and
1299-
// let it do the error-checking & reporting.
1306+
const char* ApacheProcessContext::CheckProcessScope(
1307+
const cmd_parms* cmd, bool* is_process_scope) {
1308+
VhostCommandHandlingMap::const_iterator p =
1309+
vhost_command_handling_map_.find(cmd->cmd);
1310+
*is_process_scope = (p != vhost_command_handling_map_.end());
13001311
const char* ret = NULL;
13011312
if (cmd->server->is_virtual || (cmd->directive->data != NULL)) {
1302-
VhostCommandHandlingMap::const_iterator p =
1303-
vhost_command_handling_map_.find(cmd->cmd);
1304-
if (p != vhost_command_handling_map_.end()) {
1313+
if (*is_process_scope) {
13051314
ret = CheckGlobalOption(cmd, p->second, factory_->message_handler());
13061315
}
13071316
}
@@ -1376,20 +1385,36 @@ static const char* ParseDirective(cmd_parms* cmd, void* data, const char* arg) {
13761385
if (directive.starts_with(prefix)) {
13771386
StringPiece option = directive.substr(prefix.size());
13781387
GoogleString msg;
1388+
1389+
bool use_global_config = false;
1390+
// See if it's a global option, and perhaps not in place.
1391+
ret = apache_process_context.CheckProcessScope(cmd, &use_global_config);
1392+
if (ret != NULL) {
1393+
return ret;
1394+
}
1395+
// Options that are per-process are always parsed into
1396+
// ApacheRewriteDriverFactory::default_options(), and then propagated
1397+
// in the post-config hook (pagespeed_post_config).
1398+
if (use_global_config) {
1399+
config = ApacheConfig::DynamicCast(factory->default_options());
1400+
}
1401+
13791402
// See whether generic RewriteOptions name handling can figure this one out.
13801403
RewriteOptions::OptionSettingResult result =
13811404
config->ParseAndSetOptionFromName1(option, arg, &msg, handler);
13821405
if (result == RewriteOptions::kOptionNameUnknown) {
13831406
// RewriteOptions didn't know; try the driver factory.
1407+
// TODO(morlovich): It may be cleaner to not have process-scope options
1408+
// in RewriteOptions at all, but rather something RewriteDriverFactory
1409+
// specific, as long as we can provide a painless way of integrating it
1410+
// in the server and parsing it (areas where the current manual approach
1411+
// fails).
13841412
result = factory->ParseAndSetOption1(
13851413
option, arg,
13861414
!cmd->server->is_virtual, // is_process_scope
13871415
&msg, handler);
13881416
}
13891417
if (StandardParsingHandled(cmd, result, msg, &ret)) {
1390-
if (ret == NULL) {
1391-
ret = apache_process_context.CheckCommandForVhost(cmd);
1392-
}
13931418
return ret;
13941419
}
13951420
}
@@ -1874,6 +1899,7 @@ void* merge_server_config(apr_pool_t* pool, void* base_conf, void* new_conf) {
18741899
new_non_spdy_overlay.release());
18751900
}
18761901
}
1902+
18771903
return new_conf;
18781904
}
18791905

pagespeed/apache/system_test.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,19 @@ start_test Fetch gzipped, make sure that we have cache compressed at gzip 9.
11431143
URL="$PRIMARY_SERVER/mod_pagespeed_test/invalid.css"
11441144
fetch_until -gzip $URL "wc -c" 27
11451145

1146+
if [ "$SECONDARY_HOSTNAME" != "" ]; then
1147+
start_test Process-scope configuration handling.
1148+
# Must be the same value in top-level and both vhosts
1149+
OUT=$($CURL --silent $HOSTNAME/?PageSpeedFilters=+debug)
1150+
check_from "$OUT" fgrep -q "IproMaxResponseBytes (imrb) 1048576003"
1151+
1152+
OUT=$($CURL --silent --proxy $SECONDARY_HOSTNAME http://ps1.example.com)
1153+
check_from "$OUT" fgrep -q "IproMaxResponseBytes (imrb) 1048576003"
1154+
1155+
OUT=$($CURL --silent --proxy $SECONDARY_HOSTNAME http://ps2.example.com)
1156+
check_from "$OUT" fgrep -q "IproMaxResponseBytes (imrb) 1048576003"
1157+
fi
1158+
11461159
# Cleanup
11471160
rm -rf $OUTDIR
11481161

0 commit comments

Comments
 (0)