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 | 10 |
10 #include "base/base64.h" | 11 #include "base/base64.h" |
11 #include "base/containers/hash_tables.h" | |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 #include "net/base/mime_util.h" | 21 #include "net/base/mime_util.h" |
(...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 void GetExtensionsFromHardCodedMappings( | 462 void GetExtensionsFromHardCodedMappings( |
463 const MimeInfo* mappings, | 463 const MimeInfo* mappings, |
464 size_t mappings_len, | 464 size_t mappings_len, |
465 const std::string& leading_mime_type, | 465 const std::string& leading_mime_type, |
466 base::hash_set<base::FilePath::StringType>* extensions) { | 466 std::unordered_set<base::FilePath::StringType>* extensions) { |
467 for (size_t i = 0; i < mappings_len; ++i) { | 467 for (size_t i = 0; i < mappings_len; ++i) { |
468 if (base::StartsWith(mappings[i].mime_type, leading_mime_type, | 468 if (base::StartsWith(mappings[i].mime_type, leading_mime_type, |
469 base::CompareCase::INSENSITIVE_ASCII)) { | 469 base::CompareCase::INSENSITIVE_ASCII)) { |
470 for (const base::StringPiece& this_extension : base::SplitStringPiece( | 470 for (const base::StringPiece& this_extension : base::SplitStringPiece( |
471 mappings[i].extensions, ",", base::TRIM_WHITESPACE, | 471 mappings[i].extensions, ",", base::TRIM_WHITESPACE, |
472 base::SPLIT_WANT_ALL)) { | 472 base::SPLIT_WANT_ALL)) { |
473 #if defined(OS_WIN) | 473 #if defined(OS_WIN) |
474 extensions->insert(base::UTF8ToUTF16(this_extension)); | 474 extensions->insert(base::UTF8ToUTF16(this_extension)); |
475 #else | 475 #else |
476 extensions->insert(this_extension.as_string()); | 476 extensions->insert(this_extension.as_string()); |
477 #endif | 477 #endif |
478 } | 478 } |
479 } | 479 } |
480 } | 480 } |
481 } | 481 } |
482 | 482 |
483 void GetExtensionsHelper( | 483 void GetExtensionsHelper( |
484 const char* const* standard_types, | 484 const char* const* standard_types, |
485 size_t standard_types_len, | 485 size_t standard_types_len, |
486 const std::string& leading_mime_type, | 486 const std::string& leading_mime_type, |
487 base::hash_set<base::FilePath::StringType>* extensions) { | 487 std::unordered_set<base::FilePath::StringType>* extensions) { |
488 for (size_t i = 0; i < standard_types_len; ++i) { | 488 for (size_t i = 0; i < standard_types_len; ++i) { |
489 g_mime_util.Get().GetPlatformExtensionsForMimeType(standard_types[i], | 489 g_mime_util.Get().GetPlatformExtensionsForMimeType(standard_types[i], |
490 extensions); | 490 extensions); |
491 } | 491 } |
492 | 492 |
493 // Also look up the extensions from hard-coded mappings in case that some | 493 // 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. | 494 // supported extensions are not registered in the system registry, like ogg. |
495 GetExtensionsFromHardCodedMappings(kPrimaryMappings, | 495 GetExtensionsFromHardCodedMappings(kPrimaryMappings, |
496 arraysize(kPrimaryMappings), | 496 arraysize(kPrimaryMappings), |
497 leading_mime_type, extensions); | 497 leading_mime_type, extensions); |
498 | 498 |
499 GetExtensionsFromHardCodedMappings(kSecondaryMappings, | 499 GetExtensionsFromHardCodedMappings(kSecondaryMappings, |
500 arraysize(kSecondaryMappings), | 500 arraysize(kSecondaryMappings), |
501 leading_mime_type, extensions); | 501 leading_mime_type, extensions); |
502 } | 502 } |
503 | 503 |
504 // Note that the elements in the source set will be appended to the target | 504 // Note that the elements in the source set will be appended to the target |
505 // vector. | 505 // vector. |
506 template<class T> | 506 template <class T> |
507 void HashSetToVector(base::hash_set<T>* source, std::vector<T>* target) { | 507 void UnorderedSetToVector(std::unordered_set<T>* source, |
| 508 std::vector<T>* target) { |
508 size_t old_target_size = target->size(); | 509 size_t old_target_size = target->size(); |
509 target->resize(old_target_size + source->size()); | 510 target->resize(old_target_size + source->size()); |
510 size_t i = 0; | 511 size_t i = 0; |
511 for (typename base::hash_set<T>::iterator iter = source->begin(); | 512 for (typename std::unordered_set<T>::iterator iter = source->begin(); |
512 iter != source->end(); ++iter, ++i) | 513 iter != source->end(); ++iter, ++i) |
513 (*target)[old_target_size + i] = *iter; | 514 (*target)[old_target_size + i] = *iter; |
514 } | 515 } |
515 | 516 |
516 // Characters to be used for mime multipart boundary. | 517 // Characters to be used for mime multipart boundary. |
517 // | 518 // |
518 // TODO(rsleevi): crbug.com/575779: Follow the spec or fix the spec. | 519 // TODO(rsleevi): crbug.com/575779: Follow the spec or fix the spec. |
519 // The RFC 2046 spec says the alphanumeric characters plus the | 520 // The RFC 2046 spec says the alphanumeric characters plus the |
520 // following characters are legal for boundaries: '()+_,-./:=? | 521 // following characters are legal for boundaries: '()+_,-./:=? |
521 // However the following characters, though legal, cause some sites | 522 // However the following characters, though legal, cause some sites |
522 // to fail: (),./:=+ | 523 // to fail: (),./:=+ |
523 const char kMimeBoundaryCharacters[] = | 524 const char kMimeBoundaryCharacters[] = |
524 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | 525 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
525 | 526 |
526 // Size of mime multipart boundary. | 527 // Size of mime multipart boundary. |
527 const size_t kMimeBoundarySize = 69; | 528 const size_t kMimeBoundarySize = 69; |
528 | 529 |
529 } // namespace | 530 } // namespace |
530 | 531 |
531 void GetExtensionsForMimeType( | 532 void GetExtensionsForMimeType( |
532 const std::string& unsafe_mime_type, | 533 const std::string& unsafe_mime_type, |
533 std::vector<base::FilePath::StringType>* extensions) { | 534 std::vector<base::FilePath::StringType>* extensions) { |
534 if (unsafe_mime_type == "*/*" || unsafe_mime_type == "*") | 535 if (unsafe_mime_type == "*/*" || unsafe_mime_type == "*") |
535 return; | 536 return; |
536 | 537 |
537 const std::string mime_type = base::ToLowerASCII(unsafe_mime_type); | 538 const std::string mime_type = base::ToLowerASCII(unsafe_mime_type); |
538 base::hash_set<base::FilePath::StringType> unique_extensions; | 539 std::unordered_set<base::FilePath::StringType> unique_extensions; |
539 | 540 |
540 if (base::EndsWith(mime_type, "/*", base::CompareCase::INSENSITIVE_ASCII)) { | 541 if (base::EndsWith(mime_type, "/*", base::CompareCase::INSENSITIVE_ASCII)) { |
541 std::string leading_mime_type = mime_type.substr(0, mime_type.length() - 1); | 542 std::string leading_mime_type = mime_type.substr(0, mime_type.length() - 1); |
542 | 543 |
543 // Find the matching StandardType from within kStandardTypes, or fall | 544 // Find the matching StandardType from within kStandardTypes, or fall |
544 // through to the last (default) StandardType. | 545 // through to the last (default) StandardType. |
545 const StandardType* type = NULL; | 546 const StandardType* type = NULL; |
546 for (size_t i = 0; i < arraysize(kStandardTypes); ++i) { | 547 for (size_t i = 0; i < arraysize(kStandardTypes); ++i) { |
547 type = &(kStandardTypes[i]); | 548 type = &(kStandardTypes[i]); |
548 if (type->leading_mime_type && | 549 if (type->leading_mime_type && |
(...skipping 13 matching lines...) Expand all Loading... |
562 // supported extensions are not registered in the system registry, like ogg. | 563 // supported extensions are not registered in the system registry, like ogg. |
563 GetExtensionsFromHardCodedMappings(kPrimaryMappings, | 564 GetExtensionsFromHardCodedMappings(kPrimaryMappings, |
564 arraysize(kPrimaryMappings), mime_type, | 565 arraysize(kPrimaryMappings), mime_type, |
565 &unique_extensions); | 566 &unique_extensions); |
566 | 567 |
567 GetExtensionsFromHardCodedMappings(kSecondaryMappings, | 568 GetExtensionsFromHardCodedMappings(kSecondaryMappings, |
568 arraysize(kSecondaryMappings), mime_type, | 569 arraysize(kSecondaryMappings), mime_type, |
569 &unique_extensions); | 570 &unique_extensions); |
570 } | 571 } |
571 | 572 |
572 HashSetToVector(&unique_extensions, extensions); | 573 UnorderedSetToVector(&unique_extensions, extensions); |
573 } | 574 } |
574 | 575 |
575 NET_EXPORT std::string GenerateMimeMultipartBoundary() { | 576 NET_EXPORT std::string GenerateMimeMultipartBoundary() { |
576 // 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": |
577 // Because encapsulation boundaries must not appear in the body parts being | 578 // Because encapsulation boundaries must not appear in the body parts being |
578 // encapsulated, a user agent must exercise care to choose a unique | 579 // encapsulated, a user agent must exercise care to choose a unique |
579 // boundary. The boundary in the example above could have been the result of | 580 // boundary. The boundary in the example above could have been the result of |
580 // an algorithm designed to produce boundaries with a very low probability | 581 // an algorithm designed to produce boundaries with a very low probability |
581 // of already existing in the data to be encapsulated without having to | 582 // of already existing in the data to be encapsulated without having to |
582 // prescan the data. | 583 // prescan the data. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 post_data->append("\r\n" + value + "\r\n"); | 628 post_data->append("\r\n" + value + "\r\n"); |
628 } | 629 } |
629 | 630 |
630 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 631 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
631 std::string* post_data) { | 632 std::string* post_data) { |
632 DCHECK(post_data); | 633 DCHECK(post_data); |
633 post_data->append("--" + mime_boundary + "--\r\n"); | 634 post_data->append("--" + mime_boundary + "--\r\n"); |
634 } | 635 } |
635 | 636 |
636 } // namespace net | 637 } // namespace net |
OLD | NEW |