summaryrefslogtreecommitdiff
path: root/server/modules/downloaders/curl/src/curl_download.cpp
blob: 57d4c0a4fc73359f945769bc3311bd3681ff6171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
	Copyright © 2015-2016 Gluzskiy Alexandr (sss)

    This file is part of Unknown Download Manager (UDM).

    UDM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    UDM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with UDM.  If not, see <http://www.gnu.org/licenses/>.

*/

#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)
{
	curl_download *d = (curl_download*)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_)
	{
		if(of.is_open())
		{
			try{
				of.write(ptr, size_);
			}
			catch(...) //TODO: handle exception
			{
			}
		}
		else
		{
			return -1;
			//TODO: handle error
		}

	}
	return size_;
}

curl_download::curl_download(std::map<int, std::string> params, core_api *a, module_base *m)
{
    //for now we use single transfer connection for url
    //TODO: support multiple connections in parallel for multithreaded download
    easy_handle = curl_easy_init();
    if(!easy_handle)
		; //TODO: handle error
	api = a;
	module = m;

	curl_easy_setopt(easy_handle, CURLOPT_URL, params[0].c_str()); //Url as set in module_info
	curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, curl_w_callback);
	curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, this);
	if(!params[1].empty())
		download_path = params[1];
	else
	{
		download_path = api->get_core_settings()["default_download_directory"];
		//extract name from url
		auto p1 = params[0].rfind("/");
		if(p1 != std::string::npos)
		{
			download_path += "/";
			download_path += params[0].substr(p1+1);
		}

	}
	//curl_easy_setopt(h, CURLOPT_DEFAULT_PROTOCOL, "http"); //require curl >= 7.45


}

bool curl_download::start()
{
	boost::thread(boost::bind(&curl_download::perform_internal, this));
	state = core_events::download_running;
	return true; //TODO:
}

bool curl_download::stop()
{
	cancel_transfer = true;
	state = core_events::download_stopped;
	return true; //TODO:
}

bool curl_download::delete_download()
{
	cancel_transfer = true;
	curl_easy_cleanup(easy_handle);
	return true; //TODO:
}

void curl_download::perform_internal()
{
	auto status = curl_easy_perform(easy_handle);
	if(status != CURLE_OK)
		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;
	i.state = state;
	l.push_back(i);
	api->download_state_changed(module, l);
	//TODO: save state
}

curl_download::~curl_download()
{
	//curl_easy_cleanup(easy_handle); //this crashes for some reason

}