Page Speed Optimization Libraries  1.6.29.3
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() { 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 
00116   bool IsOriginKnown(const GoogleUrl& domain_to_check) const;
00117 
00125   bool MapOrigin(const StringPiece& in, GoogleString* out,
00126                  bool* is_proxy) const;
00127   bool MapOriginUrl(const GoogleUrl& gurl, GoogleString* out,
00128                     bool* is_proxy) const;
00129 
00132 
00138   bool AddDomain(const StringPiece& domain_name, MessageHandler* handler);
00139 
00142   bool AddKnownDomain(const StringPiece& domain_name, MessageHandler* handler);
00143 
00154   bool AddRewriteDomainMapping(const StringPiece& to_domain,
00155                                const StringPiece& comma_separated_from_domains,
00156                                MessageHandler* handler);
00157 
00163   bool AddTwoProtocolRewriteDomainMapping(const StringPiece& to_domain_name,
00164                                           const StringPiece& from_domain_name,
00165                                           MessageHandler* handler);
00166 
00179   bool AddOriginDomainMapping(const StringPiece& to_domain,
00180                               const StringPiece& comma_separated_from_domains,
00181                               MessageHandler* handler);
00182 
00207   bool AddProxyDomainMapping(const StringPiece& proxy_domain,
00208                              const StringPiece& origin_domain,
00209                              const StringPiece& to_domain_name,
00210                              MessageHandler* handler);
00211 
00217   bool AddTwoProtocolOriginDomainMapping(const StringPiece& to_domain_name,
00218                                          const StringPiece& from_domain_name,
00219                                          MessageHandler* handler);
00220 
00224   bool AddShard(const StringPiece& to_domain,
00225                 const StringPiece& comma_separated_shards,
00226                 MessageHandler* handler);
00227 
00237   bool ShardDomain(const StringPiece& domain_name, uint32 hash,
00238                    GoogleString* sharded_domain) const;
00239 
00244   void Merge(const DomainLawyer& src);
00245 
00246   void Clear();
00247   bool empty() const { return domain_map_.empty(); }
00248 
00254   bool WillDomainChange(const GoogleUrl& url) const;
00255 
00257   bool IsProxyMapped(const GoogleUrl& url) const;
00258 
00261   bool can_rewrite_domains() const { return can_rewrite_domains_; }
00262 
00264   int num_wildcarded_domains() const { return wildcarded_domains_.size(); }
00265 
00268   bool DoDomainsServeSameContent(const StringPiece& domain1,
00269                                  const StringPiece& domain2) const;
00270 
00273   void FindDomainsRewrittenTo(
00274       const GoogleUrl& domain_name,
00275       ConstStringStarVector* from_domains) const;
00276 
00279   GoogleString Signature() const;
00280 
00286   GoogleString ToString(StringPiece line_prefix) const;
00287 
00289   GoogleString ToString() const { return ToString(StringPiece()); }
00290 
00291  private:
00292   class Domain;
00293   friend class DomainLawyerTest;
00294 
00295   typedef bool (Domain::*SetDomainFn)(Domain* domain, MessageHandler* handler);
00296 
00297   static GoogleString NormalizeDomainName(const StringPiece& domain_name);
00298 
00299   static bool IsSchemeSafeToMapTo(const StringPiece& domain_name,
00300                                   bool allow_https_scheme);
00301 
00302   bool MapDomainHelper(
00303       const StringPiece& to_domain_name,
00304       const StringPiece& comma_separated_from_domains,
00305       SetDomainFn set_domain_fn,
00306       bool allow_wildcards,
00307       bool allow_map_to_https,
00308       bool authorize,
00309       MessageHandler* handler);
00310 
00311   bool MapUrlHelper(const Domain& from_domain,
00312                     const Domain& to_domain,
00313                     const GoogleUrl& gurl,
00314                     GoogleUrl* mapped_gurl) const;
00315 
00316   bool DomainNameToTwoProtocols(const StringPiece& domain_name,
00317                                 GoogleString* http_url,
00318                                 GoogleString* https_url);
00319 
00320   bool TwoProtocolDomainHelper(
00321       const StringPiece& to_domain_name,
00322       const StringPiece& from_domain_name,
00323       SetDomainFn set_domain_fn,
00324       bool authorize,
00325       MessageHandler* handler);
00326 
00327   Domain* AddDomainHelper(const StringPiece& domain_name,
00328                           bool warn_on_duplicate,
00329                           bool authorize,
00330                           bool is_proxy,
00331                           MessageHandler* handler);
00332   Domain* CloneAndAdd(const Domain* src);
00333 
00334   Domain* FindDomain(const GoogleUrl& gurl) const;
00335 
00338   typedef std::map<GoogleString, Domain*> DomainMap; 
00339   DomainMap domain_map_;
00340   typedef std::vector<Domain*> DomainVector; 
00341   DomainVector wildcarded_domains_;
00342   bool can_rewrite_domains_;
00345   bool authorize_all_domains_;
00347 
00349 };
00350 
00351 }  
00352 
00353 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_DOMAIN_LAWYER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines