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

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

Issue 1547593002: Introducing a net::GenerateMimeMultipartBoundary helper. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Self-review. Created 4 years, 12 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
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 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
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 DCHECK_EQ(40u, random_base64.size());
594
595 std::string result("----MultipartBoundary--");
596 result += random_base64;
597 result += "----";
Ryan Sleevi 2015/12/28 23:30:00 pedantry: result.append(random_base64); result.app
Łukasz Anforowicz 2015/12/29 18:53:19 Done. 1. I think "append" is slightly better for
Ryan Sleevi 2015/12/29 21:18:03 StringBuilder ALL the things ;)
Łukasz Anforowicz 2015/12/29 22:19:01 Acknowledged.
598 return result;
599 }
600
564 void AddMultipartValueForUpload(const std::string& value_name, 601 void AddMultipartValueForUpload(const std::string& value_name,
565 const std::string& value, 602 const std::string& value,
566 const std::string& mime_boundary, 603 const std::string& mime_boundary,
567 const std::string& content_type, 604 const std::string& content_type,
568 std::string* post_data) { 605 std::string* post_data) {
569 DCHECK(post_data); 606 DCHECK(post_data);
570 // First line is the boundary. 607 // First line is the boundary.
571 post_data->append("--" + mime_boundary + "\r\n"); 608 post_data->append("--" + mime_boundary + "\r\n");
572 // Next line is the Content-disposition. 609 // Next line is the Content-disposition.
573 post_data->append("Content-Disposition: form-data; name=\"" + 610 post_data->append("Content-Disposition: form-data; name=\"" +
574 value_name + "\"\r\n"); 611 value_name + "\"\r\n");
575 if (!content_type.empty()) { 612 if (!content_type.empty()) {
576 // If Content-type is specified, the next line is that. 613 // If Content-type is specified, the next line is that.
577 post_data->append("Content-Type: " + content_type + "\r\n"); 614 post_data->append("Content-Type: " + content_type + "\r\n");
578 } 615 }
579 // Leave an empty line and append the value. 616 // Leave an empty line and append the value.
580 post_data->append("\r\n" + value + "\r\n"); 617 post_data->append("\r\n" + value + "\r\n");
581 } 618 }
582 619
583 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, 620 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary,
584 std::string* post_data) { 621 std::string* post_data) {
585 DCHECK(post_data); 622 DCHECK(post_data);
586 post_data->append("--" + mime_boundary + "--\r\n"); 623 post_data->append("--" + mime_boundary + "--\r\n");
587 } 624 }
588 625
589 } // namespace net 626 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698