| 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/base64.h" |
| 10 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
| 11 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/rand_util.h" |
| 13 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 14 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
| 16 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 18 #include "build/build_config.h" | 20 #include "build/build_config.h" |
| 19 #include "net/base/mime_util.h" | 21 #include "net/base/mime_util.h" |
| 20 #include "net/base/platform_mime_util.h" | 22 #include "net/base/platform_mime_util.h" |
| 21 #include "net/http/http_util.h" | 23 #include "net/http/http_util.h" |
| 22 | 24 |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 | 556 |
| 555 GetExtensionsFromHardCodedMappings(secondary_mappings, | 557 GetExtensionsFromHardCodedMappings(secondary_mappings, |
| 556 arraysize(secondary_mappings), | 558 arraysize(secondary_mappings), |
| 557 mime_type, | 559 mime_type, |
| 558 &unique_extensions); | 560 &unique_extensions); |
| 559 } | 561 } |
| 560 | 562 |
| 561 HashSetToVector(&unique_extensions, extensions); | 563 HashSetToVector(&unique_extensions, extensions); |
| 562 } | 564 } |
| 563 | 565 |
| 566 NET_EXPORT std::string GenerateMimeMultipartBoundary() { |
| 567 // Based on RFC 1341, section "7.2.1 Multipart: The common syntax": |
| 568 // Because encapsulation boundaries must not appear in the body parts being |
| 569 // encapsulated, a user agent must exercise care to choose a unique |
| 570 // boundary. The boundary in the example above could have been the result of |
| 571 // an algorithm designed to produce boundaries with a very low probability |
| 572 // of already existing in the data to be encapsulated without having to |
| 573 // prescan the data. |
| 574 // [...] |
| 575 // the boundary parameter [...] consists of 1 to 70 characters from a set of |
| 576 // characters known to be very robust through email gateways, and NOT ending |
| 577 // with white space. |
| 578 // [...] |
| 579 // boundary := 0*69<bchars> bcharsnospace |
| 580 // bchars := bcharsnospace / " " |
| 581 // bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" / "+" / |
| 582 // "_" / "," / "-" / "." / "/" / ":" / "=" / "?" |
| 583 |
| 584 char random_values[30]; |
| 585 base::RandBytes(random_values, sizeof(random_values)); |
| 586 |
| 587 // Allowed base64 characters are a subset of bcharnospace above: |
| 588 // base64char := DIGIT / ALPHA / "+" / "/" / "=" |
| 589 // ("=" is used for padding which won't apply here). |
| 590 std::string random_base64; |
| 591 base::Base64Encode(base::StringPiece(random_values, sizeof(random_values)), |
| 592 &random_base64); |
| 593 |
| 594 std::string result("----MultipartBoundary--"); |
| 595 result.append(random_base64); |
| 596 result.append("----"); |
| 597 |
| 598 // Not a strict requirement - documentation only. |
| 599 DCHECK_EQ(67u, result.size()); |
| 600 |
| 601 return result; |
| 602 } |
| 603 |
| 564 void AddMultipartValueForUpload(const std::string& value_name, | 604 void AddMultipartValueForUpload(const std::string& value_name, |
| 565 const std::string& value, | 605 const std::string& value, |
| 566 const std::string& mime_boundary, | 606 const std::string& mime_boundary, |
| 567 const std::string& content_type, | 607 const std::string& content_type, |
| 568 std::string* post_data) { | 608 std::string* post_data) { |
| 569 DCHECK(post_data); | 609 DCHECK(post_data); |
| 570 // First line is the boundary. | 610 // First line is the boundary. |
| 571 post_data->append("--" + mime_boundary + "\r\n"); | 611 post_data->append("--" + mime_boundary + "\r\n"); |
| 572 // Next line is the Content-disposition. | 612 // Next line is the Content-disposition. |
| 573 post_data->append("Content-Disposition: form-data; name=\"" + | 613 post_data->append("Content-Disposition: form-data; name=\"" + |
| 574 value_name + "\"\r\n"); | 614 value_name + "\"\r\n"); |
| 575 if (!content_type.empty()) { | 615 if (!content_type.empty()) { |
| 576 // If Content-type is specified, the next line is that. | 616 // If Content-type is specified, the next line is that. |
| 577 post_data->append("Content-Type: " + content_type + "\r\n"); | 617 post_data->append("Content-Type: " + content_type + "\r\n"); |
| 578 } | 618 } |
| 579 // Leave an empty line and append the value. | 619 // Leave an empty line and append the value. |
| 580 post_data->append("\r\n" + value + "\r\n"); | 620 post_data->append("\r\n" + value + "\r\n"); |
| 581 } | 621 } |
| 582 | 622 |
| 583 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 623 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
| 584 std::string* post_data) { | 624 std::string* post_data) { |
| 585 DCHECK(post_data); | 625 DCHECK(post_data); |
| 586 post_data->append("--" + mime_boundary + "--\r\n"); | 626 post_data->append("--" + mime_boundary + "--\r\n"); |
| 587 } | 627 } |
| 588 | 628 |
| 589 } // namespace net | 629 } // namespace net |
| OLD | NEW |