1 // Copyright (c) 2013 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.
4 
5 #include "net/quic/platform/impl/quic_hostname_utils_impl.h"
6 
7 #include "base/strings/abseil_string_conversions.h"
8 #include "net/base/url_util.h"
9 #include "url/gurl.h"
10 #include "url/url_canon.h"
11 
12 namespace quic {
13 
14 // static
IsValidSNI(absl::string_view sni)15 bool QuicHostnameUtilsImpl::IsValidSNI(absl::string_view sni) {
16   // TODO(rtenneti): Support RFC2396 hostname.
17   // NOTE: Microsoft does NOT enforce this spec, so if we throw away hostnames
18   // based on the above spec, we may be losing some hostnames that windows
19   // would consider valid. By far the most common hostname character NOT
20   // accepted by the above spec is '_'.
21   url::CanonHostInfo host_info;
22   std::string canonicalized_host(
23       net::CanonicalizeHost(base::StringViewToStringPiece(sni), &host_info));
24   return !host_info.IsIPAddress() &&
25          net::IsCanonicalizedHostCompliant(canonicalized_host) &&
26          sni.find_last_of('.') != std::string::npos;
27 }
28 
29 // static
NormalizeHostname(absl::string_view hostname)30 std::string QuicHostnameUtilsImpl::NormalizeHostname(
31     absl::string_view hostname) {
32   url::CanonHostInfo host_info;
33   std::string host(net::CanonicalizeHost(
34       base::StringPiece(hostname.data(), hostname.size()), &host_info));
35 
36   // Walk backwards over the string, stopping at the first trailing dot.
37   size_t host_end = host.length();
38   while (host_end != 0 && host[host_end - 1] == '.') {
39     host_end--;
40   }
41 
42   // Erase the trailing dots.
43   if (host_end != host.length()) {
44     host.erase(host_end, host.length() - host_end);
45   }
46 
47   return host;
48 }
49 
50 }  // namespace quic
51