mirror of
https://git.suyu.dev/suyu/breakpad.git
synced 2025-12-27 01:35:06 +01:00
New uploader for Linux with unit tests, and gflags/glog libraries
http://breakpad.appspot.com/29004 A=nealsid R=chris masone at chromium org git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@403 4c0a9323-5329-0410-9bdc-e9ce6186880e
This commit is contained in:
parent
4ee3ddcd33
commit
895d3d17ee
17 changed files with 3616 additions and 9 deletions
196
src/common/linux/google_crashdump_uploader.cc
Normal file
196
src/common/linux/google_crashdump_uploader.cc
Normal file
|
|
@ -0,0 +1,196 @@
|
|||
// Copyright (c) 2009, Google Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
#include "common/linux/google_crashdump_uploader.h"
|
||||
#include "common/linux/libcurl_wrapper.h"
|
||||
#include "third_party/linux/include/glog/logging.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace google_breakpad {
|
||||
|
||||
GoogleCrashdumpUploader::GoogleCrashdumpUploader(const std::string& product,
|
||||
const std::string& version,
|
||||
const std::string& guid,
|
||||
const std::string& ptime,
|
||||
const std::string& ctime,
|
||||
const std::string& email,
|
||||
const std::string& comments,
|
||||
const std::string& minidump_pathname,
|
||||
const std::string& crash_server,
|
||||
const std::string& proxy_host,
|
||||
const std::string& proxy_userpassword) {
|
||||
LibcurlWrapper* http_layer = new LibcurlWrapper();
|
||||
Init(product,
|
||||
version,
|
||||
guid,
|
||||
ptime,
|
||||
ctime,
|
||||
email,
|
||||
comments,
|
||||
minidump_pathname,
|
||||
crash_server,
|
||||
proxy_host,
|
||||
proxy_userpassword,
|
||||
http_layer);
|
||||
}
|
||||
|
||||
GoogleCrashdumpUploader::GoogleCrashdumpUploader(const std::string& product,
|
||||
const std::string& version,
|
||||
const std::string& guid,
|
||||
const std::string& ptime,
|
||||
const std::string& ctime,
|
||||
const std::string& email,
|
||||
const std::string& comments,
|
||||
const std::string& minidump_pathname,
|
||||
const std::string& crash_server,
|
||||
const std::string& proxy_host,
|
||||
const std::string& proxy_userpassword,
|
||||
LibcurlWrapper* http_layer) {
|
||||
Init(product,
|
||||
version,
|
||||
guid,
|
||||
ptime,
|
||||
ctime,
|
||||
email,
|
||||
comments,
|
||||
minidump_pathname,
|
||||
crash_server,
|
||||
proxy_host,
|
||||
proxy_userpassword,
|
||||
http_layer);
|
||||
}
|
||||
|
||||
void GoogleCrashdumpUploader::Init(const std::string& product,
|
||||
const std::string& version,
|
||||
const std::string& guid,
|
||||
const std::string& ptime,
|
||||
const std::string& ctime,
|
||||
const std::string& email,
|
||||
const std::string& comments,
|
||||
const std::string& minidump_pathname,
|
||||
const std::string& crash_server,
|
||||
const std::string& proxy_host,
|
||||
const std::string& proxy_userpassword,
|
||||
LibcurlWrapper* http_layer) {
|
||||
product_ = product;
|
||||
version_ = version;
|
||||
guid_ = guid;
|
||||
ptime_ = ptime;
|
||||
ctime_ = ctime;
|
||||
email_ = email;
|
||||
comments_ = comments;
|
||||
http_layer_ = http_layer;
|
||||
|
||||
crash_server_ = crash_server;
|
||||
proxy_host_ = proxy_host;
|
||||
proxy_userpassword_ = proxy_userpassword;
|
||||
minidump_pathname_ = minidump_pathname;
|
||||
LOG(INFO) << "Uploader initializing";
|
||||
LOG(INFO) << "\tProduct: " << product_;
|
||||
LOG(INFO) << "\tVersion: " << version_;
|
||||
LOG(INFO) << "\tGUID: " << guid_;
|
||||
if (!ptime_.empty()) {
|
||||
LOG(INFO) << "\tProcess uptime: " << ptime_;
|
||||
}
|
||||
if (!ctime_.empty()) {
|
||||
LOG(INFO) << "\tCumulative Process uptime: " << ctime_;
|
||||
}
|
||||
if (!email_.empty()) {
|
||||
LOG(INFO) << "\tEmail: " << email_;
|
||||
}
|
||||
if (!comments_.empty()) {
|
||||
LOG(INFO) << "\tComments: " << comments_;
|
||||
}
|
||||
}
|
||||
|
||||
bool GoogleCrashdumpUploader::CheckRequiredParametersArePresent() {
|
||||
std::string error_text;
|
||||
if (product_.empty()) {
|
||||
error_text.append("\nProduct name must be specified.");
|
||||
}
|
||||
|
||||
if (version_.empty()) {
|
||||
error_text.append("\nProduct version must be specified.");
|
||||
}
|
||||
|
||||
if (guid_.empty()) {
|
||||
error_text.append("\nClient ID must be specified.");
|
||||
}
|
||||
|
||||
if (minidump_pathname_.empty()) {
|
||||
error_text.append("\nMinidump pathname must be specified.");
|
||||
}
|
||||
|
||||
if (!error_text.empty()) {
|
||||
LOG(ERROR) << error_text;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool GoogleCrashdumpUploader::Upload() {
|
||||
bool ok = http_layer_->Init();
|
||||
if (!ok) {
|
||||
LOG(WARNING) << "http layer init failed";
|
||||
return ok;
|
||||
}
|
||||
|
||||
if (!CheckRequiredParametersArePresent()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct stat st;
|
||||
int err = stat(minidump_pathname_.c_str(), &st);
|
||||
if (err) {
|
||||
LOG(WARNING) << minidump_pathname_ << " could not be found: " << errno;
|
||||
return false;
|
||||
}
|
||||
|
||||
parameters_["prod"] = product_;
|
||||
parameters_["ver"] = version_;
|
||||
parameters_["guid"] = guid_;
|
||||
parameters_["ptime"] = ptime_;
|
||||
parameters_["ctime"] = ctime_;
|
||||
parameters_["email"] = email_;
|
||||
parameters_["comments_"] = comments_;
|
||||
if (!http_layer_->AddFile(minidump_pathname_,
|
||||
"upload_file_minidump")) {
|
||||
return false;
|
||||
}
|
||||
LOG(INFO) << "Sending request to " << crash_server_;
|
||||
return http_layer_->SendRequest(crash_server_,
|
||||
parameters_,
|
||||
NULL);
|
||||
}
|
||||
}
|
||||
98
src/common/linux/google_crashdump_uploader.h
Normal file
98
src/common/linux/google_crashdump_uploader.h
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
// Copyright (c) 2009, Google Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
namespace google_breakpad {
|
||||
|
||||
class LibcurlWrapper;
|
||||
|
||||
class GoogleCrashdumpUploader {
|
||||
public:
|
||||
GoogleCrashdumpUploader(const std::string& product,
|
||||
const std::string& version,
|
||||
const std::string& guid,
|
||||
const std::string& ptime,
|
||||
const std::string& ctime,
|
||||
const std::string& email,
|
||||
const std::string& comments,
|
||||
const std::string& minidump_pathname,
|
||||
const std::string& crash_server,
|
||||
const std::string& proxy_host,
|
||||
const std::string& proxy_userpassword);
|
||||
|
||||
GoogleCrashdumpUploader(const std::string& product,
|
||||
const std::string& version,
|
||||
const std::string& guid,
|
||||
const std::string& ptime,
|
||||
const std::string& ctime,
|
||||
const std::string& email,
|
||||
const std::string& comments,
|
||||
const std::string& minidump_pathname,
|
||||
const std::string& crash_server,
|
||||
const std::string& proxy_host,
|
||||
const std::string& proxy_userpassword,
|
||||
LibcurlWrapper* http_layer);
|
||||
|
||||
void Init(const std::string& product,
|
||||
const std::string& version,
|
||||
const std::string& guid,
|
||||
const std::string& ptime,
|
||||
const std::string& ctime,
|
||||
const std::string& email,
|
||||
const std::string& comments,
|
||||
const std::string& minidump_pathname,
|
||||
const std::string& crash_server,
|
||||
const std::string& proxy_host,
|
||||
const std::string& proxy_userpassword,
|
||||
LibcurlWrapper* http_layer);
|
||||
bool Upload();
|
||||
|
||||
private:
|
||||
bool CheckRequiredParametersArePresent();
|
||||
|
||||
LibcurlWrapper* http_layer_;
|
||||
std::string product_;
|
||||
std::string version_;
|
||||
std::string guid_;
|
||||
std::string ptime_;
|
||||
std::string ctime_;
|
||||
std::string email_;
|
||||
std::string comments_;
|
||||
std::string minidump_pathname_;
|
||||
|
||||
std::string crash_server_;
|
||||
std::string proxy_host_;
|
||||
std::string proxy_userpassword_;
|
||||
|
||||
std::map<std::string, std::string> parameters_;
|
||||
};
|
||||
}
|
||||
166
src/common/linux/google_crashdump_uploader_test.cc
Normal file
166
src/common/linux/google_crashdump_uploader_test.cc
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
// Copyright (c) 2009, Google Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// Unit test for crash dump uploader.
|
||||
|
||||
#include "common/linux/google_crashdump_uploader.h"
|
||||
#include "common/linux/libcurl_wrapper.h"
|
||||
#include "breakpad_googletest_includes.h"
|
||||
|
||||
namespace google_breakpad {
|
||||
|
||||
using ::testing::Return;
|
||||
using ::testing::_;
|
||||
|
||||
class MockLibcurlWrapper : public LibcurlWrapper {
|
||||
public:
|
||||
MOCK_METHOD0(Init, bool());
|
||||
MOCK_METHOD2(SetProxy, bool(const std::string& proxy_host,
|
||||
const std::string& proxy_userpwd));
|
||||
MOCK_METHOD2(AddFile, bool(const std::string& upload_file_path,
|
||||
const std::string& basename));
|
||||
MOCK_METHOD3(SendRequest,
|
||||
bool(const std::string& url,
|
||||
const std::map<std::string, std::string>& parameters,
|
||||
std::string* server_response));
|
||||
};
|
||||
|
||||
class GoogleCrashdumpUploaderTest : public ::testing::Test {
|
||||
};
|
||||
|
||||
TEST_F(GoogleCrashdumpUploaderTest, InitFailsCausesUploadFailure) {
|
||||
MockLibcurlWrapper m;
|
||||
EXPECT_CALL(m, Init()).Times(1).WillOnce(Return(false));
|
||||
GoogleCrashdumpUploader *uploader = new GoogleCrashdumpUploader("foobar",
|
||||
"1.0",
|
||||
"AAA-BBB",
|
||||
"",
|
||||
"",
|
||||
"test@test.com",
|
||||
"none",
|
||||
"/tmp/foo.dmp",
|
||||
"http://foo.com",
|
||||
"",
|
||||
"",
|
||||
&m);
|
||||
ASSERT_FALSE(uploader->Upload());
|
||||
}
|
||||
|
||||
TEST_F(GoogleCrashdumpUploaderTest, TestSendRequestHappensWithValidParameters) {
|
||||
// Create a temp file
|
||||
char tempfn[80] = "/tmp/googletest-upload-XXXXXX";
|
||||
int fd = mkstemp(tempfn);
|
||||
ASSERT_NE(fd, -1);
|
||||
close(fd);
|
||||
|
||||
MockLibcurlWrapper m;
|
||||
EXPECT_CALL(m, Init()).Times(1).WillOnce(Return(true));
|
||||
EXPECT_CALL(m, AddFile(tempfn, _)).WillOnce(Return(true));
|
||||
EXPECT_CALL(m,
|
||||
SendRequest("http://foo.com",_,_)).Times(1).WillOnce(Return(true));
|
||||
GoogleCrashdumpUploader *uploader = new GoogleCrashdumpUploader("foobar",
|
||||
"1.0",
|
||||
"AAA-BBB",
|
||||
"",
|
||||
"",
|
||||
"test@test.com",
|
||||
"none",
|
||||
tempfn,
|
||||
"http://foo.com",
|
||||
"",
|
||||
"",
|
||||
&m);
|
||||
ASSERT_TRUE(uploader->Upload());
|
||||
}
|
||||
|
||||
|
||||
TEST_F(GoogleCrashdumpUploaderTest, InvalidPathname) {
|
||||
MockLibcurlWrapper m;
|
||||
EXPECT_CALL(m, Init()).Times(1).WillOnce(Return(true));
|
||||
EXPECT_CALL(m, SendRequest(_,_,_)).Times(0);
|
||||
GoogleCrashdumpUploader *uploader = new GoogleCrashdumpUploader("foobar",
|
||||
"1.0",
|
||||
"AAA-BBB",
|
||||
"",
|
||||
"",
|
||||
"test@test.com",
|
||||
"none",
|
||||
"/tmp/foo.dmp",
|
||||
"http://foo.com",
|
||||
"",
|
||||
"",
|
||||
&m);
|
||||
ASSERT_FALSE(uploader->Upload());
|
||||
}
|
||||
|
||||
TEST_F(GoogleCrashdumpUploaderTest, TestRequiredParametersMustBePresent) {
|
||||
// Test with empty product name.
|
||||
GoogleCrashdumpUploader uploader("",
|
||||
"1.0",
|
||||
"AAA-BBB",
|
||||
"",
|
||||
"",
|
||||
"test@test.com",
|
||||
"none",
|
||||
"/tmp/foo.dmp",
|
||||
"http://foo.com",
|
||||
"",
|
||||
"");
|
||||
ASSERT_FALSE(uploader.Upload());
|
||||
|
||||
// Test with empty product version.
|
||||
GoogleCrashdumpUploader uploader1("product",
|
||||
"",
|
||||
"AAA-BBB",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"/tmp/foo.dmp",
|
||||
"",
|
||||
"",
|
||||
"");
|
||||
|
||||
ASSERT_FALSE(uploader1.Upload());
|
||||
|
||||
// Test with empty client GUID.
|
||||
GoogleCrashdumpUploader uploader2("product",
|
||||
"1.0",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"/tmp/foo.dmp",
|
||||
"",
|
||||
"",
|
||||
"");
|
||||
ASSERT_FALSE(uploader2.Upload());
|
||||
}
|
||||
}
|
||||
209
src/common/linux/libcurl_wrapper.cc
Normal file
209
src/common/linux/libcurl_wrapper.cc
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
// Copyright (c) 2009, Google Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include <curl/easy.h>
|
||||
#include <curl/types.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "common/linux/libcurl_wrapper.h"
|
||||
#include "third_party/linux/include/glog/logging.h"
|
||||
|
||||
namespace google_breakpad {
|
||||
LibcurlWrapper::LibcurlWrapper()
|
||||
: init_ok_(false),
|
||||
formpost_(NULL),
|
||||
lastptr_(NULL),
|
||||
headerlist_(NULL) {
|
||||
curl_lib_ = dlopen("libcurl.so", RTLD_NOW);
|
||||
if (!curl_lib_) {
|
||||
curl_lib_ = dlopen("libcurl.so.4", RTLD_NOW);
|
||||
}
|
||||
if (!curl_lib_) {
|
||||
curl_lib_ = dlopen("libcurl.so.3", RTLD_NOW);
|
||||
}
|
||||
if (!curl_lib_) {
|
||||
LOG(WARNING) << "Could not find libcurl via dlopen";
|
||||
return;
|
||||
}
|
||||
LOG(INFO) << "LibcurlWrapper init succeeded";
|
||||
init_ok_ = true;
|
||||
return;
|
||||
}
|
||||
|
||||
bool LibcurlWrapper::SetProxy(const std::string& proxy_host,
|
||||
const std::string& proxy_userpwd) {
|
||||
if (!init_ok_) {
|
||||
return false;
|
||||
}
|
||||
// Set proxy information if necessary.
|
||||
if (!proxy_host.empty()) {
|
||||
(*easy_setopt_)(curl_, CURLOPT_PROXY, proxy_host.c_str());
|
||||
} else {
|
||||
LOG(WARNING) << "SetProxy called with empty proxy host.";
|
||||
return false;
|
||||
}
|
||||
if (!proxy_userpwd.empty()) {
|
||||
(*easy_setopt_)(curl_, CURLOPT_PROXYUSERPWD, proxy_userpwd.c_str());
|
||||
} else {
|
||||
LOG(WARNING) << "SetProxy called with empty proxy username/password.";
|
||||
return false;
|
||||
}
|
||||
LOG(INFO) << "Set proxy host to " << proxy_host;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LibcurlWrapper::AddFile(const std::string& upload_file_path,
|
||||
const std::string& basename) {
|
||||
if (!init_ok_) {
|
||||
return false;
|
||||
}
|
||||
LOG(INFO) << "Adding " << upload_file_path << " to form upload.";
|
||||
// Add form file.
|
||||
(*formadd_)(&formpost_, &lastptr_,
|
||||
CURLFORM_COPYNAME, basename.c_str(),
|
||||
CURLFORM_FILE, upload_file_path.c_str(),
|
||||
CURLFORM_END);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Callback to get the response data from server.
|
||||
static size_t WriteCallback(void *ptr, size_t size,
|
||||
size_t nmemb, void *userp) {
|
||||
if (!userp)
|
||||
return 0;
|
||||
|
||||
std::string *response = reinterpret_cast<std::string *>(userp);
|
||||
size_t real_size = size * nmemb;
|
||||
response->append(reinterpret_cast<char *>(ptr), real_size);
|
||||
return real_size;
|
||||
}
|
||||
|
||||
bool LibcurlWrapper::SendRequest(const std::string& url,
|
||||
const std::map<std::string, std::string>& parameters,
|
||||
std::string* server_response) {
|
||||
(*easy_setopt_)(curl_, CURLOPT_URL, url.c_str());
|
||||
std::map<std::string, std::string>::const_iterator iter = parameters.begin();
|
||||
for (; iter != parameters.end(); ++iter)
|
||||
(*formadd_)(&formpost_, &lastptr_,
|
||||
CURLFORM_COPYNAME, iter->first.c_str(),
|
||||
CURLFORM_COPYCONTENTS, iter->second.c_str(),
|
||||
CURLFORM_END);
|
||||
|
||||
(*easy_setopt_)(curl_, CURLOPT_HTTPPOST, formpost_);
|
||||
if (server_response != NULL) {
|
||||
(*easy_setopt_)(curl_, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
(*easy_setopt_)(curl_, CURLOPT_WRITEDATA,
|
||||
reinterpret_cast<void *>(server_response));
|
||||
}
|
||||
|
||||
CURLcode err_code = CURLE_OK;
|
||||
err_code = (*easy_perform_)(curl_);
|
||||
*(void**) (&easy_strerror_) = dlsym(curl_lib_, "curl_easy_strerror");
|
||||
#ifndef NDEBUG
|
||||
if (err_code != CURLE_OK)
|
||||
fprintf(stderr, "Failed to send http request to %s, error: %s\n",
|
||||
url.c_str(),
|
||||
(*easy_strerror_)(err_code));
|
||||
#endif
|
||||
if (headerlist_ != NULL) {
|
||||
(*slist_free_all_)(headerlist_);
|
||||
}
|
||||
|
||||
(*easy_cleanup_)(curl_);
|
||||
if (formpost_ != NULL) {
|
||||
(*formfree_)(formpost_);
|
||||
}
|
||||
|
||||
return err_code == CURLE_OK;
|
||||
}
|
||||
|
||||
bool LibcurlWrapper::Init() {
|
||||
if (!init_ok_) {
|
||||
LOG(WARNING) << "Init_OK was not true in LibcurlWrapper::Init(), check earlier log messages";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!SetFunctionPointers()) {
|
||||
LOG(WARNING) << "Could not find function pointers";
|
||||
init_ok_ = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
curl_ = (*easy_init_)();
|
||||
|
||||
last_curl_error_ = "No Error";
|
||||
|
||||
if (!curl_) {
|
||||
dlclose(curl_lib_);
|
||||
LOG(WARNING) << "Curl initialization failed";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Disable 100-continue header.
|
||||
char buf[] = "Expect:";
|
||||
|
||||
headerlist_ = (*slist_append_)(headerlist_, buf);
|
||||
(*easy_setopt_)(curl_, CURLOPT_HTTPHEADER, headerlist_);
|
||||
return true;
|
||||
}
|
||||
|
||||
#define SET_AND_CHECK_FUNCTION_POINTER(var, function_name) \
|
||||
*(void**) (&var) = dlsym(curl_lib_, function_name); \
|
||||
if (!var) { \
|
||||
LOG(WARNING) << "Could not find libcurl function " << function_name; \
|
||||
init_ok_ = false; \
|
||||
return false; \
|
||||
}
|
||||
|
||||
bool LibcurlWrapper::SetFunctionPointers() {
|
||||
|
||||
SET_AND_CHECK_FUNCTION_POINTER(easy_init_,
|
||||
"curl_easy_init");
|
||||
SET_AND_CHECK_FUNCTION_POINTER(easy_setopt_,
|
||||
"curl_easy_setopt");
|
||||
SET_AND_CHECK_FUNCTION_POINTER(formadd_,
|
||||
"curl_formadd");
|
||||
SET_AND_CHECK_FUNCTION_POINTER(slist_append_,
|
||||
"curl_slist_append");
|
||||
SET_AND_CHECK_FUNCTION_POINTER(easy_perform_,
|
||||
"curl_easy_perform");
|
||||
SET_AND_CHECK_FUNCTION_POINTER(easy_cleanup_,
|
||||
"curl_easy_cleanup");
|
||||
SET_AND_CHECK_FUNCTION_POINTER(slist_free_all_,
|
||||
"curl_slist_free_all");
|
||||
SET_AND_CHECK_FUNCTION_POINTER(formfree_,
|
||||
"curl_formfree");
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
82
src/common/linux/libcurl_wrapper.h
Normal file
82
src/common/linux/libcurl_wrapper.h
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
// Copyright (c) 2009, Google Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// A wrapper for libcurl to do HTTP Uploads, to support easy mocking
|
||||
// and unit testing of the HTTPUpload class.
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <curl/curl.h>
|
||||
|
||||
namespace google_breakpad {
|
||||
class LibcurlWrapper {
|
||||
public:
|
||||
LibcurlWrapper();
|
||||
virtual bool Init();
|
||||
virtual bool SetProxy(const std::string& proxy_host,
|
||||
const std::string& proxy_userpwd);
|
||||
virtual bool AddFile(const std::string& upload_file_path,
|
||||
const std::string& basename);
|
||||
virtual bool SendRequest(const std::string& url,
|
||||
const std::map<std::string, std::string>& parameters,
|
||||
std::string* server_response);
|
||||
private:
|
||||
// This function initializes class state corresponding to function
|
||||
// pointers into the CURL library.
|
||||
bool SetFunctionPointers();
|
||||
|
||||
bool init_ok_; // Whether init succeeded
|
||||
void* curl_lib_; // Pointer to result of dlopen() on
|
||||
// curl library
|
||||
std::string last_curl_error_; // The text of the last error when
|
||||
// dealing
|
||||
// with CURL.
|
||||
|
||||
CURL *curl_; // Pointer for handle for CURL calls.
|
||||
|
||||
CURL* (*easy_init_)(void);
|
||||
|
||||
// Stateful pointers for calling into curl_formadd()
|
||||
struct curl_httppost *formpost_;
|
||||
struct curl_httppost *lastptr_;
|
||||
struct curl_slist *headerlist_;
|
||||
|
||||
// Function pointers into CURL library
|
||||
CURLcode (*easy_setopt_)(CURL *, CURLoption, ...);
|
||||
CURLFORMcode (*formadd_)(struct curl_httppost **,
|
||||
struct curl_httppost **, ...);
|
||||
struct curl_slist* (*slist_append_)(struct curl_slist *, const char *);
|
||||
void (*slist_free_all_)(struct curl_slist *);
|
||||
CURLcode (*easy_perform_)(CURL *);
|
||||
const char* (*easy_strerror_)(CURLcode);
|
||||
void (*easy_cleanup_)(CURL *);
|
||||
void (*formfree_)(struct curl_httppost *);
|
||||
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue