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

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: Changed to using hyphen replacement again 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 | no next file » | 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(const STR& input,
163 const typename STR::value_type
jschuh 2011/08/17 20:02:48 Formatting is all messed up here. Make sure visual
164 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;
1428
jschuh 2011/08/17 20:02:48 Remove the blank line.
1412 // about: and data: URLs don't have file names, but esp. data: URLs may 1429 // 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). 1430 // 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. 1431 // Therefore we don't attempt to divine a file name out of them.
1415 if (url.SchemeIs("about") || url.SchemeIs("data")) { 1432 if (url.SchemeIs("about") || url.SchemeIs("data")) {
1416 return default_name.empty() ? ASCIIToUTF16(kFinalFallbackName) 1433 return default_name.empty() ? ASCIIToUTF16(kFinalFallbackName)
1417 : default_name; 1434 : default_name;
1418 } 1435 }
1419 1436
1420 if (url.is_valid()) { 1437 if (url.is_valid()) {
1421 const std::string unescaped_url_filename = UnescapeURLComponent( 1438 const std::string unescaped_url_filename = UnescapeURLComponent(
(...skipping 10 matching lines...) Expand all
1432 &decoded_filename); 1449 &decoded_filename);
1433 } 1450 }
1434 1451
1435 filename = decoded_filename; 1452 filename = decoded_filename;
1436 } 1453 }
1437 } 1454 }
1438 1455
1439 #if defined(OS_WIN) 1456 #if defined(OS_WIN)
1440 { // Handle CreateFile() stripping trailing dots and spaces on filenames 1457 { // Handle CreateFile() stripping trailing dots and spaces on filenames
1441 // http://support.microsoft.com/kb/115827 1458 // http://support.microsoft.com/kb/115827
1459 trimmed_trailing_character_count += CountTrailingChars(filename, " .");
jschuh 2011/08/17 20:02:48 You already have the count, so no reason to duplic
kenrb 2011/08/17 20:25:51 The filename may have changed since the last time
jschuh 2011/08/17 20:32:46 To clarify, you just called your CountTrailingChar
1442 std::string::size_type pos = filename.find_last_not_of(" ."); 1460 std::string::size_type pos = filename.find_last_not_of(" .");
1443 if (pos == std::string::npos) 1461 if (pos == std::string::npos)
1444 filename.resize(0); 1462 filename.resize(0);
1445 else 1463 else
1446 filename.resize(++pos); 1464 filename.resize(++pos);
1447 } 1465 }
1448 #endif 1466 #endif
1449 // Trim '.' once more. 1467 // Trim '.' once more.
1450 TrimString(filename, ".", &filename); 1468 TrimString(filename, ".", &filename);
1451 1469
1452 // If there's no filename or it gets trimed to be empty, use 1470 // If there's no filename or it gets trimed to be empty, use
1453 // the URL hostname or default_name 1471 // the URL hostname or default_name
1454 if (filename.empty()) { 1472 if (filename.empty()) {
1473 trimmed_trailing_character_count = 0;
1455 if (!default_name.empty()) { 1474 if (!default_name.empty()) {
1456 return default_name; 1475 return default_name;
1457 } else if (url.is_valid()) { 1476 } else if (url.is_valid()) {
1458 // Some schemes (e.g. file) do not have a hostname. Even though it's 1477 // 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. 1478 // not likely to reach here, let's hardcode the last fallback name.
1460 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) 1479 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451)
1461 filename = url.host().empty() ? kFinalFallbackName : url.host(); 1480 filename = url.host().empty() ? kFinalFallbackName : url.host();
1462 } else { 1481 } else {
1463 NOTREACHED(); 1482 NOTREACHED();
1464 } 1483 }
1465 } 1484 }
1466 1485
1467 #if defined(OS_WIN) 1486 #if defined(OS_WIN)
1468 string16 path = UTF8ToUTF16(filename); 1487 string16 path = UTF8ToUTF16(filename);
1488 // On Windows we want to preserve or replace all characters including
1489 // whitespace to prevent file extension obfuscation on trusted websites
1490 // e.g. Gmail might think evil.exe. is safe, so we don't want it to become
1491 // evil.exe when we download it
1492 trimmed_trailing_character_count += path.length();
1493 TrimWhitespace(path, TRIM_TRAILING, &path);
jschuh 2011/08/17 20:02:48 Shouldn't this check include trailing . characters
kenrb 2011/08/17 20:25:51 Trailing dots should have been stripped at line 14
1494 trimmed_trailing_character_count -= path.length();
1469 file_util::ReplaceIllegalCharactersInPath(&path, '-'); 1495 file_util::ReplaceIllegalCharactersInPath(&path, '-');
1496 path.append(trimmed_trailing_character_count, '-');
1470 return path; 1497 return path;
1471 #else 1498 #else
1472 std::string path = filename; 1499 std::string path = filename;
1473 file_util::ReplaceIllegalCharactersInPath(&path, '-'); 1500 file_util::ReplaceIllegalCharactersInPath(&path, '-');
1474 return UTF8ToUTF16(path); 1501 return UTF8ToUTF16(path);
1475 #endif 1502 #endif
1476 } 1503 }
1477 1504
1478 FilePath GenerateFileName(const GURL& url, 1505 FilePath GenerateFileName(const GURL& url,
1479 const std::string& content_disposition, 1506 const std::string& content_disposition,
(...skipping 923 matching lines...) Expand 10 before | Expand all | Expand 10 after
2403 2430
2404 NetworkInterface::NetworkInterface(const std::string& name, 2431 NetworkInterface::NetworkInterface(const std::string& name,
2405 const IPAddressNumber& address) 2432 const IPAddressNumber& address)
2406 : name(name), address(address) { 2433 : name(name), address(address) {
2407 } 2434 }
2408 2435
2409 NetworkInterface::~NetworkInterface() { 2436 NetworkInterface::~NetworkInterface() {
2410 } 2437 }
2411 2438
2412 } // namespace net 2439 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698