OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <map> | 8 #include <map> |
9 #include <unicode/regex.h> | 9 #include <unicode/regex.h> |
10 #include <unicode/ucnv.h> | 10 #include <unicode/ucnv.h> |
(...skipping 1359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1370 | 1370 |
1371 return result; | 1371 return result; |
1372 } | 1372 } |
1373 | 1373 |
1374 string16 StripWWW(const string16& text) { | 1374 string16 StripWWW(const string16& text) { |
1375 const string16 www(ASCIIToUTF16("www.")); | 1375 const string16 www(ASCIIToUTF16("www.")); |
1376 return (text.compare(0, www.length(), www) == 0) ? | 1376 return (text.compare(0, www.length(), www) == 0) ? |
1377 text.substr(www.length()) : text; | 1377 text.substr(www.length()) : text; |
1378 } | 1378 } |
1379 | 1379 |
1380 FilePath GetSuggestedFilename(const GURL& url, | 1380 string16 GetSuggestedFilename(const GURL& url, |
1381 const std::string& content_disposition, | 1381 const std::string& content_disposition, |
1382 const std::string& referrer_charset, | 1382 const std::string& referrer_charset, |
1383 const FilePath& default_name) { | 1383 const string16& default_name) { |
| 1384 // TODO: this function to be updated to match the httpbis recommendations. |
| 1385 // Talk to abarth for the latest news. |
| 1386 |
1384 // We don't translate this fallback string, "download". If localization is | 1387 // We don't translate this fallback string, "download". If localization is |
1385 // needed, the caller should provide localized fallback default_name. | 1388 // needed, the caller should provide localized fallback default_name. |
1386 static const FilePath::CharType kFinalFallbackName[] = | 1389 static const char* kFinalFallbackName = "download"; |
1387 FILE_PATH_LITERAL("download"); | |
1388 | 1390 |
1389 // about: and data: URLs don't have file names, but esp. data: URLs may | 1391 // about: and data: URLs don't have file names, but esp. data: URLs may |
1390 // contain parts that look like ones (i.e., contain a slash). | 1392 // contain parts that look like ones (i.e., contain a slash). |
1391 // Therefore we don't attempt to divine a file name out of them. | 1393 // Therefore we don't attempt to divine a file name out of them. |
1392 if (url.SchemeIs("about") || url.SchemeIs("data")) { | 1394 if (url.SchemeIs("about") || url.SchemeIs("data")) { |
1393 return default_name.empty() ? FilePath(kFinalFallbackName) : default_name; | 1395 return default_name.empty() ? ASCIIToUTF16(kFinalFallbackName) |
| 1396 : default_name; |
1394 } | 1397 } |
1395 | 1398 |
1396 const std::string filename_from_cd = GetFileNameFromCD(content_disposition, | 1399 std::string filename = GetFileNameFromCD(content_disposition, |
1397 referrer_charset); | 1400 referrer_charset); |
1398 #if defined(OS_WIN) | |
1399 FilePath::StringType filename = UTF8ToWide(filename_from_cd); | |
1400 #elif defined(OS_POSIX) | |
1401 FilePath::StringType filename = filename_from_cd; | |
1402 #endif | |
1403 | 1401 |
1404 if (!filename.empty()) { | 1402 if (!filename.empty()) { |
1405 // Remove any path information the server may have sent, take the name | 1403 // Remove any path information the server may have sent, take the name |
1406 // only. | 1404 // only. |
1407 filename = FilePath(filename).BaseName().value(); | 1405 std::string::size_type slashpos = filename.find_last_of("/\\"); |
| 1406 if (slashpos != std::string::npos) |
| 1407 filename = filename.substr(slashpos + 1); |
1408 | 1408 |
1409 // Next, remove "." from the beginning and end of the file name to avoid | 1409 // Next, remove "." from the beginning and end of the file name to avoid |
1410 // tricks with hidden files, "..", and "." | 1410 // tricks with hidden files, "..", and "." |
1411 TrimString(filename, FILE_PATH_LITERAL("."), &filename); | 1411 TrimString(filename, ".", &filename); |
1412 } | 1412 } |
1413 if (filename.empty()) { | 1413 if (filename.empty()) { |
1414 if (url.is_valid()) { | 1414 if (url.is_valid()) { |
1415 const std::string unescaped_url_filename = UnescapeURLComponent( | 1415 const std::string unescaped_url_filename = UnescapeURLComponent( |
1416 url.ExtractFileName(), | 1416 url.ExtractFileName(), |
1417 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); | 1417 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); |
1418 | 1418 |
1419 // The URL's path should be escaped UTF-8, but may not be. | 1419 // The URL's path should be escaped UTF-8, but may not be. |
1420 std::string decoded_filename = unescaped_url_filename; | 1420 std::string decoded_filename = unescaped_url_filename; |
1421 if (!IsStringASCII(decoded_filename)) { | 1421 if (!IsStringASCII(decoded_filename)) { |
1422 bool ignore; | 1422 bool ignore; |
1423 // TODO(jshin): this is probably not robust enough. To be sure, we | 1423 // TODO(jshin): this is probably not robust enough. To be sure, we |
1424 // need encoding detection. | 1424 // need encoding detection. |
1425 DecodeWord(unescaped_url_filename, referrer_charset, &ignore, | 1425 DecodeWord(unescaped_url_filename, referrer_charset, &ignore, |
1426 &decoded_filename); | 1426 &decoded_filename); |
1427 } | 1427 } |
1428 | 1428 |
1429 #if defined(OS_WIN) | |
1430 filename = UTF8ToWide(decoded_filename); | |
1431 #elif defined(OS_POSIX) | |
1432 filename = decoded_filename; | 1429 filename = decoded_filename; |
1433 #endif | |
1434 } | 1430 } |
1435 } | 1431 } |
1436 | 1432 |
1437 #if defined(OS_WIN) | 1433 #if defined(OS_WIN) |
1438 { // Handle CreateFile() stripping trailing dots and spaces on filenames | 1434 { // Handle CreateFile() stripping trailing dots and spaces on filenames |
1439 // http://support.microsoft.com/kb/115827 | 1435 // http://support.microsoft.com/kb/115827 |
1440 std::string::size_type pos = filename.find_last_not_of(L" ."); | 1436 std::string::size_type pos = filename.find_last_not_of(" ."); |
1441 if (pos == std::string::npos) | 1437 if (pos == std::string::npos) |
1442 filename.resize(0); | 1438 filename.resize(0); |
1443 else | 1439 else |
1444 filename.resize(++pos); | 1440 filename.resize(++pos); |
1445 } | 1441 } |
1446 #endif | 1442 #endif |
1447 // Trim '.' once more. | 1443 // Trim '.' once more. |
1448 TrimString(filename, FILE_PATH_LITERAL("."), &filename); | 1444 TrimString(filename, ".", &filename); |
1449 | 1445 |
1450 // If there's no filename or it gets trimed to be empty, use | 1446 // If there's no filename or it gets trimed to be empty, use |
1451 // the URL hostname or default_name | 1447 // the URL hostname or default_name |
1452 if (filename.empty()) { | 1448 if (filename.empty()) { |
1453 if (!default_name.empty()) { | 1449 if (!default_name.empty()) { |
1454 filename = default_name.value(); | 1450 return default_name; |
1455 } else if (url.is_valid()) { | 1451 } else if (url.is_valid()) { |
1456 // Some schemes (e.g. file) do not have a hostname. Even though it's | 1452 // Some schemes (e.g. file) do not have a hostname. Even though it's |
1457 // not likely to reach here, let's hardcode the last fallback name. | 1453 // not likely to reach here, let's hardcode the last fallback name. |
1458 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) | 1454 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) |
1459 filename = url.host().empty() ? kFinalFallbackName : | 1455 filename = url.host().empty() ? kFinalFallbackName : url.host(); |
1460 #if defined(OS_WIN) | |
1461 UTF8ToWide(url.host()); | |
1462 #elif defined(OS_POSIX) | |
1463 url.host(); | |
1464 #endif | |
1465 } else { | 1456 } else { |
1466 NOTREACHED(); | 1457 NOTREACHED(); |
1467 } | 1458 } |
1468 } | 1459 } |
1469 | 1460 |
1470 file_util::ReplaceIllegalCharactersInPath(&filename, '-'); | 1461 #if defined(OS_WIN) |
1471 return FilePath(filename); | 1462 string16 path = UTF8ToUTF16(filename); |
| 1463 file_util::ReplaceIllegalCharactersInPath(&path, '-'); |
| 1464 return path; |
| 1465 #else |
| 1466 std::string path = filename; |
| 1467 file_util::ReplaceIllegalCharactersInPath(&path, '-'); |
| 1468 return UTF8ToUTF16(path); |
| 1469 #endif |
1472 } | 1470 } |
1473 | 1471 |
1474 bool IsPortAllowedByDefault(int port) { | 1472 bool IsPortAllowedByDefault(int port) { |
1475 int array_size = arraysize(kRestrictedPorts); | 1473 int array_size = arraysize(kRestrictedPorts); |
1476 for (int i = 0; i < array_size; i++) { | 1474 for (int i = 0; i < array_size; i++) { |
1477 if (kRestrictedPorts[i] == port) { | 1475 if (kRestrictedPorts[i] == port) { |
1478 return false; | 1476 return false; |
1479 } | 1477 } |
1480 } | 1478 } |
1481 return true; | 1479 return true; |
(...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2060 } | 2058 } |
2061 | 2059 |
2062 int GetPortFromAddrinfo(const struct addrinfo* info) { | 2060 int GetPortFromAddrinfo(const struct addrinfo* info) { |
2063 uint16* port_field = GetPortFieldFromAddrinfo(info); | 2061 uint16* port_field = GetPortFieldFromAddrinfo(info); |
2064 if (!port_field) | 2062 if (!port_field) |
2065 return -1; | 2063 return -1; |
2066 return ntohs(*port_field); | 2064 return ntohs(*port_field); |
2067 } | 2065 } |
2068 | 2066 |
2069 } // namespace net | 2067 } // namespace net |
OLD | NEW |