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

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 filename char replacement to percent encoding. 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 // This list should generally match the one in string_util.cc
162 const char16 kWhitespaceUTF16[] = {
jschuh 2011/08/17 14:38:46 Seems like we should add missed whitespace charact
163 0x0009, /* <control-0009> to <control-000D> */ \
164 0x000A, \
165 0x000B, \
166 0x000C, \
167 0x000D, \
168 0x0020, /* Space */ \
169 0x0085, /* <control-0085> */ \
170 0x00A0, /* No-Break Space */ \
171 0x1680, /* Ogham Space Mark */ \
172 0x180E, /* Mongolian Vowel Separator */ \
173 0x2000, /* En Quad to Hair Space */ \
174 0x2001, \
175 0x2002, \
176 0x2003, \
177 0x2004, \
178 0x2005, \
179 0x2006, \
180 0x2007, \
181 0x2008, \
182 0x2009, \
183 0x200A, \
184 0x200C, /* Zero Width Non-Joiner */ \
185 0x2028, /* Line Separator */ \
186 0x2029, /* Paragraph Separator */ \
187 0x202F, /* Narrow No-Break Space */ \
188 0x205F, /* Medium Mathematical Space */ \
189 0x3000, /* Ideographic Space */ \
190 0
191 };
192
193 static const char* const kHexString = "0123456789ABCDEF";
194 inline char IntToHex(int i) {
195 return kHexString[i & 0xf];
196 }
197
198 std::string EscapeCharsInString(const std::string& input) {
199 std::string escaped_string;
200
201 escaped_string.reserve(input.length() * 3);
202 for (unsigned int i = 0; i < input.length(); i++) {
203 unsigned char c = static_cast<unsigned char>(input[i]);
204 escaped_string.push_back('%');
205 escaped_string.push_back(IntToHex(c >> 4));
206 escaped_string.push_back(IntToHex(c & 0xf));
207 }
208 return escaped_string;
209 }
210
211 string16 EscapeCharsInString(const string16& input) {
212 string16 escaped_string;
213
214 escaped_string.reserve(input.length() * 6);
215 for (unsigned int i = 0; i < input.length(); i++) {
216 char16 c = static_cast<char16>(input[i]);
217 escaped_string.push_back('%');
218 escaped_string.push_back('u');
219 escaped_string.push_back(IntToHex(c >> 12));
220 escaped_string.push_back(IntToHex((c >> 8) & 0xf));
221 escaped_string.push_back(IntToHex((c >> 4) & 0xf));
222 escaped_string.push_back(IntToHex(c & 0xf));
223 }
224 return escaped_string;
225 }
226
227 template<typename STR>
228 void EscapeTrimChars(const STR& input,
229 const typename STR::value_type escape_chars[],
230 TrimPositions positions,
231 STR* output) {
232 // Find the edges of leading/trailing whitespace as desired.
233 const typename STR::size_type last_char = input.length() - 1;
234 const typename STR::size_type first_good_char = (positions & TRIM_LEADING) ?
235 input.find_first_not_of(escape_chars) : 0;
236 const typename STR::size_type last_good_char = (positions & TRIM_TRAILING) ?
237 input.find_last_not_of(escape_chars) : last_char;
238 STR temp_string;
239
240 if (input.empty())
241 return;
242
243 if ((first_good_char == STR::npos) || (last_good_char == STR::npos)) {
244 // Escapery magic on whole string
245 return;
246 }
247
248 temp_string = EscapeCharsInString(input.substr(0,first_good_char));
249 temp_string += input.substr(first_good_char, last_good_char -
250 first_good_char + 1);
251 if (last_good_char < last_char)
252 temp_string += EscapeCharsInString(input.substr(last_good_char+1));
253 *output = temp_string;
254
255 return;
256 }
257
161 // Similar to Base64Decode. Decodes a Q-encoded string to a sequence 258 // Similar to Base64Decode. Decodes a Q-encoded string to a sequence
162 // of bytes. If input is invalid, return false. 259 // of bytes. If input is invalid, return false.
163 bool QPDecode(const std::string& input, std::string* output) { 260 bool QPDecode(const std::string& input, std::string* output) {
164 std::string temp; 261 std::string temp;
165 temp.reserve(input.size()); 262 temp.reserve(input.size());
166 std::string::const_iterator it = input.begin(); 263 std::string::const_iterator it = input.begin();
167 while (it != input.end()) { 264 while (it != input.end()) {
168 if (*it == '_') { 265 if (*it == '_') {
169 temp.push_back(' '); 266 temp.push_back(' ');
170 } else if (*it == '=') { 267 } else if (*it == '=') {
(...skipping 1227 matching lines...) Expand 10 before | Expand all | Expand 10 after
1398 filename = suggested_name; 1495 filename = suggested_name;
1399 1496
1400 if (!filename.empty()) { 1497 if (!filename.empty()) {
1401 // Replace any path information the server may have sent, by changing 1498 // Replace any path information the server may have sent, by changing
1402 // path separators with underscores. 1499 // path separators with underscores.
1403 ReplaceSubstringsAfterOffset(&filename, 0, "/", "_"); 1500 ReplaceSubstringsAfterOffset(&filename, 0, "/", "_");
1404 ReplaceSubstringsAfterOffset(&filename, 0, "\\", "_"); 1501 ReplaceSubstringsAfterOffset(&filename, 0, "\\", "_");
1405 1502
1406 // Next, remove "." from the beginning and end of the file name to avoid 1503 // Next, remove "." from the beginning and end of the file name to avoid
1407 // tricks with hidden files, "..", and "." 1504 // tricks with hidden files, "..", and "."
1505 #if defined(OS_WIN)
1506 EscapeTrimChars(filename, ".", TRIM_ALL, &filename);
1507 #else
1408 TrimString(filename, ".", &filename); 1508 TrimString(filename, ".", &filename);
1509 #endif
1409 } 1510 }
1410 1511
1411 if (filename.empty()) { 1512 if (filename.empty()) {
1412 // about: and data: URLs don't have file names, but esp. data: URLs may 1513 // 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). 1514 // 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. 1515 // Therefore we don't attempt to divine a file name out of them.
1415 if (url.SchemeIs("about") || url.SchemeIs("data")) { 1516 if (url.SchemeIs("about") || url.SchemeIs("data")) {
1416 return default_name.empty() ? ASCIIToUTF16(kFinalFallbackName) 1517 return default_name.empty() ? ASCIIToUTF16(kFinalFallbackName)
1417 : default_name; 1518 : default_name;
1418 } 1519 }
(...skipping 13 matching lines...) Expand all
1432 &decoded_filename); 1533 &decoded_filename);
1433 } 1534 }
1434 1535
1435 filename = decoded_filename; 1536 filename = decoded_filename;
1436 } 1537 }
1437 } 1538 }
1438 1539
1439 #if defined(OS_WIN) 1540 #if defined(OS_WIN)
1440 { // Handle CreateFile() stripping trailing dots and spaces on filenames 1541 { // Handle CreateFile() stripping trailing dots and spaces on filenames
1441 // http://support.microsoft.com/kb/115827 1542 // http://support.microsoft.com/kb/115827
1442 std::string::size_type pos = filename.find_last_not_of(" ."); 1543 EscapeTrimChars(filename, " .", TRIM_TRAILING, &filename);
1443 if (pos == std::string::npos)
1444 filename.resize(0);
1445 else
1446 filename.resize(++pos);
1447 } 1544 }
1448 #endif 1545 #endif
1449 // Trim '.' once more. 1546 // Trim '.' once more.
1450 TrimString(filename, ".", &filename); 1547 TrimString(filename, ".", &filename);
1451 1548
1452 // If there's no filename or it gets trimed to be empty, use 1549 // If there's no filename or it gets trimed to be empty, use
1453 // the URL hostname or default_name 1550 // the URL hostname or default_name
1454 if (filename.empty()) { 1551 if (filename.empty()) {
1455 if (!default_name.empty()) { 1552 if (!default_name.empty()) {
1456 return default_name; 1553 return default_name;
1457 } else if (url.is_valid()) { 1554 } else if (url.is_valid()) {
1458 // Some schemes (e.g. file) do not have a hostname. Even though it's 1555 // 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. 1556 // not likely to reach here, let's hardcode the last fallback name.
1460 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) 1557 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451)
1461 filename = url.host().empty() ? kFinalFallbackName : url.host(); 1558 filename = url.host().empty() ? kFinalFallbackName : url.host();
1462 } else { 1559 } else {
1463 NOTREACHED(); 1560 NOTREACHED();
1464 } 1561 }
1465 } 1562 }
1466 1563
1467 #if defined(OS_WIN) 1564 #if defined(OS_WIN)
1468 string16 path = UTF8ToUTF16(filename); 1565 string16 path = UTF8ToUTF16(filename);
1566 // On Windows we want to preserve or replace all characters including
1567 // whitespace to prevent file extension obfuscation on trusted websites
1568 // e.g. Gmail might think evil.exe. is safe, so we don't want it to become
1569 // evil.exe when we download it
1570 EscapeTrimChars(path, kWhitespaceUTF16, TRIM_ALL, &path);
1469 file_util::ReplaceIllegalCharactersInPath(&path, '-'); 1571 file_util::ReplaceIllegalCharactersInPath(&path, '-');
1470 return path; 1572 return path;
1471 #else 1573 #else
1472 std::string path = filename; 1574 std::string path = filename;
1473 file_util::ReplaceIllegalCharactersInPath(&path, '-'); 1575 file_util::ReplaceIllegalCharactersInPath(&path, '-');
1474 return UTF8ToUTF16(path); 1576 return UTF8ToUTF16(path);
1475 #endif 1577 #endif
1476 } 1578 }
1477 1579
1478 FilePath GenerateFileName(const GURL& url, 1580 FilePath GenerateFileName(const GURL& url,
(...skipping 924 matching lines...) Expand 10 before | Expand all | Expand 10 after
2403 2505
2404 NetworkInterface::NetworkInterface(const std::string& name, 2506 NetworkInterface::NetworkInterface(const std::string& name,
2405 const IPAddressNumber& address) 2507 const IPAddressNumber& address)
2406 : name(name), address(address) { 2508 : name(name), address(address) {
2407 } 2509 }
2408 2510
2409 NetworkInterface::~NetworkInterface() { 2511 NetworkInterface::~NetworkInterface() {
2410 } 2512 }
2411 2513
2412 } // namespace net 2514 } // 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