Chromium Code Reviews| Index: content/common/origin_util.cc |
| diff --git a/content/common/origin_util.cc b/content/common/origin_util.cc |
| index 11873d63cfd9de6f8b7b2c4592d785abef089e42..5031ebae7bc147a49e5587639985dc0cadb285e9 100644 |
| --- a/content/common/origin_util.cc |
| +++ b/content/common/origin_util.cc |
| @@ -4,12 +4,17 @@ |
| #include "content/public/common/origin_util.h" |
| +#include <string> |
| + |
| #include "base/lazy_instance.h" |
| #include "base/macros.h" |
| #include "base/stl_util.h" |
| +#include "base/strings/string_piece.h" |
| #include "content/public/common/content_client.h" |
| +#include "content/public/common/url_constants.h" |
| #include "net/base/url_util.h" |
| #include "url/gurl.h" |
| +#include "url/url_constants.h" |
| namespace content { |
| @@ -90,4 +95,63 @@ void ResetSchemesAndOriginsWhitelistForTesting() { |
| g_trustworthy_whitelist.Get().Reset(); |
| } |
| +bool HasSuborigin(const GURL& url) { |
| + if (!url.is_valid()) |
| + return false; |
| + |
| + if (url.scheme() != kHttpSuboriginScheme && |
| + url.scheme() != kHttpsSuboriginScheme) { |
| + return false; |
| + } |
| + |
| + base::StringPiece host_piece = url.host_piece(); |
| + size_t first_period = host_piece.find('.'); |
| + |
| + // If the first period is the first position in the hostname, or there is no |
| + // period at all, there is no suborigin serialized in the hostname. |
| + 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
|
| + return false; |
| + |
| + // If there's nothing after the first dot, then there is no host for the |
| + // physical origin, which is not a valid suborigin serialization. |
| + if (first_period == (host_piece.size() - 1)) |
| + return false; |
| + |
| + return true; |
| +} |
| + |
| +std::string SuboriginFromUrl(const GURL& url) { |
| + if (!HasSuborigin(url)) |
| + return ""; |
| + |
| + std::string host = url.host(); |
| + size_t suborigin_end = host.find("."); |
| + return (suborigin_end == std::string::npos) ? "" |
| + : host.substr(0, suborigin_end); |
| +} |
| + |
| +GURL StripSuboriginFromUrl(const GURL& url) { |
| + if (!HasSuborigin(url)) |
| + return url; |
| + |
| + GURL::Replacements replacements; |
| + if (url.scheme() == kHttpSuboriginScheme) { |
| + replacements.SetSchemeStr(url::kHttpScheme); |
| + } else { |
| + DCHECK(url.scheme() == kHttpsSuboriginScheme); |
| + replacements.SetSchemeStr(url::kHttpsScheme); |
| + } |
| + |
| + std::string host = url.host(); |
| + size_t suborigin_end = host.find("."); |
| + std::string new_host( |
| + (suborigin_end == std::string::npos) |
| + ? "" |
| + : host.substr(suborigin_end + 1, |
| + url.host().length() - suborigin_end - 1)); |
| + replacements.SetHostStr(new_host); |
| + |
| + return url.ReplaceComponents(replacements); |
| +} |
| + |
| } // namespace content |