OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "url/origin.h" | 5 #include "url/origin.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | |
7 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "url/url_canon.h" | |
10 #include "url/url_constants.h" | |
11 #include "url/url_util.h" | |
8 | 12 |
9 namespace url { | 13 namespace url { |
10 | 14 |
11 Origin::Origin() : string_("null") {} | 15 Origin::Origin() { |
16 Init(GURL()); | |
17 } | |
12 | 18 |
13 Origin::Origin(const std::string& origin) : string_(origin) { | 19 Origin::Origin(const GURL& url) { |
14 DCHECK(origin == "null" || MatchPattern(origin, "?*://?*")); | 20 Init(url); |
15 DCHECK_GT(origin.size(), 0u); | 21 } |
16 DCHECK(origin == "file://" || origin[origin.size() - 1] != '/'); | 22 |
23 Origin::Origin(const std::string& scheme, | |
24 const std::string& host, | |
25 uint16 port) { | |
26 // Special-case unique origins (because GURL normalizes '://:0' into a | |
27 // valid URL, which is unexpected). Otherwise, pass the data through GURL | |
Ryan Sleevi
2015/05/28 07:46:29
Blah. You mind filing a bug on Chrome for this?
B
Mike West
2015/05/28 13:49:09
Sure. https://crbug.com/493123
| |
28 // for normalization: | |
29 if (!scheme.size() && !host.size() && !port) { | |
30 Init(GURL()); | |
31 } else { | |
32 Init(GURL(scheme + kStandardSchemeSeparator + host + | |
33 (port ? ":" + base::IntToString(port) : ""))); | |
34 } | |
35 } | |
36 | |
37 Origin::Origin(const std::string& origin) { | |
38 // Pass the string through GURL for normalization: | |
39 Init(GURL(origin)); | |
40 } | |
41 | |
42 void Origin::Init(const GURL& url) { | |
43 DCHECK(!url.SchemeIsFileSystem() || url.inner_url()); | |
44 | |
45 // Start with a unique origin, parse from there: | |
46 scheme_.clear(); | |
47 host_.clear(); | |
48 port_ = 0; | |
49 unique_ = true; | |
50 serialization_requires_port_ = false; | |
51 valid_ = false; | |
52 | |
53 url::Replacements<char> replacements; | |
54 replacements.ClearUsername(); | |
55 replacements.ClearPassword(); | |
56 replacements.ClearPath(); | |
57 replacements.ClearQuery(); | |
58 replacements.ClearRef(); | |
59 | |
60 GURL origin = url.SchemeIsFileSystem() | |
61 ? url.inner_url()->ReplaceComponents(replacements) | |
62 : url.ReplaceComponents(replacements); | |
63 | |
64 if (!origin.is_valid()) | |
65 return; | |
66 | |
67 valid_ = true; | |
68 | |
69 if (!origin.IsStandard()) | |
70 return; | |
71 | |
72 unique_ = false; | |
73 scheme_ = origin.scheme(); | |
74 | |
75 if (SchemeIs(kFileScheme)) | |
76 return; | |
77 | |
78 host_ = origin.host(); | |
79 port_ = origin.EffectiveIntPort(); | |
80 serialization_requires_port_ = origin.IntPort() != PORT_UNSPECIFIED; | |
81 } | |
82 | |
83 std::string Origin::serialize() const { | |
84 if (unique_) | |
85 return kUniqueOriginSerialization; | |
86 | |
87 if (host_.empty()) | |
88 return scheme_ + kStandardSchemeSeparator; | |
89 | |
90 return scheme_ + kStandardSchemeSeparator + host_ + | |
91 (serialization_requires_port_ ? ":" + base::IntToString(port_) : ""); | |
92 } | |
93 | |
94 bool Origin::SchemeIs(const char* scheme) const { | |
95 if (!scheme_.size()) | |
96 return scheme == nullptr; | |
97 return url::LowerCaseEqualsASCII(scheme_.data(), | |
98 scheme_.data() + scheme_.size(), scheme); | |
99 } | |
100 | |
101 bool Origin::SchemeIsCryptographic() const { | |
102 return SchemeIs(kHttpsScheme) || SchemeIs(kWssScheme); | |
103 } | |
104 | |
105 bool Origin::IsSameOriginWith(const Origin& other) const { | |
106 return !unique_ && !other.unique_ && scheme_ == other.scheme_ && | |
107 host_ == other.host_ && port_ == other.port_; | |
108 } | |
109 | |
110 std::ostream& operator<<(std::ostream& out, const url::Origin& url) { | |
111 return out << url.serialize(); | |
17 } | 112 } |
18 | 113 |
19 } // namespace url | 114 } // namespace url |
OLD | NEW |