toolkit/crashreporter/google-breakpad/src/common/linux/http_upload.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29
michael@0 30 #include "common/linux/http_upload.h"
michael@0 31
michael@0 32 #include <assert.h>
michael@0 33 #include <dlfcn.h>
michael@0 34 #include "third_party/curl/curl.h"
michael@0 35
michael@0 36 namespace {
michael@0 37
michael@0 38 // Callback to get the response data from server.
michael@0 39 static size_t WriteCallback(void *ptr, size_t size,
michael@0 40 size_t nmemb, void *userp) {
michael@0 41 if (!userp)
michael@0 42 return 0;
michael@0 43
michael@0 44 string *response = reinterpret_cast<string *>(userp);
michael@0 45 size_t real_size = size * nmemb;
michael@0 46 response->append(reinterpret_cast<char *>(ptr), real_size);
michael@0 47 return real_size;
michael@0 48 }
michael@0 49
michael@0 50 } // namespace
michael@0 51
michael@0 52 namespace google_breakpad {
michael@0 53
michael@0 54 static const char kUserAgent[] = "Breakpad/1.0 (Linux)";
michael@0 55
michael@0 56 // static
michael@0 57 bool HTTPUpload::SendRequest(const string &url,
michael@0 58 const map<string, string> &parameters,
michael@0 59 const string &upload_file,
michael@0 60 const string &file_part_name,
michael@0 61 const string &proxy,
michael@0 62 const string &proxy_user_pwd,
michael@0 63 const string &ca_certificate_file,
michael@0 64 string *response_body,
michael@0 65 long *response_code,
michael@0 66 string *error_description) {
michael@0 67 if (response_code != NULL)
michael@0 68 *response_code = 0;
michael@0 69
michael@0 70 if (!CheckParameters(parameters))
michael@0 71 return false;
michael@0 72
michael@0 73 void *curl_lib = dlopen("libcurl.so", RTLD_NOW);
michael@0 74 if (!curl_lib) {
michael@0 75 if (error_description != NULL)
michael@0 76 *error_description = dlerror();
michael@0 77 curl_lib = dlopen("libcurl.so.4", RTLD_NOW);
michael@0 78 }
michael@0 79 if (!curl_lib) {
michael@0 80 // Debian gives libcurl a different name when it is built against GnuTLS
michael@0 81 // instead of OpenSSL.
michael@0 82 curl_lib = dlopen("libcurl-gnutls.so.4", RTLD_NOW);
michael@0 83 }
michael@0 84 if (!curl_lib) {
michael@0 85 curl_lib = dlopen("libcurl.so.3", RTLD_NOW);
michael@0 86 }
michael@0 87 if (!curl_lib) {
michael@0 88 return false;
michael@0 89 }
michael@0 90
michael@0 91 CURL* (*curl_easy_init)(void);
michael@0 92 *(void**) (&curl_easy_init) = dlsym(curl_lib, "curl_easy_init");
michael@0 93 CURL *curl = (*curl_easy_init)();
michael@0 94 if (error_description != NULL)
michael@0 95 *error_description = "No Error";
michael@0 96
michael@0 97 if (!curl) {
michael@0 98 dlclose(curl_lib);
michael@0 99 return false;
michael@0 100 }
michael@0 101
michael@0 102 CURLcode err_code = CURLE_OK;
michael@0 103 CURLcode (*curl_easy_setopt)(CURL *, CURLoption, ...);
michael@0 104 *(void**) (&curl_easy_setopt) = dlsym(curl_lib, "curl_easy_setopt");
michael@0 105 (*curl_easy_setopt)(curl, CURLOPT_URL, url.c_str());
michael@0 106 (*curl_easy_setopt)(curl, CURLOPT_USERAGENT, kUserAgent);
michael@0 107 // Set proxy information if necessary.
michael@0 108 if (!proxy.empty())
michael@0 109 (*curl_easy_setopt)(curl, CURLOPT_PROXY, proxy.c_str());
michael@0 110 if (!proxy_user_pwd.empty())
michael@0 111 (*curl_easy_setopt)(curl, CURLOPT_PROXYUSERPWD, proxy_user_pwd.c_str());
michael@0 112
michael@0 113 if (!ca_certificate_file.empty())
michael@0 114 (*curl_easy_setopt)(curl, CURLOPT_CAINFO, ca_certificate_file.c_str());
michael@0 115
michael@0 116 struct curl_httppost *formpost = NULL;
michael@0 117 struct curl_httppost *lastptr = NULL;
michael@0 118 // Add form data.
michael@0 119 CURLFORMcode (*curl_formadd)(struct curl_httppost **, struct curl_httppost **, ...);
michael@0 120 *(void**) (&curl_formadd) = dlsym(curl_lib, "curl_formadd");
michael@0 121 map<string, string>::const_iterator iter = parameters.begin();
michael@0 122 for (; iter != parameters.end(); ++iter)
michael@0 123 (*curl_formadd)(&formpost, &lastptr,
michael@0 124 CURLFORM_COPYNAME, iter->first.c_str(),
michael@0 125 CURLFORM_COPYCONTENTS, iter->second.c_str(),
michael@0 126 CURLFORM_END);
michael@0 127
michael@0 128 // Add form file.
michael@0 129 (*curl_formadd)(&formpost, &lastptr,
michael@0 130 CURLFORM_COPYNAME, file_part_name.c_str(),
michael@0 131 CURLFORM_FILE, upload_file.c_str(),
michael@0 132 CURLFORM_END);
michael@0 133
michael@0 134 (*curl_easy_setopt)(curl, CURLOPT_HTTPPOST, formpost);
michael@0 135
michael@0 136 // Disable 100-continue header.
michael@0 137 struct curl_slist *headerlist = NULL;
michael@0 138 char buf[] = "Expect:";
michael@0 139 struct curl_slist* (*curl_slist_append)(struct curl_slist *, const char *);
michael@0 140 *(void**) (&curl_slist_append) = dlsym(curl_lib, "curl_slist_append");
michael@0 141 headerlist = (*curl_slist_append)(headerlist, buf);
michael@0 142 (*curl_easy_setopt)(curl, CURLOPT_HTTPHEADER, headerlist);
michael@0 143
michael@0 144 if (response_body != NULL) {
michael@0 145 (*curl_easy_setopt)(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
michael@0 146 (*curl_easy_setopt)(curl, CURLOPT_WRITEDATA,
michael@0 147 reinterpret_cast<void *>(response_body));
michael@0 148 }
michael@0 149
michael@0 150 // Fail if 400+ is returned from the web server.
michael@0 151 (*curl_easy_setopt)(curl, CURLOPT_FAILONERROR, 1);
michael@0 152
michael@0 153 CURLcode (*curl_easy_perform)(CURL *);
michael@0 154 *(void**) (&curl_easy_perform) = dlsym(curl_lib, "curl_easy_perform");
michael@0 155 err_code = (*curl_easy_perform)(curl);
michael@0 156 if (response_code != NULL) {
michael@0 157 CURLcode (*curl_easy_getinfo)(CURL *, CURLINFO, ...);
michael@0 158 *(void**) (&curl_easy_getinfo) = dlsym(curl_lib, "curl_easy_getinfo");
michael@0 159 (*curl_easy_getinfo)(curl, CURLINFO_RESPONSE_CODE, response_code);
michael@0 160 }
michael@0 161 const char* (*curl_easy_strerror)(CURLcode);
michael@0 162 *(void**) (&curl_easy_strerror) = dlsym(curl_lib, "curl_easy_strerror");
michael@0 163 #ifndef NDEBUG
michael@0 164 if (err_code != CURLE_OK)
michael@0 165 fprintf(stderr, "Failed to send http request to %s, error: %s\n",
michael@0 166 url.c_str(),
michael@0 167 (*curl_easy_strerror)(err_code));
michael@0 168 #endif
michael@0 169 if (error_description != NULL)
michael@0 170 *error_description = (*curl_easy_strerror)(err_code);
michael@0 171
michael@0 172 void (*curl_easy_cleanup)(CURL *);
michael@0 173 *(void**) (&curl_easy_cleanup) = dlsym(curl_lib, "curl_easy_cleanup");
michael@0 174 (*curl_easy_cleanup)(curl);
michael@0 175 if (formpost != NULL) {
michael@0 176 void (*curl_formfree)(struct curl_httppost *);
michael@0 177 *(void**) (&curl_formfree) = dlsym(curl_lib, "curl_formfree");
michael@0 178 (*curl_formfree)(formpost);
michael@0 179 }
michael@0 180 if (headerlist != NULL) {
michael@0 181 void (*curl_slist_free_all)(struct curl_slist *);
michael@0 182 *(void**) (&curl_slist_free_all) = dlsym(curl_lib, "curl_slist_free_all");
michael@0 183 (*curl_slist_free_all)(headerlist);
michael@0 184 }
michael@0 185 dlclose(curl_lib);
michael@0 186 return err_code == CURLE_OK;
michael@0 187 }
michael@0 188
michael@0 189 // static
michael@0 190 bool HTTPUpload::CheckParameters(const map<string, string> &parameters) {
michael@0 191 for (map<string, string>::const_iterator pos = parameters.begin();
michael@0 192 pos != parameters.end(); ++pos) {
michael@0 193 const string &str = pos->first;
michael@0 194 if (str.size() == 0)
michael@0 195 return false; // disallow empty parameter names
michael@0 196 for (unsigned int i = 0; i < str.size(); ++i) {
michael@0 197 int c = str[i];
michael@0 198 if (c < 32 || c == '"' || c > 127) {
michael@0 199 return false;
michael@0 200 }
michael@0 201 }
michael@0 202 }
michael@0 203 return true;
michael@0 204 }
michael@0 205
michael@0 206 } // namespace google_breakpad

mercurial