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

Side by Side Diff: google_apis/drive/base_requests.cc

Issue 805153006: Drive API: Generates randum boundary for multipart uploading. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "google_apis/drive/base_requests.h" 5 #include "google_apis/drive/base_requests.h"
6 6
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/rand_util.h"
11 #include "base/sequenced_task_runner.h" 12 #include "base/sequenced_task_runner.h"
12 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
14 #include "base/task_runner_util.h" 15 #include "base/task_runner_util.h"
15 #include "base/values.h" 16 #include "base/values.h"
16 #include "google_apis/drive/drive_api_parser.h" 17 #include "google_apis/drive/drive_api_parser.h"
17 #include "google_apis/drive/request_sender.h" 18 #include "google_apis/drive/request_sender.h"
18 #include "google_apis/drive/request_util.h" 19 #include "google_apis/drive/request_util.h"
19 #include "google_apis/drive/task_util.h" 20 #include "google_apis/drive/task_util.h"
20 #include "google_apis/drive/time_util.h" 21 #include "google_apis/drive/time_util.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 const char kUploadResponseRange[] = "range"; 55 const char kUploadResponseRange[] = "range";
55 56
56 // The prefix of multipart/related mime type. 57 // The prefix of multipart/related mime type.
57 const char kMultipartMimeTypePrefix[] = "multipart/related; boundary="; 58 const char kMultipartMimeTypePrefix[] = "multipart/related; boundary=";
58 59
59 // Template for multipart request body. 60 // Template for multipart request body.
60 const char kMessageFormatBeforeFile[] = 61 const char kMessageFormatBeforeFile[] =
61 "--%s\nContent-Type: %s\n\n%s\n--%s\nContent-Type: %s\n\n"; 62 "--%s\nContent-Type: %s\n\n%s\n--%s\nContent-Type: %s\n\n";
62 const char kMessageFormatAfterFile[] = "\n--%s--"; 63 const char kMessageFormatAfterFile[] = "\n--%s--";
63 64
64 // The predetermined boundary string for multipart/related. 65 // Characters to be used for multipart/related boundary.
65 // TODO(hirono): Generates the boundary string randomly and ensure the string 66 const char kBoundaryCharacters[] =
66 // does not appears in the content parts. 67 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
67 const char kBoundaryForPrototype[] = 68 // Size of multipart/related's boundary.
68 "LJx6AFbwBL94CmojDJBcmXpl1WfnvCUOenZqWrpI1Ua3l7xv8Y"; 69 const char kBoundarySize = 70;
69 70
70 // Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on 71 // Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on
71 // the calling thread when finished with either success or failure. 72 // the calling thread when finished with either success or failure.
72 // The callback must not be null. 73 // The callback must not be null.
73 void ParseJsonOnBlockingPool( 74 void ParseJsonOnBlockingPool(
74 base::TaskRunner* blocking_task_runner, 75 base::TaskRunner* blocking_task_runner,
75 const std::string& json, 76 const std::string& json,
76 const base::Callback<void(scoped_ptr<base::Value> value)>& callback) { 77 const base::Callback<void(scoped_ptr<base::Value> value)>& callback) {
77 base::PostTaskAndReplyWithResult( 78 base::PostTaskAndReplyWithResult(
78 blocking_task_runner, 79 blocking_task_runner,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 last_viewed_by_me_date)); 134 last_viewed_by_me_date));
134 } 135 }
135 136
136 std::string json_string; 137 std::string json_string;
137 base::JSONWriter::Write(&root, &json_string); 138 base::JSONWriter::Write(&root, &json_string);
138 return json_string; 139 return json_string;
139 } 140 }
140 141
141 // Obtains the multipart body for the metadata string and file contents. If 142 // Obtains the multipart body for the metadata string and file contents. If
142 // predetermined_boundary is empty, the function generates the boundary string. 143 // predetermined_boundary is empty, the function generates the boundary string.
143 // TODO(hirono): Generates the boundary string randomly and ensure the string
144 // does not appears in the content parts.
145 bool GetMultipartContent(const std::string& predetermined_boundary, 144 bool GetMultipartContent(const std::string& predetermined_boundary,
146 const std::string& metadata_json, 145 const std::string& metadata_json,
147 const std::string& content_type, 146 const std::string& content_type,
148 const base::FilePath& path, 147 const base::FilePath& path,
149 std::string* upload_content_type, 148 std::string* upload_content_type,
150 std::string* upload_content_data) { 149 std::string* upload_content_data) {
151 std::string file_content; 150 std::string file_content;
152 if (!ReadFileToString(path, &file_content)) 151 if (!ReadFileToString(path, &file_content))
153 return false; 152 return false;
154 153
155 const std::string boundary = predetermined_boundary.empty() 154 std::string boundary;
156 ? kBoundaryForPrototype 155 if (predetermined_boundary.empty()) {
157 : predetermined_boundary; 156 while (true) {
158 157 boundary.resize(kBoundarySize);
159 *upload_content_type = kMultipartMimeTypePrefix + boundary; 158 for (int i = 0; i < kBoundarySize; ++i) {
159 // Subtract 2 from the array size to exclude '\0', and to turn the size
160 // into the last index.
161 const int last_char_index = arraysize(kBoundaryCharacters) - 2;
162 boundary[i] = kBoundaryCharacters[base::RandInt(0, last_char_index)];
163 }
164 if (metadata_json.find(boundary, 0) == std::string::npos &&
165 file_content.find(boundary, 0) == std::string::npos) {
166 break;
167 }
168 }
169 } else {
170 boundary = predetermined_boundary;
171 }
160 const std::string body_before_file = base::StringPrintf( 172 const std::string body_before_file = base::StringPrintf(
161 kMessageFormatBeforeFile, boundary.c_str(), "application/json", 173 kMessageFormatBeforeFile, boundary.c_str(), "application/json",
162 metadata_json.c_str(), boundary.c_str(), content_type.c_str()); 174 metadata_json.c_str(), boundary.c_str(), content_type.c_str());
163 const std::string body_after_file = 175 const std::string body_after_file =
164 base::StringPrintf(kMessageFormatAfterFile, boundary.c_str()); 176 base::StringPrintf(kMessageFormatAfterFile, boundary.c_str());
177
178 *upload_content_type = kMultipartMimeTypePrefix + boundary;
165 *upload_content_data = body_before_file + file_content + body_after_file; 179 *upload_content_data = body_before_file + file_content + body_after_file;
166
167 return true; 180 return true;
168 } 181 }
169 182
170 } // namespace 183 } // namespace
171 184
172 namespace google_apis { 185 namespace google_apis {
173 186
174 scoped_ptr<base::Value> ParseJson(const std::string& json) { 187 scoped_ptr<base::Value> ParseJson(const std::string& json) {
175 int error_code = -1; 188 int error_code = -1;
176 std::string error_message; 189 std::string error_message;
(...skipping 768 matching lines...) Expand 10 before | Expand all | Expand 10 after
945 download_action_callback_.Run(code, temp_file); 958 download_action_callback_.Run(code, temp_file);
946 OnProcessURLFetchResultsComplete(); 959 OnProcessURLFetchResultsComplete();
947 } 960 }
948 961
949 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( 962 void DownloadFileRequestBase::RunCallbackOnPrematureFailure(
950 GDataErrorCode code) { 963 GDataErrorCode code) {
951 download_action_callback_.Run(code, base::FilePath()); 964 download_action_callback_.Run(code, base::FilePath());
952 } 965 }
953 966
954 } // namespace google_apis 967 } // namespace google_apis
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698