PageRenderTime 66ms CodeModel.GetById 16ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

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