1// Copyright (c) 2006, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30// minidump_upload.cc: Upload a minidump to a HTTP server.
31// The upload is sent as a multipart/form-data POST request with
32// the following parameters:
33// prod: the product name
34// ver: the product version
35// symbol_file: the breakpad format symbol file
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40
41#include <string>
42
43#include "common/linux/http_upload.h"
44#include "common/path_helper.h"
45#include "common/using_std_string.h"
46
47using google_breakpad::HTTPUpload;
48
49struct Options {
50 string minidumpPath;
51 string uploadURLStr;
52 string product;
53 string version;
54 string proxy;
55 string proxy_user_pwd;
56 bool success;
57};
58
59//=============================================================================
60static void Start(Options *options) {
61 std::map<string, string> parameters;
62 // Add parameters
63 parameters["prod"] = options->product;
64 parameters["ver"] = options->version;
65
66 std::map<string, string> files;
67 files["upload_file_minidump"] = options->minidumpPath;
68
69 // Send it
70 string response, error;
71 bool success = HTTPUpload::SendRequest(options->uploadURLStr,
72 parameters,
73 files,
74 options->proxy,
75 options->proxy_user_pwd,
76 "",
77 &response,
78 NULL,
79 &error);
80
81 if (success) {
82 printf("Successfully sent the minidump file.\n");
83 } else {
84 printf("Failed to send minidump: %s\n", error.c_str());
85 }
86 printf("Response:\n");
87 printf("%s\n", response.c_str());
88 options->success = success;
89}
90
91//=============================================================================
92static void
93Usage(int argc, const char *argv[]) {
94 fprintf(stderr, "Submit minidump information.\n");
95 fprintf(stderr,
96 "Usage: %s [options...] -p <product> -v <version> <minidump> "
97 "<upload-URL>\n",
98 google_breakpad::BaseName(argv[0]).c_str());
99 fprintf(stderr, "Options:\n");
100 fprintf(stderr, "<minidump> should be a minidump.\n");
101 fprintf(stderr, "<upload-URL> is the destination for the upload\n");
102
103 fprintf(stderr, "-p:\t <product> Product name\n");
104 fprintf(stderr, "-v:\t <version> Product version\n");
105 fprintf(stderr, "-x:\t <host[:port]> Use HTTP proxy on given port\n");
106 fprintf(stderr, "-u:\t <user[:password]> Set proxy user and password\n");
107 fprintf(stderr, "-h:\t Usage\n");
108 fprintf(stderr, "-?:\t Usage\n");
109}
110
111//=============================================================================
112static void
113SetupOptions(int argc, const char *argv[], Options *options) {
114 extern int optind;
115 int ch;
116
117 while ((ch = getopt(argc, (char * const*)argv, "p:u:v:x:h?")) != -1) {
118 switch (ch) {
119 case 'p':
120 options->product = optarg;
121 break;
122 case 'u':
123 options->proxy_user_pwd = optarg;
124 break;
125 case 'v':
126 options->version = optarg;
127 break;
128 case 'x':
129 options->proxy = optarg;
130 break;
131
132 default:
133 fprintf(stderr, "Invalid option '%c'\n", ch);
134 Usage(argc, argv);
135 exit(1);
136 break;
137 }
138 }
139
140 if ((argc - optind) != 2) {
141 fprintf(stderr, "%s: Missing symbols file and/or upload-URL\n", argv[0]);
142 Usage(argc, argv);
143 exit(1);
144 }
145
146 options->minidumpPath = argv[optind];
147 options->uploadURLStr = argv[optind + 1];
148}
149
150//=============================================================================
151int main(int argc, const char* argv[]) {
152 Options options;
153 SetupOptions(argc, argv, &options);
154 Start(&options);
155 return options.success ? 0 : 1;
156}
157