 Chromium Code Reviews
 Chromium Code Reviews 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
    
  
    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| OLD | NEW | 
|---|---|
| 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 Loading... | |
| 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 std::string::size_type CountTrailingChars( | |
| 162 const std::string input, | |
| 163 const std::string::value_type trailing_chars[]) { | |
| 164 const std::string::size_type last_good_char = | |
| 165 input.find_last_not_of(trailing_chars); | |
| 166 | |
| 167 if (last_good_char == std::string::npos) | |
| 168 return input.length(); | |
| 169 else | |
| 170 return input.length() - last_good_char - 1; | |
| 171 } | |
| 172 | |
| 161 // Similar to Base64Decode. Decodes a Q-encoded string to a sequence | 173 // Similar to Base64Decode. Decodes a Q-encoded string to a sequence | 
| 162 // of bytes. If input is invalid, return false. | 174 // of bytes. If input is invalid, return false. | 
| 163 bool QPDecode(const std::string& input, std::string* output) { | 175 bool QPDecode(const std::string& input, std::string* output) { | 
| 164 std::string temp; | 176 std::string temp; | 
| 165 temp.reserve(input.size()); | 177 temp.reserve(input.size()); | 
| 166 std::string::const_iterator it = input.begin(); | 178 std::string::const_iterator it = input.begin(); | 
| 167 while (it != input.end()) { | 179 while (it != input.end()) { | 
| 168 if (*it == '_') { | 180 if (*it == '_') { | 
| 169 temp.push_back(' '); | 181 temp.push_back(' '); | 
| 170 } else if (*it == '=') { | 182 } else if (*it == '=') { | 
| (...skipping 1280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1451 | 1463 | 
| 1452 // Finally try the URL hostname, but only if there's no default specified in | 1464 // Finally try the URL hostname, but only if there's no default specified in | 
| 1453 // |default_name|. Some schemes (e.g.: file:, about:, data:) do not have a | 1465 // |default_name|. Some schemes (e.g.: file:, about:, data:) do not have a | 
| 1454 // host name. | 1466 // host name. | 
| 1455 if (filename.empty() && default_name.empty() && | 1467 if (filename.empty() && default_name.empty() && | 
| 1456 url.is_valid() && !url.host().empty()) { | 1468 url.is_valid() && !url.host().empty()) { | 
| 1457 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) | 1469 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) | 
| 1458 filename = url.host(); | 1470 filename = url.host(); | 
| 1459 } | 1471 } | 
| 1460 | 1472 | 
| 1473 #if defined(OS_WIN) | |
| 1474 std::string::size_type trimmed_trailing_character_count = | |
| 1475 CountTrailingChars(filename, " ."); | |
| 1476 #endif | |
| 1461 SanitizeGeneratedFileName(filename); | 1477 SanitizeGeneratedFileName(filename); | 
| 1462 // Sanitization can cause the filename to disappear (e.g.: if the filename | 1478 // Sanitization can cause the filename to disappear (e.g.: if the filename | 
| 1463 // consisted entirely of spaces and '.'s), in which case we use the default. | 1479 // consisted entirely of spaces and '.'s), in which case we use the default. | 
| 1464 if (filename.empty() && default_name.empty()) | 1480 if (filename.empty()) { | 
| 1465 filename = kFinalFallbackName; | 1481 #if defined(OS_WIN) | 
| 1482 trimmed_trailing_character_count = 0; | |
| 1483 #endif | |
| 1484 if (default_name.empty()) | |
| 1485 filename = kFinalFallbackName; | |
| 1486 } | |
| 1466 | 1487 | 
| 1467 #if defined(OS_WIN) | 1488 #if defined(OS_WIN) | 
| 1468 string16 path = (filename.empty())? default_name : UTF8ToUTF16(filename); | 1489 string16 path = (filename.empty())? default_name : UTF8ToUTF16(filename); | 
| 1490 // On Windows we want to preserve or replace all characters including | |
| 1491 // whitespace to prevent file extension obfuscation on trusted websites | |
| 1492 // e.g. Gmail might think evil.exe. is safe, so we don't want it to become | |
| 
asanka
2011/08/22 18:01:31
What about ".evil.exe"?
 | |
| 1493 // evil.exe when we download it | |
| 1494 std::wstring::size_type path_length_before_trim = path.length(); | |
| 1495 TrimWhitespace(path, TRIM_TRAILING, &path); | |
| 1496 trimmed_trailing_character_count += path_length_before_trim - path.length(); | |
| 1469 file_util::ReplaceIllegalCharactersInPath(&path, '-'); | 1497 file_util::ReplaceIllegalCharactersInPath(&path, '-'); | 
| 1498 path.append(trimmed_trailing_character_count, '-'); | |
| 1470 FilePath result(path); | 1499 FilePath result(path); | 
| 1471 GenerateSafeFileName(mime_type, &result); | 1500 GenerateSafeFileName(mime_type, &result); | 
| 1472 return result.value(); | 1501 return result.value(); | 
| 1473 #else | 1502 #else | 
| 1474 std::string path = (filename.empty())? UTF16ToUTF8(default_name) : filename; | 1503 std::string path = (filename.empty())? UTF16ToUTF8(default_name) : filename; | 
| 1475 file_util::ReplaceIllegalCharactersInPath(&path, '-'); | 1504 file_util::ReplaceIllegalCharactersInPath(&path, '-'); | 
| 1476 FilePath result(path); | 1505 FilePath result(path); | 
| 1477 GenerateSafeFileName(mime_type, &result); | 1506 GenerateSafeFileName(mime_type, &result); | 
| 1478 return UTF8ToUTF16(result.value()); | 1507 return UTF8ToUTF16(result.value()); | 
| 1479 #endif | 1508 #endif | 
| (...skipping 921 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2401 | 2430 | 
| 2402 NetworkInterface::NetworkInterface(const std::string& name, | 2431 NetworkInterface::NetworkInterface(const std::string& name, | 
| 2403 const IPAddressNumber& address) | 2432 const IPAddressNumber& address) | 
| 2404 : name(name), address(address) { | 2433 : name(name), address(address) { | 
| 2405 } | 2434 } | 
| 2406 | 2435 | 
| 2407 NetworkInterface::~NetworkInterface() { | 2436 NetworkInterface::~NetworkInterface() { | 
| 2408 } | 2437 } | 
| 2409 | 2438 | 
| 2410 } // namespace net | 2439 } // namespace net | 
| OLD | NEW |