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

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

Issue 7647014: Replace whitespace at beginning and end of file with hyphens, rather than silently discarding. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Changes based on review comments Created 9 years, 4 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
« no previous file with comments | « no previous file | net/base/net_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <unicode/regex.h> 7 #include <unicode/regex.h>
8 #include <unicode/ucnv.h> 8 #include <unicode/ucnv.h>
9 #include <unicode/uidna.h> 9 #include <unicode/uidna.h>
10 #include <unicode/ulocdata.h> 10 #include <unicode/ulocdata.h>
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 0xFFFF, // Used to block all invalid port numbers (see 151 0xFFFF, // Used to block all invalid port numbers (see
152 // third_party/WebKit/Source/WebCore/platform/KURLGoogle.cpp, port()) 152 // third_party/WebKit/Source/WebCore/platform/KURLGoogle.cpp, port())
153 }; 153 };
154 154
155 // FTP overrides the following restricted ports. 155 // FTP overrides the following restricted ports.
156 static const int kAllowedFtpPorts[] = { 156 static const int kAllowedFtpPorts[] = {
157 21, // ftp data 157 21, // ftp data
158 22, // ssh 158 22, // ssh
159 }; 159 };
160 160
161 template<typename STR>
162 typename STR::size_type CountTrailingChars(
asanka 2011/08/19 19:51:19 Nit: This is only instantiated for std::string.
163 const STR& input,
164 const typename STR::value_type trailing_chars[]) {
165 const typename STR::size_type last_good_char =
166 input.find_last_not_of(trailing_chars);
167
168 if (last_good_char == std::string::npos)
169 return input.length();
170 else
171 return input.length() - last_good_char - 1;
172 }
173
161 // Similar to Base64Decode. Decodes a Q-encoded string to a sequence 174 // Similar to Base64Decode. Decodes a Q-encoded string to a sequence
162 // of bytes. If input is invalid, return false. 175 // of bytes. If input is invalid, return false.
163 bool QPDecode(const std::string& input, std::string* output) { 176 bool QPDecode(const std::string& input, std::string* output) {
164 std::string temp; 177 std::string temp;
165 temp.reserve(input.size()); 178 temp.reserve(input.size());
166 std::string::const_iterator it = input.begin(); 179 std::string::const_iterator it = input.begin();
167 while (it != input.end()) { 180 while (it != input.end()) {
168 if (*it == '_') { 181 if (*it == '_') {
169 temp.push_back(' '); 182 temp.push_back(' ');
170 } else if (*it == '=') { 183 } else if (*it == '=') {
(...skipping 1210 matching lines...) Expand 10 before | Expand all | Expand 10 after
1381 const std::string& suggested_name, 1394 const std::string& suggested_name,
1382 const string16& default_name) { 1395 const string16& default_name) {
1383 // TODO: this function to be updated to match the httpbis recommendations. 1396 // TODO: this function to be updated to match the httpbis recommendations.
1384 // Talk to abarth for the latest news. 1397 // Talk to abarth for the latest news.
1385 1398
1386 // We don't translate this fallback string, "download". If localization is 1399 // We don't translate this fallback string, "download". If localization is
1387 // needed, the caller should provide localized fallback default_name. 1400 // needed, the caller should provide localized fallback default_name.
1388 static const char* kFinalFallbackName = "download"; 1401 static const char* kFinalFallbackName = "download";
1389 1402
1390 std::string filename; 1403 std::string filename;
1404 std::string::size_type trimmed_trailing_character_count = 0;
1391 1405
1392 // Try to extract from content-disposition first. 1406 // Try to extract from content-disposition first.
1393 if (!content_disposition.empty()) 1407 if (!content_disposition.empty())
1394 filename = GetFileNameFromCD(content_disposition, referrer_charset); 1408 filename = GetFileNameFromCD(content_disposition, referrer_charset);
1395 1409
1396 // Then try to use suggested name. 1410 // Then try to use suggested name.
1397 if (filename.empty() && !suggested_name.empty()) 1411 if (filename.empty() && !suggested_name.empty())
1398 filename = suggested_name; 1412 filename = suggested_name;
1399 1413
1400 if (!filename.empty()) { 1414 if (!filename.empty()) {
1401 // Replace any path information the server may have sent, by changing 1415 // Replace any path information the server may have sent, by changing
1402 // path separators with underscores. 1416 // path separators with underscores.
1403 ReplaceSubstringsAfterOffset(&filename, 0, "/", "_"); 1417 ReplaceSubstringsAfterOffset(&filename, 0, "/", "_");
1404 ReplaceSubstringsAfterOffset(&filename, 0, "\\", "_"); 1418 ReplaceSubstringsAfterOffset(&filename, 0, "\\", "_");
1405 1419
1406 // Next, remove "." from the beginning and end of the file name to avoid 1420 // Next, remove "." from the beginning and end of the file name to avoid
1407 // tricks with hidden files, "..", and "." 1421 // tricks with hidden files, "..", and "."
1422 trimmed_trailing_character_count += CountTrailingChars(filename, ".");
1408 TrimString(filename, ".", &filename); 1423 TrimString(filename, ".", &filename);
1409 } 1424 }
1410 1425
1411 if (filename.empty()) { 1426 if (filename.empty()) {
1427 trimmed_trailing_character_count = 0;
1412 // about: and data: URLs don't have file names, but esp. data: URLs may 1428 // about: and data: URLs don't have file names, but esp. data: URLs may
1413 // contain parts that look like ones (i.e., contain a slash). 1429 // contain parts that look like ones (i.e., contain a slash).
1414 // Therefore we don't attempt to divine a file name out of them. 1430 // Therefore we don't attempt to divine a file name out of them.
1415 if (url.SchemeIs("about") || url.SchemeIs("data")) { 1431 if (url.SchemeIs("about") || url.SchemeIs("data")) {
1416 return default_name.empty() ? ASCIIToUTF16(kFinalFallbackName) 1432 return default_name.empty() ? ASCIIToUTF16(kFinalFallbackName)
1417 : default_name; 1433 : default_name;
1418 } 1434 }
1419 1435
1420 if (url.is_valid()) { 1436 if (url.is_valid()) {
1421 const std::string unescaped_url_filename = UnescapeURLComponent( 1437 const std::string unescaped_url_filename = UnescapeURLComponent(
(...skipping 10 matching lines...) Expand all
1432 &decoded_filename); 1448 &decoded_filename);
1433 } 1449 }
1434 1450
1435 filename = decoded_filename; 1451 filename = decoded_filename;
1436 } 1452 }
1437 } 1453 }
1438 1454
1439 #if defined(OS_WIN) 1455 #if defined(OS_WIN)
1440 { // Handle CreateFile() stripping trailing dots and spaces on filenames 1456 { // Handle CreateFile() stripping trailing dots and spaces on filenames
1441 // http://support.microsoft.com/kb/115827 1457 // http://support.microsoft.com/kb/115827
1442 std::string::size_type pos = filename.find_last_not_of(" ."); 1458 std::string::size_type trailing_spaces_dots = CountTrailingChars(
1443 if (pos == std::string::npos) 1459 filename, " .");
1444 filename.resize(0); 1460 trimmed_trailing_character_count += trailing_spaces_dots;
1445 else 1461 filename.resize(filename.length() - trailing_spaces_dots);
1446 filename.resize(++pos);
1447 } 1462 }
1448 #endif 1463 #endif
1449 // Trim '.' once more. 1464 // Trim '.' once more.
1450 TrimString(filename, ".", &filename); 1465 TrimString(filename, ".", &filename);
1451 1466
1452 // If there's no filename or it gets trimed to be empty, use 1467 // If there's no filename or it gets trimed to be empty, use
1453 // the URL hostname or default_name 1468 // the URL hostname or default_name
1454 if (filename.empty()) { 1469 if (filename.empty()) {
1470 trimmed_trailing_character_count = 0;
1455 if (!default_name.empty()) { 1471 if (!default_name.empty()) {
1456 return default_name; 1472 return default_name;
1457 } else if (url.is_valid()) { 1473 } else if (url.is_valid()) {
1458 // Some schemes (e.g. file) do not have a hostname. Even though it's 1474 // Some schemes (e.g. file) do not have a hostname. Even though it's
1459 // not likely to reach here, let's hardcode the last fallback name. 1475 // not likely to reach here, let's hardcode the last fallback name.
1460 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) 1476 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451)
1461 filename = url.host().empty() ? kFinalFallbackName : url.host(); 1477 filename = url.host().empty() ? kFinalFallbackName : url.host();
1462 } else { 1478 } else {
1463 NOTREACHED(); 1479 NOTREACHED();
1464 } 1480 }
1465 } 1481 }
1466 1482
1467 #if defined(OS_WIN) 1483 #if defined(OS_WIN)
1468 string16 path = UTF8ToUTF16(filename); 1484 string16 path = UTF8ToUTF16(filename);
1485 // On Windows we want to preserve or replace all characters including
1486 // whitespace to prevent file extension obfuscation on trusted websites
1487 // e.g. Gmail might think evil.exe. is safe, so we don't want it to become
1488 // evil.exe when we download it
1489 std::wstring::size_type path_length_before_trim = path.length();
1490 TrimWhitespace(path, TRIM_TRAILING, &path);
1491 trimmed_trailing_character_count += path_length_before_trim - path.length();
1469 file_util::ReplaceIllegalCharactersInPath(&path, '-'); 1492 file_util::ReplaceIllegalCharactersInPath(&path, '-');
1493 path.append(trimmed_trailing_character_count, '-');
1470 return path; 1494 return path;
1471 #else 1495 #else
1472 std::string path = filename; 1496 std::string path = filename;
1473 file_util::ReplaceIllegalCharactersInPath(&path, '-'); 1497 file_util::ReplaceIllegalCharactersInPath(&path, '-');
1474 return UTF8ToUTF16(path); 1498 return UTF8ToUTF16(path);
1475 #endif 1499 #endif
1476 } 1500 }
1477 1501
1478 FilePath GenerateFileName(const GURL& url, 1502 FilePath GenerateFileName(const GURL& url,
1479 const std::string& content_disposition, 1503 const std::string& content_disposition,
(...skipping 923 matching lines...) Expand 10 before | Expand all | Expand 10 after
2403 2427
2404 NetworkInterface::NetworkInterface(const std::string& name, 2428 NetworkInterface::NetworkInterface(const std::string& name,
2405 const IPAddressNumber& address) 2429 const IPAddressNumber& address)
2406 : name(name), address(address) { 2430 : name(name), address(address) {
2407 } 2431 }
2408 2432
2409 NetworkInterface::~NetworkInterface() { 2433 NetworkInterface::~NetworkInterface() {
2410 } 2434 }
2411 2435
2412 } // namespace net 2436 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/base/net_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698