-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathats_rewrite_options.cc
311 lines (273 loc) · 10.6 KB
/
ats_rewrite_options.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ats_rewrite_options.h"
#include "net/instaweb/public/version.h"
#include "net/instaweb/rewriter/public/rewrite_options.h"
#include "pagespeed/kernel/base/timer.h"
#include "pagespeed/kernel/base/message_handler.h"
#include "net/instaweb/rewriter/public/file_load_policy.h"
#include "pagespeed/kernel/base/stdio_file_system.h"
#include "ats_message_handler.h"
#include "ats_rewrite_driver_factory.h"
using namespace std;
namespace net_instaweb {
namespace {
const char kStatisticsPath[] = "StatisticsPath";
const char kGlobalStatisticsPath[] = "GlobalStatisticsPath";
const char kConsolePath[] = "ConsolePath";
const char kMessagesPath[] = "MessagesPath";
const char kAdminPath[] = "AdminPath";
const char kGlobalAdminPath[] = "GlobalAdminPath";
} // namespace
RewriteOptions::Properties *AtsRewriteOptions::ats_properties_ = NULL;
AtsRewriteOptions::AtsRewriteOptions(ThreadSystem *thread_system) : SystemRewriteOptions(thread_system)
{
Init();
}
void
AtsRewriteOptions::Init()
{
DCHECK(ats_properties_ != NULL) << "Call AtsRewriteOptions::Initialize() before construction";
InitializeOptions(ats_properties_);
}
/*
void
AtsRewriteOptions::AddProperties()
{
MergeSubclassProperties(ats_properties_);
AtsRewriteOptions dummy_config(NULL);
dummy_config.set_default_x_header_value(MOD_PAGESPEED_VERSION_STRING "-" LASTCHANGE_STRING);
}*/
void AtsRewriteOptions::AddProperties() {
// ats-specific options.
add_ats_option(
"", &AtsRewriteOptions::statistics_path_, "nsp", kStatisticsPath,
kServerScope, "Set the statistics path. Ex: /ngx_pagespeed_statistics",
false);
add_ats_option(
"", &AtsRewriteOptions::global_statistics_path_, "ngsp",
kGlobalStatisticsPath, kProcessScopeStrict,
"Set the global statistics path. Ex: /ngx_pagespeed_global_statistics",
false);
add_ats_option(
"", &AtsRewriteOptions::console_path_, "ncp", kConsolePath, kServerScope,
"Set the console path. Ex: /pagespeed_console", false);
add_ats_option(
"", &AtsRewriteOptions::messages_path_, "nmp", kMessagesPath,
kServerScope, "Set the messages path. Ex: /ngx_pagespeed_message",
false);
add_ats_option(
"", &AtsRewriteOptions::admin_path_, "nap", kAdminPath,
kServerScope, "Set the admin path. Ex: /pagespeed_admin", false);
add_ats_option(
"", &AtsRewriteOptions::global_admin_path_, "ngap", kGlobalAdminPath,
kProcessScopeStrict,
"Set the global admin path. Ex: /pagespeed_global_admin",
false);
MergeSubclassProperties(ats_properties_);
// Default properties are global but to set them the current API requires
// a RewriteOptions instance and we're in a static method.
AtsRewriteOptions dummy_config(NULL);
dummy_config.set_default_x_header_value(kModPagespeedVersion);
}
void
AtsRewriteOptions::Initialize()
{
if (Properties::Initialize(&ats_properties_)) {
SystemRewriteOptions::Initialize();
AddProperties();
}
}
void
AtsRewriteOptions::Terminate()
{
if (Properties::Terminate(&ats_properties_)) {
SystemRewriteOptions::Terminate();
}
}
bool
AtsRewriteOptions::IsDirective(StringPiece config_directive, StringPiece compare_directive)
{
return StringCaseEqual(config_directive, compare_directive);
}
RewriteOptions::OptionSettingResult
AtsRewriteOptions::ParseAndSetOptions0(StringPiece directive, GoogleString *msg, MessageHandler *handler)
{
if (IsDirective(directive, "on")) {
set_enabled(RewriteOptions::kEnabledOn);
} else if (IsDirective(directive, "off")) {
set_enabled(RewriteOptions::kEnabledOff);
} else if (IsDirective(directive, "unplugged")) {
set_enabled(RewriteOptions::kEnabledUnplugged);
} else {
return RewriteOptions::kOptionNameUnknown;
}
return RewriteOptions::kOptionOk;
}
RewriteOptions::OptionSettingResult
AtsRewriteOptions::ParseAndSetOptionFromName1(StringPiece name, StringPiece arg, GoogleString *msg, MessageHandler *handler)
{
// FileCachePath needs error checking.
if (StringCaseEqual(name, kFileCachePath)) {
if (!StringCaseStartsWith(arg, "/")) {
*msg = "must start with a slash";
return RewriteOptions::kOptionValueInvalid;
}
}
return SystemRewriteOptions::ParseAndSetOptionFromName1(name, arg, msg, handler);
}
bool
AtsRewriteOptions::SetBoolFlag(bool *v, StringPiece arg)
{
if (IsDirective(arg, "on")) {
*v = true;
return true;
} else if (IsDirective(arg, "off")) {
*v = false;
return true;
}
return false;
}
const char *
AtsRewriteOptions::ParseAndSetOptions(vector<string> args, MessageHandler *handler, global_settings &global_config)
{
int n_args = args.size();
CHECK_GE(n_args, 1);
StringPiece directive = args[0];
// Remove initial "ModPagespeed" if there is one.
StringPiece mod_pagespeed("ModPagespeed");
if (StringCaseStartsWith(directive, mod_pagespeed)) {
directive.remove_prefix(mod_pagespeed.size());
}
GoogleString msg;
OptionSettingResult result;
if (n_args == 1) {
result = ParseAndSetOptions0(directive, &msg, handler);
} else if (n_args == 2) {
StringPiece arg = args[1];
if (IsDirective(directive, "UsePerVHostStatistics")) {
if (!SetBoolFlag(&global_config.use_per_vhost_statistics, arg)) {
msg = "Failed to set UsePerVHostStatistics value";
result = RewriteOptions::kOptionValueInvalid;
} else {
result = RewriteOptions::kOptionOk;
}
} /* else if (IsDirective(directive, "InstallCrashHandler")) {
// Not applicable
} */ else if (IsDirective(directive, "MessageBufferSize")) {
int message_buffer_size;
bool ok = StringToInt(arg.as_string(), &message_buffer_size);
if (ok && message_buffer_size >= 0) {
global_config.message_buffer_size = message_buffer_size;
result = RewriteOptions::kOptionOk;
} else {
msg = "Failed to set MessageBufferSize value";
result = RewriteOptions::kOptionValueInvalid;
}
} else if (IsDirective(directive, "UseNativeFetcher")) {
if (!SetBoolFlag(&global_config.info_urls_local_only, arg)) {
msg = "Failed to set UseNativeFetcher value";
result = RewriteOptions::kOptionValueInvalid;
} else {
msg = "Native fetcher is not available in this release";
result = RewriteOptions::kOptionValueInvalid;
}
} else if (IsDirective(directive, "InfoUrlsLocalOnly")) {
if (!SetBoolFlag(&global_config.info_urls_local_only, arg)) {
msg = "Failed to set InfoUrlsLocalOnly value";
result = RewriteOptions::kOptionValueInvalid;
} else {
result = RewriteOptions::kOptionOk;
}
} /* else if (IsDirective(directive, "RateLimitBackgroundFetches")) {
if (!SetBoolFlag(&global_config.rate_limit_background_fetches, arg)) {
msg = "Failed to set RateLimitBackgroundFetches value";
result = RewriteOptions::kOptionValueInvalid;
} else {
result = RewriteOptions::kOptionOk;
}
} else if (IsDirective(directive, "ForceCaching")) {
if (!SetBoolFlag(&global_config.force_caching, arg)) {
msg = "Failed to set ForceCaching value";
result = RewriteOptions::kOptionValueInvalid;
} else {
result = RewriteOptions::kOptionOk;
}
} else if (IsDirective(directive, "ListOutstandingUrlsOnError")) {
if (!SetBoolFlag(&global_config.list_outstanding_urls_on_error, arg)) {
msg = "Failed to set ListOutstandingUrlsOnError value";
result = RewriteOptions::kOptionValueInvalid;
} else {
result = RewriteOptions::kOptionOk;
}
} else if (IsDirective(directive, "TrackOriginalContentLength")) {
if (!SetBoolFlag(&global_config.track_original_content_length, arg)) {
msg = "Failed to set TrackOriginalContentLength value";
result = RewriteOptions::kOptionValueInvalid;
} else {
result = RewriteOptions::kOptionOk;
}
} */ else {
result = ParseAndSetOptionFromName1(directive, args[1], &msg, handler);
}
} else if (n_args == 3) {
if (StringCaseEqual(directive, "CreateSharedMemoryMetadataCache")) {
int64 kb = 0;
if (!StringToInt64(args[2], &kb) || kb < 0) {
result = RewriteOptions::kOptionValueInvalid;
msg = "size_kb must be a positive 64-bit integer";
} else {
global_config.shm_cache_size_kb = kb;
result = kOptionOk;
// bool ok = driver_factory->caches()->CreateShmMetadataCache(
// args[1].as_string(), kb, &msg);
// result = ok ? kOptionOk : kOptionValueInvalid;
}
} else {
result = ParseAndSetOptionFromName2(directive, args[1], args[2], &msg, handler);
}
} else if (n_args == 4) {
result = ParseAndSetOptionFromName3(directive, args[1], args[2], args[3], &msg, handler);
} else {
return "unknown option";
}
if (msg.size()) {
handler->Message(kWarning, "Error handling config line [%s]: [%s]", JoinString(args, ' ').c_str(), msg.c_str());
}
switch (result) {
case RewriteOptions::kOptionOk:
return NULL;
case RewriteOptions::kOptionNameUnknown:
handler->Message(kWarning, "%s", JoinString(args, ' ').c_str());
return "unknown option";
case RewriteOptions::kOptionValueInvalid: {
handler->Message(kWarning, "%s", JoinString(args, ' ').c_str());
return "Invalid value";
}
}
CHECK(false);
return NULL;
}
AtsRewriteOptions *
AtsRewriteOptions::Clone() const
{
AtsRewriteOptions *options = new AtsRewriteOptions(this->thread_system());
options->Merge(*this);
return options;
}
} // namespace net_instaweb