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 "google_apis/drive/base_requests.h" | 5 #include "google_apis/drive/base_requests.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
16 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/task_runner_util.h" | 19 #include "base/task_runner_util.h" |
20 #include "base/thread_task_runner_handle.h" | 20 #include "base/thread_task_runner_handle.h" |
21 #include "base/values.h" | 21 #include "base/values.h" |
22 #include "google_apis/drive/drive_api_parser.h" | 22 #include "google_apis/drive/drive_api_parser.h" |
23 #include "google_apis/drive/request_sender.h" | 23 #include "google_apis/drive/request_sender.h" |
24 #include "google_apis/drive/request_util.h" | 24 #include "google_apis/drive/request_util.h" |
25 #include "google_apis/drive/task_util.h" | 25 #include "google_apis/drive/task_util.h" |
26 #include "google_apis/drive/time_util.h" | 26 #include "google_apis/drive/time_util.h" |
27 #include "net/base/elements_upload_data_stream.h" | 27 #include "net/base/elements_upload_data_stream.h" |
28 #include "net/base/io_buffer.h" | 28 #include "net/base/io_buffer.h" |
29 #include "net/base/load_flags.h" | 29 #include "net/base/load_flags.h" |
30 #include "net/base/mime_util.h" | |
30 #include "net/base/net_errors.h" | 31 #include "net/base/net_errors.h" |
31 #include "net/base/upload_bytes_element_reader.h" | 32 #include "net/base/upload_bytes_element_reader.h" |
32 #include "net/base/upload_data_stream.h" | 33 #include "net/base/upload_data_stream.h" |
33 #include "net/base/upload_element_reader.h" | 34 #include "net/base/upload_element_reader.h" |
34 #include "net/base/upload_file_element_reader.h" | 35 #include "net/base/upload_file_element_reader.h" |
35 #include "net/http/http_byte_range.h" | 36 #include "net/http/http_byte_range.h" |
36 #include "net/http/http_response_headers.h" | 37 #include "net/http/http_response_headers.h" |
37 #include "net/http/http_util.h" | 38 #include "net/http/http_util.h" |
38 #include "net/url_request/url_fetcher.h" | 39 #include "net/url_request/url_fetcher.h" |
39 #include "net/url_request/url_request_status.h" | 40 #include "net/url_request/url_request_status.h" |
(...skipping 29 matching lines...) Expand all Loading... | |
69 // Mime type of multipart mixed. | 70 // Mime type of multipart mixed. |
70 const char kMultipartMixedMimeTypePrefix[] = | 71 const char kMultipartMixedMimeTypePrefix[] = |
71 "multipart/mixed; boundary="; | 72 "multipart/mixed; boundary="; |
72 | 73 |
73 // Header for each item in a multipart message. | 74 // Header for each item in a multipart message. |
74 const char kMultipartItemHeaderFormat[] = "--%s\nContent-Type: %s\n\n"; | 75 const char kMultipartItemHeaderFormat[] = "--%s\nContent-Type: %s\n\n"; |
75 | 76 |
76 // Footer for whole multipart message. | 77 // Footer for whole multipart message. |
77 const char kMultipartFooterFormat[] = "--%s--"; | 78 const char kMultipartFooterFormat[] = "--%s--"; |
78 | 79 |
79 // Characters to be used for multipart/related boundary. | |
80 const char kBoundaryCharacters[] = | |
81 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
82 | |
83 // Size of multipart/related's boundary. | |
84 const char kBoundarySize = 70; | |
85 | |
86 // Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on | 80 // Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on |
87 // the calling thread when finished with either success or failure. | 81 // the calling thread when finished with either success or failure. |
88 // The callback must not be null. | 82 // The callback must not be null. |
89 void ParseJsonOnBlockingPool( | 83 void ParseJsonOnBlockingPool( |
90 base::TaskRunner* blocking_task_runner, | 84 base::TaskRunner* blocking_task_runner, |
91 const std::string& json, | 85 const std::string& json, |
92 const base::Callback<void(scoped_ptr<base::Value> value)>& callback) { | 86 const base::Callback<void(scoped_ptr<base::Value> value)>& callback) { |
93 base::PostTaskAndReplyWithResult( | 87 base::PostTaskAndReplyWithResult( |
94 blocking_task_runner, | 88 blocking_task_runner, |
95 FROM_HERE, | 89 FROM_HERE, |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 } | 213 } |
220 | 214 |
221 void GenerateMultipartBody(MultipartType multipart_type, | 215 void GenerateMultipartBody(MultipartType multipart_type, |
222 const std::string& predetermined_boundary, | 216 const std::string& predetermined_boundary, |
223 const std::vector<ContentTypeAndData>& parts, | 217 const std::vector<ContentTypeAndData>& parts, |
224 ContentTypeAndData* output, | 218 ContentTypeAndData* output, |
225 std::vector<uint64_t>* data_offset) { | 219 std::vector<uint64_t>* data_offset) { |
226 std::string boundary; | 220 std::string boundary; |
227 // Generate random boundary. | 221 // Generate random boundary. |
228 if (predetermined_boundary.empty()) { | 222 if (predetermined_boundary.empty()) { |
229 while (true) { | 223 boundary = net::GenerateMimeMultipartBoundary(); |
230 boundary.resize(kBoundarySize); | |
231 for (int i = 0; i < kBoundarySize; ++i) { | |
232 // Subtract 2 from the array size to exclude '\0', and to turn the size | |
233 // into the last index. | |
234 const int last_char_index = arraysize(kBoundaryCharacters) - 2; | |
235 boundary[i] = kBoundaryCharacters[base::RandInt(0, last_char_index)]; | |
236 } | |
237 bool conflict_with_content = false; | |
hashimoto
2016/01/05 07:16:09
The new implementation generates much less variety
Łukasz Anforowicz
2016/01/05 19:16:49
The new implementation is generating 256^30 string
hashimoto
2016/01/07 05:15:48
lgtm
Łukasz Anforowicz
2016/01/07 19:04:13
Thank you very much for pointing this out. This d
| |
238 for (auto& part : parts) { | |
239 if (part.data.find(boundary, 0) != std::string::npos) { | |
240 conflict_with_content = true; | |
241 break; | |
242 } | |
243 } | |
244 if (!conflict_with_content) | |
245 break; | |
246 } | |
247 } else { | 224 } else { |
248 boundary = predetermined_boundary; | 225 boundary = predetermined_boundary; |
249 } | 226 } |
250 | 227 |
251 switch (multipart_type) { | 228 switch (multipart_type) { |
252 case MULTIPART_RELATED: | 229 case MULTIPART_RELATED: |
253 output->type = kMultipartRelatedMimeTypePrefix + boundary; | 230 output->type = kMultipartRelatedMimeTypePrefix + boundary; |
254 break; | 231 break; |
255 case MULTIPART_MIXED: | 232 case MULTIPART_MIXED: |
256 output->type = kMultipartMixedMimeTypePrefix + boundary; | 233 output->type = kMultipartMixedMimeTypePrefix + boundary; |
(...skipping 758 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1015 download_action_callback_.Run(code, temp_file); | 992 download_action_callback_.Run(code, temp_file); |
1016 OnProcessURLFetchResultsComplete(); | 993 OnProcessURLFetchResultsComplete(); |
1017 } | 994 } |
1018 | 995 |
1019 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( | 996 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( |
1020 DriveApiErrorCode code) { | 997 DriveApiErrorCode code) { |
1021 download_action_callback_.Run(code, base::FilePath()); | 998 download_action_callback_.Run(code, base::FilePath()); |
1022 } | 999 } |
1023 | 1000 |
1024 } // namespace google_apis | 1001 } // namespace google_apis |
OLD | NEW |