1 : // Copyright (c) 2006, Google Inc.
2 : // All rights reserved.
3 : //
4 : // Redistribution and use in source and binary forms, with or without
5 : // modification, are permitted provided that the following conditions are
6 : // met:
7 : //
8 : // * Redistributions of source code must retain the above copyright
9 : // notice, this list of conditions and the following disclaimer.
10 : // * Redistributions in binary form must reproduce the above
11 : // copyright notice, this list of conditions and the following disclaimer
12 : // in the documentation and/or other materials provided with the
13 : // distribution.
14 : // * Neither the name of Google Inc. nor the names of its
15 : // contributors may be used to endorse or promote products derived from
16 : // this software without specific prior written permission.
17 : //
18 : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 :
30 : #include "common/linux/http_upload.h"
31 :
32 : #include <assert.h>
33 : #include <dlfcn.h>
34 : #include <curl/curl.h>
35 : #include <curl/easy.h>
36 :
37 : namespace {
38 :
39 : // Callback to get the response data from server.
40 0 : static size_t WriteCallback(void *ptr, size_t size,
41 : size_t nmemb, void *userp) {
42 0 : if (!userp)
43 0 : return 0;
44 :
45 0 : std::string *response = reinterpret_cast<std::string *>(userp);
46 0 : size_t real_size = size * nmemb;
47 0 : response->append(reinterpret_cast<char *>(ptr), real_size);
48 0 : return real_size;
49 : }
50 :
51 : } // namespace
52 :
53 : namespace google_breakpad {
54 :
55 : static const char kUserAgent[] = "Breakpad/1.0 (Linux)";
56 :
57 : // static
58 0 : bool HTTPUpload::SendRequest(const string &url,
59 : const map<string, string> ¶meters,
60 : const string &upload_file,
61 : const string &file_part_name,
62 : const string &proxy,
63 : const string &proxy_user_pwd,
64 : const string &ca_certificate_file,
65 : string *response_body,
66 : string *error_description) {
67 0 : if (!CheckParameters(parameters))
68 0 : return false;
69 :
70 0 : void *curl_lib = dlopen("libcurl.so", RTLD_NOW);
71 0 : if (!curl_lib) {
72 0 : if (error_description != NULL)
73 0 : *error_description = dlerror();
74 0 : curl_lib = dlopen("libcurl.so.4", RTLD_NOW);
75 : }
76 0 : if (!curl_lib) {
77 : // Debian gives libcurl a different name when it is built against GnuTLS
78 : // instead of OpenSSL.
79 0 : curl_lib = dlopen("libcurl-gnutls.so.4", RTLD_NOW);
80 : }
81 0 : if (!curl_lib) {
82 0 : curl_lib = dlopen("libcurl.so.3", RTLD_NOW);
83 : }
84 0 : if (!curl_lib) {
85 0 : return false;
86 : }
87 :
88 : CURL* (*curl_easy_init)(void);
89 0 : *(void**) (&curl_easy_init) = dlsym(curl_lib, "curl_easy_init");
90 0 : CURL *curl = (*curl_easy_init)();
91 0 : if (error_description != NULL)
92 0 : *error_description = "No Error";
93 :
94 0 : if (!curl) {
95 0 : dlclose(curl_lib);
96 0 : return false;
97 : }
98 :
99 0 : CURLcode err_code = CURLE_OK;
100 : CURLcode (*curl_easy_setopt)(CURL *, CURLoption, ...);
101 0 : *(void**) (&curl_easy_setopt) = dlsym(curl_lib, "curl_easy_setopt");
102 0 : (*curl_easy_setopt)(curl, CURLOPT_URL, url.c_str());
103 0 : (*curl_easy_setopt)(curl, CURLOPT_USERAGENT, kUserAgent);
104 : // Set proxy information if necessary.
105 0 : if (!proxy.empty())
106 0 : (*curl_easy_setopt)(curl, CURLOPT_PROXY, proxy.c_str());
107 0 : if (!proxy_user_pwd.empty())
108 0 : (*curl_easy_setopt)(curl, CURLOPT_PROXYUSERPWD, proxy_user_pwd.c_str());
109 :
110 0 : if (!ca_certificate_file.empty())
111 0 : (*curl_easy_setopt)(curl, CURLOPT_CAINFO, ca_certificate_file.c_str());
112 :
113 0 : struct curl_httppost *formpost = NULL;
114 0 : struct curl_httppost *lastptr = NULL;
115 : // Add form data.
116 : CURLFORMcode (*curl_formadd)(struct curl_httppost **, struct curl_httppost **, ...);
117 0 : *(void**) (&curl_formadd) = dlsym(curl_lib, "curl_formadd");
118 0 : map<string, string>::const_iterator iter = parameters.begin();
119 0 : for (; iter != parameters.end(); ++iter)
120 : (*curl_formadd)(&formpost, &lastptr,
121 0 : CURLFORM_COPYNAME, iter->first.c_str(),
122 0 : CURLFORM_COPYCONTENTS, iter->second.c_str(),
123 0 : CURLFORM_END);
124 :
125 : // Add form file.
126 : (*curl_formadd)(&formpost, &lastptr,
127 : CURLFORM_COPYNAME, file_part_name.c_str(),
128 : CURLFORM_FILE, upload_file.c_str(),
129 0 : CURLFORM_END);
130 :
131 0 : (*curl_easy_setopt)(curl, CURLOPT_HTTPPOST, formpost);
132 :
133 : // Disable 100-continue header.
134 0 : struct curl_slist *headerlist = NULL;
135 0 : char buf[] = "Expect:";
136 : struct curl_slist* (*curl_slist_append)(struct curl_slist *, const char *);
137 0 : *(void**) (&curl_slist_append) = dlsym(curl_lib, "curl_slist_append");
138 0 : headerlist = (*curl_slist_append)(headerlist, buf);
139 0 : (*curl_easy_setopt)(curl, CURLOPT_HTTPHEADER, headerlist);
140 :
141 0 : if (response_body != NULL) {
142 0 : (*curl_easy_setopt)(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
143 : (*curl_easy_setopt)(curl, CURLOPT_WRITEDATA,
144 0 : reinterpret_cast<void *>(response_body));
145 : }
146 :
147 : CURLcode (*curl_easy_perform)(CURL *);
148 0 : *(void**) (&curl_easy_perform) = dlsym(curl_lib, "curl_easy_perform");
149 0 : err_code = (*curl_easy_perform)(curl);
150 : const char* (*curl_easy_strerror)(CURLcode);
151 0 : *(void**) (&curl_easy_strerror) = dlsym(curl_lib, "curl_easy_strerror");
152 : #ifndef NDEBUG
153 0 : if (err_code != CURLE_OK)
154 : fprintf(stderr, "Failed to send http request to %s, error: %s\n",
155 : url.c_str(),
156 0 : (*curl_easy_strerror)(err_code));
157 : #endif
158 0 : if (error_description != NULL)
159 0 : *error_description = (*curl_easy_strerror)(err_code);
160 :
161 : void (*curl_easy_cleanup)(CURL *);
162 0 : *(void**) (&curl_easy_cleanup) = dlsym(curl_lib, "curl_easy_cleanup");
163 0 : (*curl_easy_cleanup)(curl);
164 0 : if (formpost != NULL) {
165 : void (*curl_formfree)(struct curl_httppost *);
166 0 : *(void**) (&curl_formfree) = dlsym(curl_lib, "curl_formfree");
167 0 : (*curl_formfree)(formpost);
168 : }
169 0 : if (headerlist != NULL) {
170 : void (*curl_slist_free_all)(struct curl_slist *);
171 0 : *(void**) (&curl_slist_free_all) = dlsym(curl_lib, "curl_slist_free_all");
172 0 : (*curl_slist_free_all)(headerlist);
173 : }
174 0 : dlclose(curl_lib);
175 0 : return err_code == CURLE_OK;
176 : }
177 :
178 : // static
179 0 : bool HTTPUpload::CheckParameters(const map<string, string> ¶meters) {
180 0 : for (map<string, string>::const_iterator pos = parameters.begin();
181 0 : pos != parameters.end(); ++pos) {
182 0 : const string &str = pos->first;
183 0 : if (str.size() == 0)
184 0 : return false; // disallow empty parameter names
185 0 : for (unsigned int i = 0; i < str.size(); ++i) {
186 0 : int c = str[i];
187 0 : if (c < 32 || c == '"' || c > 127) {
188 0 : return false;
189 : }
190 : }
191 : }
192 0 : return true;
193 : }
194 :
195 : } // namespace google_breakpad
|