2009-01-25 09:58:40 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2009-01-25 09:58:40 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "HttpListenCommand.h"
|
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "RecoverableException.h"
|
|
|
|
#include "message.h"
|
|
|
|
#include "Logger.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "LogFactory.h"
|
2009-01-25 09:58:40 +00:00
|
|
|
#include "SocketCore.h"
|
|
|
|
#include "HttpServerCommand.h"
|
|
|
|
#include "CUIDCounter.h"
|
|
|
|
#include "RequestGroupMan.h"
|
2009-08-30 12:25:04 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include "Option.h"
|
2010-03-21 07:05:49 +00:00
|
|
|
#include "util.h"
|
2010-08-07 15:04:21 +00:00
|
|
|
#include "A2STR.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2009-01-25 09:58:40 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
HttpListenCommand::HttpListenCommand(cuid_t cuid, DownloadEngine* e, int family,
|
|
|
|
bool secure)
|
|
|
|
: Command(cuid), e_(e), family_(family), secure_(secure)
|
|
|
|
{
|
|
|
|
}
|
2009-01-25 09:58:40 +00:00
|
|
|
|
2009-02-15 14:09:01 +00:00
|
|
|
HttpListenCommand::~HttpListenCommand()
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (serverSocket_) {
|
2010-06-21 13:51:56 +00:00
|
|
|
e_->deleteSocketForReadCheck(serverSocket_, this);
|
2009-02-15 14:09:01 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-25 09:58:40 +00:00
|
|
|
|
|
|
|
bool HttpListenCommand::execute()
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (e_->getRequestGroupMan()->downloadFinished() || e_->isHaltRequested()) {
|
2009-01-25 09:58:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
try {
|
2015-12-27 09:39:47 +00:00
|
|
|
if (serverSocket_->isReadable(0)) {
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<SocketCore> socket(serverSocket_->acceptConnection());
|
2013-01-15 14:17:21 +00:00
|
|
|
socket->setTcpNodelay(true);
|
2016-01-09 09:16:08 +00:00
|
|
|
auto endpoint = socket->getPeerInfo();
|
2009-05-09 15:55:52 +00:00
|
|
|
|
2011-03-18 13:45:25 +00:00
|
|
|
A2_LOG_INFO(fmt("RPC: Accepted the connection from %s:%u.",
|
2016-01-09 09:16:08 +00:00
|
|
|
endpoint.addr.c_str(), endpoint.port));
|
2009-05-09 15:55:52 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
e_->setNoWait(true);
|
2015-12-27 09:39:47 +00:00
|
|
|
e_->addCommand(
|
|
|
|
make_unique<HttpServerCommand>(e_->newCUID(), e_, socket, secure_));
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
catch (RecoverableException& e) {
|
2010-11-20 12:12:06 +00:00
|
|
|
A2_LOG_DEBUG_EX(fmt(MSG_ACCEPT_FAILURE, getCuid()), e);
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
2013-06-23 12:55:52 +00:00
|
|
|
e_->addCommand(std::unique_ptr<Command>(this));
|
2009-01-25 09:58:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HttpListenCommand::bindPort(uint16_t port)
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (serverSocket_) {
|
2010-06-21 13:51:56 +00:00
|
|
|
e_->deleteSocketForReadCheck(serverSocket_, this);
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
2014-09-13 09:37:57 +00:00
|
|
|
serverSocket_ = std::make_shared<SocketCore>();
|
2011-09-10 15:13:09 +00:00
|
|
|
const int ipv = (family_ == AF_INET) ? 4 : 6;
|
2009-01-25 09:58:40 +00:00
|
|
|
try {
|
2009-08-30 12:25:04 +00:00
|
|
|
int flags = 0;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (e_->getOption()->getAsBool(PREF_RPC_LISTEN_ALL)) {
|
2009-08-30 12:25:04 +00:00
|
|
|
flags = AI_PASSIVE;
|
|
|
|
}
|
2013-08-20 16:57:17 +00:00
|
|
|
serverSocket_->bind(nullptr, port, family_, flags);
|
2010-06-21 13:51:56 +00:00
|
|
|
serverSocket_->beginListen();
|
2015-12-27 09:39:47 +00:00
|
|
|
A2_LOG_INFO(fmt(MSG_LISTENING_PORT, getCuid(), port));
|
2010-06-21 13:51:56 +00:00
|
|
|
e_->addSocketForReadCheck(serverSocket_, this);
|
2013-02-25 13:29:46 +00:00
|
|
|
A2_LOG_NOTICE(fmt(_("IPv%d RPC: listening on TCP port %u"), ipv, port));
|
2009-01-25 09:58:40 +00:00
|
|
|
return true;
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
catch (RecoverableException& e) {
|
2013-02-25 13:29:46 +00:00
|
|
|
A2_LOG_ERROR_EX(fmt("IPv%d RPC: failed to bind TCP port %u", ipv, port), e);
|
2010-06-21 13:51:56 +00:00
|
|
|
serverSocket_->closeConnection();
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|