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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/* <!-- copyright */
/*
* aria2 - a simple utility for downloading files faster
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* copyright --> */
#include "HttpInitiateConnectionCommand.h"
#include "DownloadEngine.h"
#include "SegmentMan.h"
#include "SimpleLogger.h"
#include "common.h"
#include "DefaultDiskWriter.h"
#include "Util.h"
#include "InitiateConnectionCommandFactory.h"
#include <vector>
#include <algorithm>
#include <signal.h>
#include <unistd.h>
#include <libgen.h>
#include <utility>
extern char* optarg;
extern int optind, opterr, optopt;
#include <getopt.h>
#ifdef HAVE_LIBSSL
// for SSL
# include <openssl/err.h>
# include <openssl/ssl.h>
#endif // HAVE_LIBSSL
using namespace std;
void clearRequest(Request* req) {
delete(req);
}
DownloadEngine* e;
void handler(int signal) {
cout << "\nSIGINT signal received." << endl;
e->segmentMan->save();
if(e->diskWriter != NULL) {
e->diskWriter->closeFile();
}
exit(0);
}
void addCommand(int cuid, const char* url, string referer, vector<Request*> requests) {
Request* req = new Request();
req->setReferer(referer);
if(req->setUrl(url)) {
e->commands.push(InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e));
requests.push_back(req);
} else {
cerr << "Unrecognized URL or unsupported protocol: " << req->getUrl() << endl;
delete(req);
}
}
void showVersion() {
cout << PACKAGE_NAME << " version " << PACKAGE_VERSION << endl;
cout << "Copyright (C) 2006 Tatsuhiro Tsujikawa" << endl;
cout << endl;
cout << "This program is free software; you can redistribute it and/or modify" << endl;
cout << "it under the terms of the GNU General Public License as published by" << endl;
cout << "the Free Software Foundation; either version 2 of the License, or" << endl;
cout << "(at your option) any later version." << endl;
cout << endl;
cout << "This program is distributed in the hope that it will be useful," << endl;
cout << "but WITHOUT ANY WARRANTY; without even the implied warranty of" << endl;
cout << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" << endl;
cout << "GNU General Public License for more details." << endl;
cout << endl;
cout << "You should have received a copy of the GNU General Public License" << endl;
cout << "along with this program; if not, write to the Free Software" << endl;
cout << "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA" << endl;
cout << endl;
cout << "Contact Info: Tasuhiro Tsujikawa <tujikawa at users dot sourceforge dot net>" << endl;
cout << endl;
}
void showUsage() {
cout << "Usage: " << PACKAGE_NAME << " [options] URL ..." << endl;
cout << "Options:" << endl;
cout << " -d, --dir=DIR The directory to store downloaded file." << endl;
cout << " -o, --out=FILE The file name for downloaded file." << endl;
cout << " -l, --log=LOG The file path to store log. If '-' is specified," << endl;
cout << " log is written to stdout." << endl;
cout << " -D, --daemon Run as daemon." << endl;
cout << " -s, --split=N Download a file using s connections. s must be" << endl;
cout << " between 1 and 5. If this option is specified the" << endl;
cout << " first URL is used, and the other URLs are ignored." << endl;
cout << " --http-proxy=HOST:PORT Use HTTP proxy server. This affects to all" << endl;
cout << " URLs." << endl;
cout << " --http-user=USER Set HTTP user. This affects to all URLs." << endl;
cout << " --http-passwd=PASSWD Set HTTP password. This affects to all URLs." << endl;
cout << " --http-proxy-user=USER Set HTTP proxy user. This affects to all URLs" << endl;
cout << " --http-proxy-passwd=PASSWD Set HTTP proxy password. This affects to all URLs." << endl;
cout << " --http-auth-scheme=SCHEME Set HTTP authentication scheme. Currently, BASIC" << endl;
cout << " is the only supported scheme. You MUST specify" << endl;
cout << " this option in order to use HTTP authentication" << endl;
cout << " as well as --http-proxy option." << endl;
cout << " --referer Set Referer. This affects to all URLs." << endl;
cout << " --retry-wait Set amount of time in second between requests" << endl;
cout << " for errors. Specify a value between 0 and 60." << endl;
cout << " -v, --version Print the version number and exit." << endl;
cout << " -h, --help Print this message and exit." << endl;
cout << "URL:" << endl;
cout << " You can specify multiple URLs. All URLs must point to the same file" << endl;
cout << " or a download fails." << endl;
cout << "Examples:" << endl;
cout << " Download a file by 1 connection:" << endl;
cout << " aria2c http://AAA.BBB.CCC/file.zip" << endl;
cout << " Download a file by 2 connections:" << endl;
cout << " aria2c -s 2 http://AAA.BBB.CCC/file.zip" << endl;
cout << " Download a file by 2 connections, each connects to a different server." << endl;
cout << " aria2c http://AAA.BBB.CCC/file.zip http://DDD.EEE.FFF/GGG/file.zip" << endl;
cout << "Reports bugs to <tujikawa at rednoah dot com>" << endl;
}
int main(int argc, char* argv[]) {
bool stdoutLog = false;
string logfile;
string dir;
string ufilename;
int split = 0;
bool daemonMode = false;
string referer;
int c;
Option* op = new Option();
op->put("retry_wait", "5");
while(1) {
int optIndex = 0;
int lopt;
static struct option longOpts[] = {
{ "daemon", no_argument, NULL, 'D' },
{ "dir", required_argument, NULL, 'd' },
{ "out", required_argument, NULL, 'o' },
{ "log", required_argument, NULL, 'l' },
{ "split", required_argument, NULL, 's' },
{ "http-proxy", required_argument, &lopt, 1 },
{ "http-user", required_argument, &lopt, 2 },
{ "http-passwd", required_argument, &lopt, 3 },
{ "http-proxy-user", required_argument, &lopt, 4 },
{ "http-proxy-passwd", required_argument, &lopt, 5 },
{ "http-auth-scheme", required_argument, &lopt, 6 },
{ "referer", required_argument, &lopt, 7 },
{ "retry-wait", required_argument, &lopt, 8 },
{ "version", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
{ 0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "Dd:o:l:s:vh", longOpts, &optIndex);
if(c == -1) {
break;
}
switch(c) {
case 0:{
switch(lopt) {
case 1: {
pair<string, string> proxy;
Util::split(proxy, optarg, ':');
int port = (int)strtol(proxy.second.c_str(), NULL, 10);
if(proxy.first.empty() || proxy.second.empty() ||
!(0 < port && port <= 65535)) {
cerr << "unrecognized proxy format" << endl;
showUsage();
exit(1);
}
op->put("http_proxy_host", proxy.first);
op->put("http_proxy_port", Util::itos(port));
op->put("http_proxy_enabled", "true");
break;
}
case 2:
op->put("http_user", optarg);
break;
case 3:
op->put("http_passwd", optarg);
break;
case 4:
op->put("http_proxy_user", optarg);
op->put("http_proxy_auth_enabled", "true");
break;
case 5:
op->put("http_proxy_passwd", optarg);
break;
case 6:
if(string("BASIC") == optarg) {
op->put("http_auth_scheme", "BASIC");
} else {
cerr << "Currently, supported authentication scheme is BASIC." << endl;
}
break;
case 7:
referer = optarg;
break;
case 8: {
int wait = (int)strtol(optarg, NULL, 10);
if(!(0 <= wait && wait <= 60)) {
cerr << "retry-wait must be between 0 and 60." << endl;
showUsage();
exit(1);
}
op->put("retry-wait", Util::itos(wait));
break;
}
}
break;
}
case 'D':
daemonMode = true;
break;
case 'd':
dir = optarg;
break;
case 'o':
ufilename = optarg;
break;
case 'l':
if(strcmp("-", optarg) == 0) {
stdoutLog = true;
} else {
logfile = optarg;
}
break;
case 's':
split = (int)strtol(optarg, NULL, 10);
if(!(1 < split && split < 5)) {
cerr << "split must be between 1 and 5." << endl;
showUsage();
exit(1);
}
break;
case 'v':
showVersion();
exit(0);
case 'h':
showUsage();
exit(0);
default:
showUsage();
exit(1);
}
}
if(optind == argc) {
cerr << "specify at least one URL" << endl;
showUsage();
exit(1);
}
if(daemonMode) {
if(daemon(1, 1) < 0) {
perror("daemon failed");
exit(1);
}
}
#ifdef HAVE_LIBSSL
// for SSL initialization
SSL_load_error_strings();
SSL_library_init();
#endif // HAVE_LIBSSL
SimpleLogger* logger;
if(stdoutLog) {
logger = new SimpleLogger(stdout);
} else if(logfile.size()) {
logger = new SimpleLogger(logfile);
} else {
logger = new SimpleLogger("/dev/null");
}
e = new DownloadEngine();
e->logger = logger;
e->option = op;
e->diskWriter = new DefaultDiskWriter();
e->segmentMan = new SegmentMan();
e->segmentMan->dir = dir;
e->segmentMan->ufilename = ufilename;
e->segmentMan->logger = logger;
vector<Request*> requests;
if(split > 0) {
for(int i = 1; i <= split; i++) {
addCommand(i, argv[optind], referer, requests);
}
} else {
for(int i = 1; optind < argc; i++) {
addCommand(i, argv[optind++], referer, requests);
}
}
struct sigaction sigact;
sigact.sa_handler = handler;
sigact.sa_flags = 0;
sigemptyset(&sigact.sa_mask);
sigaction(SIGINT, &sigact, NULL);
e->run();
for_each(requests.begin(), requests.end(), clearRequest);
requests.clear();
delete(logger);
delete(e->segmentMan);
delete(e->option);
delete(e->diskWriter);
delete(e);
return 0;
}
|