Planeshift

libcurl_wrapper.h

Go to the documentation of this file.
00001 // Copyright (c) 2009, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 // A wrapper for libcurl to do HTTP Uploads, to support easy mocking
00031 // and unit testing of the HTTPUpload class.
00032 
00033 #include <string>
00034 #include <map>
00035 
00036 #include "common/using_std_string.h"
00037 #include "third_party/curl/curl.h"
00038 
00039 namespace google_breakpad {
00040 class LibcurlWrapper {
00041  public:
00042   LibcurlWrapper();
00043   virtual bool Init();
00044   virtual bool SetProxy(const string& proxy_host,
00045                         const string& proxy_userpwd);
00046   virtual bool AddFile(const string& upload_file_path,
00047                        const string& basename);
00048   virtual bool SendRequest(const string& url,
00049                            const std::map<string, string>& parameters,
00050                            string* server_response);
00051  private:
00052   // This function initializes class state corresponding to function
00053   // pointers into the CURL library.
00054   bool SetFunctionPointers();
00055 
00056   bool init_ok_;                 // Whether init succeeded
00057   void* curl_lib_;               // Pointer to result of dlopen() on
00058                                  // curl library
00059   string last_curl_error_;  // The text of the last error when
00060                                  // dealing
00061   // with CURL.
00062 
00063   CURL *curl_;                   // Pointer for handle for CURL calls.
00064 
00065   CURL* (*easy_init_)(void);
00066 
00067   // Stateful pointers for calling into curl_formadd()
00068   struct curl_httppost *formpost_;
00069   struct curl_httppost *lastptr_;
00070   struct curl_slist *headerlist_;
00071 
00072   // Function pointers into CURL library
00073   CURLcode (*easy_setopt_)(CURL *, CURLoption, ...);
00074   CURLFORMcode (*formadd_)(struct curl_httppost **,
00075                            struct curl_httppost **, ...);
00076   struct curl_slist* (*slist_append_)(struct curl_slist *, const char *);
00077   void (*slist_free_all_)(struct curl_slist *);
00078   CURLcode (*easy_perform_)(CURL *);
00079   const char* (*easy_strerror_)(CURLcode);
00080   void (*easy_cleanup_)(CURL *);
00081   void (*formfree_)(struct curl_httppost *);
00082 
00083 };
00084 }