DomainPlug-insDocumentation
|
00001 #ifndef _CURLCLIENT_H 00002 #define _CURLCLIENT_H 00003 00004 #ifdef _MSC_VER 00005 # ifdef curlclient_EXPORTS 00006 # define CURLCLIENTLIB_DECL __declspec(dllexport) 00007 # else 00008 # define CURLCLIENTLIB_DECL __declspec(dllimport) 00009 # endif 00010 #else 00011 # define CURLCLIENTLIB_DECL 00012 #endif 00013 00014 00015 #include <string> 00016 #include <list> 00017 #include <fstream> 00018 #include <sstream> 00019 #include <vector> 00020 #include <list> 00021 00022 #define DEFAULT_TIMEOUT 60 00023 #define DEFAULT_CONNECT_TIMEOUT 10 00024 00066 /* 00067 CURL and OpenSSL initializaers are not thread safe. We MUST explicitly call 00068 initializer during library loading when no more then 1 thread are running 00069 */ 00070 00071 class CURLCLIENTLIB_DECL CurlForm { 00072 private: 00073 struct curl_httppost* post; 00074 struct curl_httppost* last; 00075 00076 std::list<std::string> bufPart; 00077 std::list<std::string> bufName; 00078 00079 public: 00080 CurlForm(); 00081 ~CurlForm(); 00082 00083 void addContent(const std::string &part, const std::string &name, const std::string &ctype); 00084 void addFile(const std::string &part, const std::string &name, const std::string &ctype); 00085 00086 const curl_httppost* getPost(void) const {return post;}; 00087 enum CurlFormType { 00088 CurlFormTypeContent, 00089 CurlFormTypeFile 00090 }; 00091 00092 }; 00093 00094 00095 00096 /* forward declaration to allow compile w/o libcurl */ 00097 typedef void CURL; 00098 00102 class CURLCLIENTLIB_DECL CurlClient { 00103 private: 00104 /* CURL* local instance */ 00105 CURL * instance; 00106 void initInstance(); 00107 void setInstance(); 00108 // void performInstance(); 00109 void cleanInstance(); 00110 00111 /* data stored here by callbacks */ 00112 std::string bufferHeader; 00113 //std::string errorBuffer; 00114 00115 /* CURL interlan buffer to store error and error string */ 00116 char *errorBuffer; 00117 int errorCode; 00118 00119 /* linked list to store headers */ 00120 struct curl_slist* pcsHeaders; 00121 00122 /* input data parameters */ 00123 std::string url; 00124 int timeOut; 00125 int connectTimeOut; 00126 std::string contentType; 00127 std::list<std::string> lsHeader; 00128 /* Flag indicates force to use SSLv3 instead of TLSv1 */ 00129 bool useSSLv3; 00130 00131 /* client certificates and private keys */ 00132 std::string clientCert; 00133 std::string clientCertType;//Supported formats are "PEM" and "DER" 00134 std::string clientKey; 00135 std::string clientKeyType;//Supported formats are "PEM", "DER" and "ENG". 00136 00137 /* authorization parameters */ 00138 std::string username; 00139 std::string password; 00140 00141 /* output data parameter */ 00142 int httpStatus; 00143 void getInfo(); 00144 00145 static size_t callbackWriteFunction(char *data, size_t size, size_t nmemb, std::string *stream); 00146 static size_t callbackReadFunction(char *data, size_t size, size_t nmemb, std::stringstream *stream); 00147 00148 00149 void clean(); 00150 00151 public: 00152 00153 CurlClient(); 00154 00155 virtual ~CurlClient(); 00156 00160 void setUrl(const std::string &sUrl); 00161 00167 void setTimeOut(int iTimeOut); 00168 00174 void setConnectTimeOut(int iConnectTimeOut); 00175 00181 void setContentType(const std::string &sContentType); 00182 00188 void setHeader(const std::string &sHeader); 00189 00193 void setUseSSLv3(); 00194 00200 void setClientCert(const std::string &cert); 00201 00207 void setClientCertType(const std::string &type); 00208 00209 00215 void setClientKey(const std::string &key); 00216 00221 void setClientKeyType(const std::string &type); 00222 00229 void setHttpAuth(const std::string &s_username, const std::string &s_password); 00230 00235 void clearHeaders(); 00236 00237 /* public result getters */ 00243 std::string getResultHeader() const; 00244 00249 const std::string getErrorBuffer() const; 00250 00255 int getErrorCode() const; 00256 00261 int getHttpStatus() const; 00262 00268 const std::string callGet(); 00269 00275 const std::string callPost(const std::string &postData); 00276 00281 void uploadToFTP(std::stringstream &in); 00282 00287 const std::string callHttpPost(const CurlForm &data); 00288 00294 const std::string downloadFromFTP(void); 00295 00302 static std::string escapeUrl(const std::string &url); 00303 00312 static std::string unescapeUrl(const std::string &url); 00313 00322 const std::string getFullErrorMessage(void) const; 00323 00324 }; 00325 00326 #endif