Page Speed Optimization Libraries  1.9.32.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
domain_lawyer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
32 
33 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_DOMAIN_LAWYER_H_
34 #define NET_INSTAWEB_REWRITER_PUBLIC_DOMAIN_LAWYER_H_
35 
36 #include <map>
37 #include <vector>
38 
39 #include "pagespeed/kernel/base/basictypes.h"
40 #include "pagespeed/kernel/base/string.h"
41 #include "pagespeed/kernel/base/string_util.h"
42 
43 namespace net_instaweb {
44 class GoogleUrl;
45 class MessageHandler;
46 
47 class DomainLawyer {
48  public:
49  DomainLawyer() { Clear(); }
50  ~DomainLawyer();
51 
52  DomainLawyer& operator=(const DomainLawyer& src) {
53  if (&src != this) {
54  Clear();
55  Merge(src);
56  }
57  return *this;
58  }
59 
60  DomainLawyer(const DomainLawyer& src) {
61  Clear();
62  Merge(src);
63  }
64 
93  bool MapRequestToDomain(const GoogleUrl& original_request,
94  const StringPiece& resource_url,
95  GoogleString* mapped_domain_name,
96  GoogleUrl* resolved_request,
97  MessageHandler* handler) const;
98 
106  bool IsDomainAuthorized(const GoogleUrl& original_request,
107  const GoogleUrl& domain_to_check) const;
108 
109 
121  bool IsOriginKnown(const GoogleUrl& domain_to_check) const;
122 
133  bool MapOrigin(const StringPiece& in, GoogleString* out,
134  GoogleString* host_header, bool* is_proxy) const;
135  bool MapOriginUrl(const GoogleUrl& gurl, GoogleString* out,
136  GoogleString* host_header, bool* is_proxy) const;
137 
140 
146  bool AddDomain(const StringPiece& domain_name, MessageHandler* handler);
147 
150  bool AddKnownDomain(const StringPiece& domain_name, MessageHandler* handler);
151 
162  bool AddRewriteDomainMapping(const StringPiece& to_domain,
163  const StringPiece& comma_separated_from_domains,
164  MessageHandler* handler);
165 
171  bool AddTwoProtocolRewriteDomainMapping(const StringPiece& to_domain_name,
172  const StringPiece& from_domain_name,
173  MessageHandler* handler);
174 
191  bool AddOriginDomainMapping(const StringPiece& to_domain,
192  const StringPiece& comma_separated_from_domains,
193  const StringPiece& host_header,
194  MessageHandler* handler);
195 
220  bool AddProxyDomainMapping(const StringPiece& proxy_domain,
221  const StringPiece& origin_domain,
222  const StringPiece& to_domain_name,
223  MessageHandler* handler);
224 
230  bool AddTwoProtocolOriginDomainMapping(const StringPiece& to_domain_name,
231  const StringPiece& from_domain_name,
232  MessageHandler* handler);
233 
237  bool AddShard(const StringPiece& to_domain,
238  const StringPiece& comma_separated_shards,
239  MessageHandler* handler);
240 
250  bool ShardDomain(const StringPiece& domain_name, uint32 hash,
251  GoogleString* sharded_domain) const;
252 
257  void Merge(const DomainLawyer& src);
258 
259  void Clear();
260  bool empty() const { return domain_map_.empty() && proxy_suffix_.empty(); }
261 
270  bool WillDomainChange(const GoogleUrl& url) const;
271 
273  bool IsProxyMapped(const GoogleUrl& url) const;
274 
277  bool can_rewrite_domains() const {
278  return can_rewrite_domains_ || !proxy_suffix_.empty();
279  }
280 
282  int num_wildcarded_domains() const { return wildcarded_domains_.size(); }
283 
286  bool DoDomainsServeSameContent(const StringPiece& domain1,
287  const StringPiece& domain2) const;
288 
292  const GoogleUrl& domain_name,
293  ConstStringStarVector* from_domains) const;
294 
317  void set_proxy_suffix(const GoogleString& suffix) { proxy_suffix_ = suffix; }
318  const GoogleString& proxy_suffix() const { return proxy_suffix_; }
319 
324  bool StripProxySuffix(const GoogleUrl& gurl,
325  GoogleString* url, GoogleString* host) const;
326 
329  bool AddProxySuffix(const GoogleUrl& base_url, GoogleString* href) const;
330 
333  GoogleString Signature() const;
334 
340  GoogleString ToString(StringPiece line_prefix) const;
341 
343  GoogleString ToString() const { return ToString(StringPiece()); }
344 
345  private:
346  class Domain;
347  friend class DomainLawyerTest;
348 
349  typedef bool (Domain::*SetDomainFn)(Domain* domain, MessageHandler* handler);
350 
351  static GoogleString NormalizeDomainName(const StringPiece& domain_name);
352 
353  static bool IsSchemeSafeToMapTo(const StringPiece& domain_name,
354  bool allow_https_scheme);
355 
356  bool MapDomainHelper(
357  const StringPiece& to_domain_name,
358  const StringPiece& comma_separated_from_domains,
359  const StringPiece& host_header,
360  SetDomainFn set_domain_fn,
361  bool allow_wildcards,
362  bool allow_map_to_https,
363  bool authorize,
364  MessageHandler* handler);
365 
366  bool MapUrlHelper(const Domain& from_domain,
367  const Domain& to_domain,
368  const GoogleUrl& gurl,
369  GoogleUrl* mapped_gurl) const;
370 
371  bool DomainNameToTwoProtocols(const StringPiece& domain_name,
372  GoogleString* http_url,
373  GoogleString* https_url);
374 
375  bool TwoProtocolDomainHelper(
376  const StringPiece& to_domain_name,
377  const StringPiece& from_domain_name,
378  SetDomainFn set_domain_fn,
379  bool authorize,
380  MessageHandler* handler);
381 
382  Domain* AddDomainHelper(const StringPiece& domain_name,
383  bool warn_on_duplicate,
384  bool authorize,
385  bool is_proxy,
386  MessageHandler* handler);
387  Domain* CloneAndAdd(const Domain* src);
388 
389  Domain* FindDomain(const GoogleUrl& gurl) const;
390 
393  typedef std::map<GoogleString, Domain*> DomainMap;
394  DomainMap domain_map_;
395  typedef std::vector<Domain*> DomainVector;
396  DomainVector wildcarded_domains_;
397  GoogleString proxy_suffix_;
398  bool can_rewrite_domains_;
401  bool authorize_all_domains_;
403 
405 };
406 
407 }
408 
409 #endif
bool AddShard(const StringPiece &to_domain, const StringPiece &comma_separated_shards, MessageHandler *handler)
GoogleString Signature() const
bool AddProxyDomainMapping(const StringPiece &proxy_domain, const StringPiece &origin_domain, const StringPiece &to_domain_name, MessageHandler *handler)
bool can_rewrite_domains() const
Definition: domain_lawyer.h:277
bool AddProxySuffix(const GoogleUrl &base_url, GoogleString *href) const
bool MapOrigin(const StringPiece &in, GoogleString *out, GoogleString *host_header, bool *is_proxy) const
bool StripProxySuffix(const GoogleUrl &gurl, GoogleString *url, GoogleString *host) const
int num_wildcarded_domains() const
Visible for testing.
Definition: domain_lawyer.h:282
bool AddRewriteDomainMapping(const StringPiece &to_domain, const StringPiece &comma_separated_from_domains, MessageHandler *handler)
bool WillDomainChange(const GoogleUrl &url) const
bool AddDomain(const StringPiece &domain_name, MessageHandler *handler)
bool IsProxyMapped(const GoogleUrl &url) const
Determines whether a URL's domain was proxy-mapped from a different origin.
void Merge(const DomainLawyer &src)
GoogleString ToString() const
Version that's easier to call from debugger.
Definition: domain_lawyer.h:343
bool IsOriginKnown(const GoogleUrl &domain_to_check) const
bool ShardDomain(const StringPiece &domain_name, uint32 hash, GoogleString *sharded_domain) const
void set_proxy_suffix(const GoogleString &suffix)
Definition: domain_lawyer.h:317
bool AddOriginDomainMapping(const StringPiece &to_domain, const StringPiece &comma_separated_from_domains, const StringPiece &host_header, MessageHandler *handler)
void FindDomainsRewrittenTo(const GoogleUrl &domain_name, ConstStringStarVector *from_domains) const
Definition: domain_lawyer.h:47
bool MapRequestToDomain(const GoogleUrl &original_request, const StringPiece &resource_url, GoogleString *mapped_domain_name, GoogleUrl *resolved_request, MessageHandler *handler) const
bool AddTwoProtocolOriginDomainMapping(const StringPiece &to_domain_name, const StringPiece &from_domain_name, MessageHandler *handler)
bool IsDomainAuthorized(const GoogleUrl &original_request, const GoogleUrl &domain_to_check) const
bool AddTwoProtocolRewriteDomainMapping(const StringPiece &to_domain_name, const StringPiece &from_domain_name, MessageHandler *handler)
bool DoDomainsServeSameContent(const StringPiece &domain1, const StringPiece &domain2) const
bool AddKnownDomain(const StringPiece &domain_name, MessageHandler *handler)