OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "util/net/http_multipart_builder.h" | 15 #include "util/net/http_multipart_builder.h" |
16 | 16 |
17 #include <sys/types.h> | 17 #include <sys/types.h> |
18 | 18 |
19 #include <utility> | 19 #include <utility> |
20 #include <vector> | 20 #include <vector> |
21 | 21 |
22 #include "base/logging.h" | 22 #include "base/logging.h" |
23 #include "base/rand_util.h" | 23 #include "base/rand_util.h" |
24 #include "base/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
25 #include "util/net/http_body.h" | 25 #include "util/net/http_body.h" |
26 #include "util/net/http_body_gzip.h" | |
27 | 26 |
28 namespace crashpad { | 27 namespace crashpad { |
29 | 28 |
30 namespace { | 29 namespace { |
31 | 30 |
32 const char kCRLF[] = "\r\n"; | 31 const char kCRLF[] = "\r\n"; |
33 | 32 |
34 const char kBoundaryCRLF[] = "\r\n\r\n"; | 33 const char kBoundaryCRLF[] = "\r\n\r\n"; |
35 | 34 |
36 // Generates a random string suitable for use as a multipart boundary. | 35 // Generates a random string suitable for use as a multipart boundary. |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 c == '.' || | 109 c == '.' || |
111 c == '_' || | 110 c == '_' || |
112 c == '+' || | 111 c == '+' || |
113 c == '-'); | 112 c == '-'); |
114 } | 113 } |
115 } | 114 } |
116 | 115 |
117 } // namespace | 116 } // namespace |
118 | 117 |
119 HTTPMultipartBuilder::HTTPMultipartBuilder() | 118 HTTPMultipartBuilder::HTTPMultipartBuilder() |
120 : boundary_(GenerateBoundaryString()), | 119 : boundary_(GenerateBoundaryString()), form_data_(), file_attachments_() { |
121 form_data_(), | 120 } |
122 file_attachments_(), | |
123 gzip_enabled_(false) {} | |
124 | 121 |
125 HTTPMultipartBuilder::~HTTPMultipartBuilder() { | 122 HTTPMultipartBuilder::~HTTPMultipartBuilder() { |
126 } | 123 } |
127 | 124 |
128 void HTTPMultipartBuilder::SetGzipEnabled(bool gzip_enabled) { | |
129 gzip_enabled_ = gzip_enabled; | |
130 } | |
131 | |
132 void HTTPMultipartBuilder::SetFormData(const std::string& key, | 125 void HTTPMultipartBuilder::SetFormData(const std::string& key, |
133 const std::string& value) { | 126 const std::string& value) { |
134 EraseKey(key); | 127 EraseKey(key); |
135 form_data_[key] = value; | 128 form_data_[key] = value; |
136 } | 129 } |
137 | 130 |
138 void HTTPMultipartBuilder::SetFileAttachment( | 131 void HTTPMultipartBuilder::SetFileAttachment( |
139 const std::string& key, | 132 const std::string& key, |
140 const std::string& upload_file_name, | 133 const std::string& upload_file_name, |
141 const base::FilePath& path, | 134 const base::FilePath& path, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 attachment.content_type.c_str(), kBoundaryCRLF); | 172 attachment.content_type.c_str(), kBoundaryCRLF); |
180 | 173 |
181 streams.push_back(new StringHTTPBodyStream(header)); | 174 streams.push_back(new StringHTTPBodyStream(header)); |
182 streams.push_back(new FileHTTPBodyStream(attachment.path)); | 175 streams.push_back(new FileHTTPBodyStream(attachment.path)); |
183 streams.push_back(new StringHTTPBodyStream(kCRLF)); | 176 streams.push_back(new StringHTTPBodyStream(kCRLF)); |
184 } | 177 } |
185 | 178 |
186 streams.push_back( | 179 streams.push_back( |
187 new StringHTTPBodyStream("--" + boundary_ + "--" + kCRLF)); | 180 new StringHTTPBodyStream("--" + boundary_ + "--" + kCRLF)); |
188 | 181 |
189 auto composite = | 182 return std::unique_ptr<HTTPBodyStream>(new CompositeHTTPBodyStream(streams)); |
190 std::unique_ptr<HTTPBodyStream>(new CompositeHTTPBodyStream(streams)); | |
191 if (gzip_enabled_) { | |
192 return std::unique_ptr<HTTPBodyStream>( | |
193 new GzipHTTPBodyStream(std::move(composite))); | |
194 } | |
195 return composite; | |
196 } | 183 } |
197 | 184 |
198 void HTTPMultipartBuilder::PopulateContentHeaders( | 185 HTTPHeaders::value_type HTTPMultipartBuilder::GetContentType() const { |
199 HTTPHeaders* http_headers) const { | |
200 std::string content_type = | 186 std::string content_type = |
201 base::StringPrintf("multipart/form-data; boundary=%s", boundary_.c_str()); | 187 base::StringPrintf("multipart/form-data; boundary=%s", boundary_.c_str()); |
202 (*http_headers)[kContentType] = content_type; | 188 return std::make_pair(kContentType, content_type); |
203 | |
204 if (gzip_enabled_) { | |
205 (*http_headers)[kContentEncoding] = "gzip"; | |
206 } | |
207 } | 189 } |
208 | 190 |
209 void HTTPMultipartBuilder::EraseKey(const std::string& key) { | 191 void HTTPMultipartBuilder::EraseKey(const std::string& key) { |
210 auto data_it = form_data_.find(key); | 192 auto data_it = form_data_.find(key); |
211 if (data_it != form_data_.end()) | 193 if (data_it != form_data_.end()) |
212 form_data_.erase(data_it); | 194 form_data_.erase(data_it); |
213 | 195 |
214 auto file_it = file_attachments_.find(key); | 196 auto file_it = file_attachments_.find(key); |
215 if (file_it != file_attachments_.end()) | 197 if (file_it != file_attachments_.end()) |
216 file_attachments_.erase(file_it); | 198 file_attachments_.erase(file_it); |
217 } | 199 } |
218 | 200 |
219 } // namespace crashpad | 201 } // namespace crashpad |
OLD | NEW |