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

Side by Side Diff: third_party/crashpad/crashpad/util/net/http_multipart_builder.cc

Issue 1505213004: Copy Crashpad into the Chrome tree instead of importing it via DEPS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update Created 5 years 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
OLDNEW
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/net/http_multipart_builder.h"
16
17 #include <utility>
18 #include <vector>
19
20 #include "base/logging.h"
21 #include "base/rand_util.h"
22 #include "base/strings/stringprintf.h"
23 #include "util/net/http_body.h"
24
25 namespace crashpad {
26
27 namespace {
28
29 const char kCRLF[] = "\r\n";
30
31 const char kBoundaryCRLF[] = "\r\n\r\n";
32
33 // Generates a random string suitable for use as a multipart boundary.
34 std::string GenerateBoundaryString() {
35 // RFC 2046 §5.1.1 says that the boundary string may be 1 to 70 characters
36 // long, choosing from the set of alphanumeric characters along with
37 // characters from the set “'()+_,-./:=? ”, and not ending in a space.
38 // However, some servers have been observed as dealing poorly with certain
39 // nonalphanumeric characters. See
40 // blink/Source/platform/network/FormDataBuilder.cpp
41 // blink::FormDataBuilder::generateUniqueBoundaryString().
42 //
43 // This implementation produces a 56-character string with over 190 bits of
44 // randomness (62^32 > 2^190).
45 std::string boundary_string = "---MultipartBoundary-";
46 for (int index = 0; index < 32; ++index) {
47 const char kCharacters[] =
48 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
49 int random_value =
50 base::RandInt(0, static_cast<int>(strlen(kCharacters)) - 1);
51 boundary_string += kCharacters[random_value];
52 }
53 boundary_string += "---";
54 return boundary_string;
55 }
56
57 // Escapes the specified name to be suitable for the name field of a
58 // form-data part.
59 std::string EncodeMIMEField(const std::string& name) {
60 // RFC 2388 §3 says to encode non-ASCII field names according to RFC 2047, but
61 // no browsers implement that behavior. Instead, they send field names in the
62 // page hosting the form’s encoding. However, some form of escaping is needed.
63 // This URL-escapes the quote character and newline characters, per Blink. See
64 // blink/Source/platform/network/FormDataBuilder.cpp
65 // blink::appendQuotedString().
66 //
67 // TODO(mark): This encoding is not necessarily correct, and the same code in
68 // Blink is marked with a FIXME. Blink does not escape the '%' character,
69 // that’s a local addition, but it seems appropriate to be able to decode the
70 // string properly.
71 std::string encoded;
72 for (char character : name) {
73 switch (character) {
74 case '\r':
75 case '\n':
76 case '"':
77 case '%':
78 encoded += base::StringPrintf("%%%02x", character);
79 break;
80 default:
81 encoded += character;
82 break;
83 }
84 }
85
86 return encoded;
87 }
88
89 // Returns a string, formatted with a multipart boundary and a field name,
90 // after which the contents of the part at |name| can be appended.
91 std::string GetFormDataBoundary(const std::string& boundary,
92 const std::string& name) {
93 return base::StringPrintf(
94 "--%s%sContent-Disposition: form-data; name=\"%s\"",
95 boundary.c_str(),
96 kCRLF,
97 EncodeMIMEField(name).c_str());
98 }
99
100 void AssertSafeMIMEType(const std::string& string) {
101 for (size_t i = 0; i < string.length(); ++i) {
102 char c = string[i];
103 CHECK((c >= 'a' && c <= 'z') ||
104 (c >= 'A' && c <= 'Z') ||
105 (c >= '0' && c <= '9') ||
106 c == '/' ||
107 c == '.' ||
108 c == '_' ||
109 c == '+' ||
110 c == '-');
111 }
112 }
113
114 } // namespace
115
116 HTTPMultipartBuilder::HTTPMultipartBuilder()
117 : boundary_(GenerateBoundaryString()), form_data_(), file_attachments_() {
118 }
119
120 HTTPMultipartBuilder::~HTTPMultipartBuilder() {
121 }
122
123 void HTTPMultipartBuilder::SetFormData(const std::string& key,
124 const std::string& value) {
125 EraseKey(key);
126 form_data_[key] = value;
127 }
128
129 void HTTPMultipartBuilder::SetFileAttachment(
130 const std::string& key,
131 const std::string& upload_file_name,
132 const base::FilePath& path,
133 const std::string& content_type) {
134 EraseKey(upload_file_name);
135
136 FileAttachment attachment;
137 attachment.filename = EncodeMIMEField(upload_file_name);
138 attachment.path = path;
139
140 if (content_type.empty()) {
141 attachment.content_type = "application/octet-stream";
142 } else {
143 AssertSafeMIMEType(content_type);
144 attachment.content_type = content_type;
145 }
146
147 file_attachments_[key] = attachment;
148 }
149
150 scoped_ptr<HTTPBodyStream> HTTPMultipartBuilder::GetBodyStream() {
151 // The objects inserted into this vector will be owned by the returned
152 // CompositeHTTPBodyStream. Take care to not early-return without deleting
153 // this memory.
154 std::vector<HTTPBodyStream*> streams;
155
156 for (const auto& pair : form_data_) {
157 std::string field = GetFormDataBoundary(boundary_, pair.first);
158 field += kBoundaryCRLF;
159 field += pair.second;
160 field += kCRLF;
161 streams.push_back(new StringHTTPBodyStream(field));
162 }
163
164 for (const auto& pair : file_attachments_) {
165 const FileAttachment& attachment = pair.second;
166 std::string header = GetFormDataBoundary(boundary_, pair.first);
167 header += base::StringPrintf("; filename=\"%s\"%s",
168 attachment.filename.c_str(), kCRLF);
169 header += base::StringPrintf("Content-Type: %s%s",
170 attachment.content_type.c_str(), kBoundaryCRLF);
171
172 streams.push_back(new StringHTTPBodyStream(header));
173 streams.push_back(new FileHTTPBodyStream(attachment.path));
174 streams.push_back(new StringHTTPBodyStream(kCRLF));
175 }
176
177 streams.push_back(
178 new StringHTTPBodyStream("--" + boundary_ + "--" + kCRLF));
179
180 return scoped_ptr<HTTPBodyStream>(new CompositeHTTPBodyStream(streams));
181 }
182
183 HTTPHeaders::value_type HTTPMultipartBuilder::GetContentType() const {
184 std::string content_type =
185 base::StringPrintf("multipart/form-data; boundary=%s", boundary_.c_str());
186 return std::make_pair(kContentType, content_type);
187 }
188
189 void HTTPMultipartBuilder::EraseKey(const std::string& key) {
190 auto data_it = form_data_.find(key);
191 if (data_it != form_data_.end())
192 form_data_.erase(data_it);
193
194 auto file_it = file_attachments_.find(key);
195 if (file_it != file_attachments_.end())
196 file_attachments_.erase(file_it);
197 }
198
199 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698