Page Speed Optimization Libraries  1.13.35.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 
194  bool AddOriginDomainMapping(const StringPiece& to_domain,
195  const StringPiece& comma_separated_from_domains,
196  const StringPiece& host_header,
197  MessageHandler* handler);
198 
223  bool AddProxyDomainMapping(const StringPiece& proxy_domain,
224  const StringPiece& origin_domain,
225  const StringPiece& to_domain_name,
226  MessageHandler* handler);
227 
234  bool AddTwoProtocolOriginDomainMapping(const StringPiece& to_domain_name,
235  const StringPiece& from_domain_name,
236  const StringPiece& host_header,
237  MessageHandler* handler);
238 
242  bool AddShard(const StringPiece& to_domain,
243  const StringPiece& comma_separated_shards,
244  MessageHandler* handler);
245 
255  bool ShardDomain(const StringPiece& domain_name, uint32 hash,
256  GoogleString* sharded_domain) const;
257 
262  void Merge(const DomainLawyer& src);
263 
264  void Clear();
265  bool empty() const { return domain_map_.empty() && proxy_suffix_.empty(); }
266 
275  bool WillDomainChange(const GoogleUrl& url) const;
276 
278  bool IsProxyMapped(const GoogleUrl& url) const;
279 
282  bool can_rewrite_domains() const {
283  return can_rewrite_domains_ || !proxy_suffix_.empty();
284  }
285 
287  int num_wildcarded_domains() const { return wildcarded_domains_.size(); }
288 
291  bool DoDomainsServeSameContent(const StringPiece& domain1,
292  const StringPiece& domain2) const;
293 
297  const GoogleUrl& domain_name,
298  ConstStringStarVector* from_domains) const;
299 
322  void set_proxy_suffix(const GoogleString& suffix) { proxy_suffix_ = suffix; }
323  const GoogleString& proxy_suffix() const { return proxy_suffix_; }
324 
329  bool StripProxySuffix(const GoogleUrl& gurl,
330  GoogleString* url, GoogleString* host) const;
331 
334  bool AddProxySuffix(const GoogleUrl& base_url, GoogleString* href) const;
335 
338  GoogleString Signature() const;
339 
345  GoogleString ToString(StringPiece line_prefix) const;
346 
348  GoogleString ToString() const { return ToString(StringPiece()); }
349 
350  private:
351  class Domain;
352  friend class DomainLawyerTest;
353 
354  typedef bool (Domain::*SetDomainFn)(Domain* domain, MessageHandler* handler);
355 
356  static GoogleString NormalizeDomainName(const StringPiece& domain_name);
357 
358  static bool IsSchemeSafeToMapTo(const StringPiece& domain_name,
359  bool allow_https_scheme);
360 
361  bool MapDomainHelper(
362  const StringPiece& to_domain_name,
363  const StringPiece& comma_separated_from_domains,
364  const StringPiece& host_header,
365  SetDomainFn set_domain_fn,
366  bool allow_wildcards,
367  bool allow_map_to_https,
368  bool authorize,
369  MessageHandler* handler);
370 
371  bool MapUrlHelper(const Domain& from_domain,
372  const Domain& to_domain,
373  const GoogleUrl& gurl,
374  GoogleUrl* mapped_gurl) const;
375 
376  bool DomainNameToTwoProtocols(const StringPiece& domain_name,
377  GoogleString* http_url,
378  GoogleString* https_url);
379 
380  bool TwoProtocolDomainHelper(
381  const StringPiece& to_domain_name,
382  const StringPiece& from_domain_name,
383  const StringPiece& host_header,
384  SetDomainFn set_domain_fn,
385  bool authorize,
386  MessageHandler* handler);
387 
388  Domain* AddDomainHelper(const StringPiece& domain_name,
389  bool warn_on_duplicate,
390  bool authorize,
391  bool is_proxy,
392  MessageHandler* handler);
393  Domain* CloneAndAdd(const Domain* src);
394 
395  Domain* FindDomain(const GoogleUrl& gurl) const;
396 
399  typedef std::map<GoogleString, Domain*> DomainMap;
400  DomainMap domain_map_;
401  typedef std::vector<Domain*> DomainVector;
402  DomainVector wildcarded_domains_;
403  GoogleString proxy_suffix_;
404  bool can_rewrite_domains_;
407  bool authorize_all_domains_;
409 
411 };
412 
413 }
414 
415 #endif
class GoogleUrl
Definition: google_url.h:58
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:282
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:287
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:348
bool IsOriginKnown(const GoogleUrl &domain_to_check) const
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
bool ShardDomain(const StringPiece &domain_name, uint32 hash, GoogleString *sharded_domain) const
void set_proxy_suffix(const GoogleString &suffix)
Definition: domain_lawyer.h:322
bool AddOriginDomainMapping(const StringPiece &to_domain, const StringPiece &comma_separated_from_domains, const StringPiece &host_header, MessageHandler *handler)
bool AddTwoProtocolOriginDomainMapping(const StringPiece &to_domain_name, const StringPiece &from_domain_name, 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 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)
Definition: message_handler.h:39
bool DoDomainsServeSameContent(const StringPiece &domain1, const StringPiece &domain2) const
bool AddKnownDomain(const StringPiece &domain_name, MessageHandler *handler)