Page Speed Optimization Libraries
1.8.31.3
|
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 00133 bool MapOrigin(const StringPiece& in, GoogleString* out, 00134 GoogleString* host_header, bool* is_proxy) const; 00135 bool MapOriginUrl(const GoogleUrl& gurl, GoogleString* out, 00136 GoogleString* host_header, bool* is_proxy) const; 00137 00140 00146 bool AddDomain(const StringPiece& domain_name, MessageHandler* handler); 00147 00150 bool AddKnownDomain(const StringPiece& domain_name, MessageHandler* handler); 00151 00162 bool AddRewriteDomainMapping(const StringPiece& to_domain, 00163 const StringPiece& comma_separated_from_domains, 00164 MessageHandler* handler); 00165 00171 bool AddTwoProtocolRewriteDomainMapping(const StringPiece& to_domain_name, 00172 const StringPiece& from_domain_name, 00173 MessageHandler* handler); 00174 00191 bool AddOriginDomainMapping(const StringPiece& to_domain, 00192 const StringPiece& comma_separated_from_domains, 00193 const StringPiece& host_header, 00194 MessageHandler* handler); 00195 00220 bool AddProxyDomainMapping(const StringPiece& proxy_domain, 00221 const StringPiece& origin_domain, 00222 const StringPiece& to_domain_name, 00223 MessageHandler* handler); 00224 00230 bool AddTwoProtocolOriginDomainMapping(const StringPiece& to_domain_name, 00231 const StringPiece& from_domain_name, 00232 MessageHandler* handler); 00233 00237 bool AddShard(const StringPiece& to_domain, 00238 const StringPiece& comma_separated_shards, 00239 MessageHandler* handler); 00240 00250 bool ShardDomain(const StringPiece& domain_name, uint32 hash, 00251 GoogleString* sharded_domain) const; 00252 00257 void Merge(const DomainLawyer& src); 00258 00259 void Clear(); 00260 bool empty() const { return domain_map_.empty(); } 00261 00267 bool WillDomainChange(const GoogleUrl& url) const; 00268 00270 bool IsProxyMapped(const GoogleUrl& url) const; 00271 00274 bool can_rewrite_domains() const { return can_rewrite_domains_; } 00275 00277 int num_wildcarded_domains() const { return wildcarded_domains_.size(); } 00278 00281 bool DoDomainsServeSameContent(const StringPiece& domain1, 00282 const StringPiece& domain2) const; 00283 00286 void FindDomainsRewrittenTo( 00287 const GoogleUrl& domain_name, 00288 ConstStringStarVector* from_domains) const; 00289 00292 GoogleString Signature() const; 00293 00299 GoogleString ToString(StringPiece line_prefix) const; 00300 00302 GoogleString ToString() const { return ToString(StringPiece()); } 00303 00304 private: 00305 class Domain; 00306 friend class DomainLawyerTest; 00307 00308 typedef bool (Domain::*SetDomainFn)(Domain* domain, MessageHandler* handler); 00309 00310 static GoogleString NormalizeDomainName(const StringPiece& domain_name); 00311 00312 static bool IsSchemeSafeToMapTo(const StringPiece& domain_name, 00313 bool allow_https_scheme); 00314 00315 bool MapDomainHelper( 00316 const StringPiece& to_domain_name, 00317 const StringPiece& comma_separated_from_domains, 00318 const StringPiece& host_header, 00319 SetDomainFn set_domain_fn, 00320 bool allow_wildcards, 00321 bool allow_map_to_https, 00322 bool authorize, 00323 MessageHandler* handler); 00324 00325 bool MapUrlHelper(const Domain& from_domain, 00326 const Domain& to_domain, 00327 const GoogleUrl& gurl, 00328 GoogleUrl* mapped_gurl) const; 00329 00330 bool DomainNameToTwoProtocols(const StringPiece& domain_name, 00331 GoogleString* http_url, 00332 GoogleString* https_url); 00333 00334 bool TwoProtocolDomainHelper( 00335 const StringPiece& to_domain_name, 00336 const StringPiece& from_domain_name, 00337 SetDomainFn set_domain_fn, 00338 bool authorize, 00339 MessageHandler* handler); 00340 00341 Domain* AddDomainHelper(const StringPiece& domain_name, 00342 bool warn_on_duplicate, 00343 bool authorize, 00344 bool is_proxy, 00345 MessageHandler* handler); 00346 Domain* CloneAndAdd(const Domain* src); 00347 00348 Domain* FindDomain(const GoogleUrl& gurl) const; 00349 00352 typedef std::map<GoogleString, Domain*> DomainMap; 00353 DomainMap domain_map_; 00354 typedef std::vector<Domain*> DomainVector; 00355 DomainVector wildcarded_domains_; 00356 bool can_rewrite_domains_; 00359 bool authorize_all_domains_; 00361 00363 }; 00364 00365 } 00366 00367 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_DOMAIN_LAWYER_H_