1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/linux/libcurl_wrapper.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,222 @@ 1.4 +// Copyright (c) 2009, 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 <dlfcn.h> 1.34 + 1.35 +#include <iostream> 1.36 +#include <string> 1.37 + 1.38 +#include "common/linux/libcurl_wrapper.h" 1.39 +#include "common/using_std_string.h" 1.40 + 1.41 +namespace google_breakpad { 1.42 +LibcurlWrapper::LibcurlWrapper() 1.43 + : init_ok_(false), 1.44 + formpost_(NULL), 1.45 + lastptr_(NULL), 1.46 + headerlist_(NULL) { 1.47 + curl_lib_ = dlopen("libcurl.so", RTLD_NOW); 1.48 + if (!curl_lib_) { 1.49 + curl_lib_ = dlopen("libcurl.so.4", RTLD_NOW); 1.50 + } 1.51 + if (!curl_lib_) { 1.52 + curl_lib_ = dlopen("libcurl.so.3", RTLD_NOW); 1.53 + } 1.54 + if (!curl_lib_) { 1.55 + std::cout << "Could not find libcurl via dlopen"; 1.56 + return; 1.57 + } 1.58 + std::cout << "LibcurlWrapper init succeeded"; 1.59 + init_ok_ = true; 1.60 + return; 1.61 +} 1.62 + 1.63 +bool LibcurlWrapper::SetProxy(const string& proxy_host, 1.64 + const string& proxy_userpwd) { 1.65 + if (!init_ok_) { 1.66 + return false; 1.67 + } 1.68 + // Set proxy information if necessary. 1.69 + if (!proxy_host.empty()) { 1.70 + (*easy_setopt_)(curl_, CURLOPT_PROXY, proxy_host.c_str()); 1.71 + } else { 1.72 + std::cout << "SetProxy called with empty proxy host."; 1.73 + return false; 1.74 + } 1.75 + if (!proxy_userpwd.empty()) { 1.76 + (*easy_setopt_)(curl_, CURLOPT_PROXYUSERPWD, proxy_userpwd.c_str()); 1.77 + } else { 1.78 + std::cout << "SetProxy called with empty proxy username/password."; 1.79 + return false; 1.80 + } 1.81 + std::cout << "Set proxy host to " << proxy_host; 1.82 + return true; 1.83 +} 1.84 + 1.85 +bool LibcurlWrapper::AddFile(const string& upload_file_path, 1.86 + const string& basename) { 1.87 + if (!init_ok_) { 1.88 + return false; 1.89 + } 1.90 + std::cout << "Adding " << upload_file_path << " to form upload."; 1.91 + // Add form file. 1.92 + (*formadd_)(&formpost_, &lastptr_, 1.93 + CURLFORM_COPYNAME, basename.c_str(), 1.94 + CURLFORM_FILE, upload_file_path.c_str(), 1.95 + CURLFORM_END); 1.96 + 1.97 + return true; 1.98 +} 1.99 + 1.100 +// Callback to get the response data from server. 1.101 +static size_t WriteCallback(void *ptr, size_t size, 1.102 + size_t nmemb, void *userp) { 1.103 + if (!userp) 1.104 + return 0; 1.105 + 1.106 + string *response = reinterpret_cast<string *>(userp); 1.107 + size_t real_size = size * nmemb; 1.108 + response->append(reinterpret_cast<char *>(ptr), real_size); 1.109 + return real_size; 1.110 +} 1.111 + 1.112 +bool LibcurlWrapper::SendRequest(const string& url, 1.113 + const std::map<string, string>& parameters, 1.114 + string* server_response) { 1.115 + (*easy_setopt_)(curl_, CURLOPT_URL, url.c_str()); 1.116 + std::map<string, string>::const_iterator iter = parameters.begin(); 1.117 + for (; iter != parameters.end(); ++iter) 1.118 + (*formadd_)(&formpost_, &lastptr_, 1.119 + CURLFORM_COPYNAME, iter->first.c_str(), 1.120 + CURLFORM_COPYCONTENTS, iter->second.c_str(), 1.121 + CURLFORM_END); 1.122 + 1.123 + (*easy_setopt_)(curl_, CURLOPT_HTTPPOST, formpost_); 1.124 + if (server_response != NULL) { 1.125 + (*easy_setopt_)(curl_, CURLOPT_WRITEFUNCTION, WriteCallback); 1.126 + (*easy_setopt_)(curl_, CURLOPT_WRITEDATA, 1.127 + reinterpret_cast<void *>(server_response)); 1.128 + } 1.129 + 1.130 + CURLcode err_code = CURLE_OK; 1.131 + err_code = (*easy_perform_)(curl_); 1.132 + easy_strerror_ = reinterpret_cast<const char* (*)(CURLcode)> 1.133 + (dlsym(curl_lib_, "curl_easy_strerror")); 1.134 + 1.135 +#ifndef NDEBUG 1.136 + if (err_code != CURLE_OK) 1.137 + fprintf(stderr, "Failed to send http request to %s, error: %s\n", 1.138 + url.c_str(), 1.139 + (*easy_strerror_)(err_code)); 1.140 +#endif 1.141 + if (headerlist_ != NULL) { 1.142 + (*slist_free_all_)(headerlist_); 1.143 + } 1.144 + 1.145 + (*easy_cleanup_)(curl_); 1.146 + if (formpost_ != NULL) { 1.147 + (*formfree_)(formpost_); 1.148 + } 1.149 + 1.150 + return err_code == CURLE_OK; 1.151 +} 1.152 + 1.153 +bool LibcurlWrapper::Init() { 1.154 + if (!init_ok_) { 1.155 + std::cout << "Init_OK was not true in LibcurlWrapper::Init(), check earlier log messages"; 1.156 + return false; 1.157 + } 1.158 + 1.159 + if (!SetFunctionPointers()) { 1.160 + std::cout << "Could not find function pointers"; 1.161 + init_ok_ = false; 1.162 + return false; 1.163 + } 1.164 + 1.165 + curl_ = (*easy_init_)(); 1.166 + 1.167 + last_curl_error_ = "No Error"; 1.168 + 1.169 + if (!curl_) { 1.170 + dlclose(curl_lib_); 1.171 + std::cout << "Curl initialization failed"; 1.172 + return false; 1.173 + } 1.174 + 1.175 + // Disable 100-continue header. 1.176 + char buf[] = "Expect:"; 1.177 + 1.178 + headerlist_ = (*slist_append_)(headerlist_, buf); 1.179 + (*easy_setopt_)(curl_, CURLOPT_HTTPHEADER, headerlist_); 1.180 + return true; 1.181 +} 1.182 + 1.183 +#define SET_AND_CHECK_FUNCTION_POINTER(var, function_name, type) \ 1.184 + var = reinterpret_cast<type>(dlsym(curl_lib_, function_name)); \ 1.185 + if (!var) { \ 1.186 + std::cout << "Could not find libcurl function " << function_name; \ 1.187 + init_ok_ = false; \ 1.188 + return false; \ 1.189 + } 1.190 + 1.191 +bool LibcurlWrapper::SetFunctionPointers() { 1.192 + 1.193 + SET_AND_CHECK_FUNCTION_POINTER(easy_init_, 1.194 + "curl_easy_init", 1.195 + CURL*(*)()); 1.196 + 1.197 + SET_AND_CHECK_FUNCTION_POINTER(easy_setopt_, 1.198 + "curl_easy_setopt", 1.199 + CURLcode(*)(CURL*, CURLoption, ...)); 1.200 + 1.201 + SET_AND_CHECK_FUNCTION_POINTER(formadd_, "curl_formadd", 1.202 + CURLFORMcode(*)(curl_httppost**, curl_httppost**, ...)); 1.203 + 1.204 + SET_AND_CHECK_FUNCTION_POINTER(slist_append_, "curl_slist_append", 1.205 + curl_slist*(*)(curl_slist*, const char*)); 1.206 + 1.207 + SET_AND_CHECK_FUNCTION_POINTER(easy_perform_, 1.208 + "curl_easy_perform", 1.209 + CURLcode(*)(CURL*)); 1.210 + 1.211 + SET_AND_CHECK_FUNCTION_POINTER(easy_cleanup_, 1.212 + "curl_easy_cleanup", 1.213 + void(*)(CURL*)); 1.214 + 1.215 + SET_AND_CHECK_FUNCTION_POINTER(slist_free_all_, 1.216 + "curl_slist_free_all", 1.217 + void(*)(curl_slist*)); 1.218 + 1.219 + SET_AND_CHECK_FUNCTION_POINTER(formfree_, 1.220 + "curl_formfree", 1.221 + void(*)(curl_httppost*)); 1.222 + return true; 1.223 +} 1.224 + 1.225 +}