2626#include < sstream>
2727#include < stdlib.h>
2828
29- class xmllog : public XmlRpc ::XmlRpcLogHandler {
29+ class xmllog : public XmlRpc ::XmlRpcLogHandler
30+ {
3031public:
3132 virtual ~xmllog () {}
32- void log (int level, const char * msg)
33- {
33+ void log (int level, const char * msg) {
3434 LOG_INFO (" %s" ,msg);
3535 }
3636};
@@ -61,12 +61,12 @@ CHttpDownloader::~CHttpDownloader()
6161
6262bool CHttpDownloader::search (std::list<IDownload*>& res, const std::string& name, IDownload::category cat)
6363{
64- CURL* curl = CurlWrapper::CurlInit ();
64+ CurlWrapper* curlw = new CurlWrapper ();
6565 LOG_DEBUG (" %s" , name.c_str () );
6666
6767 const std::string method (XMLRPC_METHOD);
6868 // std::string category;
69- XmlRpc::XmlRpcCurlClient client (curl , XMLRPC_HOST,XMLRPC_PORT, XMLRPC_URI);
69+ XmlRpc::XmlRpcCurlClient client (curlw-> GetHandle () , XMLRPC_HOST,XMLRPC_PORT, XMLRPC_URI);
7070 XmlRpc::XmlRpcValue arg;
7171 arg[" springname" ]=name;
7272 arg[" torrent" ]=true ;
@@ -99,17 +99,20 @@ bool CHttpDownloader::search(std::list<IDownload*>& res, const std::string& name
9999
100100 if (result.getType ()!=XmlRpc::XmlRpcValue::TypeArray) {
101101 LOG_ERROR (" Returned xml isn't an array!" );
102+ delete curlw;
102103 return false ;
103104 }
104105
105106 for (int i=0 ; i<result.size (); i++) {
106107 XmlRpc::XmlRpcValue resfile = result[i];
107108
108109 if (resfile.getType ()!=XmlRpc::XmlRpcValue::TypeStruct) {
110+ delete curlw;
109111 return false ;
110112 }
111113 if (resfile[" category" ].getType ()!=XmlRpc::XmlRpcValue::TypeString) {
112114 LOG_ERROR (" No category in result" );
115+ delete curlw;
113116 return false ;
114117 }
115118 std::string filename=fileSystem->getSpringDir ();
@@ -125,8 +128,9 @@ bool CHttpDownloader::search(std::list<IDownload*>& res, const std::string& name
125128 LOG_ERROR (" Unknown Category %s" , category.c_str ());
126129 filename+=PATH_DELIMITER;
127130 if ((resfile[" mirrors" ].getType ()!=XmlRpc::XmlRpcValue::TypeArray) ||
128- (resfile[" filename" ].getType ()!=XmlRpc::XmlRpcValue::TypeString)) {
131+ (resfile[" filename" ].getType ()!=XmlRpc::XmlRpcValue::TypeString)) {
129132 LOG_ERROR (" Invalid type in result" );
133+ delete curlw;
130134 return false ;
131135 }
132136 filename.append (resfile[" filename" ]);
@@ -165,6 +169,7 @@ bool CHttpDownloader::search(std::list<IDownload*>& res, const std::string& name
165169 }
166170 res.push_back (dl);
167171 }
172+ delete curlw;
168173 return true ;
169174}
170175
@@ -299,14 +304,14 @@ bool CHttpDownloader::setupDownload(DownloadData* piece)
299304 piece->start_piece =pieces.size () > 0 ? pieces[0 ] : -1 ;
300305 assert (piece->download ->pieces .size ()<=0 || piece->start_piece >=0 );
301306 piece->pieces = pieces;
302- if (piece->easy_handle ==NULL ) {
303- piece->easy_handle = CurlWrapper::CurlInit ();
307+ if (piece->curlw ==NULL ) {
308+ piece->curlw = new CurlWrapper ();
304309 } else {
305- curl_easy_cleanup ( piece->easy_handle ) ;
306- piece->easy_handle = CurlWrapper::CurlInit ();
310+ delete piece->curlw ;
311+ piece->curlw = new CurlWrapper ();
307312 }
308313
309- CURL* curle= piece->easy_handle ;
314+ CURL* curle = piece->curlw -> GetHandle () ;
310315 piece->mirror =piece->download ->getFastestMirror ();
311316 if (piece->mirror ==NULL ) {
312317 LOG_ERROR (" No mirror found" );
@@ -351,7 +356,7 @@ bool CHttpDownloader::setupDownload(DownloadData* piece)
351356DownloadData* CHttpDownloader::getDataByHandle (const std::vector <DownloadData*>& downloads, const CURL* easy_handle) const
352357{
353358 for (int i=0 ; i<(int )downloads.size (); i++) { // search corresponding data structure
354- if (downloads[i]->easy_handle == easy_handle) {
359+ if (downloads[i]->curlw -> GetHandle () == easy_handle) {
355360 return downloads[i];
356361 }
357362 }
@@ -411,22 +416,22 @@ bool CHttpDownloader::processMessages(CURLM* curlm, std::vector <DownloadData*>&
411416 }
412417 // get speed at which this piece was downloaded + update mirror info
413418 double dlSpeed;
414- curl_easy_getinfo (data->easy_handle , CURLINFO_SPEED_DOWNLOAD, &dlSpeed);
419+ curl_easy_getinfo (data->curlw -> GetHandle () , CURLINFO_SPEED_DOWNLOAD, &dlSpeed);
415420 data->mirror ->UpdateSpeed (dlSpeed);
416421 if (data->mirror ->status == Mirror::STATUS_UNKNOWN) // set mirror status only when unset
417422 data->mirror ->status =Mirror::STATUS_OK;
418423
419424 // remove easy handle, as its finished
420- curl_multi_remove_handle (curlm, data->easy_handle );
421- curl_easy_cleanup ( data->easy_handle ) ;
422- data->easy_handle =NULL ;
425+ curl_multi_remove_handle (curlm, data->curlw -> GetHandle () );
426+ delete data->curlw ;
427+ data->curlw =NULL ;
423428 LOG_INFO (" piece finished" );
424429 // piece finished / failed, try a new one
425430 if (!setupDownload (data)) {
426431 LOG_DEBUG (" No piece found, all pieces finished / currently downloading" );
427432 break ;
428433 }
429- int ret=curl_multi_add_handle (curlm, data->easy_handle );
434+ int ret=curl_multi_add_handle (curlm, data->curlw -> GetHandle () );
430435 if (ret!=CURLM_OK) {
431436 LOG_ERROR (" curl_multi_perform_error: %d %d" , ret, CURLM_BAD_EASY_HANDLE);
432437 }
@@ -474,7 +479,7 @@ bool CHttpDownloader::download(std::list<IDownload*>& download, int max_parallel
474479 }
475480 } else {
476481 downloads.push_back (dlData);
477- curl_multi_add_handle (curlm, dlData->easy_handle );
482+ curl_multi_add_handle (curlm, dlData->curlw -> GetHandle () );
478483 }
479484 }
480485 }
@@ -516,7 +521,7 @@ bool CHttpDownloader::download(std::list<IDownload*>& download, int max_parallel
516521 double size=-1 ;
517522 for (unsigned i=0 ; i<downloads.size (); i++) {
518523 double tmp;
519- curl_easy_getinfo (downloads[i]->easy_handle , CURLINFO_CONTENT_LENGTH_DOWNLOAD, &tmp);
524+ curl_easy_getinfo (downloads[i]->curlw -> GetHandle () , CURLINFO_CONTENT_LENGTH_DOWNLOAD, &tmp);
520525 if (tmp>size) {
521526 size=tmp;
522527 }
@@ -541,7 +546,7 @@ bool CHttpDownloader::download(std::list<IDownload*>& download, int max_parallel
541546 }
542547 for (unsigned i=0 ; i<downloads.size (); i++) {
543548 long timestamp;
544- if (curl_easy_getinfo (downloads[i]->easy_handle , CURLINFO_FILETIME, ×tamp) == CURLE_OK) {
549+ if (curl_easy_getinfo (downloads[i]->curlw -> GetHandle () , CURLINFO_FILETIME, ×tamp) == CURLE_OK) {
545550 if (downloads[i]->download ->state != IDownload::STATE_FINISHED) // decrease local timestamp if download failed to force redownload next time
546551 timestamp--;
547552 downloads[i]->download ->file ->SetTimestamp (timestamp);
0 commit comments