Page Speed Optimization Libraries  1.4.26.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 
00190   bool AddProxyDomainMapping(const StringPiece& proxy_domain,
00191                              const StringPiece& origin_domain,
00192                              const StringPiece& to_domain_name,
00193                              MessageHandler* handler);
00194 
00200   bool AddTwoProtocolOriginDomainMapping(const StringPiece& to_domain_name,
00201                                          const StringPiece& from_domain_name,
00202                                          MessageHandler* handler);
00203 
00207   bool AddShard(const StringPiece& to_domain,
00208                 const StringPiece& comma_separated_shards,
00209                 MessageHandler* handler);
00210 
00220   bool ShardDomain(const StringPiece& domain_name, uint32 hash,
00221                    GoogleString* sharded_domain) const;
00222 
00227   void Merge(const DomainLawyer& src);
00228 
00232   bool WillDomainChange(const StringPiece& domain_name) const;
00233 
00236   bool can_rewrite_domains() const { return can_rewrite_domains_; }
00237 
00239   int num_wildcarded_domains() const { return wildcarded_domains_.size(); }
00240 
00243   bool DoDomainsServeSameContent(const StringPiece& domain1,
00244                                  const StringPiece& domain2) const;
00245 
00248   void FindDomainsRewrittenTo(
00249       const GoogleUrl& domain_name,
00250       ConstStringStarVector* from_domains) const;
00251 
00254   GoogleString Signature() const;
00255 
00261   GoogleString ToString(StringPiece line_prefix) const;
00262 
00264   GoogleString ToString() const { return ToString(StringPiece()); }
00265 
00266  private:
00267   class Domain;
00268 
00269   typedef bool (Domain::*SetDomainFn)(Domain* domain, MessageHandler* handler);
00270 
00271   static GoogleString NormalizeDomainName(const StringPiece& domain_name);
00272 
00273   static bool IsSchemeSafeToMapTo(const StringPiece& domain_name,
00274                                   bool allow_https_scheme);
00275 
00276   bool MapDomainHelper(
00277       const StringPiece& to_domain_name,
00278       const StringPiece& comma_separated_from_domains,
00279       SetDomainFn set_domain_fn,
00280       bool allow_wildcards,
00281       bool allow_map_to_https,
00282       bool authorize,
00283       MessageHandler* handler);
00284 
00285   bool MapUrlHelper(const Domain& from_domain,
00286                     const Domain& to_domain,
00287                     const GoogleUrl& gurl,
00288                     GoogleUrl* mapped_gurl) const;
00289 
00290   bool DomainNameToTwoProtocols(const StringPiece& domain_name,
00291                                 GoogleString* http_url,
00292                                 GoogleString* https_url);
00293 
00294   bool TwoProtocolDomainHelper(
00295       const StringPiece& to_domain_name,
00296       const StringPiece& from_domain_name,
00297       SetDomainFn set_domain_fn,
00298       bool authorize,
00299       MessageHandler* handler);
00300 
00301   Domain* AddDomainHelper(const StringPiece& domain_name,
00302                           bool warn_on_duplicate,
00303                           bool authorize,
00304                           bool is_proxy,
00305                           MessageHandler* handler);
00306   Domain* CloneAndAdd(const Domain* src);
00307 
00308   Domain* FindDomain(const GoogleUrl& gurl) const;
00309 
00312   typedef std::map<GoogleString, Domain*> DomainMap; 
00313   DomainMap domain_map_;
00314   typedef std::vector<Domain*> DomainVector; 
00315   DomainVector wildcarded_domains_;
00316   bool can_rewrite_domains_;
00318 
00319   DISALLOW_COPY_AND_ASSIGN(DomainLawyer);
00320 };
00321 
00322 }  
00323 
00324 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_DOMAIN_LAWYER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines