Page Speed Optimization Libraries
1.7.30.1
|
00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00032 00033 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_DOMAIN_LAWYER_H_ 00034 #define NET_INSTAWEB_REWRITER_PUBLIC_DOMAIN_LAWYER_H_ 00035 00036 #include <map> 00037 #include <vector> 00038 00039 #include "net/instaweb/util/public/basictypes.h" 00040 #include "net/instaweb/util/public/string.h" 00041 #include "net/instaweb/util/public/string_util.h" 00042 00043 namespace net_instaweb { 00044 class GoogleUrl; 00045 class MessageHandler; 00046 00047 class DomainLawyer { 00048 public: 00049 DomainLawyer() { Clear(); } 00050 ~DomainLawyer(); 00051 00052 DomainLawyer& operator=(const DomainLawyer& src) { 00053 if (&src != this) { 00054 Clear(); 00055 Merge(src); 00056 } 00057 return *this; 00058 } 00059 00060 DomainLawyer(const DomainLawyer& src) { 00061 Clear(); 00062 Merge(src); 00063 } 00064 00093 bool MapRequestToDomain(const GoogleUrl& original_request, 00094 const StringPiece& resource_url, 00095 GoogleString* mapped_domain_name, 00096 GoogleUrl* resolved_request, 00097 MessageHandler* handler) const; 00098 00106 bool IsDomainAuthorized(const GoogleUrl& original_request, 00107 const GoogleUrl& domain_to_check) const; 00108 00109 00121 bool IsOriginKnown(const GoogleUrl& domain_to_check) const; 00122 00130 bool MapOrigin(const StringPiece& in, GoogleString* out, 00131 bool* is_proxy) const; 00132 bool MapOriginUrl(const GoogleUrl& gurl, GoogleString* out, 00133 bool* is_proxy) const; 00134 00137 00143 bool AddDomain(const StringPiece& domain_name, MessageHandler* handler); 00144 00147 bool AddKnownDomain(const StringPiece& domain_name, MessageHandler* handler); 00148 00159 bool AddRewriteDomainMapping(const StringPiece& to_domain, 00160 const StringPiece& comma_separated_from_domains, 00161 MessageHandler* handler); 00162 00168 bool AddTwoProtocolRewriteDomainMapping(const StringPiece& to_domain_name, 00169 const StringPiece& from_domain_name, 00170 MessageHandler* handler); 00171 00184 bool AddOriginDomainMapping(const StringPiece& to_domain, 00185 const StringPiece& comma_separated_from_domains, 00186 MessageHandler* handler); 00187 00212 bool AddProxyDomainMapping(const StringPiece& proxy_domain, 00213 const StringPiece& origin_domain, 00214 const StringPiece& to_domain_name, 00215 MessageHandler* handler); 00216 00222 bool AddTwoProtocolOriginDomainMapping(const StringPiece& to_domain_name, 00223 const StringPiece& from_domain_name, 00224 MessageHandler* handler); 00225 00229 bool AddShard(const StringPiece& to_domain, 00230 const StringPiece& comma_separated_shards, 00231 MessageHandler* handler); 00232 00242 bool ShardDomain(const StringPiece& domain_name, uint32 hash, 00243 GoogleString* sharded_domain) const; 00244 00249 void Merge(const DomainLawyer& src); 00250 00251 void Clear(); 00252 bool empty() const { return domain_map_.empty(); } 00253 00259 bool WillDomainChange(const GoogleUrl& url) const; 00260 00262 bool IsProxyMapped(const GoogleUrl& url) const; 00263 00266 bool can_rewrite_domains() const { return can_rewrite_domains_; } 00267 00269 int num_wildcarded_domains() const { return wildcarded_domains_.size(); } 00270 00273 bool DoDomainsServeSameContent(const StringPiece& domain1, 00274 const StringPiece& domain2) const; 00275 00278 void FindDomainsRewrittenTo( 00279 const GoogleUrl& domain_name, 00280 ConstStringStarVector* from_domains) const; 00281 00284 GoogleString Signature() const; 00285 00291 GoogleString ToString(StringPiece line_prefix) const; 00292 00294 GoogleString ToString() const { return ToString(StringPiece()); } 00295 00296 private: 00297 class Domain; 00298 friend class DomainLawyerTest; 00299 00300 typedef bool (Domain::*SetDomainFn)(Domain* domain, MessageHandler* handler); 00301 00302 static GoogleString NormalizeDomainName(const StringPiece& domain_name); 00303 00304 static bool IsSchemeSafeToMapTo(const StringPiece& domain_name, 00305 bool allow_https_scheme); 00306 00307 bool MapDomainHelper( 00308 const StringPiece& to_domain_name, 00309 const StringPiece& comma_separated_from_domains, 00310 SetDomainFn set_domain_fn, 00311 bool allow_wildcards, 00312 bool allow_map_to_https, 00313 bool authorize, 00314 MessageHandler* handler); 00315 00316 bool MapUrlHelper(const Domain& from_domain, 00317 const Domain& to_domain, 00318 const GoogleUrl& gurl, 00319 GoogleUrl* mapped_gurl) const; 00320 00321 bool DomainNameToTwoProtocols(const StringPiece& domain_name, 00322 GoogleString* http_url, 00323 GoogleString* https_url); 00324 00325 bool TwoProtocolDomainHelper( 00326 const StringPiece& to_domain_name, 00327 const StringPiece& from_domain_name, 00328 SetDomainFn set_domain_fn, 00329 bool authorize, 00330 MessageHandler* handler); 00331 00332 Domain* AddDomainHelper(const StringPiece& domain_name, 00333 bool warn_on_duplicate, 00334 bool authorize, 00335 bool is_proxy, 00336 MessageHandler* handler); 00337 Domain* CloneAndAdd(const Domain* src); 00338 00339 Domain* FindDomain(const GoogleUrl& gurl) const; 00340 00343 typedef std::map<GoogleString, Domain*> DomainMap; 00344 DomainMap domain_map_; 00345 typedef std::vector<Domain*> DomainVector; 00346 DomainVector wildcarded_domains_; 00347 bool can_rewrite_domains_; 00350 bool authorize_all_domains_; 00352 00354 }; 00355 00356 } 00357 00358 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_DOMAIN_LAWYER_H_