host_port_pair.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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
  3. // found in the LICENSE file.
  4. #include "net/base/host_port_pair.h"
  5. #include "base/logging.h"
  6. #include "base/numerics/safe_conversions.h"
  7. #include "base/strings/string_number_conversions.h"
  8. #include "base/strings/string_piece.h"
  9. #include "base/strings/string_split.h"
  10. #include "base/strings/string_util.h"
  11. #include "base/strings/stringprintf.h"
  12. #include "base/trace_event/memory_usage_estimator.h"
  13. #include "net/base/ip_endpoint.h"
  14. #include "net/base/url_util.h"
  15. #include "url/gurl.h"
  16. #include "url/scheme_host_port.h"
  17. namespace net {
  18. HostPortPair::HostPortPair() : port_(0) {}
  19. HostPortPair::HostPortPair(base::StringPiece in_host, uint16_t in_port)
  20. : host_(in_host), port_(in_port) {}
  21. // static
  22. HostPortPair HostPortPair::FromURL(const GURL& url) {
  23. return HostPortPair(url.HostNoBrackets(),
  24. static_cast<uint16_t>(url.EffectiveIntPort()));
  25. }
  26. // static
  27. HostPortPair HostPortPair::FromSchemeHostPort(
  28. const url::SchemeHostPort& scheme_host_port) {
  29. DCHECK(scheme_host_port.IsValid());
  30. // HostPortPair assumes hostnames do not have surrounding brackets (as is
  31. // commonly used for IPv6 literals), so strip them if present.
  32. base::StringPiece host = scheme_host_port.host();
  33. if (host.size() >= 2 && host.front() == '[' && host.back() == ']') {
  34. host = host.substr(1, host.size() - 2);
  35. }
  36. return HostPortPair(host, scheme_host_port.port());
  37. }
  38. // static
  39. HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) {
  40. return HostPortPair(ipe.ToStringWithoutPort(), ipe.port());
  41. }
  42. // static
  43. HostPortPair HostPortPair::FromString(base::StringPiece str) {
  44. // Input with more than one ':' is ambiguous unless it contains an IPv6
  45. // literal (signified by starting with a '['). ParseHostAndPort() allows such
  46. // input and always uses the last ':' as the host/port delimiter, but because
  47. // HostPortPair often deals with IPv6 literals without brackets, disallow such
  48. // input here to prevent a common error.
  49. if (base::SplitStringPiece(str, ":", base::KEEP_WHITESPACE,
  50. base::SPLIT_WANT_ALL)
  51. .size() > 2 &&
  52. str.front() != '[') {
  53. return HostPortPair();
  54. }
  55. std::string host;
  56. int port;
  57. if (!ParseHostAndPort(str, &host, &port))
  58. return HostPortPair();
  59. // Require a valid port.
  60. if (port == -1)
  61. return HostPortPair();
  62. DCHECK(base::IsValueInRangeForNumericType<uint16_t>(port));
  63. return HostPortPair(host, port);
  64. }
  65. std::string HostPortPair::ToString() const {
  66. std::string ret(HostForURL());
  67. ret += ':';
  68. ret += base::NumberToString(port_);
  69. return ret;
  70. }
  71. std::string HostPortPair::HostForURL() const {
  72. // TODO(rtenneti): Add support for |host| to have '\0'.
  73. if (host_.find('\0') != std::string::npos) {
  74. std::string host_for_log(host_);
  75. size_t nullpos;
  76. while ((nullpos = host_for_log.find('\0')) != std::string::npos) {
  77. host_for_log.replace(nullpos, 1, "%00");
  78. }
  79. LOG(DFATAL) << "Host has a null char: " << host_for_log;
  80. }
  81. // Check to see if the host is an IPv6 address. If so, added brackets.
  82. if (host_.find(':') != std::string::npos) {
  83. DCHECK_NE(host_[0], '[');
  84. return base::StringPrintf("[%s]", host_.c_str());
  85. }
  86. return host_;
  87. }
  88. } // namespace net