OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/public/common/origin_util.h" | 5 #include "content/public/common/origin_util.h" |
6 | 6 |
7 #include <string> | |
8 | |
9 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
10 #include "base/macros.h" | 8 #include "base/macros.h" |
11 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
12 #include "base/strings/string_piece.h" | |
13 #include "content/public/common/content_client.h" | 10 #include "content/public/common/content_client.h" |
14 #include "content/public/common/url_constants.h" | |
15 #include "net/base/url_util.h" | 11 #include "net/base/url_util.h" |
16 #include "url/gurl.h" | 12 #include "url/gurl.h" |
17 #include "url/url_constants.h" | |
18 | 13 |
19 namespace content { | 14 namespace content { |
20 | 15 |
21 namespace { | 16 namespace { |
22 | 17 |
23 class SchemeAndOriginWhitelist { | 18 class SchemeAndOriginWhitelist { |
24 public: | 19 public: |
25 SchemeAndOriginWhitelist() { Reset(); } | 20 SchemeAndOriginWhitelist() { Reset(); } |
26 ~SchemeAndOriginWhitelist() {} | 21 ~SchemeAndOriginWhitelist() {} |
27 | 22 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 return true; | 83 return true; |
89 } | 84 } |
90 | 85 |
91 return false; | 86 return false; |
92 } | 87 } |
93 | 88 |
94 void ResetSchemesAndOriginsWhitelistForTesting() { | 89 void ResetSchemesAndOriginsWhitelistForTesting() { |
95 g_trustworthy_whitelist.Get().Reset(); | 90 g_trustworthy_whitelist.Get().Reset(); |
96 } | 91 } |
97 | 92 |
98 bool HasSuborigin(const GURL& url) { | |
99 if (!url.is_valid()) | |
100 return false; | |
101 | |
102 if (url.scheme() != kHttpSuboriginScheme && | |
103 url.scheme() != kHttpsSuboriginScheme) { | |
104 return false; | |
105 } | |
106 | |
107 base::StringPiece host_piece = url.host_piece(); | |
108 size_t first_period = host_piece.find('.'); | |
109 | |
110 // If the first period is the first position in the hostname, or there is no | |
111 // period at all, there is no suborigin serialized in the hostname. | |
112 if (first_period == 0 || first_period == base::StringPiece::npos) | |
113 return false; | |
114 | |
115 // If there's nothing after the first dot, then there is no host for the | |
116 // physical origin, which is not a valid suborigin serialization. | |
117 if (first_period == (host_piece.size() - 1)) | |
118 return false; | |
119 | |
120 return true; | |
121 } | |
122 | |
123 std::string SuboriginFromUrl(const GURL& url) { | |
124 if (!HasSuborigin(url)) | |
125 return ""; | |
126 | |
127 std::string host = url.host(); | |
128 size_t suborigin_end = host.find("."); | |
129 return (suborigin_end == std::string::npos) ? "" | |
130 : host.substr(0, suborigin_end); | |
131 } | |
132 | |
133 GURL StripSuboriginFromUrl(const GURL& url) { | |
134 if (!HasSuborigin(url)) | |
135 return url; | |
136 | |
137 GURL::Replacements replacements; | |
138 if (url.scheme() == kHttpSuboriginScheme) { | |
139 replacements.SetSchemeStr(url::kHttpScheme); | |
140 } else { | |
141 DCHECK(url.scheme() == kHttpsSuboriginScheme); | |
142 replacements.SetSchemeStr(url::kHttpsScheme); | |
143 } | |
144 | |
145 std::string host = url.host(); | |
146 size_t suborigin_end = host.find("."); | |
147 std::string new_host( | |
148 (suborigin_end == std::string::npos) | |
149 ? "" | |
150 : host.substr(suborigin_end + 1, | |
151 url.host().length() - suborigin_end - 1)); | |
152 replacements.SetHostStr(new_host); | |
153 | |
154 return url.ReplaceComponents(replacements); | |
155 } | |
156 | |
157 } // namespace content | 93 } // namespace content |
OLD | NEW |