| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <iterator> | 6 #include <iterator> |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 return MatchesMimeTypeParameters(mime_type_pattern, mime_type); | 273 return MatchesMimeTypeParameters(mime_type_pattern, mime_type); |
| 274 } else { | 274 } else { |
| 275 return false; | 275 return false; |
| 276 } | 276 } |
| 277 } | 277 } |
| 278 | 278 |
| 279 // Test length to prevent overlap between |left| and |right|. | 279 // Test length to prevent overlap between |left| and |right|. |
| 280 if (base_type.length() < base_pattern.length() - 1) | 280 if (base_type.length() < base_pattern.length() - 1) |
| 281 return false; | 281 return false; |
| 282 | 282 |
| 283 const std::string left(base_pattern.substr(0, star)); | 283 base::StringPiece base_pattern_piece(base_pattern); |
| 284 const std::string right(base_pattern.substr(star + 1)); | 284 base::StringPiece left(base_pattern_piece.substr(0, star)); |
| 285 base::StringPiece right(base_pattern_piece.substr(star + 1)); |
| 285 | 286 |
| 286 if (!base::StartsWithASCII(base_type, left, false)) | 287 if (!base::StartsWith(base_type, left, base::CompareCase::INSENSITIVE_ASCII)) |
| 287 return false; | 288 return false; |
| 288 | 289 |
| 289 if (!right.empty() && !base::EndsWith(base_type, right, false)) | 290 if (!right.empty() && |
| 291 !base::EndsWith(base_type, right, base::CompareCase::INSENSITIVE_ASCII)) |
| 290 return false; | 292 return false; |
| 291 | 293 |
| 292 return MatchesMimeTypeParameters(mime_type_pattern, mime_type); | 294 return MatchesMimeTypeParameters(mime_type_pattern, mime_type); |
| 293 } | 295 } |
| 294 | 296 |
| 295 // See http://www.iana.org/assignments/media-types/media-types.xhtml | 297 // See http://www.iana.org/assignments/media-types/media-types.xhtml |
| 296 static const char* const legal_top_level_types[] = { | 298 static const char* const legal_top_level_types[] = { |
| 297 "application", | 299 "application", |
| 298 "audio", | 300 "audio", |
| 299 "example", | 301 "example", |
| 300 "image", | 302 "image", |
| 301 "message", | 303 "message", |
| 302 "model", | 304 "model", |
| 303 "multipart", | 305 "multipart", |
| 304 "text", | 306 "text", |
| 305 "video", | 307 "video", |
| 306 }; | 308 }; |
| 307 | 309 |
| 308 bool MimeUtil::ParseMimeTypeWithoutParameter( | 310 bool MimeUtil::ParseMimeTypeWithoutParameter( |
| 309 const std::string& type_string, | 311 const std::string& type_string, |
| 310 std::string* top_level_type, | 312 std::string* top_level_type, |
| 311 std::string* subtype) const { | 313 std::string* subtype) const { |
| 312 std::vector<std::string> components; | 314 std::vector<std::string> components = base::SplitString( |
| 313 base::SplitString(type_string, '/', &components); | 315 type_string, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 314 if (components.size() != 2 || | 316 if (components.size() != 2 || |
| 315 !HttpUtil::IsToken(components[0]) || | 317 !HttpUtil::IsToken(components[0]) || |
| 316 !HttpUtil::IsToken(components[1])) | 318 !HttpUtil::IsToken(components[1])) |
| 317 return false; | 319 return false; |
| 318 | 320 |
| 319 if (top_level_type) | 321 if (top_level_type) |
| 320 *top_level_type = components[0]; | 322 *top_level_type = components[0]; |
| 321 if (subtype) | 323 if (subtype) |
| 322 *subtype = components[1]; | 324 *subtype = components[1]; |
| 323 return true; | 325 return true; |
| 324 } | 326 } |
| 325 | 327 |
| 326 bool MimeUtil::IsValidTopLevelMimeType(const std::string& type_string) const { | 328 bool MimeUtil::IsValidTopLevelMimeType(const std::string& type_string) const { |
| 327 std::string lower_type = base::StringToLowerASCII(type_string); | 329 std::string lower_type = base::StringToLowerASCII(type_string); |
| 328 for (size_t i = 0; i < arraysize(legal_top_level_types); ++i) { | 330 for (size_t i = 0; i < arraysize(legal_top_level_types); ++i) { |
| 329 if (lower_type.compare(legal_top_level_types[i]) == 0) | 331 if (lower_type.compare(legal_top_level_types[i]) == 0) |
| 330 return true; | 332 return true; |
| 331 } | 333 } |
| 332 | 334 |
| 333 return type_string.size() > 2 && | 335 return type_string.size() > 2 && |
| 334 base::StartsWithASCII(type_string, "x-", false); | 336 base::StartsWith(type_string, "x-", |
| 337 base::CompareCase::INSENSITIVE_ASCII); |
| 335 } | 338 } |
| 336 | 339 |
| 337 //---------------------------------------------------------------------------- | 340 //---------------------------------------------------------------------------- |
| 338 // Wrappers for the singleton | 341 // Wrappers for the singleton |
| 339 //---------------------------------------------------------------------------- | 342 //---------------------------------------------------------------------------- |
| 340 | 343 |
| 341 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext, | 344 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext, |
| 342 std::string* mime_type) { | 345 std::string* mime_type) { |
| 343 return g_mime_util.Get().GetMimeTypeFromExtension(ext, mime_type); | 346 return g_mime_util.Get().GetMimeTypeFromExtension(ext, mime_type); |
| 344 } | 347 } |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 { "video/", kStandardVideoTypes, arraysize(kStandardVideoTypes) }, | 454 { "video/", kStandardVideoTypes, arraysize(kStandardVideoTypes) }, |
| 452 { NULL, NULL, 0 } | 455 { NULL, NULL, 0 } |
| 453 }; | 456 }; |
| 454 | 457 |
| 455 void GetExtensionsFromHardCodedMappings( | 458 void GetExtensionsFromHardCodedMappings( |
| 456 const MimeInfo* mappings, | 459 const MimeInfo* mappings, |
| 457 size_t mappings_len, | 460 size_t mappings_len, |
| 458 const std::string& leading_mime_type, | 461 const std::string& leading_mime_type, |
| 459 base::hash_set<base::FilePath::StringType>* extensions) { | 462 base::hash_set<base::FilePath::StringType>* extensions) { |
| 460 for (size_t i = 0; i < mappings_len; ++i) { | 463 for (size_t i = 0; i < mappings_len; ++i) { |
| 461 if (base::StartsWithASCII(mappings[i].mime_type, leading_mime_type, | 464 if (base::StartsWith(mappings[i].mime_type, leading_mime_type, |
| 462 false)) { | 465 base::CompareCase::INSENSITIVE_ASCII)) { |
| 463 std::vector<string> this_extensions; | 466 for (const base::StringPiece& this_extension : base::SplitStringPiece( |
| 464 base::SplitString(mappings[i].extensions, ',', &this_extensions); | 467 mappings[i].extensions, ",", base::TRIM_WHITESPACE, |
| 465 for (size_t j = 0; j < this_extensions.size(); ++j) { | 468 base::SPLIT_WANT_ALL)) { |
| 466 #if defined(OS_WIN) | 469 #if defined(OS_WIN) |
| 467 base::FilePath::StringType extension( | 470 extensions->insert(base::UTF8ToUTF16(this_extension)); |
| 468 base::UTF8ToWide(this_extensions[j])); | |
| 469 #else | 471 #else |
| 470 base::FilePath::StringType extension(this_extensions[j]); | 472 extensions->insert(this_extension.as_string()); |
| 471 #endif | 473 #endif |
| 472 extensions->insert(extension); | |
| 473 } | 474 } |
| 474 } | 475 } |
| 475 } | 476 } |
| 476 } | 477 } |
| 477 | 478 |
| 478 void GetExtensionsHelper( | 479 void GetExtensionsHelper( |
| 479 const char* const* standard_types, | 480 const char* const* standard_types, |
| 480 size_t standard_types_len, | 481 size_t standard_types_len, |
| 481 const std::string& leading_mime_type, | 482 const std::string& leading_mime_type, |
| 482 base::hash_set<base::FilePath::StringType>* extensions) { | 483 base::hash_set<base::FilePath::StringType>* extensions) { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 post_data->append("\r\n" + value + "\r\n"); | 578 post_data->append("\r\n" + value + "\r\n"); |
| 578 } | 579 } |
| 579 | 580 |
| 580 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 581 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
| 581 std::string* post_data) { | 582 std::string* post_data) { |
| 582 DCHECK(post_data); | 583 DCHECK(post_data); |
| 583 post_data->append("--" + mime_boundary + "--\r\n"); | 584 post_data->append("--" + mime_boundary + "--\r\n"); |
| 584 } | 585 } |
| 585 | 586 |
| 586 } // namespace net | 587 } // namespace net |
| OLD | NEW |