OLD | NEW |
| (Empty) |
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 <cassert> | |
31 #include <dlfcn.h> | |
32 #include <curl/curl.h> | |
33 #include <curl/easy.h> | |
34 #include <curl/types.h> | |
35 | |
36 #include "common/linux/http_upload.h" | |
37 | |
38 namespace { | |
39 | |
40 // Callback to get the response data from server. | |
41 static size_t WriteCallback(void *ptr, size_t size, | |
42 size_t nmemb, void *userp) { | |
43 if (!userp) | |
44 return 0; | |
45 | |
46 std::string *response = reinterpret_cast<std::string *>(userp); | |
47 size_t real_size = size * nmemb; | |
48 response->append(reinterpret_cast<char *>(ptr), real_size); | |
49 return real_size; | |
50 } | |
51 | |
52 } // namespace | |
53 | |
54 namespace google_breakpad { | |
55 | |
56 static const char kUserAgent[] = "Breakpad/1.0 (Linux)"; | |
57 | |
58 // static | |
59 bool HTTPUpload::SendRequest(const string &url, | |
60 const map<string, string> ¶meters, | |
61 const string &upload_file, | |
62 const string &file_part_name, | |
63 const string &proxy, | |
64 const string &proxy_user_pwd, | |
65 string *response_body, | |
66 string *error_description) { | |
67 if (!CheckParameters(parameters)) | |
68 return false; | |
69 | |
70 void *curl_lib = dlopen("libcurl.so", RTLD_NOW); | |
71 if (!curl_lib) { | |
72 if (error_description != NULL) | |
73 *error_description = dlerror(); | |
74 curl_lib = dlopen("libcurl.so.4", RTLD_NOW); | |
75 } | |
76 if (!curl_lib) { | |
77 // Debian gives libcurl a different name when it is built against GnuTLS | |
78 // instead of OpenSSL. | |
79 curl_lib = dlopen("libcurl-gnutls.so.4", RTLD_NOW); | |
80 } | |
81 if (!curl_lib) { | |
82 curl_lib = dlopen("libcurl.so.3", RTLD_NOW); | |
83 } | |
84 if (!curl_lib) { | |
85 return false; | |
86 } | |
87 | |
88 CURL* (*curl_easy_init)(void); | |
89 *(void**) (&curl_easy_init) = dlsym(curl_lib, "curl_easy_init"); | |
90 CURL *curl = (*curl_easy_init)(); | |
91 if (error_description != NULL) | |
92 *error_description = "No Error"; | |
93 | |
94 if (!curl) { | |
95 dlclose(curl_lib); | |
96 return false; | |
97 } | |
98 | |
99 CURLcode err_code = CURLE_OK; | |
100 CURLcode (*curl_easy_setopt)(CURL *, CURLoption, ...); | |
101 *(void**) (&curl_easy_setopt) = dlsym(curl_lib, "curl_easy_setopt"); | |
102 (*curl_easy_setopt)(curl, CURLOPT_URL, url.c_str()); | |
103 (*curl_easy_setopt)(curl, CURLOPT_USERAGENT, kUserAgent); | |
104 // Set proxy information if necessary. | |
105 if (!proxy.empty()) | |
106 (*curl_easy_setopt)(curl, CURLOPT_PROXY, proxy.c_str()); | |
107 if (!proxy_user_pwd.empty()) | |
108 (*curl_easy_setopt)(curl, CURLOPT_PROXYUSERPWD, proxy_user_pwd.c_str()); | |
109 | |
110 struct curl_httppost *formpost = NULL; | |
111 struct curl_httppost *lastptr = NULL; | |
112 // Add form data. | |
113 CURLFORMcode (*curl_formadd)(struct curl_httppost **, struct curl_httppost **,
...); | |
114 *(void**) (&curl_formadd) = dlsym(curl_lib, "curl_formadd"); | |
115 map<string, string>::const_iterator iter = parameters.begin(); | |
116 for (; iter != parameters.end(); ++iter) | |
117 (*curl_formadd)(&formpost, &lastptr, | |
118 CURLFORM_COPYNAME, iter->first.c_str(), | |
119 CURLFORM_COPYCONTENTS, iter->second.c_str(), | |
120 CURLFORM_END); | |
121 | |
122 // Add form file. | |
123 (*curl_formadd)(&formpost, &lastptr, | |
124 CURLFORM_COPYNAME, file_part_name.c_str(), | |
125 CURLFORM_FILE, upload_file.c_str(), | |
126 CURLFORM_END); | |
127 | |
128 (*curl_easy_setopt)(curl, CURLOPT_HTTPPOST, formpost); | |
129 | |
130 // Disable 100-continue header. | |
131 struct curl_slist *headerlist = NULL; | |
132 char buf[] = "Expect:"; | |
133 struct curl_slist* (*curl_slist_append)(struct curl_slist *, const char *); | |
134 *(void**) (&curl_slist_append) = dlsym(curl_lib, "curl_slist_append"); | |
135 headerlist = (*curl_slist_append)(headerlist, buf); | |
136 (*curl_easy_setopt)(curl, CURLOPT_HTTPHEADER, headerlist); | |
137 | |
138 if (response_body != NULL) { | |
139 (*curl_easy_setopt)(curl, CURLOPT_WRITEFUNCTION, WriteCallback); | |
140 (*curl_easy_setopt)(curl, CURLOPT_WRITEDATA, | |
141 reinterpret_cast<void *>(response_body)); | |
142 } | |
143 | |
144 CURLcode (*curl_easy_perform)(CURL *); | |
145 *(void**) (&curl_easy_perform) = dlsym(curl_lib, "curl_easy_perform"); | |
146 err_code = (*curl_easy_perform)(curl); | |
147 const char* (*curl_easy_strerror)(CURLcode); | |
148 *(void**) (&curl_easy_strerror) = dlsym(curl_lib, "curl_easy_strerror"); | |
149 #ifndef NDEBUG | |
150 if (err_code != CURLE_OK) | |
151 fprintf(stderr, "Failed to send http request to %s, error: %s\n", | |
152 url.c_str(), | |
153 (*curl_easy_strerror)(err_code)); | |
154 #endif | |
155 if (error_description != NULL) | |
156 *error_description = (*curl_easy_strerror)(err_code); | |
157 | |
158 void (*curl_easy_cleanup)(CURL *); | |
159 *(void**) (&curl_easy_cleanup) = dlsym(curl_lib, "curl_easy_cleanup"); | |
160 (*curl_easy_cleanup)(curl); | |
161 if (formpost != NULL) { | |
162 void (*curl_formfree)(struct curl_httppost *); | |
163 *(void**) (&curl_formfree) = dlsym(curl_lib, "curl_formfree"); | |
164 (*curl_formfree)(formpost); | |
165 } | |
166 if (headerlist != NULL) { | |
167 void (*curl_slist_free_all)(struct curl_slist *); | |
168 *(void**) (&curl_slist_free_all) = dlsym(curl_lib, "curl_slist_free_all"); | |
169 (*curl_slist_free_all)(headerlist); | |
170 } | |
171 dlclose(curl_lib); | |
172 return err_code == CURLE_OK; | |
173 } | |
174 | |
175 // static | |
176 bool HTTPUpload::CheckParameters(const map<string, string> ¶meters) { | |
177 for (map<string, string>::const_iterator pos = parameters.begin(); | |
178 pos != parameters.end(); ++pos) { | |
179 const string &str = pos->first; | |
180 if (str.size() == 0) | |
181 return false; // disallow empty parameter names | |
182 for (unsigned int i = 0; i < str.size(); ++i) { | |
183 int c = str[i]; | |
184 if (c < 32 || c == '"' || c > 127) { | |
185 return false; | |
186 } | |
187 } | |
188 } | |
189 return true; | |
190 } | |
191 | |
192 } // namespace google_breakpad | |
OLD | NEW |