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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 struct CompliantHostCase { | 379 struct CompliantHostCase { |
380 const char* host; | 380 const char* host; |
381 const char* desired_tld; | 381 const char* desired_tld; |
382 bool expected_output; | 382 bool expected_output; |
383 }; | 383 }; |
384 | 384 |
385 struct SuggestedFilenameCase { | 385 struct SuggestedFilenameCase { |
386 const char* url; | 386 const char* url; |
387 const char* content_disp_header; | 387 const char* content_disp_header; |
388 const char* referrer_charset; | 388 const char* referrer_charset; |
| 389 const char* suggested_filename; |
389 const wchar_t* default_filename; | 390 const wchar_t* default_filename; |
390 const wchar_t* expected_filename; | 391 const wchar_t* expected_filename; |
391 }; | 392 }; |
392 | 393 |
393 struct UrlTestData { | 394 struct UrlTestData { |
394 const char* description; | 395 const char* description; |
395 const char* input; | 396 const char* input; |
396 const char* languages; | 397 const char* languages; |
397 FormatUrlTypes format_types; | 398 FormatUrlTypes format_types; |
398 UnescapeRule::Type escape_rules; | 399 UnescapeRule::Type escape_rules; |
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1041 EXPECT_EQ(string16(), StripWWW(ASCIIToUTF16("www."))); | 1042 EXPECT_EQ(string16(), StripWWW(ASCIIToUTF16("www."))); |
1042 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("www.blah"))); | 1043 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("www.blah"))); |
1043 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("blah"))); | 1044 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("blah"))); |
1044 } | 1045 } |
1045 | 1046 |
1046 TEST(NetUtilTest, GetSuggestedFilename) { | 1047 TEST(NetUtilTest, GetSuggestedFilename) { |
1047 const SuggestedFilenameCase test_cases[] = { | 1048 const SuggestedFilenameCase test_cases[] = { |
1048 {"http://www.google.com/", | 1049 {"http://www.google.com/", |
1049 "Content-disposition: attachment; filename=test.html", | 1050 "Content-disposition: attachment; filename=test.html", |
1050 "", | 1051 "", |
| 1052 "", |
1051 L"", | 1053 L"", |
1052 L"test.html"}, | 1054 L"test.html"}, |
1053 {"http://www.google.com/", | 1055 {"http://www.google.com/", |
1054 "Content-disposition: attachment; filename=\"test.html\"", | 1056 "Content-disposition: attachment; filename=\"test.html\"", |
1055 "", | 1057 "", |
| 1058 "", |
1056 L"", | 1059 L"", |
1057 L"test.html"}, | 1060 L"test.html"}, |
1058 {"http://www.google.com/", | 1061 {"http://www.google.com/", |
1059 "Content-disposition: attachment; filename= \"test.html\"", | 1062 "Content-disposition: attachment; filename= \"test.html\"", |
1060 "", | 1063 "", |
| 1064 "", |
1061 L"", | 1065 L"", |
1062 L"test.html"}, | 1066 L"test.html"}, |
1063 {"http://www.google.com/", | 1067 {"http://www.google.com/", |
1064 "Content-disposition: attachment; filename = \"test.html\"", | 1068 "Content-disposition: attachment; filename = \"test.html\"", |
1065 "", | 1069 "", |
| 1070 "", |
1066 L"", | 1071 L"", |
1067 L"test.html"}, | 1072 L"test.html"}, |
1068 {"http://www.google.com/", | 1073 {"http://www.google.com/", |
1069 "Content-disposition: attachment; filename= ", | 1074 "Content-disposition: attachment; filename= ", |
1070 "", | 1075 "", |
| 1076 "", |
1071 L"", | 1077 L"", |
1072 L"www.google.com"}, | 1078 L"www.google.com"}, |
1073 {"http://www.google.com/path/test.html", | 1079 {"http://www.google.com/path/test.html", |
1074 "Content-disposition: attachment", | 1080 "Content-disposition: attachment", |
1075 "", | 1081 "", |
| 1082 "", |
1076 L"", | 1083 L"", |
1077 L"test.html"}, | 1084 L"test.html"}, |
1078 {"http://www.google.com/path/test.html", | 1085 {"http://www.google.com/path/test.html", |
1079 "Content-disposition: attachment;", | 1086 "Content-disposition: attachment;", |
1080 "", | 1087 "", |
| 1088 "", |
1081 L"", | 1089 L"", |
1082 L"test.html"}, | 1090 L"test.html"}, |
1083 {"http://www.google.com/", | 1091 {"http://www.google.com/", |
1084 "", | 1092 "", |
1085 "", | 1093 "", |
| 1094 "", |
1086 L"", | 1095 L"", |
1087 L"www.google.com"}, | 1096 L"www.google.com"}, |
1088 {"http://www.google.com/test.html", | 1097 {"http://www.google.com/test.html", |
1089 "", | 1098 "", |
1090 "", | 1099 "", |
| 1100 "", |
1091 L"", | 1101 L"", |
1092 L"test.html"}, | 1102 L"test.html"}, |
1093 // Now that we use googleurl's ExtractFileName, this case falls back | 1103 // Now that we use googleurl's ExtractFileName, this case falls back |
1094 // to the hostname. If this behavior is not desirable, we'd better | 1104 // to the hostname. If this behavior is not desirable, we'd better |
1095 // change ExtractFileName (in url_parse). | 1105 // change ExtractFileName (in url_parse). |
1096 {"http://www.google.com/path/", | 1106 {"http://www.google.com/path/", |
1097 "", | 1107 "", |
1098 "", | 1108 "", |
| 1109 "", |
1099 L"", | 1110 L"", |
1100 L"www.google.com"}, | 1111 L"www.google.com"}, |
1101 {"http://www.google.com/path", | 1112 {"http://www.google.com/path", |
1102 "", | 1113 "", |
1103 "", | 1114 "", |
| 1115 "", |
1104 L"", | 1116 L"", |
1105 L"path"}, | 1117 L"path"}, |
1106 {"file:///", | 1118 {"file:///", |
1107 "", | 1119 "", |
1108 "", | 1120 "", |
| 1121 "", |
1109 L"", | 1122 L"", |
1110 L"download"}, | 1123 L"download"}, |
1111 {"non-standard-scheme:", | 1124 {"non-standard-scheme:", |
1112 "", | 1125 "", |
1113 "", | 1126 "", |
| 1127 "", |
1114 L"", | 1128 L"", |
1115 L"download"}, | 1129 L"download"}, |
1116 {"http://www.google.com/", | 1130 {"http://www.google.com/", |
1117 "Content-disposition: attachment; filename =\"test.html\"", | 1131 "Content-disposition: attachment; filename =\"test.html\"", |
1118 "", | 1132 "", |
| 1133 "", |
1119 L"download", | 1134 L"download", |
1120 L"test.html"}, | 1135 L"test.html"}, |
1121 {"http://www.google.com/", | 1136 {"http://www.google.com/", |
1122 "", | 1137 "", |
1123 "", | 1138 "", |
| 1139 "", |
1124 L"download", | 1140 L"download", |
1125 L"download"}, | 1141 L"download"}, |
1126 {"http://www.google.com/", | 1142 {"http://www.google.com/", |
1127 "Content-disposition: attachment; filename=\"../test.html\"", | 1143 "Content-disposition: attachment; filename=\"../test.html\"", |
1128 "", | 1144 "", |
| 1145 "", |
1129 L"", | 1146 L"", |
1130 L"_test.html"}, | 1147 L"_test.html"}, |
1131 {"http://www.google.com/", | 1148 {"http://www.google.com/", |
1132 "Content-disposition: attachment; filename=\"..\\test.html\"", | 1149 "Content-disposition: attachment; filename=\"..\\test.html\"", |
1133 "", | 1150 "", |
| 1151 "", |
1134 L"", | 1152 L"", |
1135 L"_test.html"}, | 1153 L"_test.html"}, |
1136 {"http://www.google.com/", | 1154 {"http://www.google.com/", |
1137 "Content-disposition: attachment; filename=\"..\"", | 1155 "Content-disposition: attachment; filename=\"..\"", |
1138 "", | 1156 "", |
| 1157 "", |
1139 L"download", | 1158 L"download", |
1140 L"download"}, | 1159 L"download"}, |
1141 {"http://www.google.com/test.html", | 1160 {"http://www.google.com/test.html", |
1142 "Content-disposition: attachment; filename=\"..\"", | 1161 "Content-disposition: attachment; filename=\"..\"", |
1143 "", | 1162 "", |
| 1163 "", |
1144 L"download", | 1164 L"download", |
1145 L"test.html"}, | 1165 L"test.html"}, |
1146 // Below is a small subset of cases taken from GetFileNameFromCD test above. | 1166 // Below is a small subset of cases taken from GetFileNameFromCD test above. |
1147 {"http://www.google.com/", | 1167 {"http://www.google.com/", |
1148 "Content-Disposition: attachment; filename=\"%EC%98%88%EC%88%A0%20" | 1168 "Content-Disposition: attachment; filename=\"%EC%98%88%EC%88%A0%20" |
1149 "%EC%98%88%EC%88%A0.jpg\"", | 1169 "%EC%98%88%EC%88%A0.jpg\"", |
1150 "", | 1170 "", |
| 1171 "", |
1151 L"", | 1172 L"", |
1152 L"\uc608\uc220 \uc608\uc220.jpg"}, | 1173 L"\uc608\uc220 \uc608\uc220.jpg"}, |
1153 {"http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", | 1174 {"http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", |
1154 "", | 1175 "", |
1155 "", | 1176 "", |
| 1177 "", |
1156 L"download", | 1178 L"download", |
1157 L"\uc608\uc220 \uc608\uc220.jpg"}, | 1179 L"\uc608\uc220 \uc608\uc220.jpg"}, |
1158 {"http://www.google.com/", | 1180 {"http://www.google.com/", |
1159 "Content-disposition: attachment;", | 1181 "Content-disposition: attachment;", |
1160 "", | 1182 "", |
| 1183 "", |
1161 L"\uB2E4\uC6B4\uB85C\uB4DC", | 1184 L"\uB2E4\uC6B4\uB85C\uB4DC", |
1162 L"\uB2E4\uC6B4\uB85C\uB4DC"}, | 1185 L"\uB2E4\uC6B4\uB85C\uB4DC"}, |
1163 {"http://www.google.com/", | 1186 {"http://www.google.com/", |
1164 "Content-Disposition: attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" | 1187 "Content-Disposition: attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" |
1165 "D13=2Epng?=\"", | 1188 "D13=2Epng?=\"", |
1166 "", | 1189 "", |
| 1190 "", |
1167 L"download", | 1191 L"download", |
1168 L"\u82b8\u88533.png"}, | 1192 L"\u82b8\u88533.png"}, |
1169 {"http://www.example.com/images?id=3", | 1193 {"http://www.example.com/images?id=3", |
1170 "Content-Disposition: attachment; filename=caf\xc3\xa9.png", | 1194 "Content-Disposition: attachment; filename=caf\xc3\xa9.png", |
1171 "iso-8859-1", | 1195 "iso-8859-1", |
| 1196 "", |
1172 L"", | 1197 L"", |
1173 L"caf\u00e9.png"}, | 1198 L"caf\u00e9.png"}, |
1174 {"http://www.example.com/images?id=3", | 1199 {"http://www.example.com/images?id=3", |
1175 "Content-Disposition: attachment; filename=caf\xe5.png", | 1200 "Content-Disposition: attachment; filename=caf\xe5.png", |
1176 "windows-1253", | 1201 "windows-1253", |
| 1202 "", |
1177 L"", | 1203 L"", |
1178 L"caf\u03b5.png"}, | 1204 L"caf\u03b5.png"}, |
1179 {"http://www.example.com/file?id=3", | 1205 {"http://www.example.com/file?id=3", |
1180 "Content-Disposition: attachment; name=\xcf\xc2\xd4\xd8.zip", | 1206 "Content-Disposition: attachment; name=\xcf\xc2\xd4\xd8.zip", |
1181 "GBK", | 1207 "GBK", |
| 1208 "", |
1182 L"", | 1209 L"", |
1183 L"\u4e0b\u8f7d.zip"}, | 1210 L"\u4e0b\u8f7d.zip"}, |
1184 // Invalid C-D header. Extracts filename from url. | 1211 // Invalid C-D header. Extracts filename from url. |
1185 {"http://www.google.com/test.html", | 1212 {"http://www.google.com/test.html", |
1186 "Content-Disposition: attachment; filename==?iiso88591?Q?caf=EG?=", | 1213 "Content-Disposition: attachment; filename==?iiso88591?Q?caf=EG?=", |
1187 "", | 1214 "", |
| 1215 "", |
1188 L"", | 1216 L"", |
1189 L"test.html"}, | 1217 L"test.html"}, |
1190 // about: and data: URLs | 1218 // about: and data: URLs |
1191 {"about:chrome", | 1219 {"about:chrome", |
1192 "", | 1220 "", |
1193 "", | 1221 "", |
| 1222 "", |
1194 L"", | 1223 L"", |
1195 L"download"}, | 1224 L"download"}, |
1196 {"data:,looks/like/a.path", | 1225 {"data:,looks/like/a.path", |
1197 "", | 1226 "", |
1198 "", | 1227 "", |
| 1228 "", |
1199 L"", | 1229 L"", |
1200 L"download"}, | 1230 L"download"}, |
1201 {"data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=", | 1231 {"data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=", |
1202 "", | 1232 "", |
1203 "", | 1233 "", |
| 1234 "", |
1204 L"", | 1235 L"", |
1205 L"download"}, | 1236 L"download"}, |
1206 {"data:,looks/like/a.path", | 1237 {"data:,looks/like/a.path", |
1207 "", | 1238 "", |
1208 "", | 1239 "", |
| 1240 "", |
1209 L"default_filename_is_given", | 1241 L"default_filename_is_given", |
1210 L"default_filename_is_given"}, | 1242 L"default_filename_is_given"}, |
1211 {"data:,looks/like/a.path", | 1243 {"data:,looks/like/a.path", |
1212 "", | 1244 "", |
1213 "", | 1245 "", |
| 1246 "", |
1214 L"\u65e5\u672c\u8a9e", // Japanese Kanji. | 1247 L"\u65e5\u672c\u8a9e", // Japanese Kanji. |
1215 L"\u65e5\u672c\u8a9e"}, | 1248 L"\u65e5\u672c\u8a9e"}, |
1216 // Dotfiles. Ensures preceeding period(s) stripped. | 1249 // Dotfiles. Ensures preceeding period(s) stripped. |
1217 {"http://www.google.com/.test.html", | 1250 {"http://www.google.com/.test.html", |
1218 "", | 1251 "", |
1219 "", | 1252 "", |
1220 L"", | 1253 "", |
1221 L"test.html"}, | 1254 L"", |
| 1255 L"test.html"}, |
1222 {"http://www.google.com/.test", | 1256 {"http://www.google.com/.test", |
1223 "", | 1257 "", |
1224 "", | 1258 "", |
1225 L"", | 1259 "", |
1226 L"test"}, | 1260 L"", |
| 1261 L"test"}, |
1227 {"http://www.google.com/..test", | 1262 {"http://www.google.com/..test", |
1228 "", | 1263 "", |
1229 "", | 1264 "", |
1230 L"", | 1265 "", |
1231 L"test"}, | 1266 L"", |
| 1267 L"test"}, |
1232 // The filename encoding is specified by the referrer charset. | 1268 // The filename encoding is specified by the referrer charset. |
1233 {"http://example.com/V%FDvojov%E1%20psychologie.doc", | 1269 {"http://example.com/V%FDvojov%E1%20psychologie.doc", |
1234 "", | 1270 "", |
1235 "iso-8859-1", | 1271 "iso-8859-1", |
| 1272 "", |
1236 L"", | 1273 L"", |
1237 L"V\u00fdvojov\u00e1 psychologie.doc"}, | 1274 L"V\u00fdvojov\u00e1 psychologie.doc"}, |
| 1275 {"http://www.google.com/test", |
| 1276 "", |
| 1277 "", |
| 1278 "suggested", |
| 1279 L"", |
| 1280 L"suggested"}, |
| 1281 // The content-disposition has higher precedence over the suggested name. |
| 1282 {"http://www.google.com/test", |
| 1283 "Content-disposition: attachment; filename=test.html", |
| 1284 "", |
| 1285 "suggested", |
| 1286 L"", |
| 1287 L"test.html"}, |
1238 // The filename encoding doesn't match the referrer charset, the | 1288 // The filename encoding doesn't match the referrer charset, the |
1239 // system charset, or UTF-8. | 1289 // system charset, or UTF-8. |
1240 // TODO(jshin): we need to handle this case. | 1290 // TODO(jshin): we need to handle this case. |
1241 #if 0 | 1291 #if 0 |
1242 {"http://example.com/V%FDvojov%E1%20psychologie.doc", | 1292 {"http://example.com/V%FDvojov%E1%20psychologie.doc", |
1243 "", | 1293 "", |
1244 "utf-8", | 1294 "utf-8", |
| 1295 "", |
1245 L"", | 1296 L"", |
1246 L"V\u00fdvojov\u00e1 psychologie.doc", | 1297 L"V\u00fdvojov\u00e1 psychologie.doc", |
1247 }, | 1298 }, |
1248 #endif | 1299 #endif |
1249 }; | 1300 }; |
1250 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 1301 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
1251 std::wstring default_name = test_cases[i].default_filename; | 1302 std::wstring default_name = test_cases[i].default_filename; |
1252 string16 filename = GetSuggestedFilename( | 1303 string16 filename = GetSuggestedFilename( |
1253 GURL(test_cases[i].url), test_cases[i].content_disp_header, | 1304 GURL(test_cases[i].url), test_cases[i].content_disp_header, |
1254 test_cases[i].referrer_charset, WideToUTF16(default_name)); | 1305 test_cases[i].referrer_charset, test_cases[i].suggested_filename, |
| 1306 WideToUTF16(default_name)); |
1255 EXPECT_EQ(std::wstring(test_cases[i].expected_filename), | 1307 EXPECT_EQ(std::wstring(test_cases[i].expected_filename), |
1256 UTF16ToWide(filename)) | 1308 UTF16ToWide(filename)) |
1257 << "Iteration " << i << ": " << test_cases[i].url; | 1309 << "Iteration " << i << ": " << test_cases[i].url; |
1258 } | 1310 } |
1259 } | 1311 } |
1260 | 1312 |
1261 // This is currently a windows specific function. | 1313 // This is currently a windows specific function. |
1262 #if defined(OS_WIN) | 1314 #if defined(OS_WIN) |
1263 namespace { | 1315 namespace { |
1264 | 1316 |
(...skipping 1029 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2294 if (it->address[i] != 0) { | 2346 if (it->address[i] != 0) { |
2295 all_zeroes = false; | 2347 all_zeroes = false; |
2296 break; | 2348 break; |
2297 } | 2349 } |
2298 } | 2350 } |
2299 EXPECT_FALSE(all_zeroes); | 2351 EXPECT_FALSE(all_zeroes); |
2300 } | 2352 } |
2301 } | 2353 } |
2302 | 2354 |
2303 } // namespace net | 2355 } // namespace net |
OLD | NEW |