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
3 // found in the LICENSE file.
5 #include "url/scheme_host_port.h"
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
12 #include "url/url_canon.h"
13 #include "url/url_canon_stdstring.h"
14 #include "url/url_constants.h"
15 #include "url/url_util.h"
19 SchemeHostPort::SchemeHostPort() : port_(0) {
22 SchemeHostPort::SchemeHostPort(base::StringPiece scheme
,
23 base::StringPiece host
,
25 : scheme_(scheme
.data(), scheme
.length()),
26 host_(host
.data(), host
.length()),
29 DCHECK(url::IsStandard(scheme
.data(),
30 url::Component(0, static_cast<int>(scheme
.length()))));
32 // Try to canonicalize the host (copy/pasted from net/base. :( ).
33 const url::Component
raw_host_component(0, static_cast<int>(host
.length()));
34 std::string canon_host
;
35 url::StdStringCanonOutput
canon_host_output(&canon_host
);
36 url::CanonHostInfo host_info
;
37 url::CanonicalizeHostVerbose(host
.data(), raw_host_component
,
38 &canon_host_output
, &host_info
);
40 if (host_info
.out_host
.is_nonempty() &&
41 host_info
.family
!= url::CanonHostInfo::BROKEN
) {
42 // Success! Assert that there's no extra garbage.
43 canon_host_output
.Complete();
44 DCHECK_EQ(host_info
.out_host
.len
, static_cast<int>(canon_host
.length()));
46 // Empty host, or canonicalization failed.
49 DCHECK_EQ(host
, canon_host
);
51 DCHECK(scheme
== kFileScheme
? port
== 0 : port
!= 0);
52 DCHECK(!host
.empty() || port
== 0);
56 SchemeHostPort::SchemeHostPort(const GURL
& url
) : port_(0) {
57 if (!url
.is_valid() || !url
.IsStandard())
60 // These schemes do not follow the generic URL syntax, so we treat them as
61 // invalid (scheme, host, port) tuples (even though such URLs' _Origin_ might
62 // have a (scheme, host, port) tuple, they themselves do not).
63 if (url
.SchemeIsBlob() || url
.SchemeIsFileSystem())
66 scheme_
= url
.scheme();
68 port_
= url
.EffectiveIntPort() == url::PORT_UNSPECIFIED
70 : url
.EffectiveIntPort();
73 SchemeHostPort::~SchemeHostPort() {
76 bool SchemeHostPort::IsInvalid() const {
77 return scheme_
.empty() && host_
.empty() && !port_
;
80 std::string
SchemeHostPort::Serialize() const {
85 bool is_default_port
=
86 port_
== url::DefaultPortForScheme(scheme_
.data(),
87 static_cast<int>(scheme_
.length()));
89 result
.append(scheme_
);
90 result
.append(kStandardSchemeSeparator
);
93 if (scheme_
!= kFileScheme
&& !is_default_port
) {
94 result
.push_back(':');
95 result
.append(base::IntToString(port_
));
101 bool SchemeHostPort::Equals(const SchemeHostPort
& other
) const {
102 return port_
== other
.port() && scheme_
== other
.scheme() &&
103 host_
== other
.host();
106 bool SchemeHostPort::operator<(const SchemeHostPort
& other
) const {
107 if (port_
!= other
.port_
)
108 return port_
< other
.port_
;
109 if (scheme_
!= other
.scheme_
)
110 return scheme_
< other
.scheme_
;
111 if (host_
!= other
.host_
)
112 return host_
< other
.host_
;