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

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 #if !defined(OS_ANDROID) 20 #if !defined(OS_ANDROID) && !defined(OS_NACL)
21 #include <ifaddrs.h> 21 #include <ifaddrs.h>
22 #endif 22 #endif
23 #if !defined(OS_NACL)
23 #include <net/if.h> 24 #include <net/if.h>
25 #endif
24 #include <netdb.h> 26 #include <netdb.h>
25 #include <netinet/in.h> 27 #include <netinet/in.h>
Ryan Sleevi 2014/03/06 22:02:17 Seems like this should be reworded as #elif defin
Sergey Ulanov 2014/03/21 19:34:41 Done.
26 #endif 28 #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"
57 #if !defined(OS_NACL)
55 #include "grit/net_resources.h" 58 #include "grit/net_resources.h"
59 #endif
Ryan Sleevi 2014/03/06 22:02:17 Likewise, it seems like this, lines 64-66, and 72-
Sergey Ulanov 2014/03/21 19:34:41 Done.
56 #include "url/gurl.h" 60 #include "url/gurl.h"
57 #include "url/url_canon.h" 61 #include "url/url_canon.h"
58 #include "url/url_canon_ip.h" 62 #include "url/url_canon_ip.h"
59 #include "url/url_parse.h" 63 #include "url/url_parse.h"
60 #if defined(OS_ANDROID) 64 #if defined(OS_ANDROID)
61 #include "net/android/network_library.h" 65 #include "net/android/network_library.h"
62 #endif 66 #endif
63 #include "net/base/dns_util.h" 67 #include "net/base/dns_util.h"
64 #include "net/base/escape.h" 68 #include "net/base/escape.h"
65 #include "net/base/mime_util.h" 69 #include "net/base/mime_util.h"
(...skipping 925 matching lines...) Expand 10 before | Expand all | Expand 10 after
991 995
992 size_t GetCountOfExplicitlyAllowedPorts() { 996 size_t GetCountOfExplicitlyAllowedPorts() {
993 return g_explicitly_allowed_ports.Get().size(); 997 return g_explicitly_allowed_ports.Get().size();
994 } 998 }
995 999
996 GURL FilePathToFileURL(const base::FilePath& path) { 1000 GURL FilePathToFileURL(const base::FilePath& path) {
997 // Produce a URL like "file:///C:/foo" for a regular file, or 1001 // 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 1002 // "file://///server/path" for UNC. The URL canonicalizer will fix up the
999 // latter case to be the canonical UNC form: "file://server/path" 1003 // latter case to be the canonical UNC form: "file://server/path"
1000 base::FilePath::StringType url_string(kFileURLPrefix); 1004 base::FilePath::StringType url_string(kFileURLPrefix);
1005 #if !defined(OS_NACL)
1001 if (!path.IsAbsolute()) { 1006 if (!path.IsAbsolute()) {
1002 base::FilePath current_dir; 1007 base::FilePath current_dir;
1003 PathService::Get(base::DIR_CURRENT, &current_dir); 1008 PathService::Get(base::DIR_CURRENT, &current_dir);
1004 url_string.append(current_dir.value()); 1009 url_string.append(current_dir.value());
1005 url_string.push_back(base::FilePath::kSeparators[0]); 1010 url_string.push_back(base::FilePath::kSeparators[0]);
1006 } 1011 }
1012 #endif
1007 url_string.append(path.value()); 1013 url_string.append(path.value());
1008 1014
1009 // Now do replacement of some characters. Since we assume the input is a 1015 // Now do replacement of some characters. Since we assume the input is a
1010 // literal filename, anything the URL parser might consider special should 1016 // literal filename, anything the URL parser might consider special should
1011 // be escaped here. 1017 // be escaped here.
1012 1018
1013 // must be the first substitution since others will introduce percents as the 1019 // must be the first substitution since others will introduce percents as the
1014 // escape character 1020 // escape character
1015 ReplaceSubstringsAfterOffset(&url_string, 0, 1021 ReplaceSubstringsAfterOffset(&url_string, 0,
1016 FILE_PATH_LITERAL("%"), FILE_PATH_LITERAL("%25")); 1022 FILE_PATH_LITERAL("%"), FILE_PATH_LITERAL("%25"));
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1085 canon_host_output.Complete(); 1091 canon_host_output.Complete();
1086 DCHECK_EQ(host_info->out_host.len, static_cast<int>(canon_host.length())); 1092 DCHECK_EQ(host_info->out_host.len, static_cast<int>(canon_host.length()));
1087 } else { 1093 } else {
1088 // Empty host, or canonicalization failed. We'll return empty. 1094 // Empty host, or canonicalization failed. We'll return empty.
1089 canon_host.clear(); 1095 canon_host.clear();
1090 } 1096 }
1091 1097
1092 return canon_host; 1098 return canon_host;
1093 } 1099 }
1094 1100
1101 #if !defined(OS_NACL)
1095 std::string GetDirectoryListingHeader(const base::string16& title) { 1102 std::string GetDirectoryListingHeader(const base::string16& title) {
1096 static const base::StringPiece header( 1103 static const base::StringPiece header(
1097 NetModule::GetResource(IDR_DIR_HEADER_HTML)); 1104 NetModule::GetResource(IDR_DIR_HEADER_HTML));
1098 // This can be null in unit tests. 1105 // This can be null in unit tests.
1099 DLOG_IF(WARNING, header.empty()) << 1106 DLOG_IF(WARNING, header.empty()) <<
1100 "Missing resource: directory listing header"; 1107 "Missing resource: directory listing header";
1101 1108
1102 std::string result; 1109 std::string result;
1103 if (!header.empty()) 1110 if (!header.empty())
1104 result.assign(header.data(), header.size()); 1111 result.assign(header.data(), header.size());
1105 1112
1106 result.append("<script>start("); 1113 result.append("<script>start(");
1107 base::EscapeJSONString(title, true, &result); 1114 base::EscapeJSONString(title, true, &result);
1108 result.append(");</script>\n"); 1115 result.append(");</script>\n");
1109 1116
1110 return result; 1117 return result;
1111 } 1118 }
1119 #endif
1112 1120
1113 inline bool IsHostCharAlphanumeric(char c) { 1121 inline bool IsHostCharAlphanumeric(char c) {
1114 // We can just check lowercase because uppercase characters have already been 1122 // We can just check lowercase because uppercase characters have already been
1115 // normalized. 1123 // normalized.
1116 return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')); 1124 return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9'));
1117 } 1125 }
1118 1126
1119 bool IsCanonicalizedHostCompliant(const std::string& host, 1127 bool IsCanonicalizedHostCompliant(const std::string& host,
1120 const std::string& desired_tld) { 1128 const std::string& desired_tld) {
1121 if (host.empty()) 1129 if (host.empty())
(...skipping 22 matching lines...) Expand all
1144 } else { 1152 } else {
1145 return false; 1153 return false;
1146 } 1154 }
1147 } 1155 }
1148 } 1156 }
1149 1157
1150 return most_recent_component_started_alphanumeric || 1158 return most_recent_component_started_alphanumeric ||
1151 (!desired_tld.empty() && IsHostCharAlphanumeric(desired_tld[0])); 1159 (!desired_tld.empty() && IsHostCharAlphanumeric(desired_tld[0]));
1152 } 1160 }
1153 1161
1162 #if !defined(OS_NACL)
1154 std::string GetDirectoryListingEntry(const base::string16& name, 1163 std::string GetDirectoryListingEntry(const base::string16& name,
1155 const std::string& raw_bytes, 1164 const std::string& raw_bytes,
1156 bool is_dir, 1165 bool is_dir,
1157 int64 size, 1166 int64 size,
1158 Time modified) { 1167 Time modified) {
1159 std::string result; 1168 std::string result;
1160 result.append("<script>addRow("); 1169 result.append("<script>addRow(");
1161 base::EscapeJSONString(name, true, &result); 1170 base::EscapeJSONString(name, true, &result);
1162 result.append(","); 1171 result.append(",");
1163 if (raw_bytes.empty()) { 1172 if (raw_bytes.empty()) {
(...skipping 19 matching lines...) Expand all
1183 // |modified| can be NULL in FTP listings. 1192 // |modified| can be NULL in FTP listings.
1184 if (!modified.is_null()) { 1193 if (!modified.is_null()) {
1185 modified_str = base::TimeFormatShortDateAndTime(modified); 1194 modified_str = base::TimeFormatShortDateAndTime(modified);
1186 } 1195 }
1187 base::EscapeJSONString(modified_str, true, &result); 1196 base::EscapeJSONString(modified_str, true, &result);
1188 1197
1189 result.append(");</script>\n"); 1198 result.append(");</script>\n");
1190 1199
1191 return result; 1200 return result;
1192 } 1201 }
1202 #endif // !defined(OS_NACL)
1193 1203
1194 base::string16 StripWWW(const base::string16& text) { 1204 base::string16 StripWWW(const base::string16& text) {
1195 const base::string16 www(base::ASCIIToUTF16("www.")); 1205 const base::string16 www(base::ASCIIToUTF16("www."));
1196 return StartsWith(text, www, true) ? text.substr(www.length()) : text; 1206 return StartsWith(text, www, true) ? text.substr(www.length()) : text;
1197 } 1207 }
1198 1208
1199 base::string16 StripWWWFromHost(const GURL& url) { 1209 base::string16 StripWWWFromHost(const GURL& url) {
1200 DCHECK(url.is_valid()); 1210 DCHECK(url.is_valid());
1201 return StripWWW(base::ASCIIToUTF16(url.host())); 1211 return StripWWW(base::ASCIIToUTF16(url.host()));
1202 } 1212 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1247 *file_path = file_path->DirName(); 1257 *file_path = file_path->DirName();
1248 if (file_path->value() == base::FilePath::kCurrentDirectory) { 1258 if (file_path->value() == base::FilePath::kCurrentDirectory) {
1249 *file_path = base::FilePath(leaf_name); 1259 *file_path = base::FilePath(leaf_name);
1250 } else { 1260 } else {
1251 *file_path = file_path->Append(leaf_name); 1261 *file_path = file_path->Append(leaf_name);
1252 } 1262 }
1253 } 1263 }
1254 #endif 1264 #endif
1255 } 1265 }
1256 1266
1267 #if !defined(OS_NACL)
1257 base::string16 GetSuggestedFilename(const GURL& url, 1268 base::string16 GetSuggestedFilename(const GURL& url,
1258 const std::string& content_disposition, 1269 const std::string& content_disposition,
1259 const std::string& referrer_charset, 1270 const std::string& referrer_charset,
1260 const std::string& suggested_name, 1271 const std::string& suggested_name,
1261 const std::string& mime_type, 1272 const std::string& mime_type,
1262 const std::string& default_name) { 1273 const std::string& default_name) {
1263 // TODO: this function to be updated to match the httpbis recommendations. 1274 // TODO: this function to be updated to match the httpbis recommendations.
1264 // Talk to abarth for the latest news. 1275 // Talk to abarth for the latest news.
1265 1276
1266 // We don't translate this fallback string, "download". If localization is 1277 // 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 1366 // normalized names. Otherwise, we won't be able to handle files with NFD
1356 // utf8 encoded characters in name. 1367 // utf8 encoded characters in name.
1357 file_util::NormalizeFileNameEncoding(&generated_name); 1368 file_util::NormalizeFileNameEncoding(&generated_name);
1358 #endif 1369 #endif
1359 1370
1360 DCHECK(!generated_name.empty()); 1371 DCHECK(!generated_name.empty());
1361 1372
1362 return generated_name; 1373 return generated_name;
1363 } 1374 }
1364 1375
1376 #endif // !defined(OS_NACL)
1377
1365 bool IsPortAllowedByDefault(int port) { 1378 bool IsPortAllowedByDefault(int port) {
1366 int array_size = arraysize(kRestrictedPorts); 1379 int array_size = arraysize(kRestrictedPorts);
1367 for (int i = 0; i < array_size; i++) { 1380 for (int i = 0; i < array_size; i++) {
1368 if (kRestrictedPorts[i] == port) { 1381 if (kRestrictedPorts[i] == port) {
1369 return false; 1382 return false;
1370 } 1383 }
1371 } 1384 }
1372 return true; 1385 return true;
1373 } 1386 }
1374 1387
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
1663 uint16 port) { 1676 uint16 port) {
1664 return IPAddressToStringWithPort(&addr.front(), addr.size(), port); 1677 return IPAddressToStringWithPort(&addr.front(), addr.size(), port);
1665 } 1678 }
1666 1679
1667 std::string IPAddressToPackedString(const IPAddressNumber& addr) { 1680 std::string IPAddressToPackedString(const IPAddressNumber& addr) {
1668 return std::string(reinterpret_cast<const char *>(&addr.front()), 1681 return std::string(reinterpret_cast<const char *>(&addr.front()),
1669 addr.size()); 1682 addr.size());
1670 } 1683 }
1671 1684
1672 std::string GetHostName() { 1685 std::string GetHostName() {
1686 #if defined(OS_NACL)
1687 NOTIMPLEMENTED();
1688 return std::string();
1689 #else // defined(OS_NACL)
1673 #if defined(OS_WIN) 1690 #if defined(OS_WIN)
1674 EnsureWinsockInit(); 1691 EnsureWinsockInit();
1675 #endif 1692 #endif
1676 1693
1677 // Host names are limited to 255 bytes. 1694 // Host names are limited to 255 bytes.
1678 char buffer[256]; 1695 char buffer[256];
1679 int result = gethostname(buffer, sizeof(buffer)); 1696 int result = gethostname(buffer, sizeof(buffer));
1680 if (result != 0) { 1697 if (result != 0) {
1681 DVLOG(1) << "gethostname() failed with " << result; 1698 DVLOG(1) << "gethostname() failed with " << result;
1682 buffer[0] = '\0'; 1699 buffer[0] = '\0';
1683 } 1700 }
1684 return std::string(buffer); 1701 return std::string(buffer);
1702 #endif // !defined(OS_NACL)
1685 } 1703 }
1686 1704
1687 void GetIdentityFromURL(const GURL& url, 1705 void GetIdentityFromURL(const GURL& url,
1688 base::string16* username, 1706 base::string16* username,
1689 base::string16* password) { 1707 base::string16* password) {
1690 UnescapeRule::Type flags = 1708 UnescapeRule::Type flags =
1691 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS; 1709 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS;
1692 *username = UnescapeAndDecodeUTF8URLComponent(url.username(), flags, NULL); 1710 *username = UnescapeAndDecodeUTF8URLComponent(url.username(), flags, NULL);
1693 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags, NULL); 1711 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags, NULL);
1694 } 1712 }
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
1940 g_explicitly_allowed_ports.Get().find(port_); 1958 g_explicitly_allowed_ports.Get().find(port_);
1941 if (it != g_explicitly_allowed_ports.Get().end()) 1959 if (it != g_explicitly_allowed_ports.Get().end())
1942 g_explicitly_allowed_ports.Get().erase(it); 1960 g_explicitly_allowed_ports.Get().erase(it);
1943 else 1961 else
1944 NOTREACHED(); 1962 NOTREACHED();
1945 } 1963 }
1946 1964
1947 bool HaveOnlyLoopbackAddresses() { 1965 bool HaveOnlyLoopbackAddresses() {
1948 #if defined(OS_ANDROID) 1966 #if defined(OS_ANDROID)
1949 return android::HaveOnlyLoopbackAddresses(); 1967 return android::HaveOnlyLoopbackAddresses();
1968 #elif defined(OS_NACL)
1969 NOTIMPLEMENTED();
1970 return false;
1950 #elif defined(OS_POSIX) 1971 #elif defined(OS_POSIX)
1951 struct ifaddrs* interface_addr = NULL; 1972 struct ifaddrs* interface_addr = NULL;
1952 int rv = getifaddrs(&interface_addr); 1973 int rv = getifaddrs(&interface_addr);
1953 if (rv != 0) { 1974 if (rv != 0) {
1954 DVLOG(1) << "getifaddrs() failed with errno = " << errno; 1975 DVLOG(1) << "getifaddrs() failed with errno = " << errno;
1955 return false; 1976 return false;
1956 } 1977 }
1957 1978
1958 bool result = true; 1979 bool result = true;
1959 for (struct ifaddrs* interface = interface_addr; 1980 for (struct ifaddrs* interface = interface_addr;
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
2222 } 2243 }
2223 return a1.size() * CHAR_BIT; 2244 return a1.size() * CHAR_BIT;
2224 } 2245 }
2225 2246
2226 unsigned MaskPrefixLength(const IPAddressNumber& mask) { 2247 unsigned MaskPrefixLength(const IPAddressNumber& mask) {
2227 IPAddressNumber all_ones(mask.size(), 0xFF); 2248 IPAddressNumber all_ones(mask.size(), 0xFF);
2228 return CommonPrefixLength(mask, all_ones); 2249 return CommonPrefixLength(mask, all_ones);
2229 } 2250 }
2230 2251
2231 } // namespace net 2252 } // namespace net
OLDNEW
« no previous file with comments | « net/base/iovec.h ('k') | net/base/net_util_posix.cc » ('j') | net/cert/crl_set.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698