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

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

Issue 271056: Do some cleanup of file path name handling. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 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 | « net/base/net_util.h ('k') | 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) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/ucnv.h> 9 #include <unicode/ucnv.h>
10 #include <unicode/uidna.h> 10 #include <unicode/uidna.h>
(...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 new_parsed->port.begin += kViewSourceLengthPlus1; 742 new_parsed->port.begin += kViewSourceLengthPlus1;
743 if (new_parsed->path.is_nonempty()) 743 if (new_parsed->path.is_nonempty())
744 new_parsed->path.begin += kViewSourceLengthPlus1; 744 new_parsed->path.begin += kViewSourceLengthPlus1;
745 if (new_parsed->query.is_nonempty()) 745 if (new_parsed->query.is_nonempty())
746 new_parsed->query.begin += kViewSourceLengthPlus1; 746 new_parsed->query.begin += kViewSourceLengthPlus1;
747 if (new_parsed->ref.is_nonempty()) 747 if (new_parsed->ref.is_nonempty())
748 new_parsed->ref.begin += kViewSourceLengthPlus1; 748 new_parsed->ref.begin += kViewSourceLengthPlus1;
749 return result; 749 return result;
750 } 750 }
751 751
752 // Converts a UTF-8 string to a FilePath string type.
753 //
754 // This is inline with the hope that the function will be "free" on non-Windows
755 // platforms.
756 inline FilePath::StringType UTF8ToFilePathString(const std::string& utf8) {
757 #if defined(OS_WIN)
758 return FilePath::StringType(UTF8ToUTF16(utf8));
759 #else
760 return utf8;
761 #endif
762 }
763
752 } // namespace 764 } // namespace
753 765
754 namespace net { 766 namespace net {
755 767
756 std::set<int> explicitly_allowed_ports; 768 std::set<int> explicitly_allowed_ports;
757 769
758 // Appends the substring |in_component| inside of the URL |spec| to |output|, 770 // Appends the substring |in_component| inside of the URL |spec| to |output|,
759 // and the resulting range will be filled into |out_component|. |unescape_rules| 771 // and the resulting range will be filled into |out_component|. |unescape_rules|
760 // defines how to clean the URL for human readability. 772 // defines how to clean the URL for human readability.
761 static void AppendFormattedComponent(const std::string& spec, 773 static void AppendFormattedComponent(const std::string& spec,
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 std::wstring GetSpecificHeader(const std::wstring& headers, 810 std::wstring GetSpecificHeader(const std::wstring& headers,
799 const std::wstring& name) { 811 const std::wstring& name) {
800 return GetSpecificHeaderT(headers, name); 812 return GetSpecificHeaderT(headers, name);
801 } 813 }
802 814
803 std::string GetSpecificHeader(const std::string& headers, 815 std::string GetSpecificHeader(const std::string& headers,
804 const std::string& name) { 816 const std::string& name) {
805 return GetSpecificHeaderT(headers, name); 817 return GetSpecificHeaderT(headers, name);
806 } 818 }
807 819
808 std::wstring GetFileNameFromCD(const std::string& header, 820 std::string GetFileNameFromCD(const std::string& header,
809 const std::string& referrer_charset) { 821 const std::string& referrer_charset) {
810 std::string param_value = GetHeaderParamValue(header, "filename"); 822 std::string param_value = GetHeaderParamValue(header, "filename");
811 if (param_value.empty()) { 823 if (param_value.empty()) {
812 // Some servers use 'name' parameter. 824 // Some servers use 'name' parameter.
813 param_value = GetHeaderParamValue(header, "name"); 825 param_value = GetHeaderParamValue(header, "name");
814 } 826 }
815 if (param_value.empty()) 827 if (param_value.empty())
816 return std::wstring(); 828 return std::string();
817 std::string decoded; 829 std::string decoded;
818 if (DecodeParamValue(param_value, referrer_charset, &decoded)) 830 if (DecodeParamValue(param_value, referrer_charset, &decoded))
819 return UTF8ToWide(decoded); 831 return decoded;
820 return std::wstring(); 832 return std::string();
821 } 833 }
822 834
823 std::wstring GetHeaderParamValue(const std::wstring& field, 835 std::wstring GetHeaderParamValue(const std::wstring& field,
824 const std::wstring& param_name) { 836 const std::wstring& param_name) {
825 return GetHeaderParamValueT(field, param_name); 837 return GetHeaderParamValueT(field, param_name);
826 } 838 }
827 839
828 std::string GetHeaderParamValue(const std::string& field, 840 std::string GetHeaderParamValue(const std::string& field,
829 const std::string& param_name) { 841 const std::string& param_name) {
830 return GetHeaderParamValueT(field, param_name); 842 return GetHeaderParamValueT(field, param_name);
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
1025 1037
1026 return result; 1038 return result;
1027 } 1039 }
1028 1040
1029 std::wstring StripWWW(const std::wstring& text) { 1041 std::wstring StripWWW(const std::wstring& text) {
1030 const std::wstring www(L"www."); 1042 const std::wstring www(L"www.");
1031 return (text.compare(0, www.length(), www) == 0) ? 1043 return (text.compare(0, www.length(), www) == 0) ?
1032 text.substr(www.length()) : text; 1044 text.substr(www.length()) : text;
1033 } 1045 }
1034 1046
1035 std::wstring GetSuggestedFilename(const GURL& url, 1047 FilePath GetSuggestedFilename(const GURL& url,
1036 const std::string& content_disposition, 1048 const std::string& content_disposition,
1037 const std::string& referrer_charset, 1049 const std::string& referrer_charset,
1038 const std::wstring& default_name) { 1050 const char* default_name) {
1039 // TODO(rolandsteiner): as pointed out by darin in the code review, this is 1051 // TODO(rolandsteiner): as pointed out by darin in the code review, this is
1040 // hardly ideal. "download" should be translated, or another solution found. 1052 // hardly ideal. "download" should be translated, or another solution found.
1041 // (cf. http://code.google.com/p/chromium/issues/detail?id=25289) 1053 // (cf. http://code.google.com/p/chromium/issues/detail?id=25289)
1042 const wchar_t kFinalFallbackName[] = L"download"; 1054 const char kFinalFallbackName[] = "download";
1043 1055
1044 // about: and data: URLs don't have file names, but esp. data: URLs may 1056 // about: and data: URLs don't have file names, but esp. data: URLs may
1045 // contain parts that look like ones (i.e., contain a slash). 1057 // contain parts that look like ones (i.e., contain a slash).
1046 // Therefore we don't attempt to divine a file name out of them. 1058 // Therefore we don't attempt to divine a file name out of them.
1047 if (url.SchemeIs("about") || url.SchemeIs("data")) 1059 if (url.SchemeIs("about") || url.SchemeIs("data")) {
1048 return default_name.empty() ? std::wstring(kFinalFallbackName) 1060 return FilePath(UTF8ToFilePathString(
1049 : default_name; 1061 default_name && default_name[0] ? default_name : kFinalFallbackName));
1062 }
1050 1063
1051 std::wstring filename = GetFileNameFromCD(content_disposition, 1064 std::string filename = GetFileNameFromCD(content_disposition,
1052 referrer_charset); 1065 referrer_charset);
1053 if (!filename.empty()) { 1066 if (!filename.empty()) {
1054 // Remove any path information the server may have sent, take the name 1067 // Remove any path information the server may have sent, take the name
1055 // only. 1068 // only.
1056 filename = file_util::GetFilenameFromPath(filename); 1069 #if defined(OS_WIN)
1070 filename = UTF16ToUTF8(FilePath(UTF8ToUTF16(filename)).BaseName().value());
1071 #else
1072 filename = FilePath(filename).BaseName().value();
1073 #endif
1074
1057 // Next, remove "." from the beginning and end of the file name to avoid 1075 // Next, remove "." from the beginning and end of the file name to avoid
1058 // tricks with hidden files, "..", and "." 1076 // tricks with hidden files, "..", and "."
1059 TrimString(filename, L".", &filename); 1077 TrimString(filename, ".", &filename);
1060 } 1078 }
1061 if (filename.empty()) { 1079 if (filename.empty()) {
1062 if (url.is_valid()) { 1080 if (url.is_valid()) {
1063 filename = UnescapeAndDecodeUTF8URLComponent( 1081 filename = UnescapeURLComponent(
1064 url.ExtractFileName(), 1082 url.ExtractFileName(),
1065 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); 1083 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS);
1066 } 1084 }
1067 } 1085 }
1068 1086
1069 // Trim '.' once more. 1087 // Trim '.' once more.
1070 TrimString(filename, L".", &filename); 1088 TrimString(filename, ".", &filename);
1089
1071 // If there's no filename or it gets trimed to be empty, use 1090 // If there's no filename or it gets trimed to be empty, use
1072 // the URL hostname or default_name 1091 // the URL hostname or default_name
1073 if (filename.empty()) { 1092 if (filename.empty()) {
1074 if (!default_name.empty()) { 1093 if (default_name && default_name[0]) {
1075 filename = default_name; 1094 filename = default_name;
1076 } else if (url.is_valid()) { 1095 } else if (url.is_valid()) {
1077 // Some schemes (e.g. file) do not have a hostname. Even though it's 1096 // Some schemes (e.g. file) do not have a hostname. Even though it's
1078 // not likely to reach here, let's hardcode the last fallback name. 1097 // not likely to reach here, let's hardcode the last fallback name.
1079 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) 1098 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451)
1080 filename = url.host().empty() ? std::wstring(kFinalFallbackName) 1099 filename = url.host().empty() ? std::string(kFinalFallbackName)
1081 : UTF8ToWide(url.host()); 1100 : url.host();
1082 } else { 1101 } else {
1083 NOTREACHED(); 1102 NOTREACHED();
1084 } 1103 }
1085 } 1104 }
1086 1105
1087 file_util::ReplaceIllegalCharacters(&filename, '-'); 1106 #if defined(OS_WIN)
1088 return filename; 1107 FilePath::StringType file_path_string = UTF8ToWide(filename);
1108 #else
1109 std::string& file_path_string = filename;
1110 #endif
1111 file_util::ReplaceIllegalCharactersInPath(&file_path_string, '-');
1112 return FilePath(file_path_string);
1089 } 1113 }
1090 1114
1091 bool IsPortAllowedByDefault(int port) { 1115 bool IsPortAllowedByDefault(int port) {
1092 int array_size = arraysize(kRestrictedPorts); 1116 int array_size = arraysize(kRestrictedPorts);
1093 for (int i = 0; i < array_size; i++) { 1117 for (int i = 0; i < array_size; i++) {
1094 if (kRestrictedPorts[i] == port) { 1118 if (kRestrictedPorts[i] == port) {
1095 return false; 1119 return false;
1096 } 1120 }
1097 } 1121 }
1098 return true; 1122 return true;
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
1427 if (length > 0) 1451 if (length > 0)
1428 ports.insert(StringToInt(WideToASCII( 1452 ports.insert(StringToInt(WideToASCII(
1429 allowed_ports.substr(last, length)))); 1453 allowed_ports.substr(last, length))));
1430 last = i + 1; 1454 last = i + 1;
1431 } 1455 }
1432 } 1456 }
1433 explicitly_allowed_ports = ports; 1457 explicitly_allowed_ports = ports;
1434 } 1458 }
1435 1459
1436 } // namespace net 1460 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util.h ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698