2006-02-17 13:35:04 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-17 13:35:04 +00:00
|
|
|
*
|
|
|
|
* 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
|
2006-09-21 15:31:24 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-02-17 13:35:04 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "common.h"
|
2008-11-03 07:49:13 +00:00
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <numeric>
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "SharedHandle.h"
|
2007-03-21 10:19:23 +00:00
|
|
|
#include "LogFactory.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2006-02-17 13:35:04 +00:00
|
|
|
#include "Util.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "BitfieldManFactory.h"
|
2006-07-30 12:58:27 +00:00
|
|
|
#include "FeatureConfig.h"
|
2007-05-20 13:51:52 +00:00
|
|
|
#include "MultiUrlRequestInfo.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
#include "SimpleRandomizer.h"
|
2007-03-26 12:16:57 +00:00
|
|
|
#include "File.h"
|
2007-07-05 15:14:00 +00:00
|
|
|
#include "message.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include "Option.h"
|
|
|
|
#include "a2algo.h"
|
2007-07-23 13:04:48 +00:00
|
|
|
#include "a2io.h"
|
2007-07-31 16:45:16 +00:00
|
|
|
#include "a2time.h"
|
2007-07-23 13:04:48 +00:00
|
|
|
#include "Platform.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "DefaultBtContext.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "FileEntry.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "RequestGroup.h"
|
2008-04-22 10:29:48 +00:00
|
|
|
#include "ConsoleStatCalc.h"
|
|
|
|
#include "NullStatCalc.h"
|
2008-11-13 13:40:40 +00:00
|
|
|
#include "download_helper.h"
|
2008-11-13 15:08:52 +00:00
|
|
|
#include "Exception.h"
|
2008-12-20 10:59:16 +00:00
|
|
|
#include "ProtocolDetector.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#ifdef ENABLE_METALINK
|
|
|
|
# include "MetalinkHelper.h"
|
|
|
|
# include "MetalinkEntry.h"
|
|
|
|
#endif // ENABLE_METALINK
|
2008-02-28 17:40:47 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
# include "MessageDigestHelper.h"
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2008-11-03 07:49:13 +00:00
|
|
|
|
2006-02-17 13:35:04 +00:00
|
|
|
extern char* optarg;
|
|
|
|
extern int optind, opterr, optopt;
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2008-04-22 10:29:48 +00:00
|
|
|
// output stream to /dev/null
|
|
|
|
std::ofstream nullout(DEV_NULL);
|
|
|
|
|
|
|
|
SharedHandle<StatCalc> getStatCalc(const Option* op)
|
|
|
|
{
|
|
|
|
SharedHandle<StatCalc> statCalc;
|
|
|
|
if(op->getAsBool(PREF_QUIET)) {
|
|
|
|
statCalc.reset(new NullStatCalc());
|
|
|
|
} else {
|
2008-05-15 14:37:02 +00:00
|
|
|
statCalc.reset(new ConsoleStatCalc(op->getAsInt(PREF_SUMMARY_INTERVAL)));
|
2008-04-22 10:29:48 +00:00
|
|
|
}
|
|
|
|
return statCalc;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& getSummaryOut(const Option* op)
|
|
|
|
{
|
|
|
|
if(op->getAsBool(PREF_QUIET)) {
|
|
|
|
return nullout;
|
|
|
|
} else {
|
|
|
|
return std::cout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-20 10:59:16 +00:00
|
|
|
static void showTorrentFile(const std::string& uri)
|
|
|
|
{
|
|
|
|
SharedHandle<DefaultBtContext> btContext(new DefaultBtContext());
|
|
|
|
btContext->load(uri);
|
|
|
|
std::cout << btContext << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void showMetalinkFile(const std::string& uri, const Option* op)
|
|
|
|
{
|
|
|
|
std::deque<SharedHandle<MetalinkEntry> > metalinkEntries;
|
|
|
|
MetalinkHelper::parseAndQuery(metalinkEntries, uri, op);
|
|
|
|
std::deque<SharedHandle<FileEntry> > fileEntries;
|
|
|
|
MetalinkEntry::toFileEntry(fileEntries, metalinkEntries);
|
|
|
|
Util::toStream(std::cout, fileEntries);
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void showFiles(const std::deque<std::string>& uris, const Option* op)
|
|
|
|
{
|
|
|
|
ProtocolDetector dt;
|
|
|
|
for(std::deque<std::string>::const_iterator i = uris.begin();
|
|
|
|
i != uris.end(); ++i) {
|
2008-12-20 11:09:05 +00:00
|
|
|
printf(MSG_SHOW_FILES, (*i).c_str());
|
2008-12-20 10:59:16 +00:00
|
|
|
printf("\n");
|
|
|
|
if(dt.guessTorrentFile(*i)) {
|
|
|
|
showTorrentFile(*i);
|
|
|
|
} else if(dt.guessMetalinkFile(*i)) {
|
|
|
|
showMetalinkFile(*i, op);
|
|
|
|
} else {
|
2008-12-20 11:09:05 +00:00
|
|
|
printf(MSG_NOT_TORRENT_METALINK);
|
2008-12-20 10:59:16 +00:00
|
|
|
printf("\n\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
extern Option* option_processing(int argc, char* const argv[]);
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2007-10-11 16:58:24 +00:00
|
|
|
Option* op = option_processing(argc, argv);
|
2008-02-08 15:53:45 +00:00
|
|
|
std::deque<std::string> args(argv+optind, argv+argc);
|
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
SimpleRandomizer::init();
|
|
|
|
BitfieldManFactory::setDefaultRandomizer(SimpleRandomizer::getInstance());
|
2008-09-22 09:26:57 +00:00
|
|
|
if(op->get(PREF_LOG) == "-") {
|
2007-07-23 13:04:48 +00:00
|
|
|
LogFactory::setLogFile(DEV_STDOUT);
|
2008-09-22 09:26:57 +00:00
|
|
|
} else if(!op->get(PREF_LOG).empty()) {
|
2006-08-03 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
* src/Option.h
(getAsBool): New function.
* src/Option.cc
(prefs.h): Included.
(defined): 0-length value is now recognized as undefined.
(getAsInt): Rewritten.
(getAsLLInt): Rewritten.
(getAsBool): New function.
* src/FeatureConfig.h: Rewritten.
* src/FeatureConfig.cc: Rewritten.
* src/prefs.h
(PREF_STDOUT_LOG): New definition.
(PREF_LOG): New definition.
(PREF_DIR): New definition.
(PREF_OUT): New definition.
(PREF_SPLIT): New definition.
(PREF_DAEMON): New definition.
(PREF_REFERER): New definition.
(PREF_TORRENT_FILE): New definition.
(PREF_LISTEN_PORT): New definition.
(PREF_METALINK_FILE): New definition.
(PREF_METALINK_VERSION): New definition.
(PREF_METALINK_LANGUAGE): New definition.
(PREF_METALINK_OS): New definition.
(PREF_METALINK_SERVERS): New definition.
* src/main.cc
(main): Following command-line parameters are now put into
Option
class: stdoutLog, logfile, dir, ufilename, split, daemonMode,
referer, torrentFile, metalinkFile, listenPort, metalinkVersion,
metalinkLanguage, metalinkOs, metalinkServers
To fix the bug that aria2 can not handle http response header
properly.
* src/HttpHeader.cc
(put): Made name lowercased.
(defined): Made name lowercased.
(getFirst): Made name lowercased.
(get): Made name lowercased.
(getFirstAsInt): Rewritten.
(getFirstAsLLInt): Rewritten.
2006-08-03 12:36:02 +00:00
|
|
|
LogFactory::setLogFile(op->get(PREF_LOG));
|
2006-08-27 12:49:17 +00:00
|
|
|
} else {
|
2007-07-23 13:04:48 +00:00
|
|
|
LogFactory::setLogFile(DEV_NULL);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2008-05-15 15:48:41 +00:00
|
|
|
LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL));
|
2008-04-22 10:29:48 +00:00
|
|
|
if(op->getAsBool(PREF_QUIET)) {
|
|
|
|
LogFactory::setConsoleOutput(false);
|
|
|
|
}
|
2007-07-21 08:56:16 +00:00
|
|
|
int32_t exitStatus = EXIT_SUCCESS;
|
2006-04-19 17:23:58 +00:00
|
|
|
try {
|
2006-08-07 16:05:00 +00:00
|
|
|
Logger* logger = LogFactory::getInstance();
|
2008-09-17 10:22:28 +00:00
|
|
|
logger->info("<<--- --- --- ---");
|
|
|
|
logger->info(" --- --- --- ---");
|
|
|
|
logger->info(" --- --- --- --->>");
|
2007-11-14 10:10:38 +00:00
|
|
|
logger->info("%s %s %s", PACKAGE, PACKAGE_VERSION, TARGET);
|
2007-07-20 17:06:21 +00:00
|
|
|
logger->info(MSG_LOGGING_STARTED);
|
2006-04-19 17:23:58 +00:00
|
|
|
|
2008-02-28 17:40:47 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
MessageDigestHelper::staticSHA1DigestInit();
|
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
|
|
|
|
2007-07-23 13:04:48 +00:00
|
|
|
#ifdef SIGPIPE
|
2006-11-09 14:04:46 +00:00
|
|
|
Util::setGlobalSignalHandler(SIGPIPE, SIG_IGN, 0);
|
2007-07-23 13:04:48 +00:00
|
|
|
#endif
|
2008-02-20 16:46:56 +00:00
|
|
|
int32_t returnValue = 0;
|
2008-11-13 13:40:40 +00:00
|
|
|
std::deque<SharedHandle<RequestGroup> > requestGroups;
|
2006-08-07 16:05:00 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2008-12-04 13:05:05 +00:00
|
|
|
if(!op->blank(PREF_TORRENT_FILE)) {
|
2007-11-03 08:58:45 +00:00
|
|
|
if(op->get(PREF_SHOW_FILES) == V_TRUE) {
|
2008-12-20 10:59:16 +00:00
|
|
|
showTorrentFile(op->get(PREF_TORRENT_FILE));
|
2008-11-30 09:35:27 +00:00
|
|
|
return EXIT_SUCCESS;
|
2007-10-11 16:58:24 +00:00
|
|
|
} else {
|
2008-11-13 13:40:40 +00:00
|
|
|
createRequestGroupForBitTorrent(requestGroups, op, args);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2006-08-07 16:05:00 +00:00
|
|
|
else
|
|
|
|
#endif // ENABLE_BITTORRENT
|
2006-07-04 10:57:56 +00:00
|
|
|
#ifdef ENABLE_METALINK
|
2008-12-04 13:05:05 +00:00
|
|
|
if(!op->blank(PREF_METALINK_FILE)) {
|
2007-11-03 12:03:53 +00:00
|
|
|
if(op->get(PREF_SHOW_FILES) == V_TRUE) {
|
2008-12-20 10:59:16 +00:00
|
|
|
showMetalinkFile(op->get(PREF_METALINK_FILE), op);
|
2008-11-30 09:35:27 +00:00
|
|
|
return EXIT_SUCCESS;
|
2007-11-03 12:03:53 +00:00
|
|
|
} else {
|
2008-11-13 13:40:40 +00:00
|
|
|
createRequestGroupForMetalink(requestGroups, op);
|
2007-11-03 12:03:53 +00:00
|
|
|
}
|
2007-05-20 13:51:52 +00:00
|
|
|
}
|
|
|
|
else
|
2006-08-07 16:05:00 +00:00
|
|
|
#endif // ENABLE_METALINK
|
2008-12-04 13:05:05 +00:00
|
|
|
if(!op->blank(PREF_INPUT_FILE)) {
|
2008-11-13 13:40:40 +00:00
|
|
|
createRequestGroupForUriList(requestGroups, op);
|
2008-12-20 10:59:16 +00:00
|
|
|
} else if(op->get(PREF_SHOW_FILES) == V_TRUE) {
|
|
|
|
showFiles(args, op);
|
2007-11-03 08:58:45 +00:00
|
|
|
} else {
|
2008-11-13 13:40:40 +00:00
|
|
|
createRequestGroupForUri(requestGroups, op, args);
|
2006-08-07 16:05:00 +00:00
|
|
|
}
|
2008-11-13 13:40:40 +00:00
|
|
|
|
2008-11-30 09:35:27 +00:00
|
|
|
if(requestGroups.empty()) {
|
|
|
|
std::cout << MSG_NO_FILES_TO_DOWNLOAD << std::endl;
|
|
|
|
} else {
|
2008-11-24 13:38:56 +00:00
|
|
|
returnValue = MultiUrlRequestInfo(requestGroups, op, getStatCalc(op),
|
|
|
|
getSummaryOut(op)).execute();
|
|
|
|
}
|
2008-02-20 16:46:56 +00:00
|
|
|
if(returnValue == 1) {
|
|
|
|
exitStatus = EXIT_FAILURE;
|
|
|
|
}
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& ex) {
|
|
|
|
std::cerr << EX_EXCEPTION_CAUGHT << "\n" << ex.stackTrace() << std::endl;
|
2008-02-20 16:46:56 +00:00
|
|
|
exitStatus = EXIT_FAILURE;
|
2006-03-02 02:54:49 +00:00
|
|
|
}
|
2006-05-09 15:54:14 +00:00
|
|
|
delete op;
|
|
|
|
LogFactory::release();
|
2008-02-08 15:53:45 +00:00
|
|
|
return exitStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
aria2::Platform platform;
|
|
|
|
|
|
|
|
int r = aria2::main(argc, argv);
|
|
|
|
|
|
|
|
return r;
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|