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 <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <map> | 9 #include <map> |
10 | 10 |
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 const char kViewSource[] = "view-source:"; | 627 const char kViewSource[] = "view-source:"; |
628 const size_t kViewSourceLength = arraysize(kViewSource) - 1; | 628 const size_t kViewSourceLength = arraysize(kViewSource) - 1; |
629 | 629 |
630 // Format the underlying URL and adjust offsets. | 630 // Format the underlying URL and adjust offsets. |
631 const std::string& url_str(url.possibly_invalid_spec()); | 631 const std::string& url_str(url.possibly_invalid_spec()); |
632 Offsets offsets_into_underlying_url(original_offsets); | 632 Offsets offsets_into_underlying_url(original_offsets); |
633 { | 633 { |
634 base::OffsetAdjuster adjuster(&offsets_into_underlying_url); | 634 base::OffsetAdjuster adjuster(&offsets_into_underlying_url); |
635 adjuster.Add(base::OffsetAdjuster::Adjustment(0, kViewSourceLength, 0)); | 635 adjuster.Add(base::OffsetAdjuster::Adjustment(0, kViewSourceLength, 0)); |
636 } | 636 } |
637 base::string16 result(ASCIIToUTF16(kViewSource) + | 637 base::string16 result(base::ASCIIToUTF16(kViewSource) + |
638 FormatUrlWithOffsets(GURL(url_str.substr(kViewSourceLength)), languages, | 638 FormatUrlWithOffsets(GURL(url_str.substr(kViewSourceLength)), languages, |
639 format_types, unescape_rules, new_parsed, prefix_end, | 639 format_types, unescape_rules, new_parsed, prefix_end, |
640 &offsets_into_underlying_url)); | 640 &offsets_into_underlying_url)); |
641 AdjustForComponentTransform(original_offsets, kViewSourceLength, | 641 AdjustForComponentTransform(original_offsets, kViewSourceLength, |
642 url_str.length(), offsets_into_underlying_url, | 642 url_str.length(), offsets_into_underlying_url, |
643 kViewSourceLength, result.length(), | 643 kViewSourceLength, result.length(), |
644 offsets_for_adjustment); | 644 offsets_for_adjustment); |
645 LimitOffsets(result, offsets_for_adjustment); | 645 LimitOffsets(result, offsets_for_adjustment); |
646 | 646 |
647 // Adjust positions of the parsed components. | 647 // Adjust positions of the parsed components. |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 std::string decoded_filename = unescaped_url_filename; | 803 std::string decoded_filename = unescaped_url_filename; |
804 if (!IsStringUTF8(decoded_filename)) { | 804 if (!IsStringUTF8(decoded_filename)) { |
805 // TODO(jshin): this is probably not robust enough. To be sure, we need | 805 // TODO(jshin): this is probably not robust enough. To be sure, we need |
806 // encoding detection. | 806 // encoding detection. |
807 base::string16 utf16_output; | 807 base::string16 utf16_output; |
808 if (!referrer_charset.empty() && | 808 if (!referrer_charset.empty() && |
809 base::CodepageToUTF16(unescaped_url_filename, | 809 base::CodepageToUTF16(unescaped_url_filename, |
810 referrer_charset.c_str(), | 810 referrer_charset.c_str(), |
811 base::OnStringConversionError::FAIL, | 811 base::OnStringConversionError::FAIL, |
812 &utf16_output)) { | 812 &utf16_output)) { |
813 decoded_filename = UTF16ToUTF8(utf16_output); | 813 decoded_filename = base::UTF16ToUTF8(utf16_output); |
814 } else { | 814 } else { |
815 decoded_filename = WideToUTF8( | 815 decoded_filename = base::WideToUTF8( |
816 base::SysNativeMBToWide(unescaped_url_filename)); | 816 base::SysNativeMBToWide(unescaped_url_filename)); |
817 } | 817 } |
818 } | 818 } |
819 // If the URL contains a (possibly empty) query, assume it is a generator, and | 819 // If the URL contains a (possibly empty) query, assume it is a generator, and |
820 // allow the determined extension to be overwritten. | 820 // allow the determined extension to be overwritten. |
821 *should_overwrite_extension = !decoded_filename.empty() && url.has_query(); | 821 *should_overwrite_extension = !decoded_filename.empty() && url.has_query(); |
822 | 822 |
823 return decoded_filename; | 823 return decoded_filename; |
824 } | 824 } |
825 | 825 |
(...skipping 24 matching lines...) Expand all Loading... |
850 // This list is taken from the MSDN article "Naming a file" | 850 // This list is taken from the MSDN article "Naming a file" |
851 // http://msdn2.microsoft.com/en-us/library/aa365247(VS.85).aspx | 851 // http://msdn2.microsoft.com/en-us/library/aa365247(VS.85).aspx |
852 // I also added clock$ because GetSaveFileName seems to consider it as a | 852 // I also added clock$ because GetSaveFileName seems to consider it as a |
853 // reserved name too. | 853 // reserved name too. |
854 static const char* const known_devices[] = { | 854 static const char* const known_devices[] = { |
855 "con", "prn", "aux", "nul", "com1", "com2", "com3", "com4", "com5", | 855 "con", "prn", "aux", "nul", "com1", "com2", "com3", "com4", "com5", |
856 "com6", "com7", "com8", "com9", "lpt1", "lpt2", "lpt3", "lpt4", | 856 "com6", "com7", "com8", "com9", "lpt1", "lpt2", "lpt3", "lpt4", |
857 "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", "clock$" | 857 "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", "clock$" |
858 }; | 858 }; |
859 #if defined(OS_WIN) | 859 #if defined(OS_WIN) |
860 std::string filename_lower = StringToLowerASCII(WideToUTF8(filename)); | 860 std::string filename_lower = StringToLowerASCII(base::WideToUTF8(filename)); |
861 #elif defined(OS_POSIX) | 861 #elif defined(OS_POSIX) |
862 std::string filename_lower = StringToLowerASCII(filename); | 862 std::string filename_lower = StringToLowerASCII(filename); |
863 #endif | 863 #endif |
864 | 864 |
865 for (size_t i = 0; i < arraysize(known_devices); ++i) { | 865 for (size_t i = 0; i < arraysize(known_devices); ++i) { |
866 // Exact match. | 866 // Exact match. |
867 if (filename_lower == known_devices[i]) | 867 if (filename_lower == known_devices[i]) |
868 return true; | 868 return true; |
869 // Starts with "DEVICE.". | 869 // Starts with "DEVICE.". |
870 if (filename_lower.find(std::string(known_devices[i]) + ".") == 0) | 870 if (filename_lower.find(std::string(known_devices[i]) + ".") == 0) |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
938 // with the preferred extension for the MIME type if one is available. | 938 // with the preferred extension for the MIME type if one is available. |
939 if (IsShellIntegratedExtension(extension)) | 939 if (IsShellIntegratedExtension(extension)) |
940 extension.assign(default_extension); | 940 extension.assign(default_extension); |
941 #endif | 941 #endif |
942 | 942 |
943 *file_name = file_name->ReplaceExtension(extension); | 943 *file_name = file_name->ReplaceExtension(extension); |
944 } | 944 } |
945 | 945 |
946 bool FilePathToString16(const base::FilePath& path, base::string16* converted) { | 946 bool FilePathToString16(const base::FilePath& path, base::string16* converted) { |
947 #if defined(OS_WIN) | 947 #if defined(OS_WIN) |
948 return WideToUTF16(path.value().c_str(), path.value().size(), converted); | 948 return base::WideToUTF16( |
| 949 path.value().c_str(), path.value().size(), converted); |
949 #elif defined(OS_POSIX) | 950 #elif defined(OS_POSIX) |
950 std::string component8 = path.AsUTF8Unsafe(); | 951 std::string component8 = path.AsUTF8Unsafe(); |
951 return !component8.empty() && | 952 return !component8.empty() && |
952 UTF8ToUTF16(component8.c_str(), component8.size(), converted); | 953 base::UTF8ToUTF16(component8.c_str(), component8.size(), converted); |
953 #endif | 954 #endif |
954 } | 955 } |
955 | 956 |
956 bool IPNumberPrefixCheck(const IPAddressNumber& ip_number, | 957 bool IPNumberPrefixCheck(const IPAddressNumber& ip_number, |
957 const unsigned char* ip_prefix, | 958 const unsigned char* ip_prefix, |
958 size_t prefix_length_in_bits) { | 959 size_t prefix_length_in_bits) { |
959 // Compare all the bytes that fall entirely within the prefix. | 960 // Compare all the bytes that fall entirely within the prefix. |
960 int num_entire_bytes_in_prefix = prefix_length_in_bits / 8; | 961 int num_entire_bytes_in_prefix = prefix_length_in_bits / 8; |
961 for (int i = 0; i < num_entire_bytes_in_prefix; ++i) { | 962 for (int i = 0; i < num_entire_bytes_in_prefix; ++i) { |
962 if (ip_number[i] != ip_prefix[i]) | 963 if (ip_number[i] != ip_prefix[i]) |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1145 std::string GetDirectoryListingEntry(const base::string16& name, | 1146 std::string GetDirectoryListingEntry(const base::string16& name, |
1146 const std::string& raw_bytes, | 1147 const std::string& raw_bytes, |
1147 bool is_dir, | 1148 bool is_dir, |
1148 int64 size, | 1149 int64 size, |
1149 Time modified) { | 1150 Time modified) { |
1150 std::string result; | 1151 std::string result; |
1151 result.append("<script>addRow("); | 1152 result.append("<script>addRow("); |
1152 base::EscapeJSONString(name, true, &result); | 1153 base::EscapeJSONString(name, true, &result); |
1153 result.append(","); | 1154 result.append(","); |
1154 if (raw_bytes.empty()) { | 1155 if (raw_bytes.empty()) { |
1155 base::EscapeJSONString(EscapePath(UTF16ToUTF8(name)), true, &result); | 1156 base::EscapeJSONString(EscapePath(base::UTF16ToUTF8(name)), true, &result); |
1156 } else { | 1157 } else { |
1157 base::EscapeJSONString(EscapePath(raw_bytes), true, &result); | 1158 base::EscapeJSONString(EscapePath(raw_bytes), true, &result); |
1158 } | 1159 } |
1159 if (is_dir) { | 1160 if (is_dir) { |
1160 result.append(",1,"); | 1161 result.append(",1,"); |
1161 } else { | 1162 } else { |
1162 result.append(",0,"); | 1163 result.append(",0,"); |
1163 } | 1164 } |
1164 | 1165 |
1165 // Negative size means unknown or not applicable (e.g. directory). | 1166 // Negative size means unknown or not applicable (e.g. directory). |
(...skipping 10 matching lines...) Expand all Loading... |
1176 modified_str = base::TimeFormatShortDateAndTime(modified); | 1177 modified_str = base::TimeFormatShortDateAndTime(modified); |
1177 } | 1178 } |
1178 base::EscapeJSONString(modified_str, true, &result); | 1179 base::EscapeJSONString(modified_str, true, &result); |
1179 | 1180 |
1180 result.append(");</script>\n"); | 1181 result.append(");</script>\n"); |
1181 | 1182 |
1182 return result; | 1183 return result; |
1183 } | 1184 } |
1184 | 1185 |
1185 base::string16 StripWWW(const base::string16& text) { | 1186 base::string16 StripWWW(const base::string16& text) { |
1186 const base::string16 www(ASCIIToUTF16("www.")); | 1187 const base::string16 www(base::ASCIIToUTF16("www.")); |
1187 return StartsWith(text, www, true) ? text.substr(www.length()) : text; | 1188 return StartsWith(text, www, true) ? text.substr(www.length()) : text; |
1188 } | 1189 } |
1189 | 1190 |
1190 base::string16 StripWWWFromHost(const GURL& url) { | 1191 base::string16 StripWWWFromHost(const GURL& url) { |
1191 DCHECK(url.is_valid()); | 1192 DCHECK(url.is_valid()); |
1192 return StripWWW(ASCIIToUTF16(url.host())); | 1193 return StripWWW(base::ASCIIToUTF16(url.host())); |
1193 } | 1194 } |
1194 | 1195 |
1195 bool IsSafePortablePathComponent(const base::FilePath& component) { | 1196 bool IsSafePortablePathComponent(const base::FilePath& component) { |
1196 base::string16 component16; | 1197 base::string16 component16; |
1197 base::FilePath::StringType sanitized = component.value(); | 1198 base::FilePath::StringType sanitized = component.value(); |
1198 SanitizeGeneratedFileName(&sanitized, true); | 1199 SanitizeGeneratedFileName(&sanitized, true); |
1199 base::FilePath::StringType extension = component.Extension(); | 1200 base::FilePath::StringType extension = component.Extension(); |
1200 if (!extension.empty()) | 1201 if (!extension.empty()) |
1201 extension.erase(extension.begin()); // Erase preceding '.'. | 1202 extension.erase(extension.begin()); // Erase preceding '.'. |
1202 return !component.empty() && | 1203 return !component.empty() && |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1285 url.is_valid() && | 1286 url.is_valid() && |
1286 !url.host().empty()) { | 1287 !url.host().empty()) { |
1287 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) | 1288 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) |
1288 filename = url.host(); | 1289 filename = url.host(); |
1289 } | 1290 } |
1290 | 1291 |
1291 bool replace_trailing = false; | 1292 bool replace_trailing = false; |
1292 base::FilePath::StringType result_str, default_name_str; | 1293 base::FilePath::StringType result_str, default_name_str; |
1293 #if defined(OS_WIN) | 1294 #if defined(OS_WIN) |
1294 replace_trailing = true; | 1295 replace_trailing = true; |
1295 result_str = UTF8ToUTF16(filename); | 1296 result_str = base::UTF8ToUTF16(filename); |
1296 default_name_str = UTF8ToUTF16(default_name); | 1297 default_name_str = base::UTF8ToUTF16(default_name); |
1297 #else | 1298 #else |
1298 result_str = filename; | 1299 result_str = filename; |
1299 default_name_str = default_name; | 1300 default_name_str = default_name; |
1300 #endif | 1301 #endif |
1301 SanitizeGeneratedFileName(&result_str, replace_trailing); | 1302 SanitizeGeneratedFileName(&result_str, replace_trailing); |
1302 if (result_str.find_last_not_of(FILE_PATH_LITERAL("-_")) == | 1303 if (result_str.find_last_not_of(FILE_PATH_LITERAL("-_")) == |
1303 base::FilePath::StringType::npos) { | 1304 base::FilePath::StringType::npos) { |
1304 result_str = !default_name_str.empty() ? default_name_str : | 1305 result_str = !default_name_str.empty() ? default_name_str : |
1305 base::FilePath::StringType(kFinalFallbackName); | 1306 base::FilePath::StringType(kFinalFallbackName); |
1306 overwrite_extension = false; | 1307 overwrite_extension = false; |
(...skipping 23 matching lines...) Expand all Loading... |
1330 content_disposition, | 1331 content_disposition, |
1331 referrer_charset, | 1332 referrer_charset, |
1332 suggested_name, | 1333 suggested_name, |
1333 mime_type, | 1334 mime_type, |
1334 default_file_name); | 1335 default_file_name); |
1335 | 1336 |
1336 #if defined(OS_WIN) | 1337 #if defined(OS_WIN) |
1337 base::FilePath generated_name(file_name); | 1338 base::FilePath generated_name(file_name); |
1338 #else | 1339 #else |
1339 base::FilePath generated_name( | 1340 base::FilePath generated_name( |
1340 base::SysWideToNativeMB(UTF16ToWide(file_name))); | 1341 base::SysWideToNativeMB(base::UTF16ToWide(file_name))); |
1341 #endif | 1342 #endif |
1342 | 1343 |
1343 #if defined(OS_CHROMEOS) | 1344 #if defined(OS_CHROMEOS) |
1344 // When doing file manager operations on ChromeOS, the file paths get | 1345 // When doing file manager operations on ChromeOS, the file paths get |
1345 // normalized in WebKit layer, so let's ensure downloaded files have | 1346 // normalized in WebKit layer, so let's ensure downloaded files have |
1346 // normalized names. Otherwise, we won't be able to handle files with NFD | 1347 // normalized names. Otherwise, we won't be able to handle files with NFD |
1347 // utf8 encoded characters in name. | 1348 // utf8 encoded characters in name. |
1348 file_util::NormalizeFileNameEncoding(&generated_name); | 1349 file_util::NormalizeFileNameEncoding(&generated_name); |
1349 #endif | 1350 #endif |
1350 | 1351 |
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1828 | 1829 |
1829 // Ref. This is valid, unescaped UTF-8, so we can just convert. | 1830 // Ref. This is valid, unescaped UTF-8, so we can just convert. |
1830 if (parsed.ref.is_valid()) | 1831 if (parsed.ref.is_valid()) |
1831 url_string.push_back('#'); | 1832 url_string.push_back('#'); |
1832 AppendFormattedComponent(spec, parsed.ref, original_offsets, | 1833 AppendFormattedComponent(spec, parsed.ref, original_offsets, |
1833 NonHostComponentTransform(UnescapeRule::NONE), &url_string, | 1834 NonHostComponentTransform(UnescapeRule::NONE), &url_string, |
1834 &new_parsed->ref, offsets_for_adjustment); | 1835 &new_parsed->ref, offsets_for_adjustment); |
1835 | 1836 |
1836 // If we need to strip out http do it after the fact. This way we don't need | 1837 // If we need to strip out http do it after the fact. This way we don't need |
1837 // to worry about how offset_for_adjustment is interpreted. | 1838 // to worry about how offset_for_adjustment is interpreted. |
1838 if (omit_http && StartsWith(url_string, ASCIIToUTF16(kHTTP), true)) { | 1839 if (omit_http && StartsWith(url_string, base::ASCIIToUTF16(kHTTP), true)) { |
1839 const size_t kHTTPSize = arraysize(kHTTP) - 1; | 1840 const size_t kHTTPSize = arraysize(kHTTP) - 1; |
1840 url_string = url_string.substr(kHTTPSize); | 1841 url_string = url_string.substr(kHTTPSize); |
1841 if (offsets_for_adjustment && !offsets_for_adjustment->empty()) { | 1842 if (offsets_for_adjustment && !offsets_for_adjustment->empty()) { |
1842 base::OffsetAdjuster offset_adjuster(offsets_for_adjustment); | 1843 base::OffsetAdjuster offset_adjuster(offsets_for_adjustment); |
1843 offset_adjuster.Add(base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0)); | 1844 offset_adjuster.Add(base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0)); |
1844 } | 1845 } |
1845 if (prefix_end) | 1846 if (prefix_end) |
1846 *prefix_end -= kHTTPSize; | 1847 *prefix_end -= kHTTPSize; |
1847 | 1848 |
1848 // Adjust new_parsed. | 1849 // Adjust new_parsed. |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2213 } | 2214 } |
2214 return a1.size() * CHAR_BIT; | 2215 return a1.size() * CHAR_BIT; |
2215 } | 2216 } |
2216 | 2217 |
2217 unsigned MaskPrefixLength(const IPAddressNumber& mask) { | 2218 unsigned MaskPrefixLength(const IPAddressNumber& mask) { |
2218 IPAddressNumber all_ones(mask.size(), 0xFF); | 2219 IPAddressNumber all_ones(mask.size(), 0xFF); |
2219 return CommonPrefixLength(mask, all_ones); | 2220 return CommonPrefixLength(mask, all_ones); |
2220 } | 2221 } |
2221 | 2222 |
2222 } // namespace net | 2223 } // namespace net |
OLD | NEW |