00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __WEHTTP_H__
00021 #define __WEHTTP_H__
00022
00023 #include "weStrings.h"
00024 #include "weBlob.h"
00025 #include "weUrl.h"
00026 #include "weiTransport.h"
00027 #include <curl/curl.h>
00028 #include <boost/algorithm/string/predicate.hpp>
00029
00030 using namespace boost::algorithm;
00031
00032 class WeHTTP;
00033 class WeHttpRequest;
00034
00043 class WeProxy
00044 {
00045 public:
00046 WeURL proxyAddr;
00047 curl_proxytype type;
00048 };
00049
00060 class WeHttpResponse : public iweResponse
00061 {
00062 public:
00063 WeHttpResponse();
00064 ~WeHttpResponse();
00065
00069 WeStringLinks& Headers(void) { return (headers); };
00070
00074 WeStringLinks& Cookies(void) { return (cookies); };
00075
00077
00078
00079
00080 int HttpCode(void) { return httpCode; };
00081 void HttpCode(int code) { httpCode = code; };
00083
00085
00086
00087 CURL* CURLHandle(void) { return curlHandle; };
00089
00095 void CurlSetOpts(WeHttpRequest* req = NULL);
00096
00100 const CURLcode &GetLastError(void) const { return(lastError); };
00101
00105 const char* GetErrorMessage(void) const { return(errorBuff); };
00106
00110 bool CurlInit(void);
00111
00112 void Process(iweTransport* proc);
00113
00114 protected:
00115 static size_t Receiver(void *ptr, size_t size, size_t nmemb, void *ourpointer);
00116 static size_t HeadersRecv(void *ptr, size_t size, size_t nmemb, void *ourpointer);
00117
00118 #ifndef __DOXYGEN__
00119 protected:
00120 WeStringLinks headers;
00121 WeStringLinks cookies;
00122 int httpCode;
00123 WeBlob headData;
00124 CURL* curlHandle;
00125 CURLcode lastError;
00126 char errorBuff[CURL_ERROR_SIZE];
00127
00128
00129
00130
00131 #endif //__DOXYGEN__
00132 };
00133
00142 class WeHttpRequest : public iweRequest
00143 {
00144 public:
00145
00146 enum weHttpMethod {wemGet, wemPost, wemPut, wemHead, wemTrace};
00147 enum {composeOverwrite, composeAdd};
00148 public:
00149 WeHttpRequest();
00150 WeHttpRequest(string url, weHttpMethod meth = wemGet, WeHttpResponse* resp = NULL);
00151
00152 virtual WeURL &RequestUrl(void) { return(reqUrl); };
00153 virtual void RequestUrl(const string &ReqUrl, iweOperation* resp = NULL);
00154 virtual void RequestUrl(const WeURL &ReqUrl, iweOperation* resp = NULL);
00155
00156 void ComposePost(int method = composeOverwrite);
00157
00161 WeBlob& Data() { return data; };
00162
00166 WeStringLinks& PostData() { return postData; };
00167
00169
00170
00171
00172 const weHttpMethod &Method(void) const { return(method); };
00173 void Method(const weHttpMethod &meth) { method = meth; };
00175
00176
00177 WeProxy *Proxy(void) const { return(proxy); };
00178 void Proxy(WeProxy *prx) { proxy = prx; };
00179
00180 #ifndef __DOXYGEN__
00181 protected:
00182 weHttpMethod method;
00183 WeURL reqUrl;
00184 WeBlob data;
00185 WeStringLinks postData;
00186 WeProxy *proxy;
00187 #endif //__DOXYGEN__
00188 };
00189
00198 class WeHTTP : public iweTransport
00199 {
00200 public:
00201 WeHTTP();
00202 ~WeHTTP();
00203
00204 virtual iweResponse* Request(iweRequest* req, iweResponse* resp = NULL);
00205 virtual iweResponse* Request(string url, iweResponse* resp = NULL);
00206 virtual iweResponse* Request(WeURL& url, iweResponse* resp = NULL);
00207
00209 const CURLMcode &GetLastError(void) const { return(lastError); };
00210
00212 const CURLMcode ProcessRequests(void);
00213
00214 virtual string& GetName() { return protoName; };
00215 virtual bool IsOwnProtocol(string& proto) {return iequals(proto, protoName); };
00216
00217 #ifndef __DOXYGEN__
00218 protected:
00219 CURLM* transferHandle;
00220 CURLMcode lastError;
00221
00222 private:
00223 static string protoName;
00224 WeHTTP(WeHTTP&) {};
00225 WeHTTP& operator=(WeHTTP&) { return *this; };
00226 #endif //__DOXYGEN__
00227 };
00228
00229 #endif //__WEHTTP_H__