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;
}
CURLcode err_code = CURLE_OK;
CURLcode (*curl_easy_setopt)(CURL *, CURLoption, ...);
*(void**) (&curl_easy_setopt) = dlsym(curl_lib, "curl_easy_setopt");
(*curl_easy_setopt)(curl, CURLOPT_URL, url.c_str());
(*curl_easy_setopt)(curl, CURLOPT_USERAGENT, kUserAgent);
// Set proxy information if necessary. // Set proxy information if necessary.
if (!proxy.empty()) if (!proxy.empty())
curl_easy_setopt(curl, CURLOPT_PROXY, proxy.c_str()); (*curl_easy_setopt)(curl, CURLOPT_PROXY, proxy.c_str());
if (!proxy_user_pwd.empty()) if (!proxy_user_pwd.empty())
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_user_pwd.c_str()); (*curl_easy_setopt)(curl, CURLOPT_PROXYUSERPWD, proxy_user_pwd.c_str());
struct curl_httppost *formpost = NULL; struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL; struct curl_httppost *lastptr = NULL;
// Add form data. // Add form data.
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(); map<string, string>::const_iterator iter = parameters.begin();
for (; iter != parameters.end(); ++iter) for (; iter != parameters.end(); ++iter)
curl_formadd(&formpost, &lastptr, (*curl_formadd)(&formpost, &lastptr,
CURLFORM_COPYNAME, iter->first.c_str(), CURLFORM_COPYNAME, iter->first.c_str(),
CURLFORM_COPYCONTENTS, iter->second.c_str(), CURLFORM_COPYCONTENTS, iter->second.c_str(),
CURLFORM_END); CURLFORM_END);
// Add form file. // Add form file.
curl_formadd(&formpost, &lastptr, (*curl_formadd)(&formpost, &lastptr,
CURLFORM_COPYNAME, file_part_name.c_str(), CURLFORM_COPYNAME, file_part_name.c_str(),
CURLFORM_FILE, upload_file.c_str(), CURLFORM_FILE, upload_file.c_str(),
CURLFORM_END); CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); (*curl_easy_setopt)(curl, CURLOPT_HTTPPOST, formpost);
// Disable 100-continue header. // Disable 100-continue header.
struct curl_slist *headerlist = NULL; struct curl_slist *headerlist = NULL;
char buf[] = "Expect:"; char buf[] = "Expect:";
headerlist = curl_slist_append(headerlist, buf); struct curl_slist* (*curl_slist_append)(struct curl_slist *, const char *);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); *(void**) (&curl_slist_append) = dlsym(curl_lib, "curl_slist_append");
headerlist = (*curl_slist_append)(headerlist, buf);
(*curl_easy_setopt)(curl, CURLOPT_HTTPHEADER, headerlist);
if (response_body != NULL) { if (response_body != NULL) {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); (*curl_easy_setopt)(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (*curl_easy_setopt)(curl, CURLOPT_WRITEDATA,
reinterpret_cast<void *>(response_body)); reinterpret_cast<void *>(response_body));
} }
err_code = curl_easy_perform(curl); CURLcode (*curl_easy_perform)(CURL *);
*(void**) (&curl_easy_perform) = dlsym(curl_lib, "curl_easy_perform");
err_code = (*curl_easy_perform)(curl);
#ifndef NDEBUG #ifndef NDEBUG
const char* (*curl_easy_strerror)(CURLcode);
*(void**) (&curl_easy_strerror) = dlsym(curl_lib, "curl_easy_strerror");
if (err_code != CURLE_OK) if (err_code != CURLE_OK)
fprintf(stderr, "Failed to send http request to %s, error: %s\n", fprintf(stderr, "Failed to send http request to %s, error: %s\n",
url.c_str(), url.c_str(),
curl_easy_strerror(err_code)); (*curl_easy_strerror)(err_code));
#endif #endif
if (error_description != NULL) if (error_description != NULL)
*error_description = curl_easy_strerror(err_code); *error_description = (*curl_easy_strerror)(err_code);
if (curl != NULL) void (*curl_easy_cleanup)(CURL *);
curl_easy_cleanup(curl); *(void**) (&curl_easy_cleanup) = dlsym(curl_lib, "curl_easy_cleanup");
if (formpost != NULL) (*curl_easy_cleanup)(curl);
curl_formfree(formpost); if (formpost != NULL) {
if (headerlist != NULL) void (*curl_formfree)(struct curl_httppost *);
curl_slist_free_all(headerlist); *(void**) (&curl_formfree) = dlsym(curl_lib, "curl_formfree");
return err_code == CURLE_OK; (*curl_formfree)(formpost);
} }
return false; 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 $^