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" | |
16 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
17 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
19 #include "base/task_runner_util.h" | 18 #include "base/task_runner_util.h" |
20 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
21 #include "base/values.h" | 20 #include "base/values.h" |
22 #include "google_apis/drive/drive_api_parser.h" | 21 #include "google_apis/drive/drive_api_parser.h" |
23 #include "google_apis/drive/request_sender.h" | 22 #include "google_apis/drive/request_sender.h" |
24 #include "google_apis/drive/request_util.h" | 23 #include "google_apis/drive/request_util.h" |
25 #include "google_apis/drive/task_util.h" | 24 #include "google_apis/drive/task_util.h" |
26 #include "google_apis/drive/time_util.h" | 25 #include "google_apis/drive/time_util.h" |
27 #include "net/base/elements_upload_data_stream.h" | 26 #include "net/base/elements_upload_data_stream.h" |
28 #include "net/base/io_buffer.h" | 27 #include "net/base/io_buffer.h" |
29 #include "net/base/load_flags.h" | 28 #include "net/base/load_flags.h" |
| 29 #include "net/base/mime_util.h" |
30 #include "net/base/net_errors.h" | 30 #include "net/base/net_errors.h" |
31 #include "net/base/upload_bytes_element_reader.h" | 31 #include "net/base/upload_bytes_element_reader.h" |
32 #include "net/base/upload_data_stream.h" | 32 #include "net/base/upload_data_stream.h" |
33 #include "net/base/upload_element_reader.h" | 33 #include "net/base/upload_element_reader.h" |
34 #include "net/base/upload_file_element_reader.h" | 34 #include "net/base/upload_file_element_reader.h" |
35 #include "net/http/http_byte_range.h" | 35 #include "net/http/http_byte_range.h" |
36 #include "net/http/http_response_headers.h" | 36 #include "net/http/http_response_headers.h" |
37 #include "net/http/http_util.h" | 37 #include "net/http/http_util.h" |
38 #include "net/url_request/url_fetcher.h" | 38 #include "net/url_request/url_fetcher.h" |
39 #include "net/url_request/url_request_status.h" | 39 #include "net/url_request/url_request_status.h" |
(...skipping 29 matching lines...) Expand all Loading... |
69 // Mime type of multipart mixed. | 69 // Mime type of multipart mixed. |
70 const char kMultipartMixedMimeTypePrefix[] = | 70 const char kMultipartMixedMimeTypePrefix[] = |
71 "multipart/mixed; boundary="; | 71 "multipart/mixed; boundary="; |
72 | 72 |
73 // Header for each item in a multipart message. | 73 // Header for each item in a multipart message. |
74 const char kMultipartItemHeaderFormat[] = "--%s\nContent-Type: %s\n\n"; | 74 const char kMultipartItemHeaderFormat[] = "--%s\nContent-Type: %s\n\n"; |
75 | 75 |
76 // Footer for whole multipart message. | 76 // Footer for whole multipart message. |
77 const char kMultipartFooterFormat[] = "--%s--"; | 77 const char kMultipartFooterFormat[] = "--%s--"; |
78 | 78 |
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 | 79 // Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on |
87 // the calling thread when finished with either success or failure. | 80 // the calling thread when finished with either success or failure. |
88 // The callback must not be null. | 81 // The callback must not be null. |
89 void ParseJsonOnBlockingPool( | 82 void ParseJsonOnBlockingPool( |
90 base::TaskRunner* blocking_task_runner, | 83 base::TaskRunner* blocking_task_runner, |
91 const std::string& json, | 84 const std::string& json, |
92 const base::Callback<void(scoped_ptr<base::Value> value)>& callback) { | 85 const base::Callback<void(scoped_ptr<base::Value> value)>& callback) { |
93 base::PostTaskAndReplyWithResult( | 86 base::PostTaskAndReplyWithResult( |
94 blocking_task_runner, | 87 blocking_task_runner, |
95 FROM_HERE, | 88 FROM_HERE, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 | 213 |
221 void GenerateMultipartBody(MultipartType multipart_type, | 214 void GenerateMultipartBody(MultipartType multipart_type, |
222 const std::string& predetermined_boundary, | 215 const std::string& predetermined_boundary, |
223 const std::vector<ContentTypeAndData>& parts, | 216 const std::vector<ContentTypeAndData>& parts, |
224 ContentTypeAndData* output, | 217 ContentTypeAndData* output, |
225 std::vector<uint64_t>* data_offset) { | 218 std::vector<uint64_t>* data_offset) { |
226 std::string boundary; | 219 std::string boundary; |
227 // Generate random boundary. | 220 // Generate random boundary. |
228 if (predetermined_boundary.empty()) { | 221 if (predetermined_boundary.empty()) { |
229 while (true) { | 222 while (true) { |
230 boundary.resize(kBoundarySize); | 223 boundary = net::GenerateMimeMultipartBoundary(); |
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; | 224 bool conflict_with_content = false; |
238 for (auto& part : parts) { | 225 for (auto& part : parts) { |
239 if (part.data.find(boundary, 0) != std::string::npos) { | 226 if (part.data.find(boundary, 0) != std::string::npos) { |
240 conflict_with_content = true; | 227 conflict_with_content = true; |
241 break; | 228 break; |
242 } | 229 } |
243 } | 230 } |
244 if (!conflict_with_content) | 231 if (!conflict_with_content) |
245 break; | 232 break; |
246 } | 233 } |
(...skipping 768 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1015 download_action_callback_.Run(code, temp_file); | 1002 download_action_callback_.Run(code, temp_file); |
1016 OnProcessURLFetchResultsComplete(); | 1003 OnProcessURLFetchResultsComplete(); |
1017 } | 1004 } |
1018 | 1005 |
1019 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( | 1006 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( |
1020 DriveApiErrorCode code) { | 1007 DriveApiErrorCode code) { |
1021 download_action_callback_.Run(code, base::FilePath()); | 1008 download_action_callback_.Run(code, base::FilePath()); |
1022 } | 1009 } |
1023 | 1010 |
1024 } // namespace google_apis | 1011 } // namespace google_apis |
OLD | NEW |