summaryrefslogtreecommitdiff
path: root/server/modules/downloaders/curl/src/curl_download.cpp
diff options
context:
space:
mode:
authorGluzskiy Alexandr <sss@sss.chaoslab.ru>2016-01-24 13:45:40 +0300
committerGluzskiy Alexandr <sss@sss.chaoslab.ru>2016-01-24 13:45:40 +0300
commit3a9a8aa78ada7c5137b4b74c41be7088d2d1fd00 (patch)
treed8bec65519f070944738cbaeecd70536f4b7abcb /server/modules/downloaders/curl/src/curl_download.cpp
parent7c4d85348ecbb9913948edc53b82efaa97d85fda (diff)
server:
curl_downloader_module: basic data transfer handling (now able to save file, still need a lot of validation) some tracing code this may be called first working prototype with vary basic functionality implemented. currently udm have working server, working qt client and able to download url via curl module (most of additional events like download progress currently not implemented, code need lots of sanity checks and exception handling)
Diffstat (limited to 'server/modules/downloaders/curl/src/curl_download.cpp')
-rw-r--r--server/modules/downloaders/curl/src/curl_download.cpp54
1 files changed, 52 insertions, 2 deletions
diff --git a/server/modules/downloaders/curl/src/curl_download.cpp b/server/modules/downloaders/curl/src/curl_download.cpp
index 054ce22..d80a6e4 100644
--- a/server/modules/downloaders/curl/src/curl_download.cpp
+++ b/server/modules/downloaders/curl/src/curl_download.cpp
@@ -21,6 +21,7 @@
#include "curl_download.h"
#include <boost/thread.hpp>
+#include <boost/log/trivial.hpp>
size_t curl_w_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
@@ -29,10 +30,34 @@ size_t curl_w_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
if(d->get_cancel_state())
return -1; //cancel transfer
//TODO: implement pause
+ std::ofstream &of(d->get_ofile());
+ if(!of.is_open())
+ {
+ //TODO: sanity check and path validation
+ try{
+ of.open(d->get_download_path(), std::ios::out | std::ios::binary);
+ }
+ catch(...) //TODO: handle exception
+ {
+ }
+ }
size_t size_ = size * nmemb;
if(size_)
{
- //TODO: write data
+ if(of.is_open())
+ {
+ try{
+ of.write(ptr, size_);
+ }
+ catch(...) //TODO: handle exception
+ {
+ }
+ }
+ else
+ {
+ return -1;
+ //TODO: handle error
+ }
}
return size_;
@@ -78,6 +103,7 @@ bool curl_download::delete_download()
{
cancel_transfer = true;
curl_easy_cleanup(easy_handle);
+ //TODO: cleanup metadata
return true; //TODO:
}
@@ -88,6 +114,30 @@ void curl_download::perform_internal()
state = core_events::download_error;
else
state = core_events::download_completed;
+ ofile.close();
+ {//curl debug
+ char *url;
+ curl_easy_getinfo(easy_handle, CURLINFO_EFFECTIVE_URL, &url);
+ BOOST_LOG_TRIVIAL(debug)<<__FILE__<<":"<<__LINE__<<"\t"<<__func__<<"\nCURLINFO_EFFECTIVE_URL:\t"<<url;
+ long code = 0;
+ curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &code);
+ BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_RESPONSE_CODE:\t"<<code;
+ curl_easy_getinfo(easy_handle, CURLINFO_HTTP_CONNECTCODE, &code);
+ BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_HTTP_CONNECTCODE:\t"<<code;
+ curl_easy_getinfo(easy_handle, CURLINFO_REDIRECT_COUNT, &code);
+ BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_REDIRECT_COUNT:\t"<<code;
+ curl_easy_getinfo(easy_handle, CURLINFO_SIZE_DOWNLOAD, &code);
+ BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_SIZE_DOWNLOAD:\t"<<code;
+ curl_easy_getinfo(easy_handle, CURLINFO_HEADER_SIZE, &code);
+ BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_HEADER_SIZE:\t"<<code;
+ double length = 0;
+ curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &length);
+ BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_CONTENT_LENGTH_DOWNLOAD:\t"<<length;
+ curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_TYPE, &url);
+ BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_CONTENT_TYPE:\t"<<url;
+
+ std::cout<<std::endl;
+ }
std::list<core_events::download_state_info> l;
core_events::download_state_info i;
i.download_id = id;
@@ -99,6 +149,6 @@ void curl_download::perform_internal()
curl_download::~curl_download()
{
- curl_easy_cleanup(easy_handle);
+ //curl_easy_cleanup(easy_handle); //this crashes for some reason
}