Support for multiple upload files in CrashReportSender/HTTPUpload

A=David Major <dmajor@mozilla.com>
BUG=https://bugzilla.mozilla.org/show_bug.cgi?id=1048091
R=ted@mielczarek.org

Review URL: https://bugzilla.mozilla.org/show_bug.cgi?id=1048091 .
This commit is contained in:
David Major 2015-10-06 10:58:50 -04:00 committed by Ted Mielczarek
parent dbf56c53a0
commit 7685dfc567
9 changed files with 63 additions and 58 deletions

View file

@ -59,7 +59,7 @@ CrashReportSender::CrashReportSender(const wstring &checkpoint_file)
ReportResult CrashReportSender::SendCrashReport( ReportResult CrashReportSender::SendCrashReport(
const wstring &url, const map<wstring, wstring> &parameters, const wstring &url, const map<wstring, wstring> &parameters,
const wstring &dump_file_name, wstring *report_code) { const map<wstring, wstring> &files, wstring *report_code) {
int today = GetCurrentDate(); int today = GetCurrentDate();
if (today == last_sent_date_ && if (today == last_sent_date_ &&
max_reports_per_day_ != -1 && max_reports_per_day_ != -1 &&
@ -69,7 +69,7 @@ ReportResult CrashReportSender::SendCrashReport(
int http_response = 0; int http_response = 0;
bool result = HTTPUpload::SendRequest( bool result = HTTPUpload::SendRequest(
url, parameters, dump_file_name, L"upload_file_minidump", NULL, report_code, url, parameters, files, NULL, report_code,
&http_response); &http_response);
if (result) { if (result) {

View file

@ -77,7 +77,7 @@ class CrashReportSender {
int max_reports_per_day() const { return max_reports_per_day_; } int max_reports_per_day() const { return max_reports_per_day_; }
// Sends the specified minidump file, along with the map of // Sends the specified files, along with the map of
// name value pairs, as a multipart POST request to the given URL. // name value pairs, as a multipart POST request to the given URL.
// Parameter names must contain only printable ASCII characters, // Parameter names must contain only printable ASCII characters,
// and may not contain a quote (") character. // and may not contain a quote (") character.
@ -89,7 +89,7 @@ class CrashReportSender {
// (Otherwise, report_code will be unchanged.) // (Otherwise, report_code will be unchanged.)
ReportResult SendCrashReport(const wstring &url, ReportResult SendCrashReport(const wstring &url,
const map<wstring, wstring> &parameters, const map<wstring, wstring> &parameters,
const wstring &dump_file_name, const map<wstring, wstring> &files,
wstring *report_code); wstring *report_code);
private: private:

View file

@ -56,8 +56,7 @@ static const char kUserAgent[] = "Breakpad/1.0 (Linux)";
// static // static
bool HTTPUpload::SendRequest(const string &url, bool HTTPUpload::SendRequest(const string &url,
const map<string, string> &parameters, const map<string, string> &parameters,
const string &upload_file, const map<string, string> &files,
const string &file_part_name,
const string &proxy, const string &proxy,
const string &proxy_user_pwd, const string &proxy_user_pwd,
const string &ca_certificate_file, const string &ca_certificate_file,
@ -135,11 +134,13 @@ bool HTTPUpload::SendRequest(const string &url,
CURLFORM_COPYCONTENTS, iter->second.c_str(), CURLFORM_COPYCONTENTS, iter->second.c_str(),
CURLFORM_END); CURLFORM_END);
// Add form file. // Add form files.
(*curl_formadd)(&formpost, &lastptr, for (iter = files.begin(); iter != files.end(); ++iter) {
CURLFORM_COPYNAME, file_part_name.c_str(), (*curl_formadd)(&formpost, &lastptr,
CURLFORM_FILE, upload_file.c_str(), CURLFORM_COPYNAME, iter->first.c_str(),
CURLFORM_END); CURLFORM_FILE, iter->second.c_str(),
CURLFORM_END);
}
(*curl_easy_setopt)(curl, CURLOPT_HTTPPOST, formpost); (*curl_easy_setopt)(curl, CURLOPT_HTTPPOST, formpost);

View file

@ -45,9 +45,9 @@ using std::map;
class HTTPUpload { class HTTPUpload {
public: public:
// Sends the given set of parameters, along with the contents of // Sends the given sets of parameters and files as a multipart POST
// upload_file, as a multipart POST request to the given URL. // request to the given URL.
// file_part_name contains the name of the file part of the request // Each key in |files| is the name of the file part of the request
// (i.e. it corresponds to the name= attribute on an <input type="file">. // (i.e. it corresponds to the name= attribute on an <input type="file">.
// Parameter names must contain only printable ASCII characters, // Parameter names must contain only printable ASCII characters,
// and may not contain a quote (") character. // and may not contain a quote (") character.
@ -60,8 +60,7 @@ class HTTPUpload {
// returned in error_description. // returned in error_description.
static bool SendRequest(const string &url, static bool SendRequest(const string &url,
const map<string, string> &parameters, const map<string, string> &parameters,
const string &upload_file, const map<string, string> &files,
const string &file_part_name,
const string &proxy, const string &proxy,
const string &proxy_user_pwd, const string &proxy_user_pwd,
const string &ca_certificate_file, const string &ca_certificate_file,

View file

@ -64,8 +64,7 @@ class HTTPUpload::AutoInternetHandle {
// static // static
bool HTTPUpload::SendRequest(const wstring &url, bool HTTPUpload::SendRequest(const wstring &url,
const map<wstring, wstring> &parameters, const map<wstring, wstring> &parameters,
const wstring &upload_file, const map<wstring, wstring> &files,
const wstring &file_part_name,
int *timeout, int *timeout,
wstring *response_body, wstring *response_body,
int *response_code) { int *response_code) {
@ -143,8 +142,7 @@ bool HTTPUpload::SendRequest(const wstring &url,
HTTP_ADDREQ_FLAG_ADD); HTTP_ADDREQ_FLAG_ADD);
string request_body; string request_body;
if (!GenerateRequestBody(parameters, upload_file, if (!GenerateRequestBody(parameters, files, boundary, &request_body)) {
file_part_name, boundary, &request_body)) {
return false; return false;
} }
@ -268,15 +266,9 @@ wstring HTTPUpload::GenerateRequestHeader(const wstring &boundary) {
// static // static
bool HTTPUpload::GenerateRequestBody(const map<wstring, wstring> &parameters, bool HTTPUpload::GenerateRequestBody(const map<wstring, wstring> &parameters,
const wstring &upload_file, const map<wstring, wstring> &files,
const wstring &file_part_name,
const wstring &boundary, const wstring &boundary,
string *request_body) { string *request_body) {
vector<char> contents;
if (!GetFileContents(upload_file, &contents)) {
return false;
}
string boundary_str = WideToUTF8(boundary); string boundary_str = WideToUTF8(boundary);
if (boundary_str.empty()) { if (boundary_str.empty()) {
return false; return false;
@ -293,28 +285,36 @@ bool HTTPUpload::GenerateRequestBody(const map<wstring, wstring> &parameters,
WideToUTF8(pos->second) + "\r\n"); WideToUTF8(pos->second) + "\r\n");
} }
// Now append the upload file as a binary (octet-stream) part for (map<wstring, wstring>::const_iterator pos = files.begin();
string filename_utf8 = WideToUTF8(upload_file); pos != files.end(); ++pos) {
if (filename_utf8.empty()) { vector<char> contents;
return false; if (!GetFileContents(pos->second, &contents)) {
} return false;
}
string file_part_name_utf8 = WideToUTF8(file_part_name); // Now append the upload files as a binary (octet-stream) part
if (file_part_name_utf8.empty()) { string filename_utf8 = WideToUTF8(pos->second);
return false; if (filename_utf8.empty()) {
} return false;
}
request_body->append("--" + boundary_str + "\r\n"); string file_part_name_utf8 = WideToUTF8(pos->first);
request_body->append("Content-Disposition: form-data; " if (file_part_name_utf8.empty()) {
"name=\"" + file_part_name_utf8 + "\"; " return false;
"filename=\"" + filename_utf8 + "\"\r\n"); }
request_body->append("Content-Type: application/octet-stream\r\n");
request_body->append("\r\n");
if (!contents.empty()) { request_body->append("--" + boundary_str + "\r\n");
request_body->append("Content-Disposition: form-data; "
"name=\"" + file_part_name_utf8 + "\"; "
"filename=\"" + filename_utf8 + "\"\r\n");
request_body->append("Content-Type: application/octet-stream\r\n");
request_body->append("\r\n");
if (!contents.empty()) {
request_body->append(&(contents[0]), contents.size()); request_body->append(&(contents[0]), contents.size());
}
request_body->append("\r\n");
} }
request_body->append("\r\n");
request_body->append("--" + boundary_str + "--\r\n"); request_body->append("--" + boundary_str + "--\r\n");
return true; return true;
} }

View file

@ -54,9 +54,9 @@ using std::vector;
class HTTPUpload { class HTTPUpload {
public: public:
// Sends the given set of parameters, along with the contents of // Sends the given sets of parameters and files as a multipart POST
// upload_file, as a multipart POST request to the given URL. // request to the given URL.
// file_part_name contains the name of the file part of the request // Each key in |files| is the name of the file part of the request
// (i.e. it corresponds to the name= attribute on an <input type="file">. // (i.e. it corresponds to the name= attribute on an <input type="file">.
// Parameter names must contain only printable ASCII characters, // Parameter names must contain only printable ASCII characters,
// and may not contain a quote (") character. // and may not contain a quote (") character.
@ -67,8 +67,7 @@ class HTTPUpload {
// received (or 0 if the request failed before getting an HTTP response). // received (or 0 if the request failed before getting an HTTP response).
static bool SendRequest(const wstring &url, static bool SendRequest(const wstring &url,
const map<wstring, wstring> &parameters, const map<wstring, wstring> &parameters,
const wstring &upload_file, const map<wstring, wstring> &files,
const wstring &file_part_name,
int *timeout, int *timeout,
wstring *response_body, wstring *response_body,
int *response_code); int *response_code);
@ -88,12 +87,11 @@ class HTTPUpload {
// Generates a HTTP request header for a multipart form submit. // Generates a HTTP request header for a multipart form submit.
static wstring GenerateRequestHeader(const wstring &boundary); static wstring GenerateRequestHeader(const wstring &boundary);
// Given a set of parameters, an upload filename, and a file part name, // Given a set of parameters, a set of upload files, and a file part name,
// generates a multipart request body string with these parameters // generates a multipart request body string with these parameters
// and minidump contents. Returns true on success. // and minidump contents. Returns true on success.
static bool GenerateRequestBody(const map<wstring, wstring> &parameters, static bool GenerateRequestBody(const map<wstring, wstring> &parameters,
const wstring &upload_file, const map<wstring, wstring> &files,
const wstring &file_part_name,
const wstring &boundary, const wstring &boundary,
string *request_body); string *request_body);

View file

@ -62,12 +62,14 @@ static void Start(Options *options) {
parameters["prod"] = options->product; parameters["prod"] = options->product;
parameters["ver"] = options->version; parameters["ver"] = options->version;
std::map<string, string> files;
files["upload_file_minidump"] = options->minidumpPath;
// Send it // Send it
string response, error; string response, error;
bool success = HTTPUpload::SendRequest(options->uploadURLStr, bool success = HTTPUpload::SendRequest(options->uploadURLStr,
parameters, parameters,
options->minidumpPath, files,
"upload_file_minidump",
options->proxy, options->proxy,
options->proxy_user_pwd, options->proxy_user_pwd,
"", "",

View file

@ -139,12 +139,15 @@ static void Start(Options *options) {
parameters["debug_file"] = module_parts[4]; parameters["debug_file"] = module_parts[4];
parameters["code_file"] = module_parts[4]; parameters["code_file"] = module_parts[4];
parameters["debug_identifier"] = compacted_id; parameters["debug_identifier"] = compacted_id;
std::map<string, string> files;
files["symbol_file"] = options->symbolsPath;
string response, error; string response, error;
long response_code; long response_code;
bool success = HTTPUpload::SendRequest(options->uploadURLStr, bool success = HTTPUpload::SendRequest(options->uploadURLStr,
parameters, parameters,
options->symbolsPath, files,
"symbol_file",
options->proxy, options->proxy,
options->proxy_user_pwd, options->proxy_user_pwd,
"", "",

View file

@ -228,12 +228,14 @@ int wmain(int argc, wchar_t *argv[]) {
fwprintf(stderr, L"Warning: Could not get file version for %s\n", module); fwprintf(stderr, L"Warning: Could not get file version for %s\n", module);
} }
map<wstring, wstring> files;
files[L"symbol_file"] = symbol_file;
bool success = true; bool success = true;
while (currentarg < argc) { while (currentarg < argc) {
int response_code; int response_code;
if (!HTTPUpload::SendRequest(argv[currentarg], parameters, if (!HTTPUpload::SendRequest(argv[currentarg], parameters, files,
symbol_file, L"symbol_file",
timeout == -1 ? NULL : &timeout, timeout == -1 ? NULL : &timeout,
nullptr, &response_code)) { nullptr, &response_code)) {
success = false; success = false;