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

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

Issue 2665953002: Don't blindly do a prefix match when evaluating net::GetExtensionsForMimeType(). (Closed)
Patch Set: Address Helen's comments Created 3 years, 10 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
« no previous file with comments | « no previous file | net/base/mime_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) 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
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 // GetExtensionsFromHardCodedMappings() adds file extensions (without a leading
463 // dot) to the set |extensions|, for all MIME types matching |mime_type|.
464 //
465 // The meaning of |mime_type| depends on the value of |prefix_match|:
466 //
467 // * If |prefix_match = false| then |mime_type| is an exact (case-insensitive)
468 // string such as "text/plain".
469 //
470 // * If |prefix_match = true| then |mime_type| is treated as the prefix for a
471 // (case-insensitive) string. For instance "Text/" would match "text/plain".
472 template <size_t N>
462 void GetExtensionsFromHardCodedMappings( 473 void GetExtensionsFromHardCodedMappings(
463 const MimeInfo* mappings, 474 const MimeInfo (&mappings)[N],
464 size_t mappings_len, 475 const std::string& mime_type,
465 const std::string& leading_mime_type, 476 bool prefix_match,
466 std::unordered_set<base::FilePath::StringType>* extensions) { 477 std::unordered_set<base::FilePath::StringType>* extensions) {
467 for (size_t i = 0; i < mappings_len; ++i) { 478 for (const auto& mapping : mappings) {
468 if (base::StartsWith(mappings[i].mime_type, leading_mime_type, 479 base::StringPiece cur_mime_type(mapping.mime_type);
469 base::CompareCase::INSENSITIVE_ASCII)) { 480
481 if (base::StartsWith(cur_mime_type, mime_type,
482 base::CompareCase::INSENSITIVE_ASCII) &&
483 (prefix_match || (cur_mime_type.length() == mime_type.length()))) {
470 for (const base::StringPiece& this_extension : base::SplitStringPiece( 484 for (const base::StringPiece& this_extension : base::SplitStringPiece(
471 mappings[i].extensions, ",", base::TRIM_WHITESPACE, 485 mapping.extensions, ",", base::TRIM_WHITESPACE,
472 base::SPLIT_WANT_ALL)) { 486 base::SPLIT_WANT_ALL)) {
473 #if defined(OS_WIN) 487 #if defined(OS_WIN)
474 extensions->insert(base::UTF8ToUTF16(this_extension)); 488 extensions->insert(base::UTF8ToUTF16(this_extension));
475 #else 489 #else
476 extensions->insert(this_extension.as_string()); 490 extensions->insert(this_extension.as_string());
477 #endif 491 #endif
478 } 492 }
479 } 493 }
480 } 494 }
481 } 495 }
482 496
483 void GetExtensionsHelper( 497 void GetExtensionsHelper(
484 const char* const* standard_types, 498 const char* const* standard_types,
485 size_t standard_types_len, 499 size_t standard_types_len,
486 const std::string& leading_mime_type, 500 const std::string& leading_mime_type,
487 std::unordered_set<base::FilePath::StringType>* extensions) { 501 std::unordered_set<base::FilePath::StringType>* extensions) {
488 for (size_t i = 0; i < standard_types_len; ++i) { 502 for (size_t i = 0; i < standard_types_len; ++i) {
489 g_mime_util.Get().GetPlatformExtensionsForMimeType(standard_types[i], 503 g_mime_util.Get().GetPlatformExtensionsForMimeType(standard_types[i],
490 extensions); 504 extensions);
491 } 505 }
492 506
493 // Also look up the extensions from hard-coded mappings in case that some 507 // 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. 508 // supported extensions are not registered in the system registry, like ogg.
495 GetExtensionsFromHardCodedMappings(kPrimaryMappings, 509 GetExtensionsFromHardCodedMappings(kPrimaryMappings, leading_mime_type, true,
496 arraysize(kPrimaryMappings), 510 extensions);
497 leading_mime_type, extensions);
498 511
499 GetExtensionsFromHardCodedMappings(kSecondaryMappings, 512 GetExtensionsFromHardCodedMappings(kSecondaryMappings, leading_mime_type,
500 arraysize(kSecondaryMappings), 513 true, extensions);
501 leading_mime_type, extensions);
502 } 514 }
503 515
504 // Note that the elements in the source set will be appended to the target 516 // Note that the elements in the source set will be appended to the target
505 // vector. 517 // vector.
506 template <class T> 518 template <class T>
507 void UnorderedSetToVector(std::unordered_set<T>* source, 519 void UnorderedSetToVector(std::unordered_set<T>* source,
508 std::vector<T>* target) { 520 std::vector<T>* target) {
509 size_t old_target_size = target->size(); 521 size_t old_target_size = target->size();
510 target->resize(old_target_size + source->size()); 522 target->resize(old_target_size + source->size());
511 size_t i = 0; 523 size_t i = 0;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 GetExtensionsHelper(type->standard_types, 566 GetExtensionsHelper(type->standard_types,
555 type->standard_types_len, 567 type->standard_types_len,
556 leading_mime_type, 568 leading_mime_type,
557 &unique_extensions); 569 &unique_extensions);
558 } else { 570 } else {
559 g_mime_util.Get().GetPlatformExtensionsForMimeType(mime_type, 571 g_mime_util.Get().GetPlatformExtensionsForMimeType(mime_type,
560 &unique_extensions); 572 &unique_extensions);
561 573
562 // Also look up the extensions from hard-coded mappings in case that some 574 // 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. 575 // supported extensions are not registered in the system registry, like ogg.
564 GetExtensionsFromHardCodedMappings(kPrimaryMappings, 576 GetExtensionsFromHardCodedMappings(kPrimaryMappings, mime_type, false,
565 arraysize(kPrimaryMappings), mime_type,
566 &unique_extensions); 577 &unique_extensions);
567 578
568 GetExtensionsFromHardCodedMappings(kSecondaryMappings, 579 GetExtensionsFromHardCodedMappings(kSecondaryMappings, mime_type, false,
569 arraysize(kSecondaryMappings), mime_type,
570 &unique_extensions); 580 &unique_extensions);
571 } 581 }
572 582
573 UnorderedSetToVector(&unique_extensions, extensions); 583 UnorderedSetToVector(&unique_extensions, extensions);
574 } 584 }
575 585
576 NET_EXPORT std::string GenerateMimeMultipartBoundary() { 586 NET_EXPORT std::string GenerateMimeMultipartBoundary() {
577 // Based on RFC 1341, section "7.2.1 Multipart: The common syntax": 587 // Based on RFC 1341, section "7.2.1 Multipart: The common syntax":
578 // Because encapsulation boundaries must not appear in the body parts being 588 // Because encapsulation boundaries must not appear in the body parts being
579 // encapsulated, a user agent must exercise care to choose a unique 589 // encapsulated, a user agent must exercise care to choose a unique
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 post_data->append("\r\n" + value + "\r\n"); 638 post_data->append("\r\n" + value + "\r\n");
629 } 639 }
630 640
631 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, 641 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary,
632 std::string* post_data) { 642 std::string* post_data) {
633 DCHECK(post_data); 643 DCHECK(post_data);
634 post_data->append("--" + mime_boundary + "--\r\n"); 644 post_data->append("--" + mime_boundary + "--\r\n");
635 } 645 }
636 646
637 } // namespace net 647 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/base/mime_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698