From a39144add7a9fd00b2b5c0f02706c3f2740d4d2f Mon Sep 17 00:00:00 2001 From: Maks Orlovich Date: Fri, 1 Feb 2013 15:14:11 -0500 Subject: [PATCH] Use the new common parsing code in PSOL --- src/ngx_rewrite_options.cc | 152 ++++--------------------------------- src/ngx_rewrite_options.h | 19 ++--- 2 files changed, 20 insertions(+), 151 deletions(-) diff --git a/src/ngx_rewrite_options.cc b/src/ngx_rewrite_options.cc index 3b355a36d..cd0241197 100644 --- a/src/ngx_rewrite_options.cc +++ b/src/ngx_rewrite_options.cc @@ -91,154 +91,27 @@ RewriteOptions::OptionSettingResult NgxRewriteOptions::ParseAndSetOptions0( return RewriteOptions::kOptionOk; } -RewriteOptions::OptionSettingResult NgxRewriteOptions::ParseAndSetOptions1( - StringPiece directive, StringPiece arg, - GoogleString* msg, MessageHandler* handler) { +RewriteOptions::OptionSettingResult + NgxRewriteOptions::ParseAndSetOptionFromEnum1( + OptionEnum directive, StringPiece arg, + GoogleString* msg, MessageHandler* handler) { // FileCachePath needs error checking. - if (IsDirective(directive, "FileCachePath")) { + if (directive == kFileCachePath) { if (!StringCaseStartsWith(arg, "/")) { *msg = "must start with a slash"; return RewriteOptions::kOptionValueInvalid; - } else { - set_file_cache_path(arg.as_string()); } } - RewriteOptions::OptionSettingResult result = - SetOptionFromName(directive, arg.as_string(), msg); - if (result != RewriteOptions::kOptionNameUnknown) { - return result; - } + // TODO(jefftk): port these (no enums for them yet, even!) + // DangerPermitFetchFromUnknownHosts, FetchWithGzip, ForceCaching - if (IsDirective(directive, "Allow")) { - Allow(arg); - } else if (IsDirective(directive, "DangerPermitFetchFromUnknownHosts")) { - // TODO(jefftk): port this. - *msg = "not supported"; - return RewriteOptions::kOptionValueInvalid; - } else if (IsDirective(directive, "DisableFilters")) { - bool ok = DisableFiltersByCommaSeparatedList(arg, handler); - if (!ok) { - *msg = "Failed to disable some filters."; - return RewriteOptions::kOptionValueInvalid; - } - } else if (IsDirective(directive, "Disallow")) { - Disallow(arg); - } else if (IsDirective(directive, "Domain")) { - domain_lawyer()->AddDomain(arg, handler); - } else if (IsDirective(directive, "EnableFilters")) { - bool ok = EnableFiltersByCommaSeparatedList(arg, handler); - if (!ok) { - *msg = "Failed to enable some filters."; - return RewriteOptions::kOptionValueInvalid; - } - } else if (IsDirective(directive, "FetchWithGzip")) { - // TODO(jefftk): port this. - *msg = "not supported"; - return RewriteOptions::kOptionValueInvalid; - } else if (IsDirective(directive, "ForceCaching")) { - // TODO(jefftk): port this. - *msg = "not supported"; - return RewriteOptions::kOptionValueInvalid; - } else if (IsDirective(directive, "ExperimentVariable")) { - int slot; - bool ok = StringToInt(arg.as_string().c_str(), &slot); - if (!ok || slot < 1 || slot > 5) { - *msg = "must be an integer between 1 and 5"; - return RewriteOptions::kOptionValueInvalid; - } - set_furious_ga_slot(slot); - } else if (IsDirective(directive, "ExperimentSpec")) { - bool ok = AddFuriousSpec(arg, handler); - if (!ok) { - *msg = "not a valid experiment spec"; - return RewriteOptions::kOptionValueInvalid; - } - } else if (IsDirective(directive, "RetainComment")) { - RetainComment(arg); - } else if (IsDirective(directive, "BlockingRewriteKey")) { - set_blocking_rewrite_key(arg); - } else { - return RewriteOptions::kOptionNameUnknown; - } - - return RewriteOptions::kOptionOk; -} - -RewriteOptions::OptionSettingResult NgxRewriteOptions::ParseAndSetOptions2( - StringPiece directive, StringPiece arg1, StringPiece arg2, - GoogleString* msg, MessageHandler* handler) { - if (IsDirective(directive, "MapRewriteDomain")) { - domain_lawyer()->AddRewriteDomainMapping(arg1, arg2, handler); - } else if (IsDirective(directive, "MapOriginDomain")) { - domain_lawyer()->AddOriginDomainMapping(arg1, arg2, handler); - } else if (IsDirective(directive, "MapProxyDomain")) { - domain_lawyer()->AddProxyDomainMapping(arg1, arg2, handler); - } else if (IsDirective(directive, "ShardDomain")) { - domain_lawyer()->AddShard(arg1, arg2, handler); - } else if (IsDirective(directive, "CustomFetchHeader")) { - AddCustomFetchHeader(arg1, arg2); - } else if (IsDirective(directive, "LoadFromFile")) { - file_load_policy()->Associate(arg1, arg2); - } else if (IsDirective(directive, "LoadFromFileMatch")) { - if (!file_load_policy()->AssociateRegexp(arg1, arg2, msg)) { - return RewriteOptions::kOptionValueInvalid; - } - } else if (IsDirective(directive, "LoadFromFileRule") - || IsDirective(directive, "LoadFromFileRuleMatch")) { - bool is_regexp = IsDirective(directive, "LoadFromFileRuleMatch"); - bool allow; - // TODO(oschaaf): we should probably define consts for Allow/Disallow - if (IsDirective(arg1, "Allow")) { - allow = true; - } else if (IsDirective(arg1, "Disallow")) { - allow = false; - } else { - *msg = "Argument 1 must be either 'Allow' or 'Disallow'"; - return RewriteOptions::kOptionValueInvalid; - } - if (!file_load_policy()->AddRule(arg2.as_string().c_str(), - is_regexp, allow, msg)) { - return RewriteOptions::kOptionValueInvalid; - } - } else { - return RewriteOptions::kOptionNameUnknown; - } - return RewriteOptions::kOptionOk; -} - -RewriteOptions::OptionSettingResult NgxRewriteOptions::ParseAndSetOptions3( - StringPiece directive, StringPiece arg1, StringPiece arg2, StringPiece arg3, - GoogleString* msg, MessageHandler* handler) { - if (IsDirective(directive, "UrlValuedAttribute")) { - semantic_type::Category category; - bool ok = semantic_type::ParseCategory(arg3, &category); - if (!ok) { - *msg = "Invalid resource category"; - return RewriteOptions::kOptionValueInvalid; - } - AddUrlValuedAttribute(arg1, arg2, category); - } else if (IsDirective(directive, "Library")) { - int64 bytes; - bool ok = StringToInt64(arg1.as_string().c_str(), &bytes); - if (!ok || bytes < 0) { - *msg = "Size must be a positive 64-bit integer"; - return RewriteOptions::kOptionValueInvalid; - } - ok = RegisterLibrary(bytes, arg2, arg3); - if (!ok) { - *msg = "Format is size md5 url; bad md5 or URL"; - return RewriteOptions::kOptionValueInvalid; - } - } else { - return RewriteOptions::kOptionNameUnknown; - } - return RewriteOptions::kOptionOk; + return SystemRewriteOptions::ParseAndSetOptionFromEnum1( + directive, arg, msg, handler); } // Very similar to apache/mod_instaweb::ParseDirective. -// TODO(jefftk): Move argument parsing to OriginRewriteOptions. const char* NgxRewriteOptions::ParseAndSetOptions( StringPiece* args, int n_args, ngx_pool_t* pool, MessageHandler* handler) { @@ -266,11 +139,12 @@ NgxRewriteOptions::ParseAndSetOptions( if (n_args == 1) { result = ParseAndSetOptions0(directive, &msg, handler); } else if (n_args == 2) { - result = ParseAndSetOptions1(directive, args[1], &msg, handler); + result = ParseAndSetOptionFromName1(directive, args[1], &msg, handler); } else if (n_args == 3) { - result = ParseAndSetOptions2(directive, args[1], args[2], &msg, handler); + result = ParseAndSetOptionFromName2(directive, args[1], args[2], + &msg, handler); } else if (n_args == 4) { - result = ParseAndSetOptions3( + result = ParseAndSetOptionFromName3( directive, args[1], args[2], args[3], &msg, handler); } else { return "unknown option"; diff --git a/src/ngx_rewrite_options.h b/src/ngx_rewrite_options.h index 66106fc02..c7d7917ae 100644 --- a/src/ngx_rewrite_options.h +++ b/src/ngx_rewrite_options.h @@ -17,10 +17,6 @@ // Author: jefftk@google.com (Jeff Kaufman) // Manage configuration for pagespeed. Compare to ApacheConfig. -// -// TODO(jefftk): Much of the code here is copied from ApacheConfig, and is very -// similar. It may be worth it to create an OriginRewriteOptions that both -// NgxRewriteOptions and ApacheConfig inherit from. #ifndef NGX_REWRITE_OPTIONS_H_ #define NGX_REWRITE_OPTIONS_H_ @@ -84,15 +80,14 @@ class NgxRewriteOptions : public SystemRewriteOptions { // detailed message goes to their log via handler. OptionSettingResult ParseAndSetOptions0( StringPiece directive, GoogleString* msg, MessageHandler* handler); - OptionSettingResult ParseAndSetOptions1( - StringPiece directive, StringPiece arg, + + // These are called via RewriteOptions::ParseAndSetOptionFromName[123] + virtual OptionSettingResult ParseAndSetOptionFromEnum1( + OptionEnum name, StringPiece arg, GoogleString* msg, MessageHandler* handler); - OptionSettingResult ParseAndSetOptions2( - StringPiece directive, StringPiece arg1, StringPiece arg2, - GoogleString* msg, MessageHandler* handler); - OptionSettingResult ParseAndSetOptions3( - StringPiece directive, StringPiece arg1, StringPiece arg2, - StringPiece arg3, GoogleString* msg, MessageHandler* handler); + + // We may want to override 2- and 3-argument versions as well in the future, + // but they are not needed yet. // Keeps the properties added by this subclass. These are merged into // RewriteOptions::all_properties_ during Initialize().