2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- 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"
|
2006-02-21 14:00:58 +00:00
|
|
|
#include "prefs.h"
|
2006-02-17 13:35:04 +00:00
|
|
|
#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>
|
|
|
|
|
2006-02-17 18:51:12 +00:00
|
|
|
#ifdef HAVE_LIBSSL
|
|
|
|
// for SSL
|
|
|
|
# include <openssl/err.h>
|
|
|
|
# include <openssl/ssl.h>
|
|
|
|
#endif // HAVE_LIBSSL
|
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2006-02-18 03:59:26 +00:00
|
|
|
void addCommand(int cuid, const char* url, string referer, vector<Request*> requests) {
|
2006-02-17 13:35:04 +00:00
|
|
|
Request* req = new Request();
|
2006-02-18 03:59:26 +00:00
|
|
|
req->setReferer(referer);
|
2006-02-17 13:35:04 +00:00
|
|
|
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;
|
2006-02-18 05:13:21 +00:00
|
|
|
cout << "Contact Info: Tasuhiro Tsujikawa <tujikawa at users dot sourceforge dot net>" << endl;
|
2006-02-17 13:35:04 +00:00
|
|
|
cout << endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void showUsage() {
|
2006-02-21 14:00:58 +00:00
|
|
|
cout << endl;
|
2006-02-17 13:35:04 +00:00
|
|
|
cout << "Usage: " << PACKAGE_NAME << " [options] URL ..." << endl;
|
2006-02-21 14:00:58 +00:00
|
|
|
cout << endl;
|
2006-02-17 13:35:04 +00:00
|
|
|
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;
|
2006-02-21 14:00:58 +00:00
|
|
|
cout << " --retry-wait=SEC Set amount of time in second between requests" << endl;
|
|
|
|
cout << " for errors. Specify a value between 0 and 60." << endl;
|
|
|
|
cout << " -t, --timeout=SEC Set timeout in second." << endl;
|
2006-02-17 13:35:04 +00:00
|
|
|
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;
|
2006-02-21 14:00:58 +00:00
|
|
|
cout << " --http-auth-scheme=SCHEME Set HTTP authentication scheme. Currently, basic" << endl;
|
2006-02-18 03:59:26 +00:00
|
|
|
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;
|
2006-02-21 14:00:58 +00:00
|
|
|
cout << " --referer=REFERER Set Referer. This affects to all URLs." << endl;
|
|
|
|
cout << " --ftp-user=USER Set FTP user. This affects to all URLs." << endl;
|
|
|
|
cout << " Default: anonymous" << endl;
|
|
|
|
cout << " --ftp-passwd=PASSWD Set FTP password. This affects to all URLs." << endl;
|
|
|
|
cout << " Default: ARIA2USER@" << endl;
|
|
|
|
cout << " --ftp-type=TYPE Set FTP transfer type. TYPE is either 'binary'" << endl;
|
|
|
|
cout << " or 'ascii'." << endl;
|
|
|
|
cout << " Default: binary" << endl;
|
|
|
|
cout << " -p, --ftp-pasv Use passive mode in FTP." << endl;
|
|
|
|
cout << " --ftp-via-http-proxy=WAY Use HTTP proxy in FTP. WAY is either 'get' or" << endl;
|
|
|
|
cout << " 'tunnel'." << endl;
|
2006-02-17 13:35:04 +00:00
|
|
|
cout << " -v, --version Print the version number and exit." << endl;
|
|
|
|
cout << " -h, --help Print this message and exit." << endl;
|
2006-02-21 14:00:58 +00:00
|
|
|
cout << endl;
|
2006-02-17 13:35:04 +00:00
|
|
|
cout << "URL:" << endl;
|
|
|
|
cout << " You can specify multiple URLs. All URLs must point to the same file" << endl;
|
2006-02-21 14:00:58 +00:00
|
|
|
cout << " or downloading fails." << endl;
|
|
|
|
cout << endl;
|
2006-02-17 13:35:04 +00:00
|
|
|
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;
|
2006-02-21 14:00:58 +00:00
|
|
|
cout << " Download a file by 2 connections, each connects to a different server:" << endl;
|
2006-02-17 13:35:04 +00:00
|
|
|
cout << " aria2c http://AAA.BBB.CCC/file.zip http://DDD.EEE.FFF/GGG/file.zip" << endl;
|
2006-02-21 14:00:58 +00:00
|
|
|
cout << " You can mix up different protocols:" << endl;
|
|
|
|
cout << " aria2c http://AAA.BBB.CCC/file.zip ftp://DDD.EEE.FFF/GGG/file.zip" << endl;
|
|
|
|
cout << endl;
|
2006-02-17 13:35:04 +00:00
|
|
|
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;
|
2006-02-18 03:59:26 +00:00
|
|
|
string referer;
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
int c;
|
|
|
|
Option* op = new Option();
|
2006-02-21 14:00:58 +00:00
|
|
|
op->put(PREF_RETRY_WAIT, "5");
|
|
|
|
op->put(PREF_TIMEOUT, "60");
|
|
|
|
op->put(PREF_FTP_USER, "anonymous");
|
|
|
|
op->put(PREF_FTP_PASSWD, "ARIA2USER@");
|
|
|
|
op->put(PREF_FTP_TYPE, V_BINARY);
|
|
|
|
op->put(PREF_FTP_PASV_ENABLED, V_TRUE);
|
|
|
|
op->put(PREF_FTP_VIA_HTTP_PROXY, V_TUNNEL);
|
2006-02-21 12:27:17 +00:00
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
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' },
|
2006-02-21 14:00:58 +00:00
|
|
|
{ "timeout", required_argument, NULL, 't' },
|
2006-02-17 13:35:04 +00:00
|
|
|
{ "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 },
|
2006-02-18 03:59:26 +00:00
|
|
|
{ "referer", required_argument, &lopt, 7 },
|
2006-02-18 05:13:21 +00:00
|
|
|
{ "retry-wait", required_argument, &lopt, 8 },
|
2006-02-21 14:00:58 +00:00
|
|
|
{ "ftp-user", required_argument, &lopt, 9 },
|
|
|
|
{ "ftp-passwd", required_argument, &lopt, 10 },
|
|
|
|
{ "ftp-type", required_argument, &lopt, 11 },
|
|
|
|
{ "ftp-pasv", no_argument, NULL, 'p' },
|
|
|
|
{ "ftp-via-http-proxy", required_argument, &lopt, 12 },
|
2006-02-17 13:35:04 +00:00
|
|
|
{ "version", no_argument, NULL, 'v' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
2006-02-21 14:00:58 +00:00
|
|
|
c = getopt_long(argc, argv, "Dd:o:l:s:pt:vh", longOpts, &optIndex);
|
2006-02-17 13:35:04 +00:00
|
|
|
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);
|
|
|
|
}
|
2006-02-21 14:00:58 +00:00
|
|
|
op->put(PREF_HTTP_PROXY_HOST, proxy.first);
|
|
|
|
op->put(PREF_HTTP_PROXY_PORT, Util::itos(port));
|
|
|
|
op->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
|
2006-02-17 13:35:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2:
|
2006-02-21 14:00:58 +00:00
|
|
|
op->put(PREF_HTTP_USER, optarg);
|
2006-02-17 13:35:04 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2006-02-21 14:00:58 +00:00
|
|
|
op->put(PREF_HTTP_PASSWD, optarg);
|
2006-02-17 13:35:04 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2006-02-21 14:00:58 +00:00
|
|
|
op->put(PREF_HTTP_PROXY_USER, optarg);
|
|
|
|
op->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
|
2006-02-17 13:35:04 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
2006-02-21 14:00:58 +00:00
|
|
|
op->put(PREF_HTTP_PROXY_PASSWD, optarg);
|
2006-02-17 13:35:04 +00:00
|
|
|
break;
|
|
|
|
case 6:
|
2006-02-21 14:00:58 +00:00
|
|
|
if(string(V_BASIC) == optarg) {
|
|
|
|
op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC);
|
2006-02-17 13:35:04 +00:00
|
|
|
} else {
|
2006-02-21 14:00:58 +00:00
|
|
|
cerr << "Currently, supported authentication scheme is basic." << endl;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
break;
|
2006-02-18 03:59:26 +00:00
|
|
|
case 7:
|
|
|
|
referer = optarg;
|
|
|
|
break;
|
2006-02-18 05:13:21 +00:00
|
|
|
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);
|
|
|
|
}
|
2006-02-21 14:00:58 +00:00
|
|
|
op->put(PREF_RETRY_WAIT, Util::itos(wait));
|
2006-02-18 05:13:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-02-21 14:00:58 +00:00
|
|
|
case 9:
|
|
|
|
op->put(PREF_FTP_USER, optarg);
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
op->put(PREF_FTP_PASSWD, optarg);
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
if(string(optarg) == V_BINARY || string(optarg) == V_ASCII) {
|
|
|
|
op->put(PREF_FTP_TYPE, optarg);
|
|
|
|
} else {
|
|
|
|
cerr << "ftp-type must be either 'binary' or 'ascii'." << endl;
|
|
|
|
showUsage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
if(string(optarg) == V_GET || string(optarg) == V_TUNNEL) {
|
|
|
|
op->put(PREF_FTP_VIA_HTTP_PROXY, optarg);
|
|
|
|
} else {
|
|
|
|
cerr << "ftp-via-http-proxy must be either 'get' or 'tunnel'." << endl;
|
|
|
|
showUsage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
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);
|
2006-02-21 14:00:58 +00:00
|
|
|
if(!(1 <= split && split <= 5)) {
|
2006-02-17 13:35:04 +00:00
|
|
|
cerr << "split must be between 1 and 5." << endl;
|
|
|
|
showUsage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2006-02-21 14:00:58 +00:00
|
|
|
case 't': {
|
|
|
|
int timeout = (int)strtol(optarg, NULL, 10);
|
|
|
|
if(1 <= timeout && timeout <= 600) {
|
|
|
|
op->put(PREF_TIMEOUT, Util::itos(timeout));
|
|
|
|
} else {
|
|
|
|
cerr << "timeout must be between 1 and 600" << endl;
|
|
|
|
showUsage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'p':
|
|
|
|
op->put(PREF_FTP_PASV_ENABLED, V_TRUE);
|
|
|
|
break;
|
2006-02-17 13:35:04 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2006-02-17 18:51:12 +00:00
|
|
|
#ifdef HAVE_LIBSSL
|
|
|
|
// for SSL initialization
|
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
|
|
|
#endif // HAVE_LIBSSL
|
2006-02-17 13:35:04 +00:00
|
|
|
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++) {
|
2006-02-18 03:59:26 +00:00
|
|
|
addCommand(i, argv[optind], referer, requests);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(int i = 1; optind < argc; i++) {
|
2006-02-18 03:59:26 +00:00
|
|
|
addCommand(i, argv[optind++], referer, requests);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|