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 "HttpServerCommand.h"
|
|
|
|
#include "SocketCore.h"
|
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "HttpServer.h"
|
|
|
|
#include "HttpHeader.h"
|
|
|
|
#include "Logger.h"
|
|
|
|
#include "RequestGroup.h"
|
|
|
|
#include "RequestGroupMan.h"
|
2009-05-08 07:58:50 +00:00
|
|
|
#include "HttpServerBodyCommand.h"
|
2009-05-09 15:38:23 +00:00
|
|
|
#include "HttpServerResponseCommand.h"
|
2009-01-25 10:55:27 +00:00
|
|
|
#include "RecoverableException.h"
|
2009-05-09 15:38:23 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include "Option.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2010-03-06 08:29:53 +00:00
|
|
|
#include "wallclock.h"
|
2010-06-08 14:11:36 +00:00
|
|
|
#include "ServerStatMan.h"
|
|
|
|
#include "FileAllocationEntry.h"
|
|
|
|
#include "CheckIntegrityEntry.h"
|
2009-01-25 09:58:40 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2010-03-20 14:30:36 +00:00
|
|
|
HttpServerCommand::HttpServerCommand(cuid_t cuid, DownloadEngine* e,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SharedHandle<SocketCore>& socket):
|
2009-01-25 09:58:40 +00:00
|
|
|
Command(cuid),
|
|
|
|
_e(e),
|
|
|
|
_socket(socket),
|
|
|
|
_httpServer(new HttpServer(socket, e))
|
|
|
|
{
|
2009-06-20 02:33:42 +00:00
|
|
|
setStatus(Command::STATUS_ONESHOT_REALTIME);
|
2009-01-25 09:58:40 +00:00
|
|
|
_e->addSocketForReadCheck(_socket, this);
|
2010-06-08 14:11:36 +00:00
|
|
|
_httpServer->setUsernamePassword(_e->getOption()->get(PREF_XML_RPC_USER),
|
|
|
|
_e->getOption()->get(PREF_XML_RPC_PASSWD));
|
2010-01-22 14:09:39 +00:00
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
_httpServer->enableGZip();
|
|
|
|
#else // !HAVE_LIBZ
|
|
|
|
_httpServer->disableGZip();
|
|
|
|
#endif // !HAVE_LIBZ
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
|
|
|
|
2010-03-20 14:30:36 +00:00
|
|
|
HttpServerCommand::HttpServerCommand(cuid_t cuid,
|
2010-01-05 16:01:46 +00:00
|
|
|
const SharedHandle<HttpServer>& httpServer,
|
|
|
|
DownloadEngine* e,
|
|
|
|
const SharedHandle<SocketCore>& socket):
|
2009-01-25 10:55:27 +00:00
|
|
|
Command(cuid),
|
|
|
|
_e(e),
|
|
|
|
_socket(socket),
|
|
|
|
_httpServer(httpServer)
|
|
|
|
{
|
|
|
|
_e->addSocketForReadCheck(_socket, this);
|
|
|
|
}
|
|
|
|
|
2009-01-25 09:58:40 +00:00
|
|
|
HttpServerCommand::~HttpServerCommand()
|
|
|
|
{
|
|
|
|
_e->deleteSocketForReadCheck(_socket, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HttpServerCommand::execute()
|
|
|
|
{
|
2010-06-08 14:11:36 +00:00
|
|
|
if(_e->getRequestGroupMan()->downloadFinished() || _e->isHaltRequested()) {
|
2009-01-25 10:55:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-05-12 16:08:23 +00:00
|
|
|
try {
|
|
|
|
if(_socket->isReadable(0)) {
|
2010-04-11 08:28:22 +00:00
|
|
|
_timeoutTimer = global::wallclock;
|
2009-05-12 16:08:23 +00:00
|
|
|
SharedHandle<HttpHeader> header;
|
|
|
|
|
2009-01-25 10:55:27 +00:00
|
|
|
header = _httpServer->receiveRequest();
|
2009-08-30 13:15:22 +00:00
|
|
|
|
|
|
|
if(header.isNull()) {
|
2010-06-08 14:11:36 +00:00
|
|
|
_e->addCommand(this);
|
2010-01-05 16:01:46 +00:00
|
|
|
return false;
|
2009-08-30 13:15:22 +00:00
|
|
|
}
|
2009-05-12 16:08:23 +00:00
|
|
|
if(!_httpServer->authenticate()) {
|
2010-01-05 16:01:46 +00:00
|
|
|
_httpServer->disableKeepAlive();
|
|
|
|
_httpServer->feedResponse("401 Unauthorized",
|
|
|
|
"WWW-Authenticate: Basic realm=\"aria2\"",
|
|
|
|
"","text/html");
|
|
|
|
Command* command =
|
2010-06-08 15:02:20 +00:00
|
|
|
new HttpServerResponseCommand(getCuid(), _httpServer, _e, _socket);
|
2010-06-08 14:11:36 +00:00
|
|
|
_e->addCommand(command);
|
2010-01-05 16:01:46 +00:00
|
|
|
_e->setNoWait(true);
|
|
|
|
return true;
|
2009-05-12 16:08:23 +00:00
|
|
|
}
|
2009-08-30 13:15:22 +00:00
|
|
|
if(static_cast<uint64_t>
|
2010-06-08 14:11:36 +00:00
|
|
|
(_e->getOption()->getAsInt(PREF_XML_RPC_MAX_REQUEST_SIZE)) <
|
2010-01-05 16:01:46 +00:00
|
|
|
_httpServer->getContentLength()) {
|
2010-06-08 15:02:20 +00:00
|
|
|
getLogger()->info("Request too long. ContentLength=%s."
|
|
|
|
" See --xml-rpc-max-request-size option to loose"
|
|
|
|
" this limitation.",
|
|
|
|
util::uitos(_httpServer->getContentLength()).c_str());
|
2010-01-05 16:01:46 +00:00
|
|
|
return true;
|
2009-05-12 16:08:23 +00:00
|
|
|
}
|
2010-06-08 15:02:20 +00:00
|
|
|
Command* command = new HttpServerBodyCommand(getCuid(), _httpServer, _e,
|
2010-01-05 16:01:46 +00:00
|
|
|
_socket);
|
2010-06-08 14:11:36 +00:00
|
|
|
_e->addCommand(command);
|
2009-08-30 13:15:22 +00:00
|
|
|
_e->setNoWait(true);
|
|
|
|
return true;
|
2009-01-25 09:58:40 +00:00
|
|
|
} else {
|
2010-04-11 08:28:22 +00:00
|
|
|
if(_timeoutTimer.difference(global::wallclock) >= 30) {
|
2010-06-08 15:02:20 +00:00
|
|
|
getLogger()->info("HTTP request timeout.");
|
2010-01-05 16:01:46 +00:00
|
|
|
return true;
|
2009-05-12 16:08:23 +00:00
|
|
|
} else {
|
2010-06-08 14:11:36 +00:00
|
|
|
_e->addCommand(this);
|
2010-01-05 16:01:46 +00:00
|
|
|
return false;
|
2009-05-12 16:08:23 +00:00
|
|
|
}
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
2009-05-12 16:08:23 +00:00
|
|
|
} catch(RecoverableException& e) {
|
2010-06-08 15:02:20 +00:00
|
|
|
if(getLogger()->info()) {
|
|
|
|
getLogger()->info("CUID#%s - Error occurred while reading HTTP request",
|
|
|
|
e, util::itos(getCuid()).c_str());
|
2010-03-21 07:05:49 +00:00
|
|
|
}
|
2009-05-12 16:08:23 +00:00
|
|
|
return true;
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
2009-05-12 16:08:23 +00:00
|
|
|
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace aria2
|