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

Side by Side Diff: net/base/net_util.cc

Issue 1629733002: net: move IsHostnameNonUnique() into url_util.h (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix ssl_errors Created 4 years, 11 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 | « net/base/net_util.h ('k') | net/base/net_util_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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "net/base/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #if defined(OS_POSIX) 9 #if defined(OS_POSIX)
10 #include <unistd.h> 10 #include <unistd.h>
11 #endif 11 #endif
12 12
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "net/base/address_list.h" 15 #include "net/base/address_list.h"
16 #include "net/base/ip_address_number.h" 16 #include "net/base/ip_address_number.h"
17 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
18 #include "net/base/url_util.h"
19 #include "url/url_canon_ip.h"
20 17
21 #if defined(OS_WIN) 18 #if defined(OS_WIN)
22 #include "net/base/winsock_init.h" 19 #include "net/base/winsock_init.h"
23 #endif 20 #endif
24 21
25 namespace net { 22 namespace net {
26 23
27 namespace { 24 namespace {
28 25
29 std::string NormalizeHostname(base::StringPiece host) { 26 std::string NormalizeHostname(base::StringPiece host) {
(...skipping 13 matching lines...) Expand all
43 IsNormalizedLocalhostTLD(host); 40 IsNormalizedLocalhostTLD(host);
44 } 41 }
45 42
46 // |host| should be normalized. 43 // |host| should be normalized.
47 bool IsLocal6Hostname(const std::string& host) { 44 bool IsLocal6Hostname(const std::string& host) {
48 return host == "localhost6" || host == "localhost6.localdomain6"; 45 return host == "localhost6" || host == "localhost6.localdomain6";
49 } 46 }
50 47
51 } // namespace 48 } // namespace
52 49
53 bool IsHostnameNonUnique(const std::string& hostname) {
54 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address.
55 const std::string host_or_ip = hostname.find(':') != std::string::npos ?
56 "[" + hostname + "]" : hostname;
57 url::CanonHostInfo host_info;
58 std::string canonical_name = CanonicalizeHost(host_or_ip, &host_info);
59
60 // If canonicalization fails, then the input is truly malformed. However,
61 // to avoid mis-reporting bad inputs as "non-unique", treat them as unique.
62 if (canonical_name.empty())
63 return false;
64
65 // If |hostname| is an IP address, check to see if it's in an IANA-reserved
66 // range.
67 if (host_info.IsIPAddress()) {
68 IPAddressNumber host_addr;
69 if (!ParseIPLiteralToNumber(hostname.substr(host_info.out_host.begin,
70 host_info.out_host.len),
71 &host_addr)) {
72 return false;
73 }
74 switch (host_info.family) {
75 case url::CanonHostInfo::IPV4:
76 case url::CanonHostInfo::IPV6:
77 return IsIPAddressReserved(host_addr);
78 case url::CanonHostInfo::NEUTRAL:
79 case url::CanonHostInfo::BROKEN:
80 return false;
81 }
82 }
83
84 // Check for a registry controlled portion of |hostname|, ignoring private
85 // registries, as they already chain to ICANN-administered registries,
86 // and explicitly ignoring unknown registries.
87 //
88 // Note: This means that as new gTLDs are introduced on the Internet, they
89 // will be treated as non-unique until the registry controlled domain list
90 // is updated. However, because gTLDs are expected to provide significant
91 // advance notice to deprecate older versions of this code, this an
92 // acceptable tradeoff.
93 return 0 == registry_controlled_domains::GetRegistryLength(
94 canonical_name,
95 registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
96 registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
97 }
98
99 std::string GetHostName() { 50 std::string GetHostName() {
100 #if defined(OS_NACL) 51 #if defined(OS_NACL)
101 NOTIMPLEMENTED(); 52 NOTIMPLEMENTED();
102 return std::string(); 53 return std::string();
103 #else // defined(OS_NACL) 54 #else // defined(OS_NACL)
104 #if defined(OS_WIN) 55 #if defined(OS_WIN)
105 EnsureWinsockInit(); 56 EnsureWinsockInit();
106 #endif 57 #endif
107 58
108 // Host names are limited to 255 bytes. 59 // Host names are limited to 255 bytes.
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 122
172 default: 123 default:
173 NOTREACHED(); 124 NOTREACHED();
174 } 125 }
175 } 126 }
176 127
177 return false; 128 return false;
178 } 129 }
179 130
180 } // namespace net 131 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util.h ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698