Chromium Code Reviews| 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 | |
| 7 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 8 #include "base/macros.h" | 10 #include "base/macros.h" |
| 9 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/strings/string_piece.h" | |
| 10 #include "content/public/common/content_client.h" | 13 #include "content/public/common/content_client.h" |
| 14 #include "content/public/common/url_constants.h" | |
| 11 #include "net/base/url_util.h" | 15 #include "net/base/url_util.h" |
| 12 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 17 #include "url/url_constants.h" | |
| 13 | 18 |
| 14 namespace content { | 19 namespace content { |
| 15 | 20 |
| 16 namespace { | 21 namespace { |
| 17 | 22 |
| 18 class SchemeAndOriginWhitelist { | 23 class SchemeAndOriginWhitelist { |
| 19 public: | 24 public: |
| 20 SchemeAndOriginWhitelist() { Reset(); } | 25 SchemeAndOriginWhitelist() { Reset(); } |
| 21 ~SchemeAndOriginWhitelist() {} | 26 ~SchemeAndOriginWhitelist() {} |
| 22 | 27 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 return true; | 88 return true; |
| 84 } | 89 } |
| 85 | 90 |
| 86 return false; | 91 return false; |
| 87 } | 92 } |
| 88 | 93 |
| 89 void ResetSchemesAndOriginsWhitelistForTesting() { | 94 void ResetSchemesAndOriginsWhitelistForTesting() { |
| 90 g_trustworthy_whitelist.Get().Reset(); | 95 g_trustworthy_whitelist.Get().Reset(); |
| 91 } | 96 } |
| 92 | 97 |
| 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 == std::string::npos) | |
|
nasko
2016/09/30 17:40:07
I think std::string::npos and base::StringPiece::n
jww
2016/09/30 17:52:17
Ah, didn't even know there was a StringPiece speci
| |
| 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 | |
| 93 } // namespace content | 157 } // namespace content |
| OLD | NEW |