Chromium Code Reviews| 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 #include <unordered_set> | 9 #include <unordered_set> |
| 10 | 10 |
| (...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 452 const char* const* standard_types; | 452 const char* const* standard_types; |
| 453 size_t standard_types_len; | 453 size_t standard_types_len; |
| 454 }; | 454 }; |
| 455 static const StandardType kStandardTypes[] = { | 455 static const StandardType kStandardTypes[] = { |
| 456 { "image/", kStandardImageTypes, arraysize(kStandardImageTypes) }, | 456 { "image/", kStandardImageTypes, arraysize(kStandardImageTypes) }, |
| 457 { "audio/", kStandardAudioTypes, arraysize(kStandardAudioTypes) }, | 457 { "audio/", kStandardAudioTypes, arraysize(kStandardAudioTypes) }, |
| 458 { "video/", kStandardVideoTypes, arraysize(kStandardVideoTypes) }, | 458 { "video/", kStandardVideoTypes, arraysize(kStandardVideoTypes) }, |
| 459 { NULL, NULL, 0 } | 459 { NULL, NULL, 0 } |
| 460 }; | 460 }; |
| 461 | 461 |
| 462 template <size_t N> | |
| 462 void GetExtensionsFromHardCodedMappings( | 463 void GetExtensionsFromHardCodedMappings( |
|
xunjieli
2017/01/31 17:21:29
nit: Could you add a comment on this function? (hi
eroman
2017/01/31 17:41:30
Done, good idea.
| |
| 463 const MimeInfo* mappings, | 464 const MimeInfo (&mappings)[N], |
| 464 size_t mappings_len, | 465 const std::string& mime_type, |
| 465 const std::string& leading_mime_type, | 466 bool prefix_match, |
| 466 std::unordered_set<base::FilePath::StringType>* extensions) { | 467 std::unordered_set<base::FilePath::StringType>* extensions) { |
| 467 for (size_t i = 0; i < mappings_len; ++i) { | 468 for (const auto& mapping : mappings) { |
| 468 if (base::StartsWith(mappings[i].mime_type, leading_mime_type, | 469 base::StringPiece cur_mime_type(mapping.mime_type); |
| 469 base::CompareCase::INSENSITIVE_ASCII)) { | 470 |
| 471 if (base::StartsWith(cur_mime_type, mime_type, | |
| 472 base::CompareCase::INSENSITIVE_ASCII) && | |
| 473 (prefix_match || (cur_mime_type.length() == mime_type.length()))) { | |
| 470 for (const base::StringPiece& this_extension : base::SplitStringPiece( | 474 for (const base::StringPiece& this_extension : base::SplitStringPiece( |
| 471 mappings[i].extensions, ",", base::TRIM_WHITESPACE, | 475 mapping.extensions, ",", base::TRIM_WHITESPACE, |
| 472 base::SPLIT_WANT_ALL)) { | 476 base::SPLIT_WANT_ALL)) { |
| 473 #if defined(OS_WIN) | 477 #if defined(OS_WIN) |
| 474 extensions->insert(base::UTF8ToUTF16(this_extension)); | 478 extensions->insert(base::UTF8ToUTF16(this_extension)); |
| 475 #else | 479 #else |
| 476 extensions->insert(this_extension.as_string()); | 480 extensions->insert(this_extension.as_string()); |
| 477 #endif | 481 #endif |
| 478 } | 482 } |
| 479 } | 483 } |
| 480 } | 484 } |
| 481 } | 485 } |
| 482 | 486 |
| 483 void GetExtensionsHelper( | 487 void GetExtensionsHelper( |
| 484 const char* const* standard_types, | 488 const char* const* standard_types, |
| 485 size_t standard_types_len, | 489 size_t standard_types_len, |
| 486 const std::string& leading_mime_type, | 490 const std::string& leading_mime_type, |
| 487 std::unordered_set<base::FilePath::StringType>* extensions) { | 491 std::unordered_set<base::FilePath::StringType>* extensions) { |
| 488 for (size_t i = 0; i < standard_types_len; ++i) { | 492 for (size_t i = 0; i < standard_types_len; ++i) { |
| 489 g_mime_util.Get().GetPlatformExtensionsForMimeType(standard_types[i], | 493 g_mime_util.Get().GetPlatformExtensionsForMimeType(standard_types[i], |
| 490 extensions); | 494 extensions); |
| 491 } | 495 } |
| 492 | 496 |
| 493 // Also look up the extensions from hard-coded mappings in case that some | 497 // Also look up the extensions from hard-coded mappings in case that some |
| 494 // supported extensions are not registered in the system registry, like ogg. | 498 // supported extensions are not registered in the system registry, like ogg. |
| 495 GetExtensionsFromHardCodedMappings(kPrimaryMappings, | 499 GetExtensionsFromHardCodedMappings(kPrimaryMappings, leading_mime_type, true, |
| 496 arraysize(kPrimaryMappings), | 500 extensions); |
| 497 leading_mime_type, extensions); | |
| 498 | 501 |
| 499 GetExtensionsFromHardCodedMappings(kSecondaryMappings, | 502 GetExtensionsFromHardCodedMappings(kSecondaryMappings, leading_mime_type, |
| 500 arraysize(kSecondaryMappings), | 503 true, extensions); |
| 501 leading_mime_type, extensions); | |
| 502 } | 504 } |
| 503 | 505 |
| 504 // Note that the elements in the source set will be appended to the target | 506 // Note that the elements in the source set will be appended to the target |
| 505 // vector. | 507 // vector. |
| 506 template <class T> | 508 template <class T> |
| 507 void UnorderedSetToVector(std::unordered_set<T>* source, | 509 void UnorderedSetToVector(std::unordered_set<T>* source, |
| 508 std::vector<T>* target) { | 510 std::vector<T>* target) { |
| 509 size_t old_target_size = target->size(); | 511 size_t old_target_size = target->size(); |
| 510 target->resize(old_target_size + source->size()); | 512 target->resize(old_target_size + source->size()); |
| 511 size_t i = 0; | 513 size_t i = 0; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 554 GetExtensionsHelper(type->standard_types, | 556 GetExtensionsHelper(type->standard_types, |
| 555 type->standard_types_len, | 557 type->standard_types_len, |
| 556 leading_mime_type, | 558 leading_mime_type, |
| 557 &unique_extensions); | 559 &unique_extensions); |
| 558 } else { | 560 } else { |
| 559 g_mime_util.Get().GetPlatformExtensionsForMimeType(mime_type, | 561 g_mime_util.Get().GetPlatformExtensionsForMimeType(mime_type, |
| 560 &unique_extensions); | 562 &unique_extensions); |
| 561 | 563 |
| 562 // Also look up the extensions from hard-coded mappings in case that some | 564 // Also look up the extensions from hard-coded mappings in case that some |
| 563 // supported extensions are not registered in the system registry, like ogg. | 565 // supported extensions are not registered in the system registry, like ogg. |
| 564 GetExtensionsFromHardCodedMappings(kPrimaryMappings, | 566 GetExtensionsFromHardCodedMappings(kPrimaryMappings, mime_type, false, |
| 565 arraysize(kPrimaryMappings), mime_type, | |
| 566 &unique_extensions); | 567 &unique_extensions); |
| 567 | 568 |
| 568 GetExtensionsFromHardCodedMappings(kSecondaryMappings, | 569 GetExtensionsFromHardCodedMappings(kSecondaryMappings, mime_type, false, |
| 569 arraysize(kSecondaryMappings), mime_type, | |
| 570 &unique_extensions); | 570 &unique_extensions); |
| 571 } | 571 } |
| 572 | 572 |
| 573 UnorderedSetToVector(&unique_extensions, extensions); | 573 UnorderedSetToVector(&unique_extensions, extensions); |
| 574 } | 574 } |
| 575 | 575 |
| 576 NET_EXPORT std::string GenerateMimeMultipartBoundary() { | 576 NET_EXPORT std::string GenerateMimeMultipartBoundary() { |
| 577 // Based on RFC 1341, section "7.2.1 Multipart: The common syntax": | 577 // Based on RFC 1341, section "7.2.1 Multipart: The common syntax": |
| 578 // Because encapsulation boundaries must not appear in the body parts being | 578 // Because encapsulation boundaries must not appear in the body parts being |
| 579 // encapsulated, a user agent must exercise care to choose a unique | 579 // encapsulated, a user agent must exercise care to choose a unique |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 628 post_data->append("\r\n" + value + "\r\n"); | 628 post_data->append("\r\n" + value + "\r\n"); |
| 629 } | 629 } |
| 630 | 630 |
| 631 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 631 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
| 632 std::string* post_data) { | 632 std::string* post_data) { |
| 633 DCHECK(post_data); | 633 DCHECK(post_data); |
| 634 post_data->append("--" + mime_boundary + "--\r\n"); | 634 post_data->append("--" + mime_boundary + "--\r\n"); |
| 635 } | 635 } |
| 636 | 636 |
| 637 } // namespace net | 637 } // namespace net |
| OLD | NEW |