/thirdparty/breakpad/common/linux/http_upload.cc

http://github.com/tomahawk-player/tomahawk · C++ · 206 lines · 144 code · 24 blank · 38 comment · 39 complexity · 945f44508bcbab72a0d4f83d26b14ce3 MD5 · raw file

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