| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <unicode/regex.h> | 9 #include <unicode/regex.h> |
| 10 #include <unicode/ucnv.h> | 10 #include <unicode/ucnv.h> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 #include "base/logging.h" | 43 #include "base/logging.h" |
| 44 #include "base/message_loop.h" | 44 #include "base/message_loop.h" |
| 45 #include "base/path_service.h" | 45 #include "base/path_service.h" |
| 46 #include "base/singleton.h" | 46 #include "base/singleton.h" |
| 47 #include "base/stl_util-inl.h" | 47 #include "base/stl_util-inl.h" |
| 48 #include "base/string_number_conversions.h" | 48 #include "base/string_number_conversions.h" |
| 49 #include "base/string_piece.h" | 49 #include "base/string_piece.h" |
| 50 #include "base/string_split.h" | 50 #include "base/string_split.h" |
| 51 #include "base/string_tokenizer.h" | 51 #include "base/string_tokenizer.h" |
| 52 #include "base/string_util.h" | 52 #include "base/string_util.h" |
| 53 #include "base/stringprintf.h" |
| 53 #include "base/sys_string_conversions.h" | 54 #include "base/sys_string_conversions.h" |
| 54 #include "base/time.h" | 55 #include "base/time.h" |
| 55 #include "base/utf_offset_string_conversions.h" | 56 #include "base/utf_offset_string_conversions.h" |
| 56 #include "base/utf_string_conversions.h" | 57 #include "base/utf_string_conversions.h" |
| 57 #include "grit/net_resources.h" | |
| 58 #include "googleurl/src/gurl.h" | 58 #include "googleurl/src/gurl.h" |
| 59 #include "googleurl/src/url_canon.h" | 59 #include "googleurl/src/url_canon.h" |
| 60 #include "googleurl/src/url_canon_ip.h" | 60 #include "googleurl/src/url_canon_ip.h" |
| 61 #include "googleurl/src/url_parse.h" | 61 #include "googleurl/src/url_parse.h" |
| 62 #include "grit/net_resources.h" |
| 62 #include "net/base/dns_util.h" | 63 #include "net/base/dns_util.h" |
| 63 #include "net/base/escape.h" | 64 #include "net/base/escape.h" |
| 64 #include "net/base/net_module.h" | 65 #include "net/base/net_module.h" |
| 65 #if defined(OS_WIN) | 66 #if defined(OS_WIN) |
| 66 #include "net/base/winsock_init.h" | 67 #include "net/base/winsock_init.h" |
| 67 #endif | 68 #endif |
| 68 #include "unicode/datefmt.h" | 69 #include "unicode/datefmt.h" |
| 69 | 70 |
| 70 | 71 |
| 71 using base::Time; | 72 using base::Time; |
| (...skipping 1430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1502 bool ParseHostAndPort(const std::string& host_and_port, | 1503 bool ParseHostAndPort(const std::string& host_and_port, |
| 1503 std::string* host, | 1504 std::string* host, |
| 1504 int* port) { | 1505 int* port) { |
| 1505 return ParseHostAndPort( | 1506 return ParseHostAndPort( |
| 1506 host_and_port.begin(), host_and_port.end(), host, port); | 1507 host_and_port.begin(), host_and_port.end(), host, port); |
| 1507 } | 1508 } |
| 1508 | 1509 |
| 1509 std::string GetHostAndPort(const GURL& url) { | 1510 std::string GetHostAndPort(const GURL& url) { |
| 1510 // For IPv6 literals, GURL::host() already includes the brackets so it is | 1511 // For IPv6 literals, GURL::host() already includes the brackets so it is |
| 1511 // safe to just append a colon. | 1512 // safe to just append a colon. |
| 1512 return StringPrintf("%s:%d", url.host().c_str(), url.EffectiveIntPort()); | 1513 return base::StringPrintf("%s:%d", url.host().c_str(), |
| 1514 url.EffectiveIntPort()); |
| 1513 } | 1515 } |
| 1514 | 1516 |
| 1515 std::string GetHostAndOptionalPort(const GURL& url) { | 1517 std::string GetHostAndOptionalPort(const GURL& url) { |
| 1516 // For IPv6 literals, GURL::host() already includes the brackets | 1518 // For IPv6 literals, GURL::host() already includes the brackets |
| 1517 // so it is safe to just append a colon. | 1519 // so it is safe to just append a colon. |
| 1518 if (url.has_port()) | 1520 if (url.has_port()) |
| 1519 return StringPrintf("%s:%s", url.host().c_str(), url.port().c_str()); | 1521 return base::StringPrintf("%s:%s", url.host().c_str(), url.port().c_str()); |
| 1520 return url.host(); | 1522 return url.host(); |
| 1521 } | 1523 } |
| 1522 | 1524 |
| 1523 std::string NetAddressToString(const struct addrinfo* net_address) { | 1525 std::string NetAddressToString(const struct addrinfo* net_address) { |
| 1524 #if defined(OS_WIN) | 1526 #if defined(OS_WIN) |
| 1525 EnsureWinsockInit(); | 1527 EnsureWinsockInit(); |
| 1526 #endif | 1528 #endif |
| 1527 | 1529 |
| 1528 // This buffer is large enough to fit the biggest IPv6 string. | 1530 // This buffer is large enough to fit the biggest IPv6 string. |
| 1529 char buffer[INET6_ADDRSTRLEN]; | 1531 char buffer[INET6_ADDRSTRLEN]; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1540 | 1542 |
| 1541 std::string NetAddressToStringWithPort(const struct addrinfo* net_address) { | 1543 std::string NetAddressToStringWithPort(const struct addrinfo* net_address) { |
| 1542 std::string ip_address_string = NetAddressToString(net_address); | 1544 std::string ip_address_string = NetAddressToString(net_address); |
| 1543 if (ip_address_string.empty()) | 1545 if (ip_address_string.empty()) |
| 1544 return std::string(); // Failed. | 1546 return std::string(); // Failed. |
| 1545 | 1547 |
| 1546 int port = GetPortFromAddrinfo(net_address); | 1548 int port = GetPortFromAddrinfo(net_address); |
| 1547 | 1549 |
| 1548 if (ip_address_string.find(':') != std::string::npos) { | 1550 if (ip_address_string.find(':') != std::string::npos) { |
| 1549 // Surround with square brackets to avoid ambiguity. | 1551 // Surround with square brackets to avoid ambiguity. |
| 1550 return StringPrintf("[%s]:%d", ip_address_string.c_str(), port); | 1552 return base::StringPrintf("[%s]:%d", ip_address_string.c_str(), port); |
| 1551 } | 1553 } |
| 1552 | 1554 |
| 1553 return StringPrintf("%s:%d", ip_address_string.c_str(), port); | 1555 return base::StringPrintf("%s:%d", ip_address_string.c_str(), port); |
| 1554 } | 1556 } |
| 1555 | 1557 |
| 1556 std::string GetHostName() { | 1558 std::string GetHostName() { |
| 1557 #if defined(OS_WIN) | 1559 #if defined(OS_WIN) |
| 1558 EnsureWinsockInit(); | 1560 EnsureWinsockInit(); |
| 1559 #endif | 1561 #endif |
| 1560 | 1562 |
| 1561 // Host names are limited to 255 bytes. | 1563 // Host names are limited to 255 bytes. |
| 1562 char buffer[256]; | 1564 char buffer[256]; |
| 1563 int result = gethostname(buffer, sizeof(buffer)); | 1565 int result = gethostname(buffer, sizeof(buffer)); |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1987 } | 1989 } |
| 1988 | 1990 |
| 1989 int GetPortFromAddrinfo(const struct addrinfo* info) { | 1991 int GetPortFromAddrinfo(const struct addrinfo* info) { |
| 1990 uint16* port_field = GetPortFieldFromAddrinfo(info); | 1992 uint16* port_field = GetPortFieldFromAddrinfo(info); |
| 1991 if (!port_field) | 1993 if (!port_field) |
| 1992 return -1; | 1994 return -1; |
| 1993 return ntohs(*port_field); | 1995 return ntohs(*port_field); |
| 1994 } | 1996 } |
| 1995 | 1997 |
| 1996 } // namespace net | 1998 } // namespace net |
| OLD | NEW |