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..97a379ecb8c31080cad564106b0bd2c40226a936 100644 |
--- a/chrome/browser/chromeos/policy/upload_job_impl.cc |
+++ b/chrome/browser/chromeos/policy/upload_job_impl.cc |
@@ -4,15 +4,16 @@ |
#include "chrome/browser/chromeos/policy/upload_job_impl.h" |
+#include <stddef.h> |
#include <set> |
#include <utility> |
#include "base/logging.h" |
#include "base/macros.h" |
-#include "base/rand_util.h" |
#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; |
} // namespace |
@@ -95,10 +73,6 @@ class DataSegment { |
// Returns the size in bytes of the blob in |data_|. |
size_t GetDataSize() const; |
- // Helper method that performs a substring match of |chunk| in |data_|. |
- // Returns |true| if |chunk| matches a substring, |false| otherwise. |
- bool CheckIfDataContains(const std::string& chunk); |
- |
private: |
const std::string name_; |
const std::string filename_; |
@@ -137,28 +111,14 @@ scoped_ptr<std::string> DataSegment::GetData() { |
return std::move(data_); |
} |
-bool DataSegment::CheckIfDataContains(const std::string& chunk) { |
- DCHECK(data_); |
- return data_->find(chunk) != std::string::npos; |
-} |
- |
size_t DataSegment::GetDataSize() const { |
DCHECK(data_); |
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 +196,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); |
- 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 +206,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; |