/ src / common / linux / libcurl_wrapper.h
libcurl_wrapper.h
  1  // Copyright 2009 Google LLC
  2  //
  3  // Redistribution and use in source and binary forms, with or without
  4  // modification, are permitted provided that the following conditions are
  5  // met:
  6  //
  7  //     * Redistributions of source code must retain the above copyright
  8  // notice, this list of conditions and the following disclaimer.
  9  //     * Redistributions in binary form must reproduce the above
 10  // copyright notice, this list of conditions and the following disclaimer
 11  // in the documentation and/or other materials provided with the
 12  // distribution.
 13  //     * Neither the name of Google LLC nor the names of its
 14  // contributors may be used to endorse or promote products derived from
 15  // this software without specific prior written permission.
 16  //
 17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  
 29  // A wrapper for libcurl to do HTTP Uploads, to support easy mocking
 30  // and unit testing of the HTTPUpload class.
 31  
 32  #ifndef COMMON_LINUX_LIBCURL_WRAPPER_H_
 33  #define COMMON_LINUX_LIBCURL_WRAPPER_H_
 34  
 35  #include <string>
 36  #include <map>
 37  
 38  #include "common/using_std_string.h"
 39  #include "third_party/curl/curl.h"
 40  
 41  namespace google_breakpad {
 42  
 43  // This class is only safe to be used on single-threaded code because of its
 44  // usage of libcurl's curl_global_cleanup().
 45  class LibcurlWrapper {
 46   public:
 47    LibcurlWrapper();
 48    virtual ~LibcurlWrapper();
 49    virtual bool Init();
 50    virtual bool SetProxy(const string& proxy_host,
 51                          const string& proxy_userpwd);
 52    virtual bool AddFile(const string& upload_file_path,
 53                         const string& basename);
 54    virtual bool SendRequest(const string& url,
 55                             const std::map<string, string>& parameters,
 56                             long* http_status_code,
 57                             string* http_header_data,
 58                             string* http_response_data);
 59    bool SendGetRequest(const string& url,
 60                        long* http_status_code,
 61                        string* http_header_data,
 62                        string* http_response_data);
 63    bool SendPutRequest(const string& url,
 64                        const string& path,
 65                        long* http_status_code,
 66                        string* http_header_data,
 67                        string* http_response_data);
 68    bool SendSimplePostRequest(const string& url,
 69                               const string& body,
 70                               const string& content_type,
 71                               long* http_status_code,
 72                               string* http_header_data,
 73                               string* http_response_data);
 74  
 75   private:
 76    // This function initializes class state corresponding to function
 77    // pointers into the CURL library.
 78    bool SetFunctionPointers();
 79  
 80    bool SendRequestInner(const string& url,
 81                          long* http_status_code,
 82                          string* http_header_data,
 83                          string* http_response_data);
 84  
 85    void Reset();
 86  
 87    bool CheckInit();
 88  
 89    bool init_ok_;                 // Whether init succeeded
 90    void* curl_lib_;               // Pointer to result of dlopen() on
 91                                   // curl library
 92    string last_curl_error_;  // The text of the last error when
 93                                   // dealing
 94    // with CURL.
 95  
 96    CURL* curl_;                   // Pointer for handle for CURL calls.
 97  
 98    CURL* (*easy_init_)(void);
 99  
100    // Stateful pointers for calling into curl_formadd()
101    struct curl_httppost* formpost_;
102    struct curl_httppost* lastptr_;
103    struct curl_slist* headerlist_;
104  
105    // Function pointers into CURL library
106    CURLcode (*easy_setopt_)(CURL*, CURLoption, ...);
107    CURLFORMcode (*formadd_)(struct curl_httppost**,
108                             struct curl_httppost**, ...);
109    struct curl_slist* (*slist_append_)(struct curl_slist*, const char*);
110    void (*slist_free_all_)(struct curl_slist*);
111    CURLcode (*easy_perform_)(CURL*);
112    const char* (*easy_strerror_)(CURLcode);
113    void (*easy_cleanup_)(CURL*);
114    CURLcode (*easy_getinfo_)(CURL*, CURLINFO info, ...);
115    void (*easy_reset_)(CURL*);
116    void (*formfree_)(struct curl_httppost*);
117    void (*global_cleanup_)(void);
118  
119  };
120  }
121  
122  #endif  // COMMON_LINUX_LIBCURL_WRAPPER_H_