Page Speed Optimization Libraries  1.2.24.1
net/instaweb/rewriter/public/domain_lawyer.h
Go to the documentation of this file.
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() : can_rewrite_domains_(false) {}
00050   ~DomainLawyer();
00051 
00080   bool MapRequestToDomain(const GoogleUrl& original_request,
00081                           const StringPiece& resource_url,
00082                           GoogleString* mapped_domain_name,
00083                           GoogleUrl* resolved_request,
00084                           MessageHandler* handler) const;
00085 
00093   bool IsDomainAuthorized(const GoogleUrl& original_request,
00094                           const GoogleUrl& domain_to_check) const;
00095 
00096 
00103   bool IsOriginKnown(const GoogleUrl& domain_to_check) const;
00104 
00112   bool MapOrigin(const StringPiece& in, GoogleString* out,
00113                  bool* is_proxy) const;
00114   bool MapOriginUrl(const GoogleUrl& gurl, GoogleString* out,
00115                     bool* is_proxy) const;
00116 
00119 
00125   bool AddDomain(const StringPiece& domain_name, MessageHandler* handler);
00126 
00137   bool AddRewriteDomainMapping(const StringPiece& to_domain,
00138                                const StringPiece& comma_separated_from_domains,
00139                                MessageHandler* handler);
00140 
00146   bool AddTwoProtocolRewriteDomainMapping(const StringPiece& to_domain_name,
00147                                           const StringPiece& from_domain_name,
00148                                           MessageHandler* handler);
00149 
00162   bool AddOriginDomainMapping(const StringPiece& to_domain,
00163                               const StringPiece& comma_separated_from_domains,
00164                               MessageHandler* handler);
00165 
00187   bool AddProxyDomainMapping(const StringPiece& proxy_domain,
00188                              const StringPiece& origin_domain,
00189                              MessageHandler* handler);
00190 
00196   bool AddTwoProtocolOriginDomainMapping(const StringPiece& to_domain_name,
00197                                          const StringPiece& from_domain_name,
00198                                          MessageHandler* handler);
00199 
00203   bool AddShard(const StringPiece& to_domain,
00204                 const StringPiece& comma_separated_shards,
00205                 MessageHandler* handler);
00206 
00216   bool ShardDomain(const StringPiece& domain_name, uint32 hash,
00217                    GoogleString* sharded_domain) const;
00218 
00223   void Merge(const DomainLawyer& src);
00224 
00228   bool WillDomainChange(const StringPiece& domain_name) const;
00229 
00232   bool can_rewrite_domains() const { return can_rewrite_domains_; }
00233 
00235   int num_wildcarded_domains() const { return wildcarded_domains_.size(); }
00236 
00239   bool DoDomainsServeSameContent(const StringPiece& domain1,
00240                                  const StringPiece& domain2) const;
00241 
00244   void FindDomainsRewrittenTo(
00245       const GoogleUrl& domain_name,
00246       ConstStringStarVector* from_domains) const;
00247 
00250   GoogleString Signature() const;
00251 
00257   GoogleString ToString(StringPiece line_prefix) const;
00258 
00260   GoogleString ToString() const { return ToString(StringPiece()); }
00261 
00262  private:
00263   class Domain;
00264 
00265   typedef bool (Domain::*SetDomainFn)(Domain* domain, MessageHandler* handler);
00266 
00267   static GoogleString NormalizeDomainName(const StringPiece& domain_name);
00268 
00269   static bool IsSchemeSafeToMapTo(const StringPiece& domain_name,
00270                                   bool allow_https_scheme);
00271 
00272   bool MapDomainHelper(
00273       const StringPiece& to_domain_name,
00274       const StringPiece& comma_separated_from_domains,
00275       SetDomainFn set_domain_fn,
00276       bool allow_wildcards,
00277       bool allow_map_to_https,
00278       bool authorize,
00279       MessageHandler* handler);
00280 
00281   bool MapUrlHelper(const Domain& from_domain,
00282                     const Domain& to_domain,
00283                     const GoogleUrl& gurl,
00284                     GoogleUrl* mapped_gurl) const;
00285 
00286   bool DomainNameToTwoProtocols(const StringPiece& domain_name,
00287                                 GoogleString* http_url,
00288                                 GoogleString* https_url);
00289 
00290   bool TwoProtocolDomainHelper(
00291       const StringPiece& to_domain_name,
00292       const StringPiece& from_domain_name,
00293       SetDomainFn set_domain_fn,
00294       bool authorize,
00295       MessageHandler* handler);
00296 
00297   Domain* AddDomainHelper(const StringPiece& domain_name,
00298                           bool warn_on_duplicate,
00299                           bool authorize,
00300                           bool is_proxy,
00301                           MessageHandler* handler);
00302   Domain* CloneAndAdd(const Domain* src);
00303 
00304   Domain* FindDomain(const GoogleUrl& gurl) const;
00305 
00308   typedef std::map<GoogleString, Domain*> DomainMap; 
00309   DomainMap domain_map_;
00310   typedef std::vector<Domain*> DomainVector; 
00311   DomainVector wildcarded_domains_;
00312   bool can_rewrite_domains_;
00314 
00315   DISALLOW_COPY_AND_ASSIGN(DomainLawyer);
00316 };
00317 
00318 }  
00319 
00320 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_DOMAIN_LAWYER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines