Issue 225 - dynamically load libcurl in http_upload. patch by Andrew Schultz <ajschult@verizon.net>, r=Liu Li

git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@232 4c0a9323-5329-0410-9bdc-e9ce6186880e
This commit is contained in:
ted.mielczarek 2007-11-20 02:35:47 +00:00
parent f0a07749af
commit 68b4798eb4
2 changed files with 88 additions and 54 deletions

View File

@ -28,6 +28,7 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cassert> #include <cassert>
#include <dlfcn.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <curl/easy.h> #include <curl/easy.h>
#include <curl/types.h> #include <curl/types.h>
@ -66,69 +67,102 @@ bool HTTPUpload::SendRequest(const string &url,
if (!CheckParameters(parameters)) if (!CheckParameters(parameters))
return false; return false;
CURL *curl = curl_easy_init(); void *curl_lib = dlopen("libcurl.so", RTLD_NOW);
CURLcode err_code = CURLE_OK; 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) {
return false;
}
CURL* (*curl_easy_init)(void);
*(void**) (&curl_easy_init) = dlsym(curl_lib, "curl_easy_init");
CURL *curl = (*curl_easy_init)();
if (error_description != NULL) if (error_description != NULL)
*error_description = "No Error"; *error_description = "No Error";
if (curl) { if (!curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); dlclose(curl_lib);
curl_easy_setopt(curl, CURLOPT_USERAGENT, kUserAgent); return false;
// Set proxy information if necessary. }
if (!proxy.empty())
curl_easy_setopt(curl, CURLOPT_PROXY, proxy.c_str());
if (!proxy_user_pwd.empty())
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_user_pwd.c_str());
struct curl_httppost *formpost = NULL; CURLcode err_code = CURLE_OK;
struct curl_httppost *lastptr = NULL; CURLcode (*curl_easy_setopt)(CURL *, CURLoption, ...);
// Add form data. *(void**) (&curl_easy_setopt) = dlsym(curl_lib, "curl_easy_setopt");
map<string, string>::const_iterator iter = parameters.begin(); (*curl_easy_setopt)(curl, CURLOPT_URL, url.c_str());
for (; iter != parameters.end(); ++iter) (*curl_easy_setopt)(curl, CURLOPT_USERAGENT, kUserAgent);
curl_formadd(&formpost, &lastptr, // Set proxy information if necessary.
CURLFORM_COPYNAME, iter->first.c_str(), if (!proxy.empty())
CURLFORM_COPYCONTENTS, iter->second.c_str(), (*curl_easy_setopt)(curl, CURLOPT_PROXY, proxy.c_str());
CURLFORM_END); if (!proxy_user_pwd.empty())
(*curl_easy_setopt)(curl, CURLOPT_PROXYUSERPWD, proxy_user_pwd.c_str());
// Add form file. struct curl_httppost *formpost = NULL;
curl_formadd(&formpost, &lastptr, struct curl_httppost *lastptr = NULL;
CURLFORM_COPYNAME, file_part_name.c_str(), // Add form data.
CURLFORM_FILE, upload_file.c_str(), CURLFORMcode (*curl_formadd)(struct curl_httppost **, struct curl_httppost **, ...);
*(void**) (&curl_formadd) = dlsym(curl_lib, "curl_formadd");
map<string, string>::const_iterator iter = parameters.begin();
for (; iter != parameters.end(); ++iter)
(*curl_formadd)(&formpost, &lastptr,
CURLFORM_COPYNAME, iter->first.c_str(),
CURLFORM_COPYCONTENTS, iter->second.c_str(),
CURLFORM_END); CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); // Add form file.
(*curl_formadd)(&formpost, &lastptr,
CURLFORM_COPYNAME, file_part_name.c_str(),
CURLFORM_FILE, upload_file.c_str(),
CURLFORM_END);
// Disable 100-continue header. (*curl_easy_setopt)(curl, CURLOPT_HTTPPOST, formpost);
struct curl_slist *headerlist = NULL;
char buf[] = "Expect:";
headerlist = curl_slist_append(headerlist, buf);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
if (response_body != NULL) { // Disable 100-continue header.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); struct curl_slist *headerlist = NULL;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, char buf[] = "Expect:";
reinterpret_cast<void *>(response_body)); struct curl_slist* (*curl_slist_append)(struct curl_slist *, const char *);
} *(void**) (&curl_slist_append) = dlsym(curl_lib, "curl_slist_append");
headerlist = (*curl_slist_append)(headerlist, buf);
(*curl_easy_setopt)(curl, CURLOPT_HTTPHEADER, headerlist);
err_code = curl_easy_perform(curl); if (response_body != NULL) {
#ifndef NDEBUG (*curl_easy_setopt)(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
if (err_code != CURLE_OK) (*curl_easy_setopt)(curl, CURLOPT_WRITEDATA,
fprintf(stderr, "Failed to send http request to %s, error: %s\n", reinterpret_cast<void *>(response_body));
url.c_str(),
curl_easy_strerror(err_code));
#endif
if (error_description != NULL)
*error_description = curl_easy_strerror(err_code);
if (curl != NULL)
curl_easy_cleanup(curl);
if (formpost != NULL)
curl_formfree(formpost);
if (headerlist != NULL)
curl_slist_free_all(headerlist);
return err_code == CURLE_OK;
} }
return false;
CURLcode (*curl_easy_perform)(CURL *);
*(void**) (&curl_easy_perform) = dlsym(curl_lib, "curl_easy_perform");
err_code = (*curl_easy_perform)(curl);
#ifndef NDEBUG
const char* (*curl_easy_strerror)(CURLcode);
*(void**) (&curl_easy_strerror) = dlsym(curl_lib, "curl_easy_strerror");
if (err_code != CURLE_OK)
fprintf(stderr, "Failed to send http request to %s, error: %s\n",
url.c_str(),
(*curl_easy_strerror)(err_code));
#endif
if (error_description != NULL)
*error_description = (*curl_easy_strerror)(err_code);
void (*curl_easy_cleanup)(CURL *);
*(void**) (&curl_easy_cleanup) = dlsym(curl_lib, "curl_easy_cleanup");
(*curl_easy_cleanup)(curl);
if (formpost != NULL) {
void (*curl_formfree)(struct curl_httppost *);
*(void**) (&curl_formfree) = dlsym(curl_lib, "curl_formfree");
(*curl_formfree)(formpost);
}
if (headerlist != NULL) {
void (*curl_slist_free_all)(struct curl_slist *);
*(void**) (&curl_slist_free_all) = dlsym(curl_lib, "curl_slist_free_all");
(*curl_slist_free_all)(headerlist);
}
dlclose(curl_lib);
return err_code == CURLE_OK;
} }
// static // static

View File

@ -12,10 +12,10 @@ DUMP_UPLOAD_OBJ=minidump_upload.o http_upload.o
SYM_UPLOAD_OBJ=sym_upload.o http_upload.o SYM_UPLOAD_OBJ=sym_upload.o http_upload.o
minidump_upload:$(DUMP_UPLOAD_OBJ) minidump_upload:$(DUMP_UPLOAD_OBJ)
$(CXX) $(CXXFLAGS) `curl-config --libs` -o $@ $^ $(CXX) $(CXXFLAGS) -ldl -o $@ $^
sym_upload:$(SYM_UPLOAD_OBJ) sym_upload:$(SYM_UPLOAD_OBJ)
$(CXX) $(CXXFLAGS) `curl-config --libs` -o $@ $^ $(CXX) $(CXXFLAGS) -ldl -o $@ $^
http_upload.o:../../../common/linux/http_upload.cc http_upload.o:../../../common/linux/http_upload.cc
$(CXX) $(CXXFLAGS) `curl-config --cflags` -c $^ $(CXX) $(CXXFLAGS) `curl-config --cflags` -c $^