1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/linux/http_upload.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,206 @@ 1.4 +// Copyright (c) 2006, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +#include "common/linux/http_upload.h" 1.34 + 1.35 +#include <assert.h> 1.36 +#include <dlfcn.h> 1.37 +#include "third_party/curl/curl.h" 1.38 + 1.39 +namespace { 1.40 + 1.41 +// Callback to get the response data from server. 1.42 +static size_t WriteCallback(void *ptr, size_t size, 1.43 + size_t nmemb, void *userp) { 1.44 + if (!userp) 1.45 + return 0; 1.46 + 1.47 + string *response = reinterpret_cast<string *>(userp); 1.48 + size_t real_size = size * nmemb; 1.49 + response->append(reinterpret_cast<char *>(ptr), real_size); 1.50 + return real_size; 1.51 +} 1.52 + 1.53 +} // namespace 1.54 + 1.55 +namespace google_breakpad { 1.56 + 1.57 +static const char kUserAgent[] = "Breakpad/1.0 (Linux)"; 1.58 + 1.59 +// static 1.60 +bool HTTPUpload::SendRequest(const string &url, 1.61 + const map<string, string> ¶meters, 1.62 + const string &upload_file, 1.63 + const string &file_part_name, 1.64 + const string &proxy, 1.65 + const string &proxy_user_pwd, 1.66 + const string &ca_certificate_file, 1.67 + string *response_body, 1.68 + long *response_code, 1.69 + string *error_description) { 1.70 + if (response_code != NULL) 1.71 + *response_code = 0; 1.72 + 1.73 + if (!CheckParameters(parameters)) 1.74 + return false; 1.75 + 1.76 + void *curl_lib = dlopen("libcurl.so", RTLD_NOW); 1.77 + if (!curl_lib) { 1.78 + if (error_description != NULL) 1.79 + *error_description = dlerror(); 1.80 + curl_lib = dlopen("libcurl.so.4", RTLD_NOW); 1.81 + } 1.82 + if (!curl_lib) { 1.83 + // Debian gives libcurl a different name when it is built against GnuTLS 1.84 + // instead of OpenSSL. 1.85 + curl_lib = dlopen("libcurl-gnutls.so.4", RTLD_NOW); 1.86 + } 1.87 + if (!curl_lib) { 1.88 + curl_lib = dlopen("libcurl.so.3", RTLD_NOW); 1.89 + } 1.90 + if (!curl_lib) { 1.91 + return false; 1.92 + } 1.93 + 1.94 + CURL* (*curl_easy_init)(void); 1.95 + *(void**) (&curl_easy_init) = dlsym(curl_lib, "curl_easy_init"); 1.96 + CURL *curl = (*curl_easy_init)(); 1.97 + if (error_description != NULL) 1.98 + *error_description = "No Error"; 1.99 + 1.100 + if (!curl) { 1.101 + dlclose(curl_lib); 1.102 + return false; 1.103 + } 1.104 + 1.105 + CURLcode err_code = CURLE_OK; 1.106 + CURLcode (*curl_easy_setopt)(CURL *, CURLoption, ...); 1.107 + *(void**) (&curl_easy_setopt) = dlsym(curl_lib, "curl_easy_setopt"); 1.108 + (*curl_easy_setopt)(curl, CURLOPT_URL, url.c_str()); 1.109 + (*curl_easy_setopt)(curl, CURLOPT_USERAGENT, kUserAgent); 1.110 + // Set proxy information if necessary. 1.111 + if (!proxy.empty()) 1.112 + (*curl_easy_setopt)(curl, CURLOPT_PROXY, proxy.c_str()); 1.113 + if (!proxy_user_pwd.empty()) 1.114 + (*curl_easy_setopt)(curl, CURLOPT_PROXYUSERPWD, proxy_user_pwd.c_str()); 1.115 + 1.116 + if (!ca_certificate_file.empty()) 1.117 + (*curl_easy_setopt)(curl, CURLOPT_CAINFO, ca_certificate_file.c_str()); 1.118 + 1.119 + struct curl_httppost *formpost = NULL; 1.120 + struct curl_httppost *lastptr = NULL; 1.121 + // Add form data. 1.122 + CURLFORMcode (*curl_formadd)(struct curl_httppost **, struct curl_httppost **, ...); 1.123 + *(void**) (&curl_formadd) = dlsym(curl_lib, "curl_formadd"); 1.124 + map<string, string>::const_iterator iter = parameters.begin(); 1.125 + for (; iter != parameters.end(); ++iter) 1.126 + (*curl_formadd)(&formpost, &lastptr, 1.127 + CURLFORM_COPYNAME, iter->first.c_str(), 1.128 + CURLFORM_COPYCONTENTS, iter->second.c_str(), 1.129 + CURLFORM_END); 1.130 + 1.131 + // Add form file. 1.132 + (*curl_formadd)(&formpost, &lastptr, 1.133 + CURLFORM_COPYNAME, file_part_name.c_str(), 1.134 + CURLFORM_FILE, upload_file.c_str(), 1.135 + CURLFORM_END); 1.136 + 1.137 + (*curl_easy_setopt)(curl, CURLOPT_HTTPPOST, formpost); 1.138 + 1.139 + // Disable 100-continue header. 1.140 + struct curl_slist *headerlist = NULL; 1.141 + char buf[] = "Expect:"; 1.142 + struct curl_slist* (*curl_slist_append)(struct curl_slist *, const char *); 1.143 + *(void**) (&curl_slist_append) = dlsym(curl_lib, "curl_slist_append"); 1.144 + headerlist = (*curl_slist_append)(headerlist, buf); 1.145 + (*curl_easy_setopt)(curl, CURLOPT_HTTPHEADER, headerlist); 1.146 + 1.147 + if (response_body != NULL) { 1.148 + (*curl_easy_setopt)(curl, CURLOPT_WRITEFUNCTION, WriteCallback); 1.149 + (*curl_easy_setopt)(curl, CURLOPT_WRITEDATA, 1.150 + reinterpret_cast<void *>(response_body)); 1.151 + } 1.152 + 1.153 + // Fail if 400+ is returned from the web server. 1.154 + (*curl_easy_setopt)(curl, CURLOPT_FAILONERROR, 1); 1.155 + 1.156 + CURLcode (*curl_easy_perform)(CURL *); 1.157 + *(void**) (&curl_easy_perform) = dlsym(curl_lib, "curl_easy_perform"); 1.158 + err_code = (*curl_easy_perform)(curl); 1.159 + if (response_code != NULL) { 1.160 + CURLcode (*curl_easy_getinfo)(CURL *, CURLINFO, ...); 1.161 + *(void**) (&curl_easy_getinfo) = dlsym(curl_lib, "curl_easy_getinfo"); 1.162 + (*curl_easy_getinfo)(curl, CURLINFO_RESPONSE_CODE, response_code); 1.163 + } 1.164 + const char* (*curl_easy_strerror)(CURLcode); 1.165 + *(void**) (&curl_easy_strerror) = dlsym(curl_lib, "curl_easy_strerror"); 1.166 +#ifndef NDEBUG 1.167 + if (err_code != CURLE_OK) 1.168 + fprintf(stderr, "Failed to send http request to %s, error: %s\n", 1.169 + url.c_str(), 1.170 + (*curl_easy_strerror)(err_code)); 1.171 +#endif 1.172 + if (error_description != NULL) 1.173 + *error_description = (*curl_easy_strerror)(err_code); 1.174 + 1.175 + void (*curl_easy_cleanup)(CURL *); 1.176 + *(void**) (&curl_easy_cleanup) = dlsym(curl_lib, "curl_easy_cleanup"); 1.177 + (*curl_easy_cleanup)(curl); 1.178 + if (formpost != NULL) { 1.179 + void (*curl_formfree)(struct curl_httppost *); 1.180 + *(void**) (&curl_formfree) = dlsym(curl_lib, "curl_formfree"); 1.181 + (*curl_formfree)(formpost); 1.182 + } 1.183 + if (headerlist != NULL) { 1.184 + void (*curl_slist_free_all)(struct curl_slist *); 1.185 + *(void**) (&curl_slist_free_all) = dlsym(curl_lib, "curl_slist_free_all"); 1.186 + (*curl_slist_free_all)(headerlist); 1.187 + } 1.188 + dlclose(curl_lib); 1.189 + return err_code == CURLE_OK; 1.190 +} 1.191 + 1.192 +// static 1.193 +bool HTTPUpload::CheckParameters(const map<string, string> ¶meters) { 1.194 + for (map<string, string>::const_iterator pos = parameters.begin(); 1.195 + pos != parameters.end(); ++pos) { 1.196 + const string &str = pos->first; 1.197 + if (str.size() == 0) 1.198 + return false; // disallow empty parameter names 1.199 + for (unsigned int i = 0; i < str.size(); ++i) { 1.200 + int c = str[i]; 1.201 + if (c < 32 || c == '"' || c > 127) { 1.202 + return false; 1.203 + } 1.204 + } 1.205 + } 1.206 + return true; 1.207 +} 1.208 + 1.209 +} // namespace google_breakpad