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> | |
10 | 9 |
11 #include "base/base64.h" | 10 #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 std::unordered_set<base::FilePath::StringType>* extensions) { | 466 base::hash_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 std::unordered_set<base::FilePath::StringType>* extensions) { | 487 base::hash_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 UnorderedSetToVector(std::unordered_set<T>* source, | 507 void HashSetToVector(base::hash_set<T>* source, std::vector<T>* target) { |
508 std::vector<T>* target) { | |
509 size_t old_target_size = target->size(); | 508 size_t old_target_size = target->size(); |
510 target->resize(old_target_size + source->size()); | 509 target->resize(old_target_size + source->size()); |
511 size_t i = 0; | 510 size_t i = 0; |
512 for (typename std::unordered_set<T>::iterator iter = source->begin(); | 511 for (typename base::hash_set<T>::iterator iter = source->begin(); |
513 iter != source->end(); ++iter, ++i) | 512 iter != source->end(); ++iter, ++i) |
514 (*target)[old_target_size + i] = *iter; | 513 (*target)[old_target_size + i] = *iter; |
515 } | 514 } |
516 | 515 |
517 // Characters to be used for mime multipart boundary. | 516 // Characters to be used for mime multipart boundary. |
518 // | 517 // |
519 // TODO(rsleevi): crbug.com/575779: Follow the spec or fix the spec. | 518 // TODO(rsleevi): crbug.com/575779: Follow the spec or fix the spec. |
520 // The RFC 2046 spec says the alphanumeric characters plus the | 519 // The RFC 2046 spec says the alphanumeric characters plus the |
521 // following characters are legal for boundaries: '()+_,-./:=? | 520 // following characters are legal for boundaries: '()+_,-./:=? |
522 // However the following characters, though legal, cause some sites | 521 // However the following characters, though legal, cause some sites |
523 // to fail: (),./:=+ | 522 // to fail: (),./:=+ |
524 const char kMimeBoundaryCharacters[] = | 523 const char kMimeBoundaryCharacters[] = |
525 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | 524 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
526 | 525 |
527 // Size of mime multipart boundary. | 526 // Size of mime multipart boundary. |
528 const size_t kMimeBoundarySize = 69; | 527 const size_t kMimeBoundarySize = 69; |
529 | 528 |
530 } // namespace | 529 } // namespace |
531 | 530 |
532 void GetExtensionsForMimeType( | 531 void GetExtensionsForMimeType( |
533 const std::string& unsafe_mime_type, | 532 const std::string& unsafe_mime_type, |
534 std::vector<base::FilePath::StringType>* extensions) { | 533 std::vector<base::FilePath::StringType>* extensions) { |
535 if (unsafe_mime_type == "*/*" || unsafe_mime_type == "*") | 534 if (unsafe_mime_type == "*/*" || unsafe_mime_type == "*") |
536 return; | 535 return; |
537 | 536 |
538 const std::string mime_type = base::ToLowerASCII(unsafe_mime_type); | 537 const std::string mime_type = base::ToLowerASCII(unsafe_mime_type); |
539 std::unordered_set<base::FilePath::StringType> unique_extensions; | 538 base::hash_set<base::FilePath::StringType> unique_extensions; |
540 | 539 |
541 if (base::EndsWith(mime_type, "/*", base::CompareCase::INSENSITIVE_ASCII)) { | 540 if (base::EndsWith(mime_type, "/*", base::CompareCase::INSENSITIVE_ASCII)) { |
542 std::string leading_mime_type = mime_type.substr(0, mime_type.length() - 1); | 541 std::string leading_mime_type = mime_type.substr(0, mime_type.length() - 1); |
543 | 542 |
544 // Find the matching StandardType from within kStandardTypes, or fall | 543 // Find the matching StandardType from within kStandardTypes, or fall |
545 // through to the last (default) StandardType. | 544 // through to the last (default) StandardType. |
546 const StandardType* type = NULL; | 545 const StandardType* type = NULL; |
547 for (size_t i = 0; i < arraysize(kStandardTypes); ++i) { | 546 for (size_t i = 0; i < arraysize(kStandardTypes); ++i) { |
548 type = &(kStandardTypes[i]); | 547 type = &(kStandardTypes[i]); |
549 if (type->leading_mime_type && | 548 if (type->leading_mime_type && |
(...skipping 13 matching lines...) Expand all Loading... |
563 // supported extensions are not registered in the system registry, like ogg. | 562 // supported extensions are not registered in the system registry, like ogg. |
564 GetExtensionsFromHardCodedMappings(kPrimaryMappings, | 563 GetExtensionsFromHardCodedMappings(kPrimaryMappings, |
565 arraysize(kPrimaryMappings), mime_type, | 564 arraysize(kPrimaryMappings), mime_type, |
566 &unique_extensions); | 565 &unique_extensions); |
567 | 566 |
568 GetExtensionsFromHardCodedMappings(kSecondaryMappings, | 567 GetExtensionsFromHardCodedMappings(kSecondaryMappings, |
569 arraysize(kSecondaryMappings), mime_type, | 568 arraysize(kSecondaryMappings), mime_type, |
570 &unique_extensions); | 569 &unique_extensions); |
571 } | 570 } |
572 | 571 |
573 UnorderedSetToVector(&unique_extensions, extensions); | 572 HashSetToVector(&unique_extensions, extensions); |
574 } | 573 } |
575 | 574 |
576 NET_EXPORT std::string GenerateMimeMultipartBoundary() { | 575 NET_EXPORT std::string GenerateMimeMultipartBoundary() { |
577 // Based on RFC 1341, section "7.2.1 Multipart: The common syntax": | 576 // Based on RFC 1341, section "7.2.1 Multipart: The common syntax": |
578 // Because encapsulation boundaries must not appear in the body parts being | 577 // Because encapsulation boundaries must not appear in the body parts being |
579 // encapsulated, a user agent must exercise care to choose a unique | 578 // encapsulated, a user agent must exercise care to choose a unique |
580 // boundary. The boundary in the example above could have been the result of | 579 // boundary. The boundary in the example above could have been the result of |
581 // an algorithm designed to produce boundaries with a very low probability | 580 // an algorithm designed to produce boundaries with a very low probability |
582 // of already existing in the data to be encapsulated without having to | 581 // of already existing in the data to be encapsulated without having to |
583 // prescan the data. | 582 // prescan the data. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
628 post_data->append("\r\n" + value + "\r\n"); | 627 post_data->append("\r\n" + value + "\r\n"); |
629 } | 628 } |
630 | 629 |
631 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 630 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
632 std::string* post_data) { | 631 std::string* post_data) { |
633 DCHECK(post_data); | 632 DCHECK(post_data); |
634 post_data->append("--" + mime_boundary + "--\r\n"); | 633 post_data->append("--" + mime_boundary + "--\r\n"); |
635 } | 634 } |
636 | 635 |
637 } // namespace net | 636 } // namespace net |
OLD | NEW |