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

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

Issue 188873004: Compile src/net for PNaCl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months 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 | Annotate | Revision Log
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/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 #include <map> 9 #include <map>
10 10
11 #include "build/build_config.h" 11 #include "build/build_config.h"
12 12
13 #if defined(OS_WIN) 13 #if defined(OS_WIN)
14 #include <windows.h> 14 #include <windows.h>
15 #include <iphlpapi.h> 15 #include <iphlpapi.h>
16 #include <winsock2.h> 16 #include <winsock2.h>
17 #pragma comment(lib, "iphlpapi.lib") 17 #pragma comment(lib, "iphlpapi.lib")
18 #elif defined(OS_POSIX) 18 #elif defined(OS_POSIX)
19 #include <fcntl.h> 19 #include <fcntl.h>
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #if !defined(OS_NACL)
23 #include <net/if.h>
20 #if !defined(OS_ANDROID) 24 #if !defined(OS_ANDROID)
21 #include <ifaddrs.h> 25 #include <ifaddrs.h>
22 #endif 26 #endif // !defined(OS_NACL)
23 #include <net/if.h> 27 #endif // !defined(OS_ANDROID)
24 #include <netdb.h> 28 #endif // defined(OS_POSIX)
25 #include <netinet/in.h>
26 #endif
27 29
28 #include "base/basictypes.h" 30 #include "base/basictypes.h"
29 #include "base/file_util.h" 31 #include "base/file_util.h"
30 #include "base/files/file_path.h" 32 #include "base/files/file_path.h"
31 #include "base/i18n/file_util_icu.h" 33 #include "base/i18n/file_util_icu.h"
32 #include "base/i18n/icu_string_conversions.h" 34 #include "base/i18n/icu_string_conversions.h"
33 #include "base/i18n/time_formatting.h" 35 #include "base/i18n/time_formatting.h"
34 #include "base/json/string_escape.h" 36 #include "base/json/string_escape.h"
35 #include "base/lazy_instance.h" 37 #include "base/lazy_instance.h"
36 #include "base/logging.h" 38 #include "base/logging.h"
37 #include "base/memory/singleton.h" 39 #include "base/memory/singleton.h"
38 #include "base/message_loop/message_loop.h" 40 #include "base/message_loop/message_loop.h"
39 #include "base/metrics/histogram.h" 41 #include "base/metrics/histogram.h"
40 #include "base/path_service.h" 42 #include "base/path_service.h"
41 #include "base/stl_util.h" 43 #include "base/stl_util.h"
42 #include "base/strings/string_number_conversions.h" 44 #include "base/strings/string_number_conversions.h"
43 #include "base/strings/string_piece.h" 45 #include "base/strings/string_piece.h"
44 #include "base/strings/string_split.h" 46 #include "base/strings/string_split.h"
45 #include "base/strings/string_tokenizer.h" 47 #include "base/strings/string_tokenizer.h"
46 #include "base/strings/string_util.h" 48 #include "base/strings/string_util.h"
47 #include "base/strings/stringprintf.h" 49 #include "base/strings/stringprintf.h"
48 #include "base/strings/sys_string_conversions.h" 50 #include "base/strings/sys_string_conversions.h"
49 #include "base/strings/utf_offset_string_conversions.h" 51 #include "base/strings/utf_offset_string_conversions.h"
50 #include "base/strings/utf_string_conversions.h" 52 #include "base/strings/utf_string_conversions.h"
51 #include "base/synchronization/lock.h" 53 #include "base/synchronization/lock.h"
52 #include "base/sys_byteorder.h" 54 #include "base/sys_byteorder.h"
53 #include "base/time/time.h" 55 #include "base/time/time.h"
54 #include "base/values.h" 56 #include "base/values.h"
55 #include "grit/net_resources.h"
56 #include "url/gurl.h" 57 #include "url/gurl.h"
57 #include "url/url_canon.h" 58 #include "url/url_canon.h"
58 #include "url/url_canon_ip.h" 59 #include "url/url_canon_ip.h"
59 #include "url/url_parse.h" 60 #include "url/url_parse.h"
60 #if defined(OS_ANDROID)
61 #include "net/android/network_library.h"
62 #endif
63 #include "net/base/dns_util.h" 61 #include "net/base/dns_util.h"
64 #include "net/base/escape.h" 62 #include "net/base/escape.h"
65 #include "net/base/mime_util.h" 63 #include "net/base/mime_util.h"
66 #include "net/base/net_module.h" 64 #include "net/base/net_module.h"
67 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 65 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
68 #if defined(OS_WIN)
69 #include "net/base/winsock_init.h"
70 #endif
71 #include "net/http/http_content_disposition.h" 66 #include "net/http/http_content_disposition.h"
72 #include "third_party/icu/source/common/unicode/uidna.h" 67 #include "third_party/icu/source/common/unicode/uidna.h"
73 #include "third_party/icu/source/common/unicode/uniset.h" 68 #include "third_party/icu/source/common/unicode/uniset.h"
74 #include "third_party/icu/source/common/unicode/uscript.h" 69 #include "third_party/icu/source/common/unicode/uscript.h"
75 #include "third_party/icu/source/common/unicode/uset.h" 70 #include "third_party/icu/source/common/unicode/uset.h"
76 #include "third_party/icu/source/i18n/unicode/datefmt.h" 71 #include "third_party/icu/source/i18n/unicode/datefmt.h"
77 #include "third_party/icu/source/i18n/unicode/regex.h" 72 #include "third_party/icu/source/i18n/unicode/regex.h"
78 #include "third_party/icu/source/i18n/unicode/ulocdata.h" 73 #include "third_party/icu/source/i18n/unicode/ulocdata.h"
79 74
75 #if defined(OS_ANDROID)
76 #include "net/android/network_library.h"
77 #endif
78 #if defined(OS_WIN)
79 #include "net/base/winsock_init.h"
80 #endif
81 #if !defined(OS_NACL)
82 #include "grit/net_resources.h"
83 #endif
84
80 using base::Time; 85 using base::Time;
81 86
82 namespace net { 87 namespace net {
83 88
84 namespace { 89 namespace {
85 90
86 typedef std::vector<size_t> Offsets; 91 typedef std::vector<size_t> Offsets;
87 92
88 // what we prepend to get a file URL 93 // what we prepend to get a file URL
89 static const base::FilePath::CharType kFileURLPrefix[] = 94 static const base::FilePath::CharType kFileURLPrefix[] =
(...skipping 901 matching lines...) Expand 10 before | Expand all | Expand 10 after
991 996
992 size_t GetCountOfExplicitlyAllowedPorts() { 997 size_t GetCountOfExplicitlyAllowedPorts() {
993 return g_explicitly_allowed_ports.Get().size(); 998 return g_explicitly_allowed_ports.Get().size();
994 } 999 }
995 1000
996 GURL FilePathToFileURL(const base::FilePath& path) { 1001 GURL FilePathToFileURL(const base::FilePath& path) {
997 // Produce a URL like "file:///C:/foo" for a regular file, or 1002 // Produce a URL like "file:///C:/foo" for a regular file, or
998 // "file://///server/path" for UNC. The URL canonicalizer will fix up the 1003 // "file://///server/path" for UNC. The URL canonicalizer will fix up the
999 // latter case to be the canonical UNC form: "file://server/path" 1004 // latter case to be the canonical UNC form: "file://server/path"
1000 base::FilePath::StringType url_string(kFileURLPrefix); 1005 base::FilePath::StringType url_string(kFileURLPrefix);
1006 #if !defined(OS_NACL)
Ryan Sleevi 2014/03/27 01:04:41 What part of this is broken under NACL?
Sergey Ulanov 2014/04/01 00:30:14 base_nacl doesn't compile PathService.
1001 if (!path.IsAbsolute()) { 1007 if (!path.IsAbsolute()) {
1002 base::FilePath current_dir; 1008 base::FilePath current_dir;
1003 PathService::Get(base::DIR_CURRENT, &current_dir); 1009 PathService::Get(base::DIR_CURRENT, &current_dir);
1004 url_string.append(current_dir.value()); 1010 url_string.append(current_dir.value());
1005 url_string.push_back(base::FilePath::kSeparators[0]); 1011 url_string.push_back(base::FilePath::kSeparators[0]);
1006 } 1012 }
1013 #endif
1007 url_string.append(path.value()); 1014 url_string.append(path.value());
1008 1015
1009 // Now do replacement of some characters. Since we assume the input is a 1016 // Now do replacement of some characters. Since we assume the input is a
1010 // literal filename, anything the URL parser might consider special should 1017 // literal filename, anything the URL parser might consider special should
1011 // be escaped here. 1018 // be escaped here.
1012 1019
1013 // must be the first substitution since others will introduce percents as the 1020 // must be the first substitution since others will introduce percents as the
1014 // escape character 1021 // escape character
1015 ReplaceSubstringsAfterOffset(&url_string, 0, 1022 ReplaceSubstringsAfterOffset(&url_string, 0,
1016 FILE_PATH_LITERAL("%"), FILE_PATH_LITERAL("%25")); 1023 FILE_PATH_LITERAL("%"), FILE_PATH_LITERAL("%25"));
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1085 canon_host_output.Complete(); 1092 canon_host_output.Complete();
1086 DCHECK_EQ(host_info->out_host.len, static_cast<int>(canon_host.length())); 1093 DCHECK_EQ(host_info->out_host.len, static_cast<int>(canon_host.length()));
1087 } else { 1094 } else {
1088 // Empty host, or canonicalization failed. We'll return empty. 1095 // Empty host, or canonicalization failed. We'll return empty.
1089 canon_host.clear(); 1096 canon_host.clear();
1090 } 1097 }
1091 1098
1092 return canon_host; 1099 return canon_host;
1093 } 1100 }
1094 1101
1102 #if !defined(OS_NACL)
Ryan Sleevi 2014/03/27 01:04:41 Does this really need to be excluded - eg: if you
Sergey Ulanov 2014/04/01 00:30:14 Done.
1095 std::string GetDirectoryListingHeader(const base::string16& title) { 1103 std::string GetDirectoryListingHeader(const base::string16& title) {
1096 static const base::StringPiece header( 1104 static const base::StringPiece header(
1097 NetModule::GetResource(IDR_DIR_HEADER_HTML)); 1105 NetModule::GetResource(IDR_DIR_HEADER_HTML));
1098 // This can be null in unit tests. 1106 // This can be null in unit tests.
1099 DLOG_IF(WARNING, header.empty()) << 1107 DLOG_IF(WARNING, header.empty()) <<
1100 "Missing resource: directory listing header"; 1108 "Missing resource: directory listing header";
1101 1109
1102 std::string result; 1110 std::string result;
1103 if (!header.empty()) 1111 if (!header.empty())
1104 result.assign(header.data(), header.size()); 1112 result.assign(header.data(), header.size());
1105 1113
1106 result.append("<script>start("); 1114 result.append("<script>start(");
1107 base::EscapeJSONString(title, true, &result); 1115 base::EscapeJSONString(title, true, &result);
1108 result.append(");</script>\n"); 1116 result.append(");</script>\n");
1109 1117
1110 return result; 1118 return result;
1111 } 1119 }
1120 #endif
1112 1121
1113 inline bool IsHostCharAlphanumeric(char c) { 1122 inline bool IsHostCharAlphanumeric(char c) {
1114 // We can just check lowercase because uppercase characters have already been 1123 // We can just check lowercase because uppercase characters have already been
1115 // normalized. 1124 // normalized.
1116 return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')); 1125 return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9'));
1117 } 1126 }
1118 1127
1119 bool IsCanonicalizedHostCompliant(const std::string& host, 1128 bool IsCanonicalizedHostCompliant(const std::string& host,
1120 const std::string& desired_tld) { 1129 const std::string& desired_tld) {
1121 if (host.empty()) 1130 if (host.empty())
(...skipping 22 matching lines...) Expand all
1144 } else { 1153 } else {
1145 return false; 1154 return false;
1146 } 1155 }
1147 } 1156 }
1148 } 1157 }
1149 1158
1150 return most_recent_component_started_alphanumeric || 1159 return most_recent_component_started_alphanumeric ||
1151 (!desired_tld.empty() && IsHostCharAlphanumeric(desired_tld[0])); 1160 (!desired_tld.empty() && IsHostCharAlphanumeric(desired_tld[0]));
1152 } 1161 }
1153 1162
1163 #if !defined(OS_NACL)
1154 std::string GetDirectoryListingEntry(const base::string16& name, 1164 std::string GetDirectoryListingEntry(const base::string16& name,
1155 const std::string& raw_bytes, 1165 const std::string& raw_bytes,
1156 bool is_dir, 1166 bool is_dir,
1157 int64 size, 1167 int64 size,
1158 Time modified) { 1168 Time modified) {
1159 std::string result; 1169 std::string result;
1160 result.append("<script>addRow("); 1170 result.append("<script>addRow(");
1161 base::EscapeJSONString(name, true, &result); 1171 base::EscapeJSONString(name, true, &result);
1162 result.append(","); 1172 result.append(",");
1163 if (raw_bytes.empty()) { 1173 if (raw_bytes.empty()) {
(...skipping 19 matching lines...) Expand all
1183 // |modified| can be NULL in FTP listings. 1193 // |modified| can be NULL in FTP listings.
1184 if (!modified.is_null()) { 1194 if (!modified.is_null()) {
1185 modified_str = base::TimeFormatShortDateAndTime(modified); 1195 modified_str = base::TimeFormatShortDateAndTime(modified);
1186 } 1196 }
1187 base::EscapeJSONString(modified_str, true, &result); 1197 base::EscapeJSONString(modified_str, true, &result);
1188 1198
1189 result.append(");</script>\n"); 1199 result.append(");</script>\n");
1190 1200
1191 return result; 1201 return result;
1192 } 1202 }
1203 #endif // !defined(OS_NACL)
1193 1204
1194 base::string16 StripWWW(const base::string16& text) { 1205 base::string16 StripWWW(const base::string16& text) {
1195 const base::string16 www(base::ASCIIToUTF16("www.")); 1206 const base::string16 www(base::ASCIIToUTF16("www."));
1196 return StartsWith(text, www, true) ? text.substr(www.length()) : text; 1207 return StartsWith(text, www, true) ? text.substr(www.length()) : text;
1197 } 1208 }
1198 1209
1199 base::string16 StripWWWFromHost(const GURL& url) { 1210 base::string16 StripWWWFromHost(const GURL& url) {
1200 DCHECK(url.is_valid()); 1211 DCHECK(url.is_valid());
1201 return StripWWW(base::ASCIIToUTF16(url.host())); 1212 return StripWWW(base::ASCIIToUTF16(url.host()));
1202 } 1213 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1247 *file_path = file_path->DirName(); 1258 *file_path = file_path->DirName();
1248 if (file_path->value() == base::FilePath::kCurrentDirectory) { 1259 if (file_path->value() == base::FilePath::kCurrentDirectory) {
1249 *file_path = base::FilePath(leaf_name); 1260 *file_path = base::FilePath(leaf_name);
1250 } else { 1261 } else {
1251 *file_path = file_path->Append(leaf_name); 1262 *file_path = file_path->Append(leaf_name);
1252 } 1263 }
1253 } 1264 }
1254 #endif 1265 #endif
1255 } 1266 }
1256 1267
1268 #if !defined(OS_NACL)
1257 base::string16 GetSuggestedFilename(const GURL& url, 1269 base::string16 GetSuggestedFilename(const GURL& url,
1258 const std::string& content_disposition, 1270 const std::string& content_disposition,
1259 const std::string& referrer_charset, 1271 const std::string& referrer_charset,
1260 const std::string& suggested_name, 1272 const std::string& suggested_name,
1261 const std::string& mime_type, 1273 const std::string& mime_type,
1262 const std::string& default_name) { 1274 const std::string& default_name) {
1263 // TODO: this function to be updated to match the httpbis recommendations. 1275 // TODO: this function to be updated to match the httpbis recommendations.
1264 // Talk to abarth for the latest news. 1276 // Talk to abarth for the latest news.
1265 1277
1266 // We don't translate this fallback string, "download". If localization is 1278 // We don't translate this fallback string, "download". If localization is
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1355 // normalized names. Otherwise, we won't be able to handle files with NFD 1367 // normalized names. Otherwise, we won't be able to handle files with NFD
1356 // utf8 encoded characters in name. 1368 // utf8 encoded characters in name.
1357 file_util::NormalizeFileNameEncoding(&generated_name); 1369 file_util::NormalizeFileNameEncoding(&generated_name);
1358 #endif 1370 #endif
1359 1371
1360 DCHECK(!generated_name.empty()); 1372 DCHECK(!generated_name.empty());
1361 1373
1362 return generated_name; 1374 return generated_name;
1363 } 1375 }
1364 1376
1377 #endif // !defined(OS_NACL)
1378
1365 bool IsPortAllowedByDefault(int port) { 1379 bool IsPortAllowedByDefault(int port) {
1366 int array_size = arraysize(kRestrictedPorts); 1380 int array_size = arraysize(kRestrictedPorts);
1367 for (int i = 0; i < array_size; i++) { 1381 for (int i = 0; i < array_size; i++) {
1368 if (kRestrictedPorts[i] == port) { 1382 if (kRestrictedPorts[i] == port) {
1369 return false; 1383 return false;
1370 } 1384 }
1371 } 1385 }
1372 return true; 1386 return true;
1373 } 1387 }
1374 1388
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
1663 uint16 port) { 1677 uint16 port) {
1664 return IPAddressToStringWithPort(&addr.front(), addr.size(), port); 1678 return IPAddressToStringWithPort(&addr.front(), addr.size(), port);
1665 } 1679 }
1666 1680
1667 std::string IPAddressToPackedString(const IPAddressNumber& addr) { 1681 std::string IPAddressToPackedString(const IPAddressNumber& addr) {
1668 return std::string(reinterpret_cast<const char *>(&addr.front()), 1682 return std::string(reinterpret_cast<const char *>(&addr.front()),
1669 addr.size()); 1683 addr.size());
1670 } 1684 }
1671 1685
1672 std::string GetHostName() { 1686 std::string GetHostName() {
1687 #if defined(OS_NACL)
1688 NOTIMPLEMENTED();
1689 return std::string();
1690 #else // defined(OS_NACL)
1673 #if defined(OS_WIN) 1691 #if defined(OS_WIN)
1674 EnsureWinsockInit(); 1692 EnsureWinsockInit();
1675 #endif 1693 #endif
1676 1694
1677 // Host names are limited to 255 bytes. 1695 // Host names are limited to 255 bytes.
1678 char buffer[256]; 1696 char buffer[256];
1679 int result = gethostname(buffer, sizeof(buffer)); 1697 int result = gethostname(buffer, sizeof(buffer));
1680 if (result != 0) { 1698 if (result != 0) {
1681 DVLOG(1) << "gethostname() failed with " << result; 1699 DVLOG(1) << "gethostname() failed with " << result;
1682 buffer[0] = '\0'; 1700 buffer[0] = '\0';
1683 } 1701 }
1684 return std::string(buffer); 1702 return std::string(buffer);
1703 #endif // !defined(OS_NACL)
1685 } 1704 }
1686 1705
1687 void GetIdentityFromURL(const GURL& url, 1706 void GetIdentityFromURL(const GURL& url,
1688 base::string16* username, 1707 base::string16* username,
1689 base::string16* password) { 1708 base::string16* password) {
1690 UnescapeRule::Type flags = 1709 UnescapeRule::Type flags =
1691 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS; 1710 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS;
1692 *username = UnescapeAndDecodeUTF8URLComponent(url.username(), flags, NULL); 1711 *username = UnescapeAndDecodeUTF8URLComponent(url.username(), flags, NULL);
1693 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags, NULL); 1712 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags, NULL);
1694 } 1713 }
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
1940 g_explicitly_allowed_ports.Get().find(port_); 1959 g_explicitly_allowed_ports.Get().find(port_);
1941 if (it != g_explicitly_allowed_ports.Get().end()) 1960 if (it != g_explicitly_allowed_ports.Get().end())
1942 g_explicitly_allowed_ports.Get().erase(it); 1961 g_explicitly_allowed_ports.Get().erase(it);
1943 else 1962 else
1944 NOTREACHED(); 1963 NOTREACHED();
1945 } 1964 }
1946 1965
1947 bool HaveOnlyLoopbackAddresses() { 1966 bool HaveOnlyLoopbackAddresses() {
1948 #if defined(OS_ANDROID) 1967 #if defined(OS_ANDROID)
1949 return android::HaveOnlyLoopbackAddresses(); 1968 return android::HaveOnlyLoopbackAddresses();
1969 #elif defined(OS_NACL)
1970 NOTIMPLEMENTED();
Ryan Sleevi 2014/03/27 01:04:41 Here, you intentionally NOTIMPLEMENTED(), but in G
Sergey Ulanov 2014/04/01 00:30:14 Added NOTIMPLEMENTED in GetNetworkList() too.
1971 return false;
1950 #elif defined(OS_POSIX) 1972 #elif defined(OS_POSIX)
1951 struct ifaddrs* interface_addr = NULL; 1973 struct ifaddrs* interface_addr = NULL;
1952 int rv = getifaddrs(&interface_addr); 1974 int rv = getifaddrs(&interface_addr);
1953 if (rv != 0) { 1975 if (rv != 0) {
1954 DVLOG(1) << "getifaddrs() failed with errno = " << errno; 1976 DVLOG(1) << "getifaddrs() failed with errno = " << errno;
1955 return false; 1977 return false;
1956 } 1978 }
1957 1979
1958 bool result = true; 1980 bool result = true;
1959 for (struct ifaddrs* interface = interface_addr; 1981 for (struct ifaddrs* interface = interface_addr;
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
2224 } 2246 }
2225 return a1.size() * CHAR_BIT; 2247 return a1.size() * CHAR_BIT;
2226 } 2248 }
2227 2249
2228 unsigned MaskPrefixLength(const IPAddressNumber& mask) { 2250 unsigned MaskPrefixLength(const IPAddressNumber& mask) {
2229 IPAddressNumber all_ones(mask.size(), 0xFF); 2251 IPAddressNumber all_ones(mask.size(), 0xFF);
2230 return CommonPrefixLength(mask, all_ones); 2252 return CommonPrefixLength(mask, all_ones);
2231 } 2253 }
2232 2254
2233 } // namespace net 2255 } // namespace net
OLDNEW
« no previous file with comments | « net/base/iovec.h ('k') | net/base/net_util_posix.cc » ('j') | net/net.gypi » ('J')

Powered by Google App Engine
This is Rietveld 408576698