OLD | NEW |
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 <errno.h> | |
8 | |
9 #include <algorithm> | 7 #include <algorithm> |
10 #include <iterator> | 8 #include <iterator> |
11 #include <map> | 9 #include <map> |
12 #include <set> | |
13 | 10 |
14 #include "build/build_config.h" | 11 #include "build/build_config.h" |
15 | 12 |
16 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
17 #include <windows.h> | 14 #include <windows.h> |
18 #include <iphlpapi.h> | 15 #include <iphlpapi.h> |
19 #include <winsock2.h> | 16 #include <winsock2.h> |
20 #pragma comment(lib, "iphlpapi.lib") | 17 #pragma comment(lib, "iphlpapi.lib") |
21 #elif defined(OS_POSIX) | 18 #elif defined(OS_POSIX) |
22 #include <fcntl.h> | 19 #include <fcntl.h> |
23 #if !defined(OS_ANDROID) | 20 #if !defined(OS_ANDROID) |
24 #include <ifaddrs.h> | 21 #include <ifaddrs.h> |
25 #endif | 22 #endif |
26 #include <net/if.h> | 23 #include <net/if.h> |
27 #include <netdb.h> | 24 #include <netdb.h> |
28 #include <netinet/in.h> | 25 #include <netinet/in.h> |
29 #endif | 26 #endif |
30 | 27 |
31 #include "base/basictypes.h" | 28 #include "base/basictypes.h" |
| 29 #include "base/file_util.h" |
| 30 #include "base/files/file_path.h" |
| 31 #include "base/i18n/file_util_icu.h" |
| 32 #include "base/i18n/icu_string_conversions.h" |
32 #include "base/i18n/time_formatting.h" | 33 #include "base/i18n/time_formatting.h" |
33 #include "base/json/string_escape.h" | 34 #include "base/json/string_escape.h" |
34 #include "base/lazy_instance.h" | 35 #include "base/lazy_instance.h" |
35 #include "base/logging.h" | 36 #include "base/logging.h" |
36 #include "base/memory/singleton.h" | 37 #include "base/memory/singleton.h" |
37 #include "base/message_loop/message_loop.h" | 38 #include "base/message_loop/message_loop.h" |
38 #include "base/metrics/histogram.h" | 39 #include "base/metrics/histogram.h" |
| 40 #include "base/path_service.h" |
39 #include "base/stl_util.h" | 41 #include "base/stl_util.h" |
40 #include "base/strings/string_number_conversions.h" | 42 #include "base/strings/string_number_conversions.h" |
41 #include "base/strings/string_piece.h" | 43 #include "base/strings/string_piece.h" |
42 #include "base/strings/string_split.h" | 44 #include "base/strings/string_split.h" |
43 #include "base/strings/string_tokenizer.h" | 45 #include "base/strings/string_tokenizer.h" |
44 #include "base/strings/string_util.h" | 46 #include "base/strings/string_util.h" |
45 #include "base/strings/stringprintf.h" | 47 #include "base/strings/stringprintf.h" |
46 #include "base/strings/sys_string_conversions.h" | 48 #include "base/strings/sys_string_conversions.h" |
47 #include "base/strings/utf_offset_string_conversions.h" | 49 #include "base/strings/utf_offset_string_conversions.h" |
48 #include "base/strings/utf_string_conversions.h" | 50 #include "base/strings/utf_string_conversions.h" |
49 #include "base/synchronization/lock.h" | 51 #include "base/synchronization/lock.h" |
50 #include "base/sys_byteorder.h" | 52 #include "base/sys_byteorder.h" |
51 #include "base/time/time.h" | 53 #include "base/time/time.h" |
52 #include "base/values.h" | 54 #include "base/values.h" |
53 #include "grit/net_resources.h" | 55 #include "grit/net_resources.h" |
54 #include "url/gurl.h" | 56 #include "url/gurl.h" |
55 #include "url/url_canon.h" | 57 #include "url/url_canon.h" |
56 #include "url/url_canon_ip.h" | 58 #include "url/url_canon_ip.h" |
57 #include "url/url_parse.h" | 59 #include "url/url_parse.h" |
58 #if defined(OS_ANDROID) | 60 #if defined(OS_ANDROID) |
59 #include "net/android/network_library.h" | 61 #include "net/android/network_library.h" |
60 #endif | 62 #endif |
61 #include "net/base/dns_util.h" | 63 #include "net/base/dns_util.h" |
62 #include "net/base/escape.h" | 64 #include "net/base/escape.h" |
| 65 #include "net/base/mime_util.h" |
63 #include "net/base/net_module.h" | 66 #include "net/base/net_module.h" |
64 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 67 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
65 #if defined(OS_WIN) | 68 #if defined(OS_WIN) |
66 #include "net/base/winsock_init.h" | 69 #include "net/base/winsock_init.h" |
67 #endif | 70 #endif |
68 #include "net/http/http_content_disposition.h" | 71 #include "net/http/http_content_disposition.h" |
69 #include "third_party/icu/source/common/unicode/uidna.h" | 72 #include "third_party/icu/source/common/unicode/uidna.h" |
70 #include "third_party/icu/source/common/unicode/uniset.h" | 73 #include "third_party/icu/source/common/unicode/uniset.h" |
71 #include "third_party/icu/source/common/unicode/uscript.h" | 74 #include "third_party/icu/source/common/unicode/uscript.h" |
72 #include "third_party/icu/source/common/unicode/uset.h" | 75 #include "third_party/icu/source/common/unicode/uset.h" |
73 #include "third_party/icu/source/i18n/unicode/datefmt.h" | 76 #include "third_party/icu/source/i18n/unicode/datefmt.h" |
74 #include "third_party/icu/source/i18n/unicode/regex.h" | 77 #include "third_party/icu/source/i18n/unicode/regex.h" |
75 #include "third_party/icu/source/i18n/unicode/ulocdata.h" | 78 #include "third_party/icu/source/i18n/unicode/ulocdata.h" |
76 | 79 |
77 using base::Time; | 80 using base::Time; |
78 | 81 |
79 namespace net { | 82 namespace net { |
80 | 83 |
81 namespace { | 84 namespace { |
82 | 85 |
83 typedef std::vector<size_t> Offsets; | 86 typedef std::vector<size_t> Offsets; |
84 | 87 |
| 88 // what we prepend to get a file URL |
| 89 static const base::FilePath::CharType kFileURLPrefix[] = |
| 90 FILE_PATH_LITERAL("file:///"); |
| 91 |
85 // The general list of blocked ports. Will be blocked unless a specific | 92 // The general list of blocked ports. Will be blocked unless a specific |
86 // protocol overrides it. (Ex: ftp can use ports 20 and 21) | 93 // protocol overrides it. (Ex: ftp can use ports 20 and 21) |
87 static const int kRestrictedPorts[] = { | 94 static const int kRestrictedPorts[] = { |
88 1, // tcpmux | 95 1, // tcpmux |
89 7, // echo | 96 7, // echo |
90 9, // discard | 97 9, // discard |
91 11, // systat | 98 11, // systat |
92 13, // daytime | 99 13, // daytime |
93 15, // netstat | 100 15, // netstat |
94 17, // qotd | 101 17, // qotd |
(...skipping 647 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 if (output_component) { | 749 if (output_component) { |
743 output_component->begin = static_cast<int>(output_component_begin); | 750 output_component->begin = static_cast<int>(output_component_begin); |
744 output_component->len = | 751 output_component->len = |
745 static_cast<int>(output->length() - output_component_begin); | 752 static_cast<int>(output->length() - output_component_begin); |
746 } | 753 } |
747 } else if (output_component) { | 754 } else if (output_component) { |
748 output_component->reset(); | 755 output_component->reset(); |
749 } | 756 } |
750 } | 757 } |
751 | 758 |
| 759 void SanitizeGeneratedFileName(base::FilePath::StringType* filename, |
| 760 bool replace_trailing) { |
| 761 const base::FilePath::CharType kReplace[] = FILE_PATH_LITERAL("-"); |
| 762 if (filename->empty()) |
| 763 return; |
| 764 if (replace_trailing) { |
| 765 // Handle CreateFile() stripping trailing dots and spaces on filenames |
| 766 // http://support.microsoft.com/kb/115827 |
| 767 size_t length = filename->size(); |
| 768 size_t pos = filename->find_last_not_of(FILE_PATH_LITERAL(" .")); |
| 769 filename->resize((pos == std::string::npos) ? 0 : (pos + 1)); |
| 770 base::TrimWhitespace(*filename, base::TRIM_TRAILING, filename); |
| 771 if (filename->empty()) |
| 772 return; |
| 773 size_t trimmed = length - filename->size(); |
| 774 if (trimmed) |
| 775 filename->insert(filename->end(), trimmed, kReplace[0]); |
| 776 } |
| 777 base::TrimString(*filename, FILE_PATH_LITERAL("."), filename); |
| 778 if (filename->empty()) |
| 779 return; |
| 780 // Replace any path information by changing path separators. |
| 781 ReplaceSubstringsAfterOffset(filename, 0, FILE_PATH_LITERAL("/"), kReplace); |
| 782 ReplaceSubstringsAfterOffset(filename, 0, FILE_PATH_LITERAL("\\"), kReplace); |
| 783 } |
| 784 |
| 785 // Returns the filename determined from the last component of the path portion |
| 786 // of the URL. Returns an empty string if the URL doesn't have a path or is |
| 787 // invalid. If the generated filename is not reliable, |
| 788 // |should_overwrite_extension| will be set to true, in which case a better |
| 789 // extension should be determined based on the content type. |
| 790 std::string GetFileNameFromURL(const GURL& url, |
| 791 const std::string& referrer_charset, |
| 792 bool* should_overwrite_extension) { |
| 793 // about: and data: URLs don't have file names, but esp. data: URLs may |
| 794 // contain parts that look like ones (i.e., contain a slash). Therefore we |
| 795 // don't attempt to divine a file name out of them. |
| 796 if (!url.is_valid() || url.SchemeIs("about") || url.SchemeIs("data")) |
| 797 return std::string(); |
| 798 |
| 799 const std::string unescaped_url_filename = UnescapeURLComponent( |
| 800 url.ExtractFileName(), |
| 801 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); |
| 802 |
| 803 // The URL's path should be escaped UTF-8, but may not be. |
| 804 std::string decoded_filename = unescaped_url_filename; |
| 805 if (!IsStringUTF8(decoded_filename)) { |
| 806 // TODO(jshin): this is probably not robust enough. To be sure, we need |
| 807 // encoding detection. |
| 808 base::string16 utf16_output; |
| 809 if (!referrer_charset.empty() && |
| 810 base::CodepageToUTF16(unescaped_url_filename, |
| 811 referrer_charset.c_str(), |
| 812 base::OnStringConversionError::FAIL, |
| 813 &utf16_output)) { |
| 814 decoded_filename = base::UTF16ToUTF8(utf16_output); |
| 815 } else { |
| 816 decoded_filename = base::WideToUTF8( |
| 817 base::SysNativeMBToWide(unescaped_url_filename)); |
| 818 } |
| 819 } |
| 820 // If the URL contains a (possibly empty) query, assume it is a generator, and |
| 821 // allow the determined extension to be overwritten. |
| 822 *should_overwrite_extension = !decoded_filename.empty() && url.has_query(); |
| 823 |
| 824 return decoded_filename; |
| 825 } |
| 826 |
| 827 // Returns whether the specified extension is automatically integrated into the |
| 828 // windows shell. |
| 829 bool IsShellIntegratedExtension(const base::FilePath::StringType& extension) { |
| 830 base::FilePath::StringType extension_lower = StringToLowerASCII(extension); |
| 831 |
| 832 // http://msdn.microsoft.com/en-us/library/ms811694.aspx |
| 833 // Right-clicking on shortcuts can be magical. |
| 834 if ((extension_lower == FILE_PATH_LITERAL("local")) || |
| 835 (extension_lower == FILE_PATH_LITERAL("lnk"))) |
| 836 return true; |
| 837 |
| 838 // http://www.juniper.net/security/auto/vulnerabilities/vuln2612.html |
| 839 // Files become magical if they end in a CLSID, so block such extensions. |
| 840 if (!extension_lower.empty() && |
| 841 (extension_lower[0] == FILE_PATH_LITERAL('{')) && |
| 842 (extension_lower[extension_lower.length() - 1] == FILE_PATH_LITERAL('}'))) |
| 843 return true; |
| 844 return false; |
| 845 } |
| 846 |
| 847 // Returns whether the specified file name is a reserved name on windows. |
| 848 // This includes names like "com2.zip" (which correspond to devices) and |
| 849 // desktop.ini and thumbs.db which have special meaning to the windows shell. |
| 850 bool IsReservedName(const base::FilePath::StringType& filename) { |
| 851 // This list is taken from the MSDN article "Naming a file" |
| 852 // http://msdn2.microsoft.com/en-us/library/aa365247(VS.85).aspx |
| 853 // I also added clock$ because GetSaveFileName seems to consider it as a |
| 854 // reserved name too. |
| 855 static const char* const known_devices[] = { |
| 856 "con", "prn", "aux", "nul", "com1", "com2", "com3", "com4", "com5", |
| 857 "com6", "com7", "com8", "com9", "lpt1", "lpt2", "lpt3", "lpt4", |
| 858 "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", "clock$" |
| 859 }; |
| 860 #if defined(OS_WIN) |
| 861 std::string filename_lower = StringToLowerASCII(base::WideToUTF8(filename)); |
| 862 #elif defined(OS_POSIX) |
| 863 std::string filename_lower = StringToLowerASCII(filename); |
| 864 #endif |
| 865 |
| 866 for (size_t i = 0; i < arraysize(known_devices); ++i) { |
| 867 // Exact match. |
| 868 if (filename_lower == known_devices[i]) |
| 869 return true; |
| 870 // Starts with "DEVICE.". |
| 871 if (filename_lower.find(std::string(known_devices[i]) + ".") == 0) |
| 872 return true; |
| 873 } |
| 874 |
| 875 static const char* const magic_names[] = { |
| 876 // These file names are used by the "Customize folder" feature of the shell. |
| 877 "desktop.ini", |
| 878 "thumbs.db", |
| 879 }; |
| 880 |
| 881 for (size_t i = 0; i < arraysize(magic_names); ++i) { |
| 882 if (filename_lower == magic_names[i]) |
| 883 return true; |
| 884 } |
| 885 |
| 886 return false; |
| 887 } |
| 888 |
| 889 // Examines the current extension in |file_name| and modifies it if necessary in |
| 890 // order to ensure the filename is safe. If |file_name| doesn't contain an |
| 891 // extension or if |ignore_extension| is true, then a new extension will be |
| 892 // constructed based on the |mime_type|. |
| 893 // |
| 894 // We're addressing two things here: |
| 895 // |
| 896 // 1) Usability. If there is no reliable file extension, we want to guess a |
| 897 // reasonable file extension based on the content type. |
| 898 // |
| 899 // 2) Shell integration. Some file extensions automatically integrate with the |
| 900 // shell. We block these extensions to prevent a malicious web site from |
| 901 // integrating with the user's shell. |
| 902 void EnsureSafeExtension(const std::string& mime_type, |
| 903 bool ignore_extension, |
| 904 base::FilePath* file_name) { |
| 905 // See if our file name already contains an extension. |
| 906 base::FilePath::StringType extension = file_name->Extension(); |
| 907 if (!extension.empty()) |
| 908 extension.erase(extension.begin()); // Erase preceding '.'. |
| 909 |
| 910 if ((ignore_extension || extension.empty()) && !mime_type.empty()) { |
| 911 base::FilePath::StringType preferred_mime_extension; |
| 912 std::vector<base::FilePath::StringType> all_mime_extensions; |
| 913 // The GetPreferredExtensionForMimeType call will end up going to disk. Do |
| 914 // this on another thread to avoid slowing the IO thread. |
| 915 // http://crbug.com/61827 |
| 916 // TODO(asanka): Remove this ScopedAllowIO once all callers have switched |
| 917 // over to IO safe threads. |
| 918 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 919 net::GetPreferredExtensionForMimeType(mime_type, &preferred_mime_extension); |
| 920 net::GetExtensionsForMimeType(mime_type, &all_mime_extensions); |
| 921 // If the existing extension is in the list of valid extensions for the |
| 922 // given type, use it. This avoids doing things like pointlessly renaming |
| 923 // "foo.jpg" to "foo.jpeg". |
| 924 if (std::find(all_mime_extensions.begin(), |
| 925 all_mime_extensions.end(), |
| 926 extension) != all_mime_extensions.end()) { |
| 927 // leave |extension| alone |
| 928 } else if (!preferred_mime_extension.empty()) { |
| 929 extension = preferred_mime_extension; |
| 930 } |
| 931 } |
| 932 |
| 933 #if defined(OS_WIN) |
| 934 static const base::FilePath::CharType default_extension[] = |
| 935 FILE_PATH_LITERAL("download"); |
| 936 |
| 937 // Rename shell-integrated extensions. |
| 938 // TODO(asanka): Consider stripping out the bad extension and replacing it |
| 939 // with the preferred extension for the MIME type if one is available. |
| 940 if (IsShellIntegratedExtension(extension)) |
| 941 extension.assign(default_extension); |
| 942 #endif |
| 943 |
| 944 *file_name = file_name->ReplaceExtension(extension); |
| 945 } |
| 946 |
| 947 bool FilePathToString16(const base::FilePath& path, base::string16* converted) { |
| 948 #if defined(OS_WIN) |
| 949 return base::WideToUTF16( |
| 950 path.value().c_str(), path.value().size(), converted); |
| 951 #elif defined(OS_POSIX) |
| 952 std::string component8 = path.AsUTF8Unsafe(); |
| 953 return !component8.empty() && |
| 954 base::UTF8ToUTF16(component8.c_str(), component8.size(), converted); |
| 955 #endif |
| 956 } |
| 957 |
752 bool IPNumberPrefixCheck(const IPAddressNumber& ip_number, | 958 bool IPNumberPrefixCheck(const IPAddressNumber& ip_number, |
753 const unsigned char* ip_prefix, | 959 const unsigned char* ip_prefix, |
754 size_t prefix_length_in_bits) { | 960 size_t prefix_length_in_bits) { |
755 // Compare all the bytes that fall entirely within the prefix. | 961 // Compare all the bytes that fall entirely within the prefix. |
756 int num_entire_bytes_in_prefix = prefix_length_in_bits / 8; | 962 int num_entire_bytes_in_prefix = prefix_length_in_bits / 8; |
757 for (int i = 0; i < num_entire_bytes_in_prefix; ++i) { | 963 for (int i = 0; i < num_entire_bytes_in_prefix; ++i) { |
758 if (ip_number[i] != ip_prefix[i]) | 964 if (ip_number[i] != ip_prefix[i]) |
759 return false; | 965 return false; |
760 } | 966 } |
761 | 967 |
(...skipping 18 matching lines...) Expand all Loading... |
780 const FormatUrlType kFormatUrlOmitAll = kFormatUrlOmitUsernamePassword | | 986 const FormatUrlType kFormatUrlOmitAll = kFormatUrlOmitUsernamePassword | |
781 kFormatUrlOmitHTTP | kFormatUrlOmitTrailingSlashOnBareHostname; | 987 kFormatUrlOmitHTTP | kFormatUrlOmitTrailingSlashOnBareHostname; |
782 | 988 |
783 static base::LazyInstance<std::multiset<int> >::Leaky | 989 static base::LazyInstance<std::multiset<int> >::Leaky |
784 g_explicitly_allowed_ports = LAZY_INSTANCE_INITIALIZER; | 990 g_explicitly_allowed_ports = LAZY_INSTANCE_INITIALIZER; |
785 | 991 |
786 size_t GetCountOfExplicitlyAllowedPorts() { | 992 size_t GetCountOfExplicitlyAllowedPorts() { |
787 return g_explicitly_allowed_ports.Get().size(); | 993 return g_explicitly_allowed_ports.Get().size(); |
788 } | 994 } |
789 | 995 |
| 996 GURL FilePathToFileURL(const base::FilePath& path) { |
| 997 // 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 |
| 999 // latter case to be the canonical UNC form: "file://server/path" |
| 1000 base::FilePath::StringType url_string(kFileURLPrefix); |
| 1001 if (!path.IsAbsolute()) { |
| 1002 base::FilePath current_dir; |
| 1003 PathService::Get(base::DIR_CURRENT, ¤t_dir); |
| 1004 url_string.append(current_dir.value()); |
| 1005 url_string.push_back(base::FilePath::kSeparators[0]); |
| 1006 } |
| 1007 url_string.append(path.value()); |
| 1008 |
| 1009 // Now do replacement of some characters. Since we assume the input is a |
| 1010 // literal filename, anything the URL parser might consider special should |
| 1011 // be escaped here. |
| 1012 |
| 1013 // must be the first substitution since others will introduce percents as the |
| 1014 // escape character |
| 1015 ReplaceSubstringsAfterOffset(&url_string, 0, |
| 1016 FILE_PATH_LITERAL("%"), FILE_PATH_LITERAL("%25")); |
| 1017 |
| 1018 // semicolon is supposed to be some kind of separator according to RFC 2396 |
| 1019 ReplaceSubstringsAfterOffset(&url_string, 0, |
| 1020 FILE_PATH_LITERAL(";"), FILE_PATH_LITERAL("%3B")); |
| 1021 |
| 1022 ReplaceSubstringsAfterOffset(&url_string, 0, |
| 1023 FILE_PATH_LITERAL("#"), FILE_PATH_LITERAL("%23")); |
| 1024 |
| 1025 ReplaceSubstringsAfterOffset(&url_string, 0, |
| 1026 FILE_PATH_LITERAL("?"), FILE_PATH_LITERAL("%3F")); |
| 1027 |
| 1028 #if defined(OS_POSIX) |
| 1029 ReplaceSubstringsAfterOffset(&url_string, 0, |
| 1030 FILE_PATH_LITERAL("\\"), FILE_PATH_LITERAL("%5C")); |
| 1031 #endif |
| 1032 |
| 1033 return GURL(url_string); |
| 1034 } |
| 1035 |
790 std::string GetSpecificHeader(const std::string& headers, | 1036 std::string GetSpecificHeader(const std::string& headers, |
791 const std::string& name) { | 1037 const std::string& name) { |
792 // We want to grab the Value from the "Key: Value" pairs in the headers, | 1038 // We want to grab the Value from the "Key: Value" pairs in the headers, |
793 // which should look like this (no leading spaces, \n-separated) (we format | 1039 // which should look like this (no leading spaces, \n-separated) (we format |
794 // them this way in url_request_inet.cc): | 1040 // them this way in url_request_inet.cc): |
795 // HTTP/1.1 200 OK\n | 1041 // HTTP/1.1 200 OK\n |
796 // ETag: "6d0b8-947-24f35ec0"\n | 1042 // ETag: "6d0b8-947-24f35ec0"\n |
797 // Content-Length: 2375\n | 1043 // Content-Length: 2375\n |
798 // Content-Type: text/html; charset=UTF-8\n | 1044 // Content-Type: text/html; charset=UTF-8\n |
799 // Last-Modified: Sun, 03 Sep 2006 04:34:43 GMT\n | 1045 // Last-Modified: Sun, 03 Sep 2006 04:34:43 GMT\n |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
948 base::string16 StripWWW(const base::string16& text) { | 1194 base::string16 StripWWW(const base::string16& text) { |
949 const base::string16 www(base::ASCIIToUTF16("www.")); | 1195 const base::string16 www(base::ASCIIToUTF16("www.")); |
950 return StartsWith(text, www, true) ? text.substr(www.length()) : text; | 1196 return StartsWith(text, www, true) ? text.substr(www.length()) : text; |
951 } | 1197 } |
952 | 1198 |
953 base::string16 StripWWWFromHost(const GURL& url) { | 1199 base::string16 StripWWWFromHost(const GURL& url) { |
954 DCHECK(url.is_valid()); | 1200 DCHECK(url.is_valid()); |
955 return StripWWW(base::ASCIIToUTF16(url.host())); | 1201 return StripWWW(base::ASCIIToUTF16(url.host())); |
956 } | 1202 } |
957 | 1203 |
| 1204 bool IsSafePortablePathComponent(const base::FilePath& component) { |
| 1205 base::string16 component16; |
| 1206 base::FilePath::StringType sanitized = component.value(); |
| 1207 SanitizeGeneratedFileName(&sanitized, true); |
| 1208 base::FilePath::StringType extension = component.Extension(); |
| 1209 if (!extension.empty()) |
| 1210 extension.erase(extension.begin()); // Erase preceding '.'. |
| 1211 return !component.empty() && |
| 1212 (component == component.BaseName()) && |
| 1213 (component == component.StripTrailingSeparators()) && |
| 1214 FilePathToString16(component, &component16) && |
| 1215 file_util::IsFilenameLegal(component16) && |
| 1216 !IsShellIntegratedExtension(extension) && |
| 1217 (sanitized == component.value()) && |
| 1218 !IsReservedName(component.value()); |
| 1219 } |
| 1220 |
| 1221 bool IsSafePortableRelativePath(const base::FilePath& path) { |
| 1222 if (path.empty() || path.IsAbsolute() || path.EndsWithSeparator()) |
| 1223 return false; |
| 1224 std::vector<base::FilePath::StringType> components; |
| 1225 path.GetComponents(&components); |
| 1226 if (components.empty()) |
| 1227 return false; |
| 1228 for (size_t i = 0; i < components.size() - 1; ++i) { |
| 1229 if (!IsSafePortablePathComponent(base::FilePath(components[i]))) |
| 1230 return false; |
| 1231 } |
| 1232 return IsSafePortablePathComponent(path.BaseName()); |
| 1233 } |
| 1234 |
| 1235 void GenerateSafeFileName(const std::string& mime_type, |
| 1236 bool ignore_extension, |
| 1237 base::FilePath* file_path) { |
| 1238 // Make sure we get the right file extension |
| 1239 EnsureSafeExtension(mime_type, ignore_extension, file_path); |
| 1240 |
| 1241 #if defined(OS_WIN) |
| 1242 // Prepend "_" to the file name if it's a reserved name |
| 1243 base::FilePath::StringType leaf_name = file_path->BaseName().value(); |
| 1244 DCHECK(!leaf_name.empty()); |
| 1245 if (IsReservedName(leaf_name)) { |
| 1246 leaf_name = base::FilePath::StringType(FILE_PATH_LITERAL("_")) + leaf_name; |
| 1247 *file_path = file_path->DirName(); |
| 1248 if (file_path->value() == base::FilePath::kCurrentDirectory) { |
| 1249 *file_path = base::FilePath(leaf_name); |
| 1250 } else { |
| 1251 *file_path = file_path->Append(leaf_name); |
| 1252 } |
| 1253 } |
| 1254 #endif |
| 1255 } |
| 1256 |
| 1257 base::string16 GetSuggestedFilename(const GURL& url, |
| 1258 const std::string& content_disposition, |
| 1259 const std::string& referrer_charset, |
| 1260 const std::string& suggested_name, |
| 1261 const std::string& mime_type, |
| 1262 const std::string& default_name) { |
| 1263 // TODO: this function to be updated to match the httpbis recommendations. |
| 1264 // Talk to abarth for the latest news. |
| 1265 |
| 1266 // We don't translate this fallback string, "download". If localization is |
| 1267 // needed, the caller should provide localized fallback in |default_name|. |
| 1268 static const base::FilePath::CharType kFinalFallbackName[] = |
| 1269 FILE_PATH_LITERAL("download"); |
| 1270 std::string filename; // In UTF-8 |
| 1271 bool overwrite_extension = false; |
| 1272 |
| 1273 // Try to extract a filename from content-disposition first. |
| 1274 if (!content_disposition.empty()) { |
| 1275 HttpContentDisposition header(content_disposition, referrer_charset); |
| 1276 filename = header.filename(); |
| 1277 } |
| 1278 |
| 1279 // Then try to use the suggested name. |
| 1280 if (filename.empty() && !suggested_name.empty()) |
| 1281 filename = suggested_name; |
| 1282 |
| 1283 // Now try extracting the filename from the URL. GetFileNameFromURL() only |
| 1284 // looks at the last component of the URL and doesn't return the hostname as a |
| 1285 // failover. |
| 1286 if (filename.empty()) |
| 1287 filename = GetFileNameFromURL(url, referrer_charset, &overwrite_extension); |
| 1288 |
| 1289 // Finally try the URL hostname, but only if there's no default specified in |
| 1290 // |default_name|. Some schemes (e.g.: file:, about:, data:) do not have a |
| 1291 // host name. |
| 1292 if (filename.empty() && |
| 1293 default_name.empty() && |
| 1294 url.is_valid() && |
| 1295 !url.host().empty()) { |
| 1296 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) |
| 1297 filename = url.host(); |
| 1298 } |
| 1299 |
| 1300 bool replace_trailing = false; |
| 1301 base::FilePath::StringType result_str, default_name_str; |
| 1302 #if defined(OS_WIN) |
| 1303 replace_trailing = true; |
| 1304 result_str = base::UTF8ToUTF16(filename); |
| 1305 default_name_str = base::UTF8ToUTF16(default_name); |
| 1306 #else |
| 1307 result_str = filename; |
| 1308 default_name_str = default_name; |
| 1309 #endif |
| 1310 SanitizeGeneratedFileName(&result_str, replace_trailing); |
| 1311 if (result_str.find_last_not_of(FILE_PATH_LITERAL("-_")) == |
| 1312 base::FilePath::StringType::npos) { |
| 1313 result_str = !default_name_str.empty() ? default_name_str : |
| 1314 base::FilePath::StringType(kFinalFallbackName); |
| 1315 overwrite_extension = false; |
| 1316 } |
| 1317 file_util::ReplaceIllegalCharactersInPath(&result_str, '-'); |
| 1318 base::FilePath result(result_str); |
| 1319 GenerateSafeFileName(mime_type, overwrite_extension, &result); |
| 1320 |
| 1321 base::string16 result16; |
| 1322 if (!FilePathToString16(result, &result16)) { |
| 1323 result = base::FilePath(default_name_str); |
| 1324 if (!FilePathToString16(result, &result16)) { |
| 1325 result = base::FilePath(kFinalFallbackName); |
| 1326 FilePathToString16(result, &result16); |
| 1327 } |
| 1328 } |
| 1329 return result16; |
| 1330 } |
| 1331 |
| 1332 base::FilePath GenerateFileName(const GURL& url, |
| 1333 const std::string& content_disposition, |
| 1334 const std::string& referrer_charset, |
| 1335 const std::string& suggested_name, |
| 1336 const std::string& mime_type, |
| 1337 const std::string& default_file_name) { |
| 1338 base::string16 file_name = GetSuggestedFilename(url, |
| 1339 content_disposition, |
| 1340 referrer_charset, |
| 1341 suggested_name, |
| 1342 mime_type, |
| 1343 default_file_name); |
| 1344 |
| 1345 #if defined(OS_WIN) |
| 1346 base::FilePath generated_name(file_name); |
| 1347 #else |
| 1348 base::FilePath generated_name( |
| 1349 base::SysWideToNativeMB(base::UTF16ToWide(file_name))); |
| 1350 #endif |
| 1351 |
| 1352 #if defined(OS_CHROMEOS) |
| 1353 // When doing file manager operations on ChromeOS, the file paths get |
| 1354 // normalized in WebKit layer, so let's ensure downloaded files have |
| 1355 // normalized names. Otherwise, we won't be able to handle files with NFD |
| 1356 // utf8 encoded characters in name. |
| 1357 file_util::NormalizeFileNameEncoding(&generated_name); |
| 1358 #endif |
| 1359 |
| 1360 DCHECK(!generated_name.empty()); |
| 1361 |
| 1362 return generated_name; |
| 1363 } |
958 | 1364 |
959 bool IsPortAllowedByDefault(int port) { | 1365 bool IsPortAllowedByDefault(int port) { |
960 int array_size = arraysize(kRestrictedPorts); | 1366 int array_size = arraysize(kRestrictedPorts); |
961 for (int i = 0; i < array_size; i++) { | 1367 for (int i = 0; i < array_size; i++) { |
962 if (kRestrictedPorts[i] == port) { | 1368 if (kRestrictedPorts[i] == port) { |
963 return false; | 1369 return false; |
964 } | 1370 } |
965 } | 1371 } |
966 return true; | 1372 return true; |
967 } | 1373 } |
(...skipping 850 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1818 } | 2224 } |
1819 return a1.size() * CHAR_BIT; | 2225 return a1.size() * CHAR_BIT; |
1820 } | 2226 } |
1821 | 2227 |
1822 unsigned MaskPrefixLength(const IPAddressNumber& mask) { | 2228 unsigned MaskPrefixLength(const IPAddressNumber& mask) { |
1823 IPAddressNumber all_ones(mask.size(), 0xFF); | 2229 IPAddressNumber all_ones(mask.size(), 0xFF); |
1824 return CommonPrefixLength(mask, all_ones); | 2230 return CommonPrefixLength(mask, all_ones); |
1825 } | 2231 } |
1826 | 2232 |
1827 } // namespace net | 2233 } // namespace net |
OLD | NEW |