Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 <vector> | |
| 18 | |
| 19 #include "base/rand_util.h" | |
| 20 #include "base/strings/stringprintf.h" | |
| 21 #include "util/net/http_body.h" | |
| 22 | |
| 23 namespace crashpad { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // Generates a random string suitable for use as a multipart boundary. | |
| 28 std::string GenerateBoundaryString() { | |
| 29 // RFC 2046 §5.1.1 says that the boundary string may be 1 to 70 characters | |
| 30 // long, choosing from the set of alphanumeric characters along with | |
| 31 // characters from the set “'()+_,-./:=? ”, and not ending in a space. | |
| 32 // However, some servers have been observed as dealing poorly with certain | |
| 33 // nonalphanumeric characters. See | |
| 34 // blink/Source/platform/network/FormDataBuilder.cpp | |
| 35 // blink::FormDataBuilder::generateUniqueBoundaryString(). | |
| 36 // | |
| 37 // This implementation produces a 56-character string with over 190 bits of | |
| 38 // randomness (62^32 > 2^190). | |
| 39 std::string boundary_string = "---MultipartBoundary-"; | |
| 40 for (int index = 0; index < 32; ++index) { | |
| 41 const char kCharacters[] = | |
| 42 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
| 43 int random_value = base::RandGenerator(strlen(kCharacters)); | |
| 44 boundary_string += kCharacters[random_value]; | |
| 45 } | |
| 46 boundary_string += "---"; | |
| 47 return boundary_string; | |
| 48 } | |
| 49 | |
| 50 // Escapes the specified name to be suitable for the name field of a | |
| 51 // form-data part. | |
| 52 std::string EncodeFieldName(const std::string& name) { | |
| 53 // RFC 2388 §3 says to encode non-ASCII field names according to RFC 2047, but | |
| 54 // no browsers implement that behavior. Instead, they send field names in the | |
| 55 // page hosting the form’s encoding. However, some form of escaping is needed. | |
| 56 // This URL-escapes the quote character and newline characters, per Blink. See | |
| 57 // blink/Source/platform/network/FormDataBuilder.cpp | |
| 58 // blink::appendQuotedString(). | |
| 59 // | |
| 60 // TODO(mark): This encoding is not necessarily correct, and the same code in | |
| 61 // Blink is marked with a FIXME. Blink does not escape the '%' character, | |
| 62 // that’s a local addition, but it seems appropriate to be able to decode the | |
| 63 // string properly. | |
| 64 std::string encoded; | |
| 65 for (char character : name) { | |
| 66 switch (character) { | |
| 67 case '\r': | |
| 68 case '\n': | |
| 69 case '"': | |
| 70 case '%': | |
| 71 encoded += base::StringPrintf("%%%02x", character); | |
| 72 break; | |
| 73 default: | |
| 74 encoded += character; | |
| 75 break; | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 return encoded; | |
| 80 } | |
| 81 | |
| 82 const char kBoundaryCRLF[] = "\r\n\r\n"; | |
|
Mark Mentovai
2014/10/29 21:29:13
Can you move this above or below the function defi
Robert Sesek
2014/10/29 21:47:53
Done.
| |
| 83 | |
| 84 // Returns a string, formatted with a multipart boundary and a field name, | |
| 85 // after which the contents of the part at |name| can be appended. | |
| 86 std::string GetFormDataBoundary(const std::string& boundary, | |
| 87 const std::string& name) { | |
| 88 return base::StringPrintf( | |
| 89 "--%s\r\nContent-Disposition: form-data; name=\"%s\"", | |
| 90 boundary.c_str(), | |
| 91 EncodeFieldName(name).c_str()); | |
| 92 } | |
| 93 | |
| 94 } // namespace | |
| 95 | |
| 96 HTTPMultipartBuilder::HTTPMultipartBuilder() | |
| 97 : boundary_(GenerateBoundaryString()), form_data_(), file_attachments_() { | |
| 98 } | |
| 99 | |
| 100 HTTPMultipartBuilder::~HTTPMultipartBuilder() { | |
| 101 } | |
| 102 | |
| 103 void HTTPMultipartBuilder::SetFormData(const std::string& key, | |
| 104 const std::string& value) { | |
| 105 EraseKey(key); | |
| 106 form_data_[key] = value; | |
| 107 } | |
| 108 | |
| 109 void HTTPMultipartBuilder::SetFileAttachment( | |
| 110 const std::string& key, | |
| 111 const std::string& upload_file_name, | |
| 112 const base::FilePath& path, | |
| 113 const std::string& content_type) { | |
| 114 EraseKey(upload_file_name); | |
| 115 | |
| 116 FileAttachment attachment; | |
| 117 attachment.filename = upload_file_name; | |
| 118 attachment.content_type = content_type; | |
| 119 if (attachment.content_type.empty()) | |
|
Mark Mentovai
2014/10/29 21:29:14
Since you need the if anyway, I’d write this as
Robert Sesek
2014/10/29 21:47:53
Done.
Robert Sesek
2014/10/29 21:47:53
Done.
| |
| 120 attachment.content_type = "application/octet-stream"; | |
| 121 attachment.path = path; | |
| 122 | |
| 123 file_attachments_[key] = attachment; | |
| 124 } | |
| 125 | |
| 126 scoped_ptr<HTTPBodyStream> HTTPMultipartBuilder::GetBodyStream() { | |
| 127 // The objects inserted into this vector will be owned by the returned | |
| 128 // CompositeHTTPBodyStream. Take care to not early-return without deleting | |
| 129 // this memory. | |
| 130 std::vector<HTTPBodyStream*> streams; | |
| 131 | |
| 132 for (const auto& pair : form_data_) { | |
| 133 std::string field = GetFormDataBoundary(boundary(), pair.first); | |
| 134 field += kBoundaryCRLF; | |
| 135 field += pair.second; | |
| 136 field += "\r\n"; | |
|
Mark Mentovai
2014/10/29 21:29:14
Since you have kBoundaryCRLF for a pair of CRLFs,
Robert Sesek
2014/10/29 21:47:53
Done.
| |
| 137 streams.push_back(new StringHTTPBodyStream(field)); | |
| 138 } | |
| 139 | |
| 140 for (const auto& pair : file_attachments_) { | |
| 141 const FileAttachment& attachment = pair.second; | |
| 142 std::string header = GetFormDataBoundary(boundary(), pair.first); | |
| 143 header += base::StringPrintf("; filename=\"%s\"\r\n", | |
| 144 attachment.filename.c_str()); | |
|
Mark Mentovai
2014/10/29 21:29:13
Encode me with EncodeFieldName(). Blink does this
Robert Sesek
2014/10/29 21:47:53
Done.
| |
| 145 header += base::StringPrintf("Content-Type: %s%s", | |
| 146 attachment.content_type.c_str(), kBoundaryCRLF); | |
|
Mark Mentovai
2014/10/29 21:29:14
Not checking Content-Type for bad characters gives
Robert Sesek
2014/10/29 21:47:53
I don't see the point of checking for "fishyness".
Mark Mentovai
2014/10/29 22:46:55
Robert Sesek wrote:
Robert Sesek
2014/10/29 23:12:04
Per offline discussion, limiting to [a-zA-Z0-9-_./
| |
| 147 | |
| 148 streams.push_back(new StringHTTPBodyStream(header)); | |
| 149 streams.push_back(new FileHTTPBodyStream(attachment.path)); | |
| 150 streams.push_back(new StringHTTPBodyStream("\r\n")); | |
| 151 } | |
| 152 | |
| 153 streams.push_back(new StringHTTPBodyStream(boundary() + "--")); | |
|
Mark Mentovai
2014/10/29 21:29:13
I would also end this with a CRLF even if no stand
Mark Mentovai
2014/10/29 21:29:14
No leading “--” here too? I think it needs it.
RF
Robert Sesek
2014/10/29 21:47:53
Done.
Robert Sesek
2014/10/29 21:47:53
Done.
| |
| 154 | |
| 155 return scoped_ptr<HTTPBodyStream>(new CompositeHTTPBodyStream(streams)); | |
| 156 } | |
| 157 | |
| 158 void HTTPMultipartBuilder::EraseKey(const std::string& key) { | |
| 159 auto data_it = form_data_.find(key); | |
| 160 if (data_it != form_data_.end()) | |
| 161 form_data_.erase(data_it); | |
| 162 | |
| 163 auto file_it = file_attachments_.find(key); | |
| 164 if (file_it != file_attachments_.end()) | |
| 165 file_attachments_.erase(file_it); | |
| 166 } | |
| 167 | |
| 168 } // namespace crashpad | |
| OLD | NEW |