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

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

Issue 718273002: Use uint16 for port numbers, net/ edition (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Self review Created 6 years, 1 month 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
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/host_port_pair.h" 5 #include "net/base/host_port_pair.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_split.h" 9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "net/base/ip_endpoint.h" 12 #include "net/base/ip_endpoint.h"
13 #include "url/gurl.h" 13 #include "url/gurl.h"
14 14
15 namespace net { 15 namespace net {
16 16
17 HostPortPair::HostPortPair() : port_(0) {} 17 HostPortPair::HostPortPair() : port_(0) {}
18 HostPortPair::HostPortPair(const std::string& in_host, uint16 in_port) 18 HostPortPair::HostPortPair(const std::string& in_host, uint16 in_port)
19 : host_(in_host), port_(in_port) {} 19 : host_(in_host), port_(in_port) {}
20 20
21 // static 21 // static
22 HostPortPair HostPortPair::FromURL(const GURL& url) { 22 HostPortPair HostPortPair::FromURL(const GURL& url) {
23 return HostPortPair(url.HostNoBrackets(), url.EffectiveIntPort()); 23 return HostPortPair(url.HostNoBrackets(),
24 static_cast<uint16>(url.EffectiveIntPort()));
24 } 25 }
25 26
26 // static 27 // static
27 HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) { 28 HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) {
28 return HostPortPair(ipe.ToStringWithoutPort(), ipe.port()); 29 return HostPortPair(ipe.ToStringWithoutPort(), ipe.port());
29 } 30 }
30 31
31 HostPortPair HostPortPair::FromString(const std::string& str) { 32 HostPortPair HostPortPair::FromString(const std::string& str) {
32 std::vector<std::string> key_port; 33 std::vector<std::string> key_port;
33 base::SplitString(str, ':', &key_port); 34 base::SplitString(str, ':', &key_port);
34 if (key_port.size() != 2) 35 if (key_port.size() != 2)
35 return HostPortPair(); 36 return HostPortPair();
36 int port; 37 int port;
37 if (!base::StringToInt(key_port[1], &port)) 38 if (!base::StringToInt(key_port[1], &port))
38 return HostPortPair(); 39 return HostPortPair();
39 DCHECK_LT(port, 1 << 16); 40 DCHECK_LT(port, 1 << 16);
40 HostPortPair host_port_pair; 41 HostPortPair host_port_pair;
41 host_port_pair.set_host(key_port[0]); 42 host_port_pair.set_host(key_port[0]);
42 host_port_pair.set_port(port); 43 host_port_pair.set_port(static_cast<uint16>(port));
43 return host_port_pair; 44 return host_port_pair;
44 } 45 }
45 46
46 std::string HostPortPair::ToString() const { 47 std::string HostPortPair::ToString() const {
47 return base::StringPrintf("%s:%u", HostForURL().c_str(), port_); 48 return base::StringPrintf("%s:%u", HostForURL().c_str(), port_);
48 } 49 }
49 50
50 std::string HostPortPair::HostForURL() const { 51 std::string HostPortPair::HostForURL() const {
51 // TODO(rtenneti): Add support for |host| to have '\0'. 52 // TODO(rtenneti): Add support for |host| to have '\0'.
52 if (host_.find('\0') != std::string::npos) { 53 if (host_.find('\0') != std::string::npos) {
53 std::string host_for_log(host_); 54 std::string host_for_log(host_);
54 size_t nullpos; 55 size_t nullpos;
55 while ((nullpos = host_for_log.find('\0')) != std::string::npos) { 56 while ((nullpos = host_for_log.find('\0')) != std::string::npos) {
56 host_for_log.replace(nullpos, 1, "%00"); 57 host_for_log.replace(nullpos, 1, "%00");
57 } 58 }
58 LOG(DFATAL) << "Host has a null char: " << host_for_log; 59 LOG(DFATAL) << "Host has a null char: " << host_for_log;
59 } 60 }
60 // Check to see if the host is an IPv6 address. If so, added brackets. 61 // Check to see if the host is an IPv6 address. If so, added brackets.
61 if (host_.find(':') != std::string::npos) { 62 if (host_.find(':') != std::string::npos) {
62 DCHECK_NE(host_[0], '['); 63 DCHECK_NE(host_[0], '[');
63 return base::StringPrintf("[%s]", host_.c_str()); 64 return base::StringPrintf("[%s]", host_.c_str());
64 } 65 }
65 66
66 return host_; 67 return host_;
67 } 68 }
68 69
69 } // namespace net 70 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698