| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 <getopt.h> |
| 16 #include <stdio.h> |
| 17 #include <stdlib.h> |
| 18 #include <sys/types.h> |
| 19 |
| 20 #include <memory> |
| 21 #include <string> |
| 22 |
| 23 #include "base/files/file_path.h" |
| 24 #include "tools/tool_support.h" |
| 25 #include "util/file/file_writer.h" |
| 26 #include "util/net/http_body.h" |
| 27 #include "util/net/http_multipart_builder.h" |
| 28 #include "util/net/http_transport.h" |
| 29 #include "util/string/split_string.h" |
| 30 |
| 31 namespace crashpad { |
| 32 namespace { |
| 33 |
| 34 void Usage(const base::FilePath& me) { |
| 35 fprintf(stderr, |
| 36 "Usage: %" PRFilePath " [OPTION]...\n" |
| 37 "Send an HTTP POST request.\n" |
| 38 " -f, --file=KEY=PATH upload the file at PATH for the HTTP KEY parameter\n" |
| 39 " --no-upload-gzip don't use gzip compression when uploading\n" |
| 40 " -o, --output=FILE write the response body to FILE instead of stdout\n" |
| 41 " -s, --string=KEY=VALUE set the HTTP KEY parameter to VALUE\n" |
| 42 " -u, --url=URL send the request to URL\n" |
| 43 " --help display this help and exit\n" |
| 44 " --version output version information and exit\n", |
| 45 me.value().c_str()); |
| 46 ToolSupport::UsageTail(me); |
| 47 } |
| 48 |
| 49 int HTTPUploadMain(int argc, char* argv[]) { |
| 50 const base::FilePath argv0( |
| 51 ToolSupport::CommandLineArgumentToFilePathStringType(argv[0])); |
| 52 const base::FilePath me(argv0.BaseName()); |
| 53 |
| 54 enum OptionFlags { |
| 55 // “Short” (single-character) options. |
| 56 kOptionFile = 'f', |
| 57 kOptionOutput = 'o', |
| 58 kOptionString = 's', |
| 59 kOptionURL = 'u', |
| 60 |
| 61 // Long options without short equivalents. |
| 62 kOptionLastChar = 255, |
| 63 kOptionNoUploadGzip, |
| 64 |
| 65 // Standard options. |
| 66 kOptionHelp = -2, |
| 67 kOptionVersion = -3, |
| 68 }; |
| 69 |
| 70 struct { |
| 71 std::string url; |
| 72 const char* output; |
| 73 bool upload_gzip; |
| 74 } options = {}; |
| 75 options.upload_gzip = true; |
| 76 |
| 77 const option long_options[] = { |
| 78 {"file", required_argument, nullptr, kOptionFile}, |
| 79 {"no-upload-gzip", no_argument, nullptr, kOptionNoUploadGzip}, |
| 80 {"output", required_argument, nullptr, kOptionOutput}, |
| 81 {"string", required_argument, nullptr, kOptionString}, |
| 82 {"url", required_argument, nullptr, kOptionURL}, |
| 83 {"help", no_argument, nullptr, kOptionHelp}, |
| 84 {"version", no_argument, nullptr, kOptionVersion}, |
| 85 {nullptr, 0, nullptr, 0}, |
| 86 }; |
| 87 |
| 88 HTTPMultipartBuilder http_multipart_builder; |
| 89 |
| 90 int opt; |
| 91 while ((opt = getopt_long(argc, argv, "f:o:s:u:", long_options, nullptr)) != |
| 92 -1) { |
| 93 switch (opt) { |
| 94 case kOptionFile: { |
| 95 std::string key; |
| 96 std::string path; |
| 97 if (!SplitStringFirst(optarg, '=', &key, &path)) { |
| 98 ToolSupport::UsageHint(me, "--file requires KEY=STRING"); |
| 99 return EXIT_FAILURE; |
| 100 } |
| 101 base::FilePath file_path( |
| 102 ToolSupport::CommandLineArgumentToFilePathStringType(path)); |
| 103 std::string file_name( |
| 104 ToolSupport::FilePathToCommandLineArgument(file_path.BaseName())); |
| 105 http_multipart_builder.SetFileAttachment( |
| 106 key, file_name, file_path, "application/octet-stream"); |
| 107 break; |
| 108 } |
| 109 case kOptionNoUploadGzip: { |
| 110 options.upload_gzip = false; |
| 111 break; |
| 112 } |
| 113 case kOptionOutput: { |
| 114 options.output = optarg; |
| 115 break; |
| 116 } |
| 117 case kOptionString: { |
| 118 std::string key; |
| 119 std::string value; |
| 120 if (!SplitStringFirst(optarg, '=', &key, &value)) { |
| 121 ToolSupport::UsageHint(me, "--string requires KEY=VALUE"); |
| 122 return EXIT_FAILURE; |
| 123 } |
| 124 http_multipart_builder.SetFormData(key, value); |
| 125 break; |
| 126 } |
| 127 case kOptionURL: |
| 128 options.url = optarg; |
| 129 break; |
| 130 case kOptionHelp: |
| 131 Usage(me); |
| 132 return EXIT_SUCCESS; |
| 133 case kOptionVersion: |
| 134 ToolSupport::Version(me); |
| 135 return EXIT_SUCCESS; |
| 136 default: |
| 137 ToolSupport::UsageHint(me, nullptr); |
| 138 return EXIT_FAILURE; |
| 139 } |
| 140 } |
| 141 argc -= optind; |
| 142 argv += optind; |
| 143 |
| 144 if (options.url.empty()) { |
| 145 ToolSupport::UsageHint(me, "--url is required"); |
| 146 return EXIT_FAILURE; |
| 147 } |
| 148 |
| 149 if (argc) { |
| 150 ToolSupport::UsageHint(me, nullptr); |
| 151 return EXIT_FAILURE; |
| 152 } |
| 153 |
| 154 std::unique_ptr<FileWriterInterface> file_writer; |
| 155 if (options.output) { |
| 156 FileWriter* file_writer_impl = new FileWriter(); |
| 157 file_writer.reset(file_writer_impl); |
| 158 base::FilePath output_path( |
| 159 ToolSupport::CommandLineArgumentToFilePathStringType(options.output)); |
| 160 if (!file_writer_impl->Open(output_path, |
| 161 FileWriteMode::kTruncateOrCreate, |
| 162 FilePermissions::kWorldReadable)) { |
| 163 return EXIT_FAILURE; |
| 164 } |
| 165 } else { |
| 166 file_writer.reset(new WeakStdioFileWriter(stdout)); |
| 167 } |
| 168 |
| 169 http_multipart_builder.SetGzipEnabled(options.upload_gzip); |
| 170 |
| 171 std::unique_ptr<HTTPTransport> http_transport(HTTPTransport::Create()); |
| 172 http_transport->SetURL(options.url); |
| 173 |
| 174 HTTPHeaders content_headers; |
| 175 http_multipart_builder.PopulateContentHeaders(&content_headers); |
| 176 for (const auto& content_header : content_headers) { |
| 177 http_transport->SetHeader(content_header.first, content_header.second); |
| 178 } |
| 179 |
| 180 http_transport->SetBodyStream(http_multipart_builder.GetBodyStream()); |
| 181 |
| 182 std::string response_body; |
| 183 if (!http_transport->ExecuteSynchronously(&response_body)) { |
| 184 return EXIT_FAILURE; |
| 185 } |
| 186 |
| 187 if (!response_body.empty() && |
| 188 !file_writer->Write(&response_body[0], response_body.size())) { |
| 189 return EXIT_FAILURE; |
| 190 } |
| 191 |
| 192 return EXIT_SUCCESS; |
| 193 } |
| 194 |
| 195 } // namespace |
| 196 } // namespace crashpad |
| 197 |
| 198 #if defined(OS_POSIX) |
| 199 int main(int argc, char* argv[]) { |
| 200 return crashpad::HTTPUploadMain(argc, argv); |
| 201 } |
| 202 #elif defined(OS_WIN) |
| 203 int wmain(int argc, wchar_t* argv[]) { |
| 204 return crashpad::ToolSupport::Wmain(argc, argv, crashpad::HTTPUploadMain); |
| 205 } |
| 206 #endif // OS_POSIX |
| OLD | NEW |