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