Page Speed Optimization Libraries  1.9.32.1
 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 
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(); }
261 
267  bool WillDomainChange(const GoogleUrl& url) const;
268 
270  bool IsProxyMapped(const GoogleUrl& url) const;
271 
274  bool can_rewrite_domains() const { return can_rewrite_domains_; }
275 
277  int num_wildcarded_domains() const { return wildcarded_domains_.size(); }
278 
281  bool DoDomainsServeSameContent(const StringPiece& domain1,
282  const StringPiece& domain2) const;
283 
287  const GoogleUrl& domain_name,
288  ConstStringStarVector* from_domains) const;
289 
292  GoogleString Signature() const;
293 
299  GoogleString ToString(StringPiece line_prefix) const;
300 
302  GoogleString ToString() const { return ToString(StringPiece()); }
303 
304  private:
305  class Domain;
306  friend class DomainLawyerTest;
307 
308  typedef bool (Domain::*SetDomainFn)(Domain* domain, MessageHandler* handler);
309 
310  static GoogleString NormalizeDomainName(const StringPiece& domain_name);
311 
312  static bool IsSchemeSafeToMapTo(const StringPiece& domain_name,
313  bool allow_https_scheme);
314 
315  bool MapDomainHelper(
316  const StringPiece& to_domain_name,
317  const StringPiece& comma_separated_from_domains,
318  const StringPiece& host_header,
319  SetDomainFn set_domain_fn,
320  bool allow_wildcards,
321  bool allow_map_to_https,
322  bool authorize,
323  MessageHandler* handler);
324 
325  bool MapUrlHelper(const Domain& from_domain,
326  const Domain& to_domain,
327  const GoogleUrl& gurl,
328  GoogleUrl* mapped_gurl) const;
329 
330  bool DomainNameToTwoProtocols(const StringPiece& domain_name,
331  GoogleString* http_url,
332  GoogleString* https_url);
333 
334  bool TwoProtocolDomainHelper(
335  const StringPiece& to_domain_name,
336  const StringPiece& from_domain_name,
337  SetDomainFn set_domain_fn,
338  bool authorize,
339  MessageHandler* handler);
340 
341  Domain* AddDomainHelper(const StringPiece& domain_name,
342  bool warn_on_duplicate,
343  bool authorize,
344  bool is_proxy,
345  MessageHandler* handler);
346  Domain* CloneAndAdd(const Domain* src);
347 
348  Domain* FindDomain(const GoogleUrl& gurl) const;
349 
352  typedef std::map<GoogleString, Domain*> DomainMap;
353  DomainMap domain_map_;
354  typedef std::vector<Domain*> DomainVector;
355  DomainVector wildcarded_domains_;
356  bool can_rewrite_domains_;
359  bool authorize_all_domains_;
361 
363 };
364 
365 }
366 
367 #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:274
bool MapOrigin(const StringPiece &in, GoogleString *out, GoogleString *host_header, bool *is_proxy) const
int num_wildcarded_domains() const
Visible for testing.
Definition: domain_lawyer.h:277
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:302
bool IsOriginKnown(const GoogleUrl &domain_to_check) const
bool ShardDomain(const StringPiece &domain_name, uint32 hash, GoogleString *sharded_domain) const
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)