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

Unified Diff: chrome/browser/chromeos/policy/upload_job_impl.cc

Issue 1547593002: Introducing a net::GenerateMimeMultipartBoundary helper. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebasing... 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/policy/upload_job_impl.cc
diff --git a/chrome/browser/chromeos/policy/upload_job_impl.cc b/chrome/browser/chromeos/policy/upload_job_impl.cc
index f1a4783040c9f6d73659bc901711d06511f9ffdb..4b8c966884869d12fa52ffa649a4f7f6b8546460 100644
--- a/chrome/browser/chromeos/policy/upload_job_impl.cc
+++ b/chrome/browser/chromeos/policy/upload_job_impl.cc
@@ -13,6 +13,7 @@
#include "base/strings/stringprintf.h"
#include "google_apis/gaia/gaia_constants.h"
#include "google_apis/gaia/google_service_auth_error.h"
+#include "net/base/mime_util.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_request_status.h"
@@ -20,41 +21,18 @@ namespace policy {
namespace {
-// Defines the characters that might appear in strings generated by
-// GenerateRandomString().
-const char kAlphaNum[] =
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-
// Format for bearer tokens in HTTP requests to access OAuth 2.0 protected
// resources.
const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s";
-// Prefix added to a randomly generated string when choosing the MIME boundary.
-const char kMultipartBoundaryPrefix[] = "----**--";
-
-// Postfix added to a randomly generated string when choosing the MIME boundary.
-const char kMultipartBoundaryPostfix[] = "--**----";
-
// Value the "Content-Type" field will be set to in the POST request.
const char kUploadContentType[] = "multipart/form-data";
-// Number of retries when randomly generating a MIME boundary.
-const int kMimeBoundaryRetries = 3;
-
-// Length of the random string for the MIME boundary.
-const int kMimeBoundarySize = 32;
-
// Number of upload retries.
const int kMaxRetries = 1;
-// Generates a random alphanumeric string of length |length|.
-std::string GenerateRandomString(size_t length) {
- std::string random;
- random.reserve(length);
- for (size_t i = 0; i < length; i++)
- random.push_back(kAlphaNum[base::RandGenerator(sizeof(kAlphaNum) - 1)]);
- return random;
-}
+// Max size of MIME boundary according to RFC 1341, section 7.2.1.
+const size_t kMaxMimeBoundarySize = 70;
bartfab (slow) 2016/01/05 14:20:45 Nit: #include <stddef.h>
Łukasz Anforowicz 2016/01/05 19:16:48 Done.
} // namespace
@@ -147,18 +125,9 @@ size_t DataSegment::GetDataSize() const {
return data_->size();
}
-std::string UploadJobImpl::RandomMimeBoundaryGenerator::GenerateBoundary(
- size_t length) const {
- std::string boundary;
- boundary.reserve(length);
- DCHECK_GT(length, sizeof(kMultipartBoundaryPrefix) +
- sizeof(kMultipartBoundaryPostfix));
- const size_t random_part_length = length - sizeof(kMultipartBoundaryPrefix) -
- sizeof(kMultipartBoundaryPostfix);
- boundary.append(kMultipartBoundaryPrefix);
- boundary.append(GenerateRandomString(random_part_length));
- boundary.append(kMultipartBoundaryPostfix);
- return boundary;
+std::string UploadJobImpl::RandomMimeBoundaryGenerator::GenerateBoundary()
+ const {
+ return net::GenerateMimeMultipartBoundary();
}
UploadJobImpl::UploadJobImpl(
@@ -236,30 +205,8 @@ bool UploadJobImpl::SetUpMultipart() {
return false;
}
- // Generates random MIME boundaries and tests if they appear in any of the
- // data segments. Tries up to |kMimeBoundaryRetries| times to find a MIME
- // boundary that does not appear within any data segment.
- bool found = false;
- int retry = 0;
- do {
- found = true;
- mime_boundary_.reset(new std::string(
- boundary_generator_->GenerateBoundary(kMimeBoundarySize)));
- for (const auto& data_segment : data_segments_) {
- if (data_segment->CheckIfDataContains(*mime_boundary_)) {
- found = false;
- break;
- }
- }
- ++retry;
- } while (!found && retry <= kMimeBoundaryRetries);
-
- // Notify the delegate that content encoding failed.
- if (!found) {
- delegate_->OnFailure(CONTENT_ENCODING_ERROR);
bartfab (slow) 2016/01/05 14:20:45 Nit: Remove CONTENT_ENCODING_ERROR from upload_job
Łukasz Anforowicz 2016/01/05 19:16:48 Done.
- mime_boundary_.reset();
- return false;
- }
+ mime_boundary_.reset(
+ new std::string(boundary_generator_->GenerateBoundary()));
// Estimate an upper bound for the total message size to make memory
// allocation more efficient. It is not an error if this turns out to be too
@@ -268,7 +215,7 @@ bool UploadJobImpl::SetUpMultipart() {
for (const auto& data_segment : data_segments_) {
for (const auto& entry : data_segment->GetHeaderEntries())
size += entry.first.size() + entry.second.size();
- size += kMimeBoundarySize + data_segment->GetName().size() +
+ size += kMaxMimeBoundarySize + data_segment->GetName().size() +
data_segment->GetFilename().size() + data_segment->GetDataSize();
// Add some extra space for all the constants and control characters.
size += 128;

Powered by Google App Engine
This is Rietveld 408576698