Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: url/scheme_host_port.cc

Issue 2387143003: Fix SchemeHostPort::GetURL() and add more tests (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « url/origin_unittest.cc ('k') | url/scheme_host_port_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "url/scheme_host_port.h" 5 #include "url/scheme_host_port.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <tuple> 10 #include <tuple>
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 // Null checking for |parsed| in SerializeInternal is probably slower than 146 // Null checking for |parsed| in SerializeInternal is probably slower than
147 // just filling it in and discarding it here. 147 // just filling it in and discarding it here.
148 url::Parsed parsed; 148 url::Parsed parsed;
149 return SerializeInternal(&parsed); 149 return SerializeInternal(&parsed);
150 } 150 }
151 151
152 GURL SchemeHostPort::GetURL() const { 152 GURL SchemeHostPort::GetURL() const {
153 url::Parsed parsed; 153 url::Parsed parsed;
154 std::string serialized = SerializeInternal(&parsed); 154 std::string serialized = SerializeInternal(&parsed);
155 155
156 if (IsInvalid())
157 return GURL(std::move(serialized), parsed, false);
158
156 // If the serialized string is passed to GURL for parsing, it will append an 159 // If the serialized string is passed to GURL for parsing, it will append an
157 // empty path "/". Add that here. Note: per RFC 6454 we cannot do this for 160 // empty path "/". Add that here. Note: per RFC 6454 we cannot do this for
158 // normal Origin serialization. 161 // normal Origin serialization.
159 DCHECK(!parsed.path.is_valid()); 162 DCHECK(!parsed.path.is_valid());
160 parsed.path = Component(serialized.length(), 1); 163 parsed.path = Component(serialized.length(), 1);
161 serialized.append("/"); 164 serialized.append("/");
162 return GURL(std::move(serialized), parsed, true); 165 return GURL(std::move(serialized), parsed, true);
163 } 166 }
164 167
165 bool SchemeHostPort::Equals(const SchemeHostPort& other) const { 168 bool SchemeHostPort::Equals(const SchemeHostPort& other) const {
166 return port_ == other.port() && scheme_ == other.scheme() && 169 return port_ == other.port() && scheme_ == other.scheme() &&
167 host_ == other.host(); 170 host_ == other.host();
168 } 171 }
169 172
170 bool SchemeHostPort::operator<(const SchemeHostPort& other) const { 173 bool SchemeHostPort::operator<(const SchemeHostPort& other) const {
171 return std::tie(port_, scheme_, host_) < 174 return std::tie(port_, scheme_, host_) <
172 std::tie(other.port_, other.scheme_, other.host_); 175 std::tie(other.port_, other.scheme_, other.host_);
173 } 176 }
174 177
175 std::string SchemeHostPort::SerializeInternal(url::Parsed* parsed) const { 178 std::string SchemeHostPort::SerializeInternal(url::Parsed* parsed) const {
176 std::string result; 179 std::string result;
177 if (IsInvalid()) 180 if (IsInvalid())
178 return result; 181 return result;
179 182
180 parsed->scheme = Component(0, scheme_.length()); 183 if (!scheme_.empty()) {
181 result.append(scheme_); 184 parsed->scheme = Component(0, scheme_.length());
185 result.append(scheme_);
186 }
182 187
183 result.append(kStandardSchemeSeparator); 188 result.append(kStandardSchemeSeparator);
184 189
185 parsed->host = Component(result.length(), host_.length()); 190 if (!host_.empty()) {
186 result.append(host_); 191 parsed->host = Component(result.length(), host_.length());
192 result.append(host_);
193 }
187 194
188 if (port_ == 0) 195 if (port_ == 0)
189 return result; 196 return result;
190 197
191 // Omit the port component if the port matches with the default port 198 // Omit the port component if the port matches with the default port
192 // defined for the scheme, if any. 199 // defined for the scheme, if any.
193 int default_port = DefaultPortForScheme(scheme_.data(), 200 int default_port = DefaultPortForScheme(scheme_.data(),
194 static_cast<int>(scheme_.length())); 201 static_cast<int>(scheme_.length()));
195 if (default_port == PORT_UNSPECIFIED) 202 if (default_port == PORT_UNSPECIFIED)
196 return result; 203 return result;
197 if (port_ != default_port) { 204 if (port_ != default_port) {
198 result.push_back(':'); 205 result.push_back(':');
199 std::string port(base::UintToString(port_)); 206 std::string port(base::UintToString(port_));
200 parsed->port = Component(result.length(), port.length()); 207 parsed->port = Component(result.length(), port.length());
201 result.append(std::move(port)); 208 result.append(std::move(port));
202 } 209 }
203 210
204 return result; 211 return result;
205 } 212 }
206 213
207 } // namespace url 214 } // namespace url
OLDNEW
« no previous file with comments | « url/origin_unittest.cc ('k') | url/scheme_host_port_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698