00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <boost/lexical_cast.hpp>
00021 #include <algorithm>
00022 #include "weHelper.h"
00023 #include "weHTTP.h"
00024
00025 #ifndef __DOXYGEN__
00026 string WeHTTP::protoName = "http";
00027 #endif // __DOXYGEN__
00028
00034 WeHttpRequest::WeHttpRequest()
00035 {
00037 proxy = NULL;
00038 }
00039
00050 WeHttpRequest::WeHttpRequest( string url, WeHttpRequest::weHttpMethod meth , WeHttpResponse* resp )
00051 {
00052 method = meth;
00053 RequestUrl(url, resp);
00055 throw runtime_error("Not implemented");
00056 }
00057
00058 void WeHttpRequest::RequestUrl( const string &ReqUrl, iweOperation* resp )
00059 {
00060 WeURL weurl;
00061
00062 weurl = ReqUrl;
00063 return RequestUrl(weurl, resp);
00064 }
00065
00066 void WeHttpRequest::RequestUrl( const WeURL &ReqUrl, iweOperation* resp )
00067 {
00068 reqUrl = ReqUrl;
00069 if (resp != NULL) {
00071 }
00072 }
00073
00074 void WeHttpRequest::ComposePost( int method )
00075 {
00076 if (method == wemPost || method == wemPut) {
00078 }
00079 }
00086 WeHTTP::WeHTTP()
00087 {
00088 transferHandle = curl_multi_init();
00089 if (transferHandle == NULL) {
00090 throw WeError("curl_multi_init failed!");
00091 }
00092 responces.clear();
00093 WeLibInit();
00094 }
00095
00101 WeHTTP::~WeHTTP()
00102 {
00103 WeResponseList::iterator hnd;
00104 WeHttpResponse* point;
00105
00106 if (transferHandle != NULL) {
00107 for (hnd = responces.begin(); hnd != responces.end(); hnd++) {
00108 try {
00109 point = dynamic_cast<WeHttpResponse*>(*hnd);
00110 curl_multi_remove_handle(transferHandle, point->CURLHandle());
00111 delete point;
00112 }
00113 catch (...) {};
00114 }
00115 curl_multi_cleanup(transferHandle);
00116 }
00117 }
00118
00130 iweResponse* WeHTTP::Request( iweRequest* req, iweResponse* resp )
00131 {
00132 WeHttpResponse* retval;
00133
00134 if (!req->RequestUrl().IsValid()) {
00135 if (resp != NULL) {
00136 string url = req->RequestUrl().ToString();
00137 req->RequestUrl().Restore(url, &(resp->RealUrl()));
00138 }
00139 }
00140
00141 if (!req->RequestUrl().IsValid()) {
00142
00144 }
00145
00146 if (resp == NULL) {
00147 retval = new WeHttpResponse();
00148 retval->BaseUrl(req->RequestUrl());
00149 retval->RelocCount(0);
00150 }
00151 else {
00152 retval = dynamic_cast<WeHttpResponse*>(resp);
00153 if (!retval->BaseUrl().IsValid()) {
00154 retval->BaseUrl(req->RequestUrl());
00155 }
00156 if (!retval->CurlInit()) {
00158 return NULL;
00159 }
00160 }
00161
00162 try {
00163 WeHttpRequest* r = dynamic_cast<WeHttpRequest*>(req);
00164 retval->CurlSetOpts(r);
00165 }
00166 catch (...) {};
00167 retval->Processed(false);
00168
00169 responces.push_back(retval);
00170
00171 if (transferHandle != NULL) {
00172 curl_multi_add_handle(transferHandle, retval->CURLHandle());
00173 ProcessRequests();
00174 }
00175 return (iweResponse*)retval;
00176 }
00177
00188 iweResponse* WeHTTP::Request( string url, iweResponse* resp )
00189 {
00190 WeHttpRequest* req = new WeHttpRequest;
00191
00192 if (req == NULL) {
00193 throw WeError(string("Can't create WeHttpRequest! ") + __FILE__ + boost::lexical_cast<std::string>(__LINE__));
00194 return NULL;
00195 }
00196 req->RequestUrl(url, resp);
00197 if (!req->RequestUrl().IsValid()) {
00198 if (resp != NULL) {
00199 req->RequestUrl().Restore(url, &(resp->RealUrl()));
00200 }
00201 }
00202 req->Method(WeHttpRequest::wemGet);
00203 return Request(req, resp);
00204 }
00205
00216 iweResponse* WeHTTP::Request( WeURL& url, iweResponse* resp )
00217 {
00218 WeHttpRequest* req = new WeHttpRequest;
00219
00220 if (req == NULL) {
00221 throw WeError(string("Can't create WeHttpRequest! ") + __FILE__ + boost::lexical_cast<std::string>(__LINE__));
00222 return NULL;
00223 }
00224 req->RequestUrl(url, resp);
00225 req->Method(WeHttpRequest::wemGet);
00226 return Request(req, resp);
00227 }
00228
00229 const CURLMcode WeHTTP::ProcessRequests( void )
00230 {
00231 int running_handles;
00232 int msgs_in_queue;
00233 CURLMsg *cmsg;
00234 WeResponseList::iterator hnd;
00235 WeHttpResponse* resp;
00236
00237 if (transferHandle != NULL) {
00238 lastError = curl_multi_perform(transferHandle, &running_handles);
00239
00241 while (lastError == CURLM_CALL_MULTI_PERFORM) {
00242 lastError = curl_multi_perform(transferHandle, &running_handles);
00243 }
00244
00245 cmsg = curl_multi_info_read(transferHandle, &msgs_in_queue);
00246 while (cmsg != NULL)
00247 {
00248 if (cmsg->msg == CURLMSG_DONE) {
00249
00250 for (hnd = responces.begin(); hnd != responces.end();) {
00251 try {
00252 resp = dynamic_cast<WeHttpResponse*>(*hnd);
00253 if ( resp->CURLHandle() == cmsg->easy_handle) {
00254 responces.erase(hnd);
00255 hnd = responces.begin();
00256 curl_multi_remove_handle(transferHandle, resp->CURLHandle());
00257 resp->Process(this);
00258 break;
00259 }
00260 }
00261 catch (...) {};
00262 hnd++;
00263 }
00264 }
00265
00266 cmsg = curl_multi_info_read(transferHandle, &msgs_in_queue);
00267 }
00268 }
00269 else {
00270 lastError = CURLM_BAD_HANDLE;
00271 }
00272 return lastError;
00273 }