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 <curl/curl.h>
00016 #include <string>
00017 #include <list>
00018 #include <fstream>
00019 #include <sstream>
00020
00021 #define DEFAULT_TIMEOUT 60
00022 #define DEFAULT_CONNECT_TIMEOUT 10
00023
00065
00066
00067
00068
00069
00070 struct CURLCLIENTLIB_DECL CurlInitializer {
00071 CurlInitializer();
00072 ~CurlInitializer();
00073 };
00074
00075 #ifndef _CURLCLIENT_CPP_
00076 static CurlInitializer __CurlInitializer__;
00077 #endif
00078
00082 class CURLCLIENTLIB_DECL CurlClient {
00083 private:
00084
00085 CURL * instance;
00086 void initInstance();
00087 void setInstance();
00088
00089 void cleanInstance();
00090
00091
00092 std::string bufferHeader;
00093
00094
00095
00096 char errorBuffer[CURL_ERROR_SIZE];
00097 int errorCode;
00098
00099
00100 struct curl_slist* pcsHeaders;
00101
00102
00103 std::string url;
00104 int timeOut;
00105 int connectTimeOut;
00106 std::string contentType;
00107 std::list<std::string> lsHeader;
00108 bool useSSLv3;
00109
00110
00111 std::string clientCert;
00112 std::string clientCertType;
00113 std::string clientKey;
00114 std::string clientKeyType;
00115
00116
00117 std::string username;
00118 std::string password;
00119
00120
00121 int httpStatus;
00122 void getInfo();
00123
00124 static size_t callbackWriteFunction(char *data, size_t size, size_t nmemb, std::string *stream);
00125 static size_t callbackReadFunction(char *data, size_t size, size_t nmemb, std::stringstream *stream);
00126
00127
00128 void clean() { bufferHeader = ""; errorCode = 0; httpStatus = 0; }
00129
00130 public:
00131 CurlClient(): bufferHeader(), errorCode(0), url(""),
00132 timeOut(DEFAULT_TIMEOUT), connectTimeOut(DEFAULT_CONNECT_TIMEOUT), useSSLv3(false)
00133 { initInstance(); };
00134
00135 virtual ~CurlClient();
00136
00140 void setUrl(const std::string &sUrl) { url = sUrl; };
00141
00147 void setTimeOut(int iTimeOut) { timeOut = iTimeOut; }
00148
00154 void setConnectTimeOut(int iConnectTimeOut) { connectTimeOut = iConnectTimeOut; }
00155
00161 void setContentType(const std::string &sContentType) { contentType = sContentType; }
00162
00168 void setHeader(const std::string &sHeader) { lsHeader.push_back(sHeader); }
00169
00173 void setUseSSLv3() { useSSLv3 = true; }
00174
00180 void setClientCert(const std::string &cert) {clientCert = cert;}
00181
00187 void setClientCertType(const std::string &type) {clientCertType = type;}
00188
00189
00195 void setClientKey(const std::string &key) {clientKey = key;}
00196
00201 void setClientKeyType(const std::string &type) {clientKeyType = type;}
00202
00209 void setHttpAuth(const std::string &s_username, const std::string &s_password) {
00210 username = s_username;
00211 password = s_password;
00212 }
00213
00218 void clearHeaders() { lsHeader.clear(); }
00219
00220
00226 std::string getResultHeader() {return bufferHeader; }
00227
00232 const std::string getErrorBuffer() const { return std::string(errorBuffer); }
00233
00238 const int getErrorCode() const { return errorCode; }
00239
00244 int getHttpStatus() { return httpStatus; }
00245
00251 const std::string callGet();
00252
00258 const std::string callPost(const std::string &postData);
00259
00260
00265 void uploadToFTP(std::stringstream &in);
00266
00272 const std::string downloadFromFTP(void);
00273
00280 std::string escapeUrl(const std::string &url);
00281
00290 std::string unescapeUrl(const std::string &url);
00291
00300 const std::string getFullErrorMessage(void) const;
00301
00302 };
00303
00304 #endif